Check VPN via capabilities

pull/214/head
M66B 1 year ago
parent de8f20d5df
commit 0626bb596d

@ -27,7 +27,6 @@ import android.net.LinkProperties;
import android.net.Network; import android.net.Network;
import android.net.NetworkCapabilities; import android.net.NetworkCapabilities;
import android.net.NetworkInfo; import android.net.NetworkInfo;
import android.net.TransportInfo;
import android.os.Build; import android.os.Build;
import android.provider.Settings; import android.provider.Settings;
import android.telephony.TelephonyManager; import android.telephony.TelephonyManager;
@ -362,11 +361,24 @@ public class ConnectionHelper {
Log.i("isMetered: underlying caps=" + caps); Log.i("isMetered: underlying caps=" + caps);
if (!caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN)) {
Log.i("isMetered: underlying VPN");
continue;
}
if (!caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) { if (!caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
Log.i("isMetered: underlying no internet"); Log.i("isMetered: underlying no internet");
continue; continue;
} }
boolean captive = caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_CAPTIVE_PORTAL);
if ((require_validated || (require_validated_captive && captive)) &&
Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
!caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)) {
Log.i("isMetered: underlying not validated captive=" + captive);
continue;
}
if (!caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED)) { if (!caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED)) {
Log.i("isMetered: underlying restricted"); Log.i("isMetered: underlying restricted");
continue; continue;
@ -378,19 +390,6 @@ public class ConnectionHelper {
continue; continue;
} }
if (caps.hasTransport(NetworkCapabilities.TRANSPORT_VPN)) {
Log.i("isMetered: underlying VPN transport");
continue;
}
boolean captive = caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_CAPTIVE_PORTAL);
if ((require_validated || (require_validated_captive && captive)) &&
Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
!caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)) {
Log.i("isMetered: underlying not validated captive=" + captive);
continue;
}
if (caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN)) { if (caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN)) {
underlying = true; underlying = true;
Log.i("isMetered: underlying is connected"); Log.i("isMetered: underlying is connected");
@ -541,13 +540,13 @@ public class ConnectionHelper {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
for (Network network : cm.getAllNetworks()) { for (Network network : cm.getAllNetworks()) {
NetworkCapabilities caps = cm.getNetworkCapabilities(network); NetworkCapabilities caps = cm.getNetworkCapabilities(network);
if (caps != null && caps.hasTransport(NetworkCapabilities.TRANSPORT_VPN)) if (caps != null && !caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN))
return true; return true;
} }
} else { } else {
Network active = cm.getActiveNetwork(); Network active = cm.getActiveNetwork();
NetworkCapabilities caps = (active == null ? null : cm.getNetworkCapabilities(active)); NetworkCapabilities caps = (active == null ? null : cm.getNetworkCapabilities(active));
if (caps != null && caps.hasTransport(NetworkCapabilities.TRANSPORT_VPN)) if (caps != null && !caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN))
return true; return true;
} }
} catch (Throwable ex) { } catch (Throwable ex) {

Loading…
Cancel
Save