Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix race condition where a VPN is still active while detecting connetion type #543

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 56 additions & 12 deletions app/src/main/java/eu/faircode/netguard/ServiceSinkhole.java
Original file line number Diff line number Diff line change
Expand Up @@ -569,10 +569,7 @@ private void reload(boolean interactive) {
stopNative(vpn);
stopVPN(vpn);
vpn = null;
try {
Thread.sleep(500);
} catch (InterruptedException ignored) {
}
waitForVPNStop();
}
vpn = startVPN(last_builder);

Expand All @@ -599,10 +596,7 @@ private void reload(boolean interactive) {
stopNative(prev);
stopVPN(prev);
prev = null;
try {
Thread.sleep(3000);
} catch (InterruptedException ignored) {
}
waitForVPNStop();
vpn = startVPN(last_builder);
if (vpn == null)
throw new IllegalStateException("Handover failed");
Expand All @@ -611,11 +605,13 @@ private void reload(boolean interactive) {
if (prev != null) {
stopNative(prev);
stopVPN(prev);
waitForVPNStop();
}
} else {
if (vpn != null) {
stopNative(vpn);
stopVPN(vpn);
waitForVPNStop();
}

vpn = startVPN(builder);
Expand Down Expand Up @@ -1233,22 +1229,70 @@ public static List<InetAddress> getDns(Context context) {
return listDns;
}

private void waitForVPNStop() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);

if (cm != null) {
Network active;
NetworkInfo activeNetworkInfo;
while (true) {
active = cm.getActiveNetwork();
activeNetworkInfo = active == null ? null : cm.getNetworkInfo(active);

if (activeNetworkInfo == null) {
Network[] networks = cm.getAllNetworks();
boolean isConnected = false;

// if there's only one network
if (networks.length == 1) {
break;
}
for (Network network : networks) {
activeNetworkInfo = cm.getNetworkInfo(network);
if (activeNetworkInfo != null && activeNetworkInfo.getType() != ConnectivityManager.TYPE_VPN && activeNetworkInfo.isConnected()) {
isConnected = true;
break;
}
}
if (!isConnected)
break;
Log.i(TAG, "Waiting for VPN to stop. network=null");
} else {
if (activeNetworkInfo.getType() != ConnectivityManager.TYPE_VPN) {
break;
}
Log.i(TAG, "Waiting for VPN to stop. network=" + active + " " + activeNetworkInfo);
}
try {
Thread.sleep(10);
} catch (InterruptedException ignored) {
}
}
}
}
else {
try {
Thread.sleep(500);
} catch (InterruptedException ignored) {
}
}
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private ParcelFileDescriptor startVPN(Builder builder) throws SecurityException {
try {
ParcelFileDescriptor pfd = builder.establish();

// Set underlying network
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
Network active = (cm == null ? null : cm.getActiveNetwork());
if (active != null) {
Log.i(TAG, "Setting underlying network=" + active + " " + cm.getNetworkInfo(active));
setUnderlyingNetworks(new Network[]{active});
builder.setUnderlyingNetworks(new Network[]{active});
}
}

return pfd;
return builder.establish();
} catch (SecurityException ex) {
throw ex;
} catch (Throwable ex) {
Expand Down