summaryrefslogtreecommitdiff
path: root/app/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java')
-rw-r--r--app/src/main/java/de/blinkt/openvpn/core/OpenVPNService.java3
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/Constants.java6
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/firewall/ConfigureTetheringTask.java111
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/firewall/FirewallCallback.java3
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/firewall/FirewallManager.java75
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/firewall/SetupTetheringTask.java187
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/firewall/ShutdownTetheringTask.java89
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/fragments/TetheringDialog.java26
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/tethering/TetheringBroadcastReceiver.java11
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/tethering/TetheringObservable.java62
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/tethering/TetheringState.java34
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/tethering/TetheringStateManager.java70
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/utils/PreferenceHelper.java36
13 files changed, 503 insertions, 210 deletions
diff --git a/app/src/main/java/de/blinkt/openvpn/core/OpenVPNService.java b/app/src/main/java/de/blinkt/openvpn/core/OpenVPNService.java
index a48d6477..72962493 100644
--- a/app/src/main/java/de/blinkt/openvpn/core/OpenVPNService.java
+++ b/app/src/main/java/de/blinkt/openvpn/core/OpenVPNService.java
@@ -518,7 +518,7 @@ public class OpenVPNService extends VpnService implements StateListener, Callbac
super.onCreate();
notificationManager = new VpnNotificationManager(this, this);
notificationManager.createOpenVpnNotificationChannel();
- firewallManager = new FirewallManager(this);
+ firewallManager = new FirewallManager(this, true);
}
@Override
@@ -537,6 +537,7 @@ public class OpenVPNService extends VpnService implements StateListener, Callbac
VpnStatus.flushLog();
notificationManager.deleteNotificationChannel(NOTIFICATION_CHANNEL_BG_ID);
notificationManager.deleteNotificationChannel(NOTIFICATION_CHANNEL_NEWSTATUS_ID);
+ firewallManager.onDestroy();
}
private String getTunConfigString() {
diff --git a/app/src/main/java/se/leap/bitmaskclient/Constants.java b/app/src/main/java/se/leap/bitmaskclient/Constants.java
index 58a92360..6a065d84 100644
--- a/app/src/main/java/se/leap/bitmaskclient/Constants.java
+++ b/app/src/main/java/se/leap/bitmaskclient/Constants.java
@@ -16,9 +16,9 @@ public interface Constants {
String EXCLUDED_APPS = "excluded_apps";
String USE_PLUGGABLE_TRANSPORTS = "usePluggableTransports";
String SU_PERMISSION = "su_permission";
- String TETHERING_BLUETOOTH = "tethering_bluetooth";
- String TETHERING_WIFI = "tethering_wifi";
- String TETHERING_USB = "tethering_usb";
+ String ALLOW_TETHERING_BLUETOOTH = "tethering_bluetooth";
+ String ALLOW_TETHERING_WIFI = "tethering_wifi";
+ String ALLOW_TETHERING_USB = "tethering_usb";
//////////////////////////////////////////////
diff --git a/app/src/main/java/se/leap/bitmaskclient/firewall/ConfigureTetheringTask.java b/app/src/main/java/se/leap/bitmaskclient/firewall/ConfigureTetheringTask.java
deleted file mode 100644
index 3363152e..00000000
--- a/app/src/main/java/se/leap/bitmaskclient/firewall/ConfigureTetheringTask.java
+++ /dev/null
@@ -1,111 +0,0 @@
-/**
- * Copyright (c) 2020 LEAP Encryption Access Project and contributers
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-package se.leap.bitmaskclient.firewall;
-
-import android.os.AsyncTask;
-import android.util.Log;
-
-import java.lang.ref.WeakReference;
-
-import se.leap.bitmaskclient.tethering.TetheringState;
-
-import static se.leap.bitmaskclient.firewall.FirewallManager.BITMASK_FORWARD;
-import static se.leap.bitmaskclient.firewall.FirewallManager.BITMASK_POSTROUTING;
-import static se.leap.bitmaskclient.utils.Cmd.runBlockingCmd;
-
-public class ConfigureTetheringTask extends AsyncTask<TetheringState, Boolean, Boolean> {
-
- private WeakReference<FirewallCallback> callbackWeakReference;
-
- ConfigureTetheringTask(FirewallCallback callback) {
- callbackWeakReference = new WeakReference<>(callback);
- }
-
- @Override
- protected Boolean doInBackground(TetheringState... tetheringStates) {
- TetheringState tetheringState = tetheringStates[0];
- StringBuilder log = new StringBuilder();
-
- String[] bitmaskChain = new String[]{
- "su",
- "id",
- "iptables --list " + BITMASK_FORWARD + " && iptables --list " + BITMASK_POSTROUTING };
-
- try {
- boolean hasBitmaskChain = runBlockingCmd(bitmaskChain, log) == 0;
- boolean allowSu = log.toString().contains("uid=0");
- callbackWeakReference.get().onSuRequested(allowSu);
- if (!allowSu) {
- return false;
- }
-
- boolean success;
- log = new StringBuilder();
- if (hasAnyTetheringEnabled(tetheringState)) {
- if (!hasBitmaskChain) {
- String[] createChains = new String[]{
- "su",
- "iptables -t filter --new-chain " + BITMASK_FORWARD,
- "iptables -t nat --new-chain " + BITMASK_POSTROUTING,
- "iptables -t filter --insert FORWARD --jump " + BITMASK_FORWARD,
- "iptables -t nat --insert POSTROUTING --jump " + BITMASK_POSTROUTING,
- };
- success = runBlockingCmd(createChains, log) == 0;
- Log.d(FirewallManager.TAG, "added " + BITMASK_FORWARD + " and " + BITMASK_POSTROUTING+" to iptables: " + success);
- Log.d(FirewallManager.TAG, log.toString());
- }
-
- String[] addRules = new String[] {
- "su",
- "iptables -t filter --flush " + BITMASK_FORWARD,
- "iptables -t nat --flush " + BITMASK_POSTROUTING,
- "iptables -t filter --append " + BITMASK_FORWARD + " --jump ACCEPT",
- "iptables -t nat --append " + BITMASK_POSTROUTING + " --jump MASQUERADE" };
- return runBlockingCmd(addRules, log) == 0;
- } else {
- if (!hasBitmaskChain) return true;
- String[] removeChains = new String[] {
- "su",
- "iptables -t filter --delete FORWARD --jump " + BITMASK_FORWARD,
- "iptables -t nat --delete POSTROUTING --jump " + BITMASK_POSTROUTING,
- "iptables -t filter --flush " + BITMASK_FORWARD,
- "iptables -t nat --flush " + BITMASK_POSTROUTING,
- "iptables -t filter --delete-chain " + BITMASK_FORWARD,
- "iptables -t nat --delete-chain " + BITMASK_POSTROUTING
- };
- return runBlockingCmd(removeChains, log) == 0;
- }
- } catch (Exception e) {
- e.printStackTrace();
- Log.e(FirewallManager.TAG, log.toString());
- }
- return false;
- }
-
- @Override
- protected void onPostExecute(Boolean result) {
- super.onPostExecute(result);
- FirewallCallback callback = callbackWeakReference.get();
- if (callback != null) {
- callback.onTetheringConfigured(result);
- }
- }
-
- private boolean hasAnyTetheringEnabled(TetheringState state) {
- return state.isBluetoothTetheringEnabled || state.isUsbTetheringEnabled || state.isWifiTetheringEnabled;
- }
-}
diff --git a/app/src/main/java/se/leap/bitmaskclient/firewall/FirewallCallback.java b/app/src/main/java/se/leap/bitmaskclient/firewall/FirewallCallback.java
index c6b3934c..15fa426f 100644
--- a/app/src/main/java/se/leap/bitmaskclient/firewall/FirewallCallback.java
+++ b/app/src/main/java/se/leap/bitmaskclient/firewall/FirewallCallback.java
@@ -19,6 +19,7 @@ package se.leap.bitmaskclient.firewall;
interface FirewallCallback {
void onFirewallStarted(boolean success);
void onFirewallStopped(boolean success);
- void onTetheringConfigured(boolean success);
+ void onTetheringStarted(boolean success);
+ void onTetheringStopped(boolean success);
void onSuRequested(boolean success);
}
diff --git a/app/src/main/java/se/leap/bitmaskclient/firewall/FirewallManager.java b/app/src/main/java/se/leap/bitmaskclient/firewall/FirewallManager.java
index 78166671..82888668 100644
--- a/app/src/main/java/se/leap/bitmaskclient/firewall/FirewallManager.java
+++ b/app/src/main/java/se/leap/bitmaskclient/firewall/FirewallManager.java
@@ -18,13 +18,15 @@ package se.leap.bitmaskclient.firewall;
import android.content.Context;
+import java.util.Observable;
+import java.util.Observer;
+
import de.blinkt.openvpn.core.VpnStatus;
import se.leap.bitmaskclient.tethering.TetheringObservable;
import se.leap.bitmaskclient.tethering.TetheringState;
import se.leap.bitmaskclient.utils.PreferenceHelper;
-
-public class FirewallManager implements FirewallCallback {
+public class FirewallManager implements FirewallCallback, Observer {
public static String BITMASK_CHAIN = "bitmask_fw";
public static String BITMASK_FORWARD = "bitmask_forward";
public static String BITMASK_POSTROUTING = "bitmask_postrouting";
@@ -32,11 +34,13 @@ public class FirewallManager implements FirewallCallback {
private Context context;
- public FirewallManager(Context context) {
+ public FirewallManager(Context context, boolean observeTethering) {
this.context = context;
+ if (observeTethering) {
+ TetheringObservable.getInstance().addObserver(this);
+ }
}
-
@Override
public void onFirewallStarted(boolean success) {
if (success) {
@@ -56,11 +60,20 @@ public class FirewallManager implements FirewallCallback {
}
@Override
- public void onTetheringConfigured(boolean success) {
+ public void onTetheringStarted(boolean success) {
+ if (success) {
+ VpnStatus.logInfo("[FIREWALL] Rules for tethering enabled");
+ } else {
+ VpnStatus.logError("[FIREWALL] Could not enable rules for tethering.");
+ }
+ }
+
+ @Override
+ public void onTetheringStopped(boolean success) {
if (success) {
- VpnStatus.logInfo("[FIREWALL] Rules for tethering configured");
+ VpnStatus.logInfo("[FIREWALL] Rules for tethering successfully disabled");
} else {
- VpnStatus.logError("[FIREWALL] Could not establish rules for tethering ");
+ VpnStatus.logError("[FIREWALL] Could not disable rules for tethering.");
}
}
@@ -72,30 +85,32 @@ public class FirewallManager implements FirewallCallback {
}
}
+ public void onDestroy() {
+ TetheringObservable.getInstance().deleteObserver(this);
+ }
+
public void start() {
startIPv6Firewall();
- if (TetheringObservable.getInstance().hasAnyTetheringEnabled()) {
- TetheringState deviceTethering = TetheringObservable.getInstance().getTetheringState();
- TetheringState vpnTethering = new TetheringState();
- vpnTethering.isWifiTetheringEnabled = deviceTethering.isWifiTetheringEnabled && PreferenceHelper.getWifiTethering(context);
- vpnTethering.isUsbTetheringEnabled = deviceTethering.isUsbTetheringEnabled && PreferenceHelper.getUsbTethering(context);
- vpnTethering.isBluetoothTetheringEnabled = deviceTethering.isBluetoothTetheringEnabled && PreferenceHelper.getBluetoothTethering(context);
- configureTethering(vpnTethering);
+ TetheringState tetheringState = TetheringObservable.getInstance().getTetheringState();
+ if (tetheringState.hasAnyDeviceTetheringEnabled() && tetheringState.hasAnyVpnTetheringAllowed()) {
+ startTethering();
}
}
+
public void stop() {
- shutdownIPv6Firewall();
- TetheringState allowedTethering = new TetheringState();
- allowedTethering.isWifiTetheringEnabled = PreferenceHelper.getWifiTethering(context);
- allowedTethering.isUsbTetheringEnabled = PreferenceHelper.getUsbTethering(context);
- allowedTethering.isBluetoothTetheringEnabled = PreferenceHelper.getBluetoothTethering(context);
- configureTethering(allowedTethering);
+ stopIPv6Firewall();
+ stopTethering();
+ }
+
+ public void startTethering() {
+ SetupTetheringTask task = new SetupTetheringTask(this);
+ task.execute();
}
- public void configureTethering(TetheringState state) {
- ConfigureTetheringTask task = new ConfigureTetheringTask(this);
- task.execute(state);
+ private void stopTethering() {
+ ShutdownTetheringTask task = new ShutdownTetheringTask(this);
+ task.execute();
}
private void startIPv6Firewall() {
@@ -103,9 +118,21 @@ public class FirewallManager implements FirewallCallback {
task.execute();
}
- private void shutdownIPv6Firewall() {
+ private void stopIPv6Firewall() {
ShutdownIPv6FirewallTask task = new ShutdownIPv6FirewallTask(this);
task.execute();
}
+ @Override
+ public void update(Observable o, Object arg) {
+ if (o instanceof TetheringObservable) {
+ TetheringObservable observable = (TetheringObservable) o;
+ TetheringState state = observable.getTetheringState();
+ if (state.hasAnyVpnTetheringAllowed() && state.hasAnyDeviceTetheringEnabled()) {
+ startTethering();
+ } else {
+ stopTethering();
+ }
+ }
+ }
}
diff --git a/app/src/main/java/se/leap/bitmaskclient/firewall/SetupTetheringTask.java b/app/src/main/java/se/leap/bitmaskclient/firewall/SetupTetheringTask.java
new file mode 100644
index 00000000..93054099
--- /dev/null
+++ b/app/src/main/java/se/leap/bitmaskclient/firewall/SetupTetheringTask.java
@@ -0,0 +1,187 @@
+/**
+ * Copyright (c) 2020 LEAP Encryption Access Project and contributers
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package se.leap.bitmaskclient.firewall;
+
+import android.os.AsyncTask;
+import android.util.Log;
+
+import java.lang.ref.WeakReference;
+import java.net.NetworkInterface;
+import java.util.Enumeration;
+
+import se.leap.bitmaskclient.tethering.TetheringObservable;
+import se.leap.bitmaskclient.tethering.TetheringState;
+
+import static se.leap.bitmaskclient.firewall.FirewallManager.BITMASK_FORWARD;
+import static se.leap.bitmaskclient.firewall.FirewallManager.BITMASK_POSTROUTING;
+import static se.leap.bitmaskclient.utils.Cmd.runBlockingCmd;
+
+public class SetupTetheringTask extends AsyncTask<Void, Boolean, Boolean> {
+
+ private static final String TAG = SetupTetheringTask.class.getSimpleName();
+ private WeakReference<FirewallCallback> callbackWeakReference;
+
+ SetupTetheringTask(FirewallCallback callback) {
+ callbackWeakReference = new WeakReference<>(callback);
+ }
+
+ @Override
+ protected Boolean doInBackground(Void... args) {
+ TetheringState tetheringState = TetheringObservable.getInstance().getTetheringState();
+ StringBuilder log = new StringBuilder();
+
+ String[] bitmaskChain = new String[]{
+ "su",
+ "id",
+ "iptables -t filter --list " + BITMASK_FORWARD + " && iptables -t nat --list " + BITMASK_POSTROUTING };
+
+ try {
+ boolean hasBitmaskChain = runBlockingCmd(bitmaskChain, log) == 0;
+ boolean allowSu = log.toString().contains("uid=0");
+ callbackWeakReference.get().onSuRequested(allowSu);
+ if (!allowSu) {
+ return false;
+ }
+
+ boolean success = true;
+ log = new StringBuilder();
+
+ if (!hasBitmaskChain && tetheringState.hasAnyVpnTetheringAllowed() && tetheringState.hasAnyDeviceTetheringEnabled()) {
+ createChains(log);
+ }
+
+ if (tetheringState.tetherWifiVpn()) {
+ log = new StringBuilder();
+ success = addWifiTetheringRules(tetheringState, log);
+ } else if (!tetheringState.isVpnWifiTetheringAllowed){
+ success = removeWifiTetheringRules(tetheringState, log);
+ }
+
+ if (tetheringState.tetherUsbVpn()) {
+ success = success && addUsbTetheringRules(tetheringState, log);
+ } else if (!tetheringState.isVpnUsbTetheringAllowed) {
+ success = success && removeUsbTetheringRules(tetheringState, log);
+ }
+
+ if (tetheringState.tetherBluetoothVpn()) {
+ success = success && addBluetoothTetheringRules(tetheringState, log);
+ } else if (!tetheringState.isVpnBluetoothTetheringAllowed) {
+ success = success && removeBluetoothTetheringRules(tetheringState, log);
+ }
+ return success;
+ } catch (Exception e) {
+ e.printStackTrace();
+ Log.e(FirewallManager.TAG, log.toString());
+ }
+ return false;
+ }
+
+
+ //TODO: implement the follwing methods -v
+ private boolean removeBluetoothTetheringRules(TetheringState tetheringState, StringBuilder log) {
+ return true;
+ }
+
+ private boolean removeUsbTetheringRules(TetheringState tetheringState, StringBuilder log) {
+ return true;
+ }
+
+ private boolean addBluetoothTetheringRules(TetheringState tetheringState, StringBuilder log) {
+ return true;
+ }
+
+ private boolean addUsbTetheringRules(TetheringState tetheringState, StringBuilder log) {
+ return true;
+ }
+
+
+ private void createChains(StringBuilder log) throws Exception {
+ boolean success;
+ String[] createChains = new String[]{
+ "su",
+ "iptables -t filter --new-chain " + BITMASK_FORWARD,
+ "iptables -t nat --new-chain " + BITMASK_POSTROUTING,
+ "iptables -t filter --insert FORWARD --jump " + BITMASK_FORWARD,
+ "iptables -t nat --insert POSTROUTING --jump " + BITMASK_POSTROUTING,
+ };
+ success = runBlockingCmd(createChains, log) == 0;
+ Log.d(FirewallManager.TAG, "added " + BITMASK_FORWARD + " and " + BITMASK_POSTROUTING+" to iptables: " + success);
+ Log.d(FirewallManager.TAG, log.toString());
+ }
+
+ private boolean addWifiTetheringRules(TetheringState state, StringBuilder log) throws Exception {
+ Log.d(TAG, "add Wifi tethering Rules");
+ String[] addRules = new String[] {
+ "su",
+ "iptables -t filter --flush " + BITMASK_FORWARD,
+ "iptables -t nat --flush " + BITMASK_POSTROUTING,
+ "iptables -t filter --append " + BITMASK_FORWARD + " --jump ACCEPT",
+ "iptables -t nat --append " + BITMASK_POSTROUTING + " --jump MASQUERADE",
+ "if [[ ! `ip rule show from "+ state.wifiAddress+" lookup 61` ]]; " +
+ "then ip rule add from " + state.wifiAddress + " lookup 61; " +
+ "fi",
+ "if [[ ! `ip route list table 61 | grep 'default dev " + getTunName() + " scope link'` ]]; " +
+ "then ip route add default dev " + getTunName() + " scope link table 61; " +
+ "fi",
+ "if [[ ! `ip route list table 61 | grep '"+ state.wifiAddress+" dev "+ state.wifiInterface+" scope link'` ]]; " +
+ "then ip route add " + state.wifiAddress + " dev " + state.wifiInterface + " scope link table 61; " +
+ "fi",
+ "if [[ ! `ip route list table 61 | grep 'broadcast 255.255.255.255 dev " + state.wifiInterface + " scope link'` ]]; " +
+ "then ip route add broadcast 255.255.255.255 dev " + state.wifiInterface + " scope link table 61; " +
+ "fi"
+ };
+
+ return runBlockingCmd(addRules, log) == 0;
+ }
+
+ private boolean removeWifiTetheringRules(TetheringState state, StringBuilder log) throws Exception {
+ Log.d(TAG, "add Wifi tethering Rules");
+ String[] removeRules = new String[] {
+ "su",
+ "ip route delete broadcast 255.255.255.255 dev " + state.wifiInterface +" scope link table 61",
+ "ip route delete " + state.lastWifiAddress + " dev " + state.wifiInterface +" scope link table 61",
+ "ip route delete default dev " + getTunName() + " scope link table 61",
+ "if [[ `ip rule show from " + state.wifiAddress+ " lookup 61` ]]; " +
+ "then ip rule del from " + state.wifiAddress + " lookup 61; " +
+ "fi",
+ };
+ return runBlockingCmd(removeRules, log) == 0;
+ }
+
+ private String getTunName() {
+ try {
+ for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
+ NetworkInterface networkInterface = en.nextElement();
+ if (networkInterface.getName().contains("tun")) {
+ return networkInterface.getName();
+ }
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ return "";
+ }
+
+ @Override
+ protected void onPostExecute(Boolean result) {
+ super.onPostExecute(result);
+ FirewallCallback callback = callbackWeakReference.get();
+ if (callback != null) {
+ callback.onTetheringStarted(result);
+ }
+ }
+}
diff --git a/app/src/main/java/se/leap/bitmaskclient/firewall/ShutdownTetheringTask.java b/app/src/main/java/se/leap/bitmaskclient/firewall/ShutdownTetheringTask.java
new file mode 100644
index 00000000..6c15c3e3
--- /dev/null
+++ b/app/src/main/java/se/leap/bitmaskclient/firewall/ShutdownTetheringTask.java
@@ -0,0 +1,89 @@
+/**
+ * Copyright (c) 2020 LEAP Encryption Access Project and contributers
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package se.leap.bitmaskclient.firewall;
+
+import android.os.AsyncTask;
+import android.util.Log;
+
+import java.lang.ref.WeakReference;
+
+import se.leap.bitmaskclient.tethering.TetheringObservable;
+import se.leap.bitmaskclient.tethering.TetheringState;
+
+import static se.leap.bitmaskclient.firewall.FirewallManager.BITMASK_FORWARD;
+import static se.leap.bitmaskclient.firewall.FirewallManager.BITMASK_POSTROUTING;
+import static se.leap.bitmaskclient.utils.Cmd.runBlockingCmd;
+
+public class ShutdownTetheringTask extends AsyncTask<Void, Boolean, Boolean> {
+
+ private WeakReference<FirewallCallback> callbackWeakReference;
+
+ ShutdownTetheringTask(FirewallCallback callback) {
+ callbackWeakReference = new WeakReference<>(callback);
+ }
+
+ @Override
+ protected Boolean doInBackground(Void... args) {
+ TetheringState tetheringState = TetheringObservable.getInstance().getTetheringState();
+ StringBuilder log = new StringBuilder();
+
+ String[] bitmaskChain = new String[]{
+ "su",
+ "id",
+ "iptables -t filter --list " + BITMASK_FORWARD + " && iptables -t nat --list " + BITMASK_POSTROUTING };
+
+ try {
+ boolean hasBitmaskChain = runBlockingCmd(bitmaskChain, log) == 0;
+ boolean allowSu = log.toString().contains("uid=0");
+ callbackWeakReference.get().onSuRequested(allowSu);
+ if (!allowSu || !hasBitmaskChain) {
+ return false;
+ }
+
+ log = new StringBuilder();
+
+ String[] removeChains = new String[] {
+ "su",
+ "ip route flush table 61",
+ "if [[ `ip rule show from " + tetheringState.wifiAddress+ " lookup 61` ]]; " +
+ "then ip rule del from " + tetheringState.wifiAddress + " lookup 61; " +
+ "fi",
+ "iptables -t filter --delete FORWARD --jump " + BITMASK_FORWARD,
+ "iptables -t nat --delete POSTROUTING --jump " + BITMASK_POSTROUTING,
+ "iptables -t filter --flush " + BITMASK_FORWARD,
+ "iptables -t nat --flush " + BITMASK_POSTROUTING,
+ "iptables -t filter --delete-chain " + BITMASK_FORWARD,
+ "iptables -t nat --delete-chain " + BITMASK_POSTROUTING,
+ };
+ return runBlockingCmd(removeChains, log) == 0;
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ Log.e(FirewallManager.TAG, log.toString());
+ }
+ return false;
+ }
+
+ @Override
+ protected void onPostExecute(Boolean result) {
+ super.onPostExecute(result);
+ FirewallCallback callback = callbackWeakReference.get();
+ if (callback != null) {
+ callback.onTetheringStarted(result);
+ }
+ }
+}
diff --git a/app/src/main/java/se/leap/bitmaskclient/fragments/TetheringDialog.java b/app/src/main/java/se/leap/bitmaskclient/fragments/TetheringDialog.java
index a8f96986..2e1e8f95 100644
--- a/app/src/main/java/se/leap/bitmaskclient/fragments/TetheringDialog.java
+++ b/app/src/main/java/se/leap/bitmaskclient/fragments/TetheringDialog.java
@@ -26,10 +26,8 @@ import java.util.Observer;
import butterknife.ButterKnife;
import butterknife.InjectView;
import se.leap.bitmaskclient.R;
-import se.leap.bitmaskclient.eip.EipCommand;
import se.leap.bitmaskclient.firewall.FirewallManager;
import se.leap.bitmaskclient.tethering.TetheringObservable;
-import se.leap.bitmaskclient.tethering.TetheringState;
import se.leap.bitmaskclient.utils.PreferenceHelper;
import se.leap.bitmaskclient.views.IconCheckboxEntry;
@@ -136,16 +134,14 @@ public class TetheringDialog extends AppCompatDialogFragment implements Observer
builder.setView(view)
.setPositiveButton(android.R.string.ok, (dialog, id) -> {
- PreferenceHelper.wifiTethering(getContext(), dataset[0].checked);
- PreferenceHelper.usbTethering(getContext(), dataset[1].checked);
- PreferenceHelper.bluetoothTethering(getContext(), dataset[2].checked);
- FirewallManager firewallManager = new FirewallManager(getContext().getApplicationContext());
- TetheringState runningTethering = TetheringObservable.getInstance().getTetheringState();
- TetheringState vpnTethering = new TetheringState();
- vpnTethering.isWifiTetheringEnabled = runningTethering.isWifiTetheringEnabled && dataset[0].checked;
- vpnTethering.isUsbTetheringEnabled = runningTethering.isUsbTetheringEnabled && dataset[1].checked;
- vpnTethering.isBluetoothTetheringEnabled = runningTethering.isBluetoothTetheringEnabled && dataset[2].checked;
- firewallManager.configureTethering(vpnTethering);
+ PreferenceHelper.allowWifiTethering(getContext(), dataset[0].checked);
+ PreferenceHelper.allowUsbTethering(getContext(), dataset[1].checked);
+ PreferenceHelper.allowBluetoothTethering(getContext(), dataset[2].checked);
+ TetheringObservable.allowVpnWifiTethering(dataset[0].checked);
+ TetheringObservable.allowVpnUsbTethering(dataset[1].checked);
+ TetheringObservable.allowVpnBluetoothTethering(dataset[2].checked);
+ FirewallManager firewallManager = new FirewallManager(getContext().getApplicationContext(), false);
+ firewallManager.startTethering();
})
.setNegativeButton(R.string.cancel, (dialog, id) -> dialog.cancel());
return builder.create();
@@ -195,15 +191,15 @@ public class TetheringDialog extends AppCompatDialogFragment implements Observer
dataset = new DialogListAdapter.ViewModel[] {
new DialogListAdapter.ViewModel(getContext().getResources().getDrawable(R.drawable.ic_wifi),
getContext().getString(R.string.tethering_wifi),
- PreferenceHelper.getWifiTethering(getContext()),
+ PreferenceHelper.isWifiTetheringAllowed(getContext()),
TetheringObservable.getInstance().isWifiTetheringEnabled()),
new DialogListAdapter.ViewModel(getContext().getResources().getDrawable(R.drawable.ic_usb),
getContext().getString(R.string.tethering_usb),
- PreferenceHelper.getUsbTethering(getContext()),
+ PreferenceHelper.isUsbTetheringAllowed(getContext()),
TetheringObservable.getInstance().isUsbTetheringEnabled()),
new DialogListAdapter.ViewModel(getContext().getResources().getDrawable(R.drawable.ic_bluetooth),
getContext().getString(R.string.tethering_bluetooth),
- PreferenceHelper.getBluetoothTethering(getContext()),
+ PreferenceHelper.isBluetoothTetheringAllowed(getContext()),
TetheringObservable.getInstance().isUsbTetheringEnabled())
};
}
diff --git a/app/src/main/java/se/leap/bitmaskclient/tethering/TetheringBroadcastReceiver.java b/app/src/main/java/se/leap/bitmaskclient/tethering/TetheringBroadcastReceiver.java
index 8dab49ce..369a6cf6 100644
--- a/app/src/main/java/se/leap/bitmaskclient/tethering/TetheringBroadcastReceiver.java
+++ b/app/src/main/java/se/leap/bitmaskclient/tethering/TetheringBroadcastReceiver.java
@@ -30,16 +30,7 @@ public class TetheringBroadcastReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
if ("android.net.wifi.WIFI_AP_STATE_CHANGED".equals(intent.getAction())) {
Log.d(TAG, "TETHERING WIFI_AP_STATE_CHANGED");
- int apState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
- if (WifiHotspotState.WIFI_AP_STATE_ENABLED.ordinal() == apState % 10) {
- if (!TetheringObservable.getInstance().isWifiTetheringEnabled()) {
- TetheringObservable.setWifiTethering(true);
- }
- } else {
- if (TetheringObservable.getInstance().isWifiTetheringEnabled()) {
- TetheringObservable.setWifiTethering(false);
- }
- }
+ TetheringStateManager.updateWifiTetheringState();
} else if ("android.net.conn.TETHER_STATE_CHANGED".equals(intent.getAction())) {
Log.d(TAG, "TETHERING TETHER_STATE_CHANGED");
TetheringStateManager.updateUsbTetheringState();
diff --git a/app/src/main/java/se/leap/bitmaskclient/tethering/TetheringObservable.java b/app/src/main/java/se/leap/bitmaskclient/tethering/TetheringObservable.java
index 79c1ec6d..b84f3494 100644
--- a/app/src/main/java/se/leap/bitmaskclient/tethering/TetheringObservable.java
+++ b/app/src/main/java/se/leap/bitmaskclient/tethering/TetheringObservable.java
@@ -34,22 +34,60 @@ public class TetheringObservable extends Observable {
return instance;
}
- static void setWifiTethering(boolean enabled) {
- getInstance().tetheringState.isWifiTetheringEnabled = enabled;
- getInstance().setChanged();
- getInstance().notifyObservers();
+ public static void allowVpnWifiTethering(boolean enabled) {
+ if (getInstance().tetheringState.isVpnWifiTetheringAllowed != enabled) {
+ getInstance().tetheringState.isVpnWifiTetheringAllowed = enabled;
+ getInstance().setChanged();
+ getInstance().notifyObservers();
+ }
+ }
+
+ public static void allowVpnUsbTethering(boolean enabled) {
+ if (getInstance().tetheringState.isUsbTetheringEnabled != enabled) {
+ getInstance().tetheringState.isUsbTetheringEnabled = enabled;
+ getInstance().setChanged();
+ getInstance().notifyObservers();
+ }
+ }
+
+ public static void allowVpnBluetoothTethering(boolean enabled) {
+ if (getInstance().tetheringState.isBluetoothTetheringEnabled != enabled) {
+ getInstance().tetheringState.isBluetoothTetheringEnabled = enabled;
+ getInstance().setChanged();
+ getInstance().notifyObservers();
+ }
+ }
+
+ static void setWifiTethering(boolean enabled, String address, String interfaceName) {
+ if (getInstance().tetheringState.isWifiTetheringEnabled != enabled ||
+ !getInstance().tetheringState.wifiInterface.equals(interfaceName) ||
+ !getInstance().tetheringState.wifiAddress.equals(address)) {
+ getInstance().tetheringState.isWifiTetheringEnabled = enabled;
+ getInstance().tetheringState.wifiInterface = interfaceName;
+ getInstance().tetheringState.wifiAddress = address;
+ if ("".equals(address)) {
+ getInstance().tetheringState.lastWifiAddress = address;
+ }
+ getInstance().setChanged();
+ getInstance().notifyObservers();
+ }
+
}
static void setUsbTethering(boolean enabled) {
- getInstance().tetheringState.isUsbTetheringEnabled = enabled;
- getInstance().setChanged();
- getInstance().notifyObservers();
+ if (getInstance().tetheringState.isUsbTetheringEnabled != enabled) {
+ getInstance().tetheringState.isUsbTetheringEnabled = enabled;
+ getInstance().setChanged();
+ getInstance().notifyObservers();
+ }
}
static void setBluetoothTethering(boolean enabled) {
- getInstance().tetheringState.isBluetoothTetheringEnabled = enabled;
- getInstance().setChanged();
- getInstance().notifyObservers();
+ if (getInstance().tetheringState.isBluetoothTetheringEnabled != enabled) {
+ getInstance().tetheringState.isBluetoothTetheringEnabled = enabled;
+ getInstance().setChanged();
+ getInstance().notifyObservers();
+ }
}
public boolean isBluetoothTetheringEnabled() {
@@ -67,8 +105,4 @@ public class TetheringObservable extends Observable {
public TetheringState getTetheringState() {
return tetheringState;
}
-
- public boolean hasAnyTetheringEnabled() {
- return tetheringState.isBluetoothTetheringEnabled || tetheringState.isUsbTetheringEnabled || tetheringState.isWifiTetheringEnabled;
- }
}
diff --git a/app/src/main/java/se/leap/bitmaskclient/tethering/TetheringState.java b/app/src/main/java/se/leap/bitmaskclient/tethering/TetheringState.java
index 50fead91..9708639a 100644
--- a/app/src/main/java/se/leap/bitmaskclient/tethering/TetheringState.java
+++ b/app/src/main/java/se/leap/bitmaskclient/tethering/TetheringState.java
@@ -1,7 +1,39 @@
package se.leap.bitmaskclient.tethering;
-public class TetheringState {
+public class TetheringState implements Cloneable {
public boolean isWifiTetheringEnabled;
public boolean isUsbTetheringEnabled;
public boolean isBluetoothTetheringEnabled;
+ public boolean isVpnWifiTetheringAllowed;
+ public boolean isVpnUsbTetheringAllowed;
+ public boolean isVpnBluetoothTetheringAllowed;
+ public String wifiInterface = "";
+ public String usbInterface = "";
+ public String bluetoothInterface = "";
+ public String wifiAddress = "";
+ public String lastWifiAddress = "";
+ public String usbAddress = "";
+ public String bluetoothAddress = "";
+
+ public boolean tetherWifiVpn() {
+ return isWifiTetheringEnabled && isVpnWifiTetheringAllowed;
+ }
+
+ public boolean tetherUsbVpn() {
+ return isUsbTetheringEnabled && isVpnUsbTetheringAllowed;
+ }
+
+ public boolean tetherBluetoothVpn() {
+ return isBluetoothTetheringEnabled && isVpnBluetoothTetheringAllowed;
+ }
+
+ public boolean hasAnyDeviceTetheringEnabled() {
+ return isBluetoothTetheringEnabled || isUsbTetheringEnabled || isWifiTetheringEnabled;
+ }
+
+ public boolean hasAnyVpnTetheringAllowed() {
+ return isVpnWifiTetheringAllowed || isVpnUsbTetheringAllowed || isVpnBluetoothTetheringAllowed;
+ }
+
+
}
diff --git a/app/src/main/java/se/leap/bitmaskclient/tethering/TetheringStateManager.java b/app/src/main/java/se/leap/bitmaskclient/tethering/TetheringStateManager.java
index 0d4f56d8..58d67872 100644
--- a/app/src/main/java/se/leap/bitmaskclient/tethering/TetheringStateManager.java
+++ b/app/src/main/java/se/leap/bitmaskclient/tethering/TetheringStateManager.java
@@ -18,12 +18,20 @@ package se.leap.bitmaskclient.tethering;
import android.content.Context;
import android.content.IntentFilter;
+import android.util.Log;
+import java.net.Inet4Address;
+import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
+import java.util.List;
import se.leap.bitmaskclient.utils.Cmd;
+import static se.leap.bitmaskclient.utils.PreferenceHelper.isBluetoothTetheringAllowed;
+import static se.leap.bitmaskclient.utils.PreferenceHelper.isUsbTetheringAllowed;
+import static se.leap.bitmaskclient.utils.PreferenceHelper.isWifiTetheringAllowed;
+
/**
* This manager tries to figure out the current tethering states for Wifi, USB and Bluetooth
* The default behavior differs for failing attempts to get these states:
@@ -55,15 +63,14 @@ public class TetheringStateManager {
intentFilter.addAction("android.net.wifi.WIFI_AP_STATE_CHANGED");
context.getApplicationContext().registerReceiver(broadcastReceiver, intentFilter);
instance.wifiManager = new WifiManagerWrapper(context);
+ TetheringObservable.allowVpnWifiTethering(isWifiTetheringAllowed(context));
+ TetheringObservable.allowVpnUsbTethering(isUsbTetheringAllowed(context));
+ TetheringObservable.allowVpnBluetoothTethering(isBluetoothTetheringAllowed(context));
updateWifiTetheringState();
updateUsbTetheringState();
updateBluetoothTetheringState();
}
- private static boolean isWifiApEnabled() throws Exception {
- return instance.wifiManager.isWifiAPEnabled();
- }
-
private static boolean getUsbTetheringState() {
try {
@@ -82,6 +89,54 @@ public class TetheringStateManager {
return false;
}
+ public static String getWifiAddressRange() {
+ String interfaceAddress = getWifiInterfaceAddress();
+ if (interfaceAddress.split("\\.").length == 4) {
+ String result = interfaceAddress.substring(0, interfaceAddress.lastIndexOf("."));
+ result = result + ".0/24";
+ Log.d(TAG, "wifiAddressRange = " + result);
+ return result;
+ }
+ return "";
+ }
+
+ private static String getWifiInterfaceAddress() {
+ NetworkInterface networkInterface = getWlanInterface();
+ if (networkInterface != null) {
+ List<InterfaceAddress> ifaceAddresses = networkInterface.getInterfaceAddresses();
+ for (InterfaceAddress ifaceAddres : ifaceAddresses) {
+ if (ifaceAddres.getAddress() instanceof Inet4Address) {
+ return ifaceAddres.getAddress().getHostAddress();
+ }
+ }
+ }
+ return "";
+ }
+
+ public static String getWifiInterfaceName() {
+ NetworkInterface networkInterface = getWlanInterface();
+ if (networkInterface != null) {
+ return networkInterface.getName();
+ }
+ return "";
+ }
+
+ private static NetworkInterface getWlanInterface() {
+ try {
+ for(Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
+ NetworkInterface networkInterface = en.nextElement();
+ if(!networkInterface.isLoopback()){
+ if(networkInterface.getName().contains("wlan") || networkInterface.getName().contains("eth")){
+ return networkInterface;
+ }
+ }
+ }
+ } catch(Exception e){
+ e.printStackTrace();
+ }
+ return null;
+ }
+
private static boolean isBluetoothTetheringEnabled() {
StringBuilder log = new StringBuilder();
boolean hasBtPan = false;
@@ -104,12 +159,9 @@ public class TetheringStateManager {
}
static void updateWifiTetheringState() {
- boolean lastState = TetheringObservable.getInstance().isWifiTetheringEnabled();
+ WifiManagerWrapper manager = getInstance().wifiManager;
try {
- boolean currentState = isWifiApEnabled();
- if (currentState != lastState) {
- TetheringObservable.setWifiTethering(currentState);
- }
+ TetheringObservable.setWifiTethering(manager.isWifiAPEnabled(), getWifiAddressRange(), getWifiInterfaceName());
} catch (Exception e) {
e.printStackTrace();
}
diff --git a/app/src/main/java/se/leap/bitmaskclient/utils/PreferenceHelper.java b/app/src/main/java/se/leap/bitmaskclient/utils/PreferenceHelper.java
index 25e4b797..073e338c 100644
--- a/app/src/main/java/se/leap/bitmaskclient/utils/PreferenceHelper.java
+++ b/app/src/main/java/se/leap/bitmaskclient/utils/PreferenceHelper.java
@@ -3,18 +3,13 @@ package se.leap.bitmaskclient.utils;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
-import android.support.annotation.Nullable;
import org.json.JSONException;
import org.json.JSONObject;
import java.net.MalformedURLException;
import java.net.URL;
-import java.util.ArrayList;
import java.util.HashSet;
-import java.util.List;
-import java.util.Locale;
-import java.util.Map;
import java.util.Set;
import de.blinkt.openvpn.VpnProfile;
@@ -25,16 +20,15 @@ import static se.leap.bitmaskclient.Constants.ALWAYS_ON_SHOW_DIALOG;
import static se.leap.bitmaskclient.Constants.DEFAULT_SHARED_PREFS_BATTERY_SAVER;
import static se.leap.bitmaskclient.Constants.EXCLUDED_APPS;
import static se.leap.bitmaskclient.Constants.LAST_USED_PROFILE;
-import static se.leap.bitmaskclient.Constants.PREFERENCES_APP_VERSION;
import static se.leap.bitmaskclient.Constants.PROVIDER_CONFIGURED;
import static se.leap.bitmaskclient.Constants.PROVIDER_EIP_DEFINITION;
import static se.leap.bitmaskclient.Constants.PROVIDER_PRIVATE_KEY;
import static se.leap.bitmaskclient.Constants.PROVIDER_VPN_CERTIFICATE;
import static se.leap.bitmaskclient.Constants.SHARED_PREFERENCES;
import static se.leap.bitmaskclient.Constants.SU_PERMISSION;
-import static se.leap.bitmaskclient.Constants.TETHERING_BLUETOOTH;
-import static se.leap.bitmaskclient.Constants.TETHERING_USB;
-import static se.leap.bitmaskclient.Constants.TETHERING_WIFI;
+import static se.leap.bitmaskclient.Constants.ALLOW_TETHERING_BLUETOOTH;
+import static se.leap.bitmaskclient.Constants.ALLOW_TETHERING_USB;
+import static se.leap.bitmaskclient.Constants.ALLOW_TETHERING_WIFI;
import static se.leap.bitmaskclient.Constants.USE_PLUGGABLE_TRANSPORTS;
/**
@@ -149,28 +143,28 @@ public class PreferenceHelper {
return getBoolean(context, DEFAULT_SHARED_PREFS_BATTERY_SAVER, false);
}
- public static void usbTethering(Context context, boolean isEnabled) {
- putBoolean(context, TETHERING_USB, isEnabled);
+ public static void allowUsbTethering(Context context, boolean isEnabled) {
+ putBoolean(context, ALLOW_TETHERING_USB, isEnabled);
}
- public static boolean getUsbTethering(Context context) {
- return getBoolean(context, TETHERING_USB, false);
+ public static boolean isUsbTetheringAllowed(Context context) {
+ return getBoolean(context, ALLOW_TETHERING_USB, false);
}
- public static void wifiTethering(Context context, boolean isEnabled) {
- putBoolean(context, TETHERING_WIFI, isEnabled);
+ public static void allowWifiTethering(Context context, boolean isEnabled) {
+ putBoolean(context, ALLOW_TETHERING_WIFI, isEnabled);
}
- public static boolean getWifiTethering(Context context) {
- return getBoolean(context, TETHERING_WIFI, false);
+ public static boolean isWifiTetheringAllowed(Context context) {
+ return getBoolean(context, ALLOW_TETHERING_WIFI, false);
}
- public static void bluetoothTethering(Context context, boolean isEnabled) {
- putBoolean(context, TETHERING_BLUETOOTH, isEnabled);
+ public static void allowBluetoothTethering(Context context, boolean isEnabled) {
+ putBoolean(context, ALLOW_TETHERING_BLUETOOTH, isEnabled);
}
- public static boolean getBluetoothTethering(Context context) {
- return getBoolean(context, TETHERING_BLUETOOTH, false);
+ public static boolean isBluetoothTetheringAllowed(Context context) {
+ return getBoolean(context, ALLOW_TETHERING_BLUETOOTH, false);
}
public static void saveShowAlwaysOnDialog(Context context, boolean showAlwaysOnDialog) {