From ace1b6faa9e9dd3b6bb6c4a60317d5db7048c0ea Mon Sep 17 00:00:00 2001 From: EliyahuSternDAI <108889726+EliyahuSternDAI@users.noreply.github.com> Date: Fri, 9 Aug 2024 18:09:37 +0300 Subject: Add ignorenetworkstate restriction, to allow tunnel over USB (#1746) * Add ignorenetworkstate restriction, to allow tunnel over USB * Add to app_restrictions.xml * Parse restriction value into SharedPreferences Co-authored-by: Eliyahu Stern --- .../main/java/de/blinkt/openvpn/api/AppRestrictions.java | 8 +++++++- .../java/de/blinkt/openvpn/core/DeviceStateReceiver.java | 14 +++++++------- main/src/main/res/values/untranslatable.xml | 1 + main/src/main/res/xml/app_restrictions.xml | 4 ++++ 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/main/src/main/java/de/blinkt/openvpn/api/AppRestrictions.java b/main/src/main/java/de/blinkt/openvpn/api/AppRestrictions.java index 48fbb2da..9e3764e3 100644 --- a/main/src/main/java/de/blinkt/openvpn/api/AppRestrictions.java +++ b/main/src/main/java/de/blinkt/openvpn/api/AppRestrictions.java @@ -14,7 +14,6 @@ import android.text.TextUtils; import de.blinkt.openvpn.VpnProfile; import de.blinkt.openvpn.core.ConfigParser; -import de.blinkt.openvpn.core.OpenVPNService; import de.blinkt.openvpn.core.Preferences; import de.blinkt.openvpn.core.ProfileManager; import de.blinkt.openvpn.core.VpnStatus; @@ -147,6 +146,13 @@ public class AppRestrictions { editor.putBoolean("screenoff", pauseVPN); editor.apply(); } + if(restrictions.containsKey("ignorenetworkstate")) + { + boolean ignoreNetworkState = restrictions.getBoolean("ignorenetworkstate"); + SharedPreferences.Editor editor = defaultPrefs.edit(); + editor.putBoolean("ignorenetstate", ignoreNetworkState); + editor.apply(); + } if (restrictions.containsKey("restartvpnonboot")) { boolean restartVPNonBoot = restrictions.getBoolean("restartvpnonboot"); diff --git a/main/src/main/java/de/blinkt/openvpn/core/DeviceStateReceiver.java b/main/src/main/java/de/blinkt/openvpn/core/DeviceStateReceiver.java index 8c5772a8..51a84875 100644 --- a/main/src/main/java/de/blinkt/openvpn/core/DeviceStateReceiver.java +++ b/main/src/main/java/de/blinkt/openvpn/core/DeviceStateReceiver.java @@ -14,14 +14,11 @@ import android.net.NetworkInfo; import android.net.NetworkInfo.State; import android.os.Handler; import android.os.Looper; -import android.preference.PreferenceManager; import de.blinkt.openvpn.R; import de.blinkt.openvpn.core.VpnStatus.ByteCountListener; import java.util.LinkedList; -import java.util.Objects; -import java.util.StringTokenizer; import static de.blinkt.openvpn.core.OpenVPNManagement.pauseReason; @@ -38,7 +35,6 @@ public class DeviceStateReceiver extends BroadcastReceiver implements ByteCountL // Time to wait after network disconnect to pause the VPN private final int DISCONNECT_WAIT = 20; - connectState network = connectState.DISCONNECTED; connectState screen = connectState.SHOULDBECONNECTED; connectState userpause = connectState.SHOULDBECONNECTED; @@ -179,12 +175,16 @@ public class DeviceStateReceiver extends BroadcastReceiver implements ByteCountL return (a == null) ? (b == null) : a.equals(b); } - public void networkStateChange(Context context) { - NetworkInfo networkInfo = getCurrentNetworkInfo(context); SharedPreferences prefs = Preferences.getDefaultSharedPreferences(context); - boolean sendusr1 = prefs.getBoolean("netchangereconnect", true); + boolean ignoreNetworkState = prefs.getBoolean("ignorenetstate", false); + if (ignoreNetworkState) { + network = connectState.SHOULDBECONNECTED; + return; + } + NetworkInfo networkInfo = getCurrentNetworkInfo(context); + boolean sendusr1 = prefs.getBoolean("netchangereconnect", true); String netstatestring; if (networkInfo == null) { diff --git a/main/src/main/res/values/untranslatable.xml b/main/src/main/res/values/untranslatable.xml index edafd76e..5caa853d 100644 --- a/main/src/main/res/values/untranslatable.xml +++ b/main/src/main/res/values/untranslatable.xml @@ -86,6 +86,7 @@ URL Pause VPN when screen is off and less than 64 kB transferred data in 60s Enable the workaround to use an on boot receiver to start the VPN if the Always On VPN functionality is not available + Keep the VPN connected even when no network is detected, e.g. when reverse tethering over USB using adb List of apps that are allowed to use the remote AIDL. If this list is in the restrictions, the app will not allowed any changes to the list by the user. Package names of allowed apps separated by comma, space or newlines Remote API access diff --git a/main/src/main/res/xml/app_restrictions.xml b/main/src/main/res/xml/app_restrictions.xml index 81171d6f..9843fada 100644 --- a/main/src/main/res/xml/app_restrictions.xml +++ b/main/src/main/res/xml/app_restrictions.xml @@ -76,6 +76,10 @@ android:key="restartvpnonboot" android:restrictionType="bool" android:title="@string/restriction_restartvpnonboot" /> +