From 7fbea5459e59de1327f2c76a23f5940d67e4ae8d Mon Sep 17 00:00:00 2001 From: cyberta Date: Wed, 29 Jan 2020 02:55:25 -0600 Subject: implement wifi tethering --- .../tethering/TetheringBroadcastReceiver.java | 11 +--- .../tethering/TetheringObservable.java | 62 ++++++++++++++----- .../bitmaskclient/tethering/TetheringState.java | 34 ++++++++++- .../tethering/TetheringStateManager.java | 70 +++++++++++++++++++--- 4 files changed, 143 insertions(+), 34 deletions(-) (limited to 'app/src/main/java/se/leap/bitmaskclient/tethering') 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 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 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(); } -- cgit v1.2.3