Skip to content

Commit

Permalink
Workaround for active network being VPN
Browse files Browse the repository at this point in the history
  • Loading branch information
M66B committed Dec 28, 2024
1 parent 3481d0f commit ba0e8c2
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions app/src/main/java/eu/faircode/netguard/ServiceSinkhole.java
Original file line number Diff line number Diff line change
Expand Up @@ -2750,23 +2750,43 @@ private Network getActiveNetwork() {
if (cm == null)
return null;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
return cm.getActiveNetwork();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Network active = cm.getActiveNetwork();
if (active == null) {
Log.i(TAG, "getActiveNetwork: no active network");
return null;
}

NetworkCapabilities caps = cm.getNetworkCapabilities(active);
if (caps != null && caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN))
return active;
else
Log.w(TAG, "getActiveNetwork: active network is VPN");

}

NetworkInfo ani = cm.getActiveNetworkInfo();
if (ani == null)
return null;

Network[] networks = cm.getAllNetworks();
for (Network network : networks) {
NetworkCapabilities caps = cm.getNetworkCapabilities(network);
Log.i(TAG, "getActiveNetwork: network=" + network + " caps=" + caps);
if (caps == null || !caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN))
continue;

NetworkInfo ni = cm.getNetworkInfo(network);
if (ni == null)
continue;
if (ni.getType() == ani.getType() &&
ni.getSubtype() == ani.getSubtype())
ni.getSubtype() == ani.getSubtype()) {
Log.i(TAG, "getActiveNetwork: returning network=" + network);
return network;
}
}

Log.i(TAG, "getActiveNetwork: no active network found");
return null;
}

Expand Down

0 comments on commit ba0e8c2

Please sign in to comment.