Recover VPN fabric lease expiry
This commit is contained in:
@@ -30,8 +30,8 @@ android {
|
||||
applicationId "su.cin.rapvpn"
|
||||
minSdk 26
|
||||
targetSdk 35
|
||||
versionCode 203
|
||||
versionName "0.2.203"
|
||||
versionCode 204
|
||||
versionName "0.2.204"
|
||||
buildConfigField "String", "DEFAULT_BACKEND_URL", "\"${normalizeGradleString(defaultBackendUrl)}\""
|
||||
buildConfigField "String", "DEFAULT_CLUSTER_ID", "\"${normalizeGradleString(defaultClusterId)}\""
|
||||
buildConfigField "String", "DEFAULT_ORGANIZATION_ID", "\"${normalizeGradleString(defaultOrganizationId)}\""
|
||||
|
||||
@@ -91,6 +91,7 @@ public class RapDiagnosticService extends Service {
|
||||
private volatile long heartbeatStartedAt = 0;
|
||||
private volatile long commandPollStartedAt = 0;
|
||||
private volatile long commandStartedAt = 0;
|
||||
private volatile long lastFabricLeaseRefreshAttemptAt = 0;
|
||||
private String controlNetworkMode = "";
|
||||
private final AtomicBoolean heartbeatInProgress = new AtomicBoolean(false);
|
||||
private final AtomicBoolean commandPollInProgress = new AtomicBoolean(false);
|
||||
@@ -332,6 +333,11 @@ public class RapDiagnosticService extends Service {
|
||||
} catch (Exception e) {
|
||||
serviceState = "upgrade restart check warning: " + e.getMessage();
|
||||
}
|
||||
try {
|
||||
maybeRecoverExpiredFabricLease();
|
||||
} catch (Exception e) {
|
||||
serviceState = "fabric lease recovery warning: " + e.getMessage();
|
||||
}
|
||||
lastWorkerProgressAt = System.currentTimeMillis();
|
||||
reportStatusWithFallback(backendUrl, clusterId, deviceId, statusPayload("heartbeat"));
|
||||
lastWorkerProgressAt = System.currentTimeMillis();
|
||||
@@ -803,7 +809,7 @@ public class RapDiagnosticService extends Service {
|
||||
try {
|
||||
String refreshToken = new SecureTokenStore(this).get(PREF_REFRESH_TOKEN);
|
||||
if (refreshToken.isEmpty()) {
|
||||
String savedUserId = prefs.getString(PREF_USER_ID, "");
|
||||
String savedUserId = savedUserIdForProfileRefresh(prefs);
|
||||
if (savedUserId == null || savedUserId.trim().isEmpty()) {
|
||||
return "refresh_profile skipped: refresh token and saved user missing";
|
||||
}
|
||||
@@ -818,6 +824,56 @@ public class RapDiagnosticService extends Service {
|
||||
}
|
||||
}
|
||||
|
||||
private String savedUserIdForProfileRefresh(SharedPreferences prefs) {
|
||||
String savedUserId = prefs.getString(PREF_USER_ID, "");
|
||||
if (savedUserId != null && !savedUserId.trim().isEmpty()) {
|
||||
return savedUserId.trim();
|
||||
}
|
||||
try {
|
||||
String profileJson = prefs.getString(PREF_PROFILE_JSON, "");
|
||||
if (profileJson == null || profileJson.trim().isEmpty()) {
|
||||
return "";
|
||||
}
|
||||
JSONObject root = new JSONObject(profileJson);
|
||||
JSONObject profile = root.optJSONObject("vpn_client_profile");
|
||||
if (profile == null) {
|
||||
profile = root;
|
||||
}
|
||||
String profileUserId = profile.optString("user_id", "").trim();
|
||||
if (!profileUserId.isEmpty()) {
|
||||
prefs.edit().putString(PREF_USER_ID, profileUserId).apply();
|
||||
return profileUserId;
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
private void maybeRecoverExpiredFabricLease() {
|
||||
SharedPreferences runtime = getSharedPreferences(RUNTIME_PREFS, MODE_PRIVATE);
|
||||
String downlinkError = runtime.getString("downlink_error_type", "") + " " + runtime.getString("downlink_message", "");
|
||||
String uplinkError = runtime.getString("uplink_sender_error_type", "") + " " + runtime.getString("uplink_sender_message", "");
|
||||
String combined = (downlinkError + " " + uplinkError).toLowerCase();
|
||||
if (!combined.contains("403 forbidden") && !combined.contains("service channel lease expired")) {
|
||||
return;
|
||||
}
|
||||
long now = System.currentTimeMillis();
|
||||
if (now - lastFabricLeaseRefreshAttemptAt < 60000) {
|
||||
return;
|
||||
}
|
||||
lastFabricLeaseRefreshAttemptAt = now;
|
||||
String refresh = refreshProfile();
|
||||
if (!refresh.startsWith("refresh_profile ok")) {
|
||||
serviceState = "fabric lease recovery refresh skipped: " + refresh;
|
||||
return;
|
||||
}
|
||||
String start = startVPNFromSavedProfile();
|
||||
serviceState = "fabric lease recovery restarted vpn: " + start;
|
||||
lastCommandType = "auto_fabric_lease_recovery";
|
||||
lastCommandResult = start + " profile_refresh=" + refresh;
|
||||
lastCommandAt = now;
|
||||
}
|
||||
|
||||
private String refreshProfileForUser(SharedPreferences prefs, String userId, String trustedDeviceId) throws Exception {
|
||||
String backendUrl = normalizeBackendUrl(prefs.getString("backend_url", DEFAULT_BACKEND_URL));
|
||||
String organizationId = prefs.getString("organization_id", DEFAULT_ORGANIZATION_ID);
|
||||
|
||||
Reference in New Issue
Block a user