Skip to content

Commit

Permalink
Add ignorenetworkstate restriction, to allow tunnel over USB (schwabe…
Browse files Browse the repository at this point in the history
…#1746)

* Add ignorenetworkstate restriction, to allow tunnel over USB

* Add to app_restrictions.xml

* Parse restriction value into SharedPreferences


Co-authored-by: Eliyahu Stern <[email protected]>
  • Loading branch information
2 people authored and czdawid committed Sep 11, 2024
1 parent d26e60d commit 0d2651e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

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;
Expand Down Expand Up @@ -147,6 +146,13 @@ private static void setMiscSettings(Context c, Bundle restrictions) {
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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,11 @@
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;

Expand All @@ -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;
Expand Down Expand Up @@ -179,12 +175,16 @@ public static boolean equalsObj(Object a, Object b) {
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) {
Expand Down
1 change: 1 addition & 0 deletions main/src/main/res/values/untranslatable.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
<string name="import_from_URL">URL</string>
<string name="restriction_pausevpn">Pause VPN when screen is off and less than 64 kB transferred data in 60s</string>
<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>
<string name="restriction_ignorenetworkstate">Keep the VPN connected even when no network is detected, e.g. when reverse tethering over USB using adb</string>
<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>
<string name="apprest_remoteaidl">Remote API access</string>

Expand Down
4 changes: 4 additions & 0 deletions main/src/main/res/xml/app_restrictions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@
android:key="restartvpnonboot"
android:restrictionType="bool"
android:title="@string/restriction_restartvpnonboot" />
<restriction
android:key="ignorenetworkstate"
android:restrictionType="bool"
android:title="@string/restriction_ignorenetworkstate" />

<restriction
android:description="@string/apprest_aidl_list"
Expand Down

0 comments on commit 0d2651e

Please sign in to comment.