Skip to content

Commit ace1b6f

Browse files
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 <estern@digital.ai>
1 parent ad7dfd5 commit ace1b6f

File tree

4 files changed

+19
-8
lines changed

4 files changed

+19
-8
lines changed

main/src/main/java/de/blinkt/openvpn/api/AppRestrictions.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
import de.blinkt.openvpn.VpnProfile;
1616
import de.blinkt.openvpn.core.ConfigParser;
17-
import de.blinkt.openvpn.core.OpenVPNService;
1817
import de.blinkt.openvpn.core.Preferences;
1918
import de.blinkt.openvpn.core.ProfileManager;
2019
import de.blinkt.openvpn.core.VpnStatus;
@@ -147,6 +146,13 @@ private static void setMiscSettings(Context c, Bundle restrictions) {
147146
editor.putBoolean("screenoff", pauseVPN);
148147
editor.apply();
149148
}
149+
if(restrictions.containsKey("ignorenetworkstate"))
150+
{
151+
boolean ignoreNetworkState = restrictions.getBoolean("ignorenetworkstate");
152+
SharedPreferences.Editor editor = defaultPrefs.edit();
153+
editor.putBoolean("ignorenetstate", ignoreNetworkState);
154+
editor.apply();
155+
}
150156
if (restrictions.containsKey("restartvpnonboot"))
151157
{
152158
boolean restartVPNonBoot = restrictions.getBoolean("restartvpnonboot");

main/src/main/java/de/blinkt/openvpn/core/DeviceStateReceiver.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,11 @@
1414
import android.net.NetworkInfo.State;
1515
import android.os.Handler;
1616
import android.os.Looper;
17-
import android.preference.PreferenceManager;
1817

1918
import de.blinkt.openvpn.R;
2019
import de.blinkt.openvpn.core.VpnStatus.ByteCountListener;
2120

2221
import java.util.LinkedList;
23-
import java.util.Objects;
24-
import java.util.StringTokenizer;
2522

2623
import static de.blinkt.openvpn.core.OpenVPNManagement.pauseReason;
2724

@@ -38,7 +35,6 @@ public class DeviceStateReceiver extends BroadcastReceiver implements ByteCountL
3835
// Time to wait after network disconnect to pause the VPN
3936
private final int DISCONNECT_WAIT = 20;
4037

41-
4238
connectState network = connectState.DISCONNECTED;
4339
connectState screen = connectState.SHOULDBECONNECTED;
4440
connectState userpause = connectState.SHOULDBECONNECTED;
@@ -179,12 +175,16 @@ public static boolean equalsObj(Object a, Object b) {
179175
return (a == null) ? (b == null) : a.equals(b);
180176
}
181177

182-
183178
public void networkStateChange(Context context) {
184-
NetworkInfo networkInfo = getCurrentNetworkInfo(context);
185179
SharedPreferences prefs = Preferences.getDefaultSharedPreferences(context);
186-
boolean sendusr1 = prefs.getBoolean("netchangereconnect", true);
180+
boolean ignoreNetworkState = prefs.getBoolean("ignorenetstate", false);
181+
if (ignoreNetworkState) {
182+
network = connectState.SHOULDBECONNECTED;
183+
return;
184+
}
187185

186+
NetworkInfo networkInfo = getCurrentNetworkInfo(context);
187+
boolean sendusr1 = prefs.getBoolean("netchangereconnect", true);
188188

189189
String netstatestring;
190190
if (networkInfo == null) {

main/src/main/res/values/untranslatable.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
<string name="import_from_URL">URL</string>
8787
<string name="restriction_pausevpn">Pause VPN when screen is off and less than 64 kB transferred data in 60s</string>
8888
<string name="restriction_restartvpnonboot">Enable the workaround to use an on boot receiver to start the VPN if the Always On VPN functionality is not available</string>
89+
<string name="restriction_ignorenetworkstate">Keep the VPN connected even when no network is detected, e.g. when reverse tethering over USB using adb</string>
8990
<string name="apprest_aidl_list">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</string>
9091
<string name="apprest_remoteaidl">Remote API access</string>
9192

main/src/main/res/xml/app_restrictions.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@
7676
android:key="restartvpnonboot"
7777
android:restrictionType="bool"
7878
android:title="@string/restriction_restartvpnonboot" />
79+
<restriction
80+
android:key="ignorenetworkstate"
81+
android:restrictionType="bool"
82+
android:title="@string/restriction_ignorenetworkstate" />
7983

8084
<restriction
8185
android:description="@string/apprest_aidl_list"

0 commit comments

Comments
 (0)