diff options
| author | cyBerta <cyberta@riseup.net> | 2025-03-25 21:42:39 +0100 |
|---|---|---|
| committer | cyBerta <cyberta@riseup.net> | 2025-04-11 16:08:59 +0200 |
| commit | fe9609d14c7ce713e5f55527c7b0732544071011 (patch) | |
| tree | 5f127e1f47a65c15a89446578427d8c9356892e2 /app/src/main/java/de/blinkt | |
| parent | 0024333a0ce771c2c94c01965b83020578fb619f (diff) | |
improve state handling of obfsvpn; try to restart obfsvpn in on different proxy port in case the default one is already boudn
Diffstat (limited to 'app/src/main/java/de/blinkt')
| -rw-r--r-- | app/src/main/java/de/blinkt/openvpn/core/OpenVPNService.java | 76 | ||||
| -rw-r--r-- | app/src/main/java/de/blinkt/openvpn/core/connection/Obfs4Connection.java | 2 |
2 files changed, 43 insertions, 35 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 c8ac965f..1ed7bc39 100644 --- a/app/src/main/java/de/blinkt/openvpn/core/OpenVPNService.java +++ b/app/src/main/java/de/blinkt/openvpn/core/OpenVPNService.java @@ -182,6 +182,7 @@ public class OpenVPNService extends VpnService implements StateListener, Callbac synchronized (mProcessLock) { mProcessThread = null; } + stopObfsvpn(); VpnStatus.removeByteCountListener(this); unregisterDeviceStateReceiver(mDeviceStateReceiver); mDeviceStateReceiver = null; @@ -230,15 +231,20 @@ public class OpenVPNService extends VpnService implements StateListener, Callbac mDeviceStateReceiver.userPause(shouldBePaused); } + private boolean stopObfsvpn() { + if (obfsVpnClient == null || !obfsVpnClient.isStarted()) { + return true; + } + boolean success = obfsVpnClient.stop(); + obfsVpnClient = null; + return success; + } @Override public boolean stopVPN(boolean replaceConnection) { + stopObfsvpn(); if(isVpnRunning()) { if (getManagement() != null && getManagement().stopVPN(replaceConnection)) { if (!replaceConnection) { - if (obfsVpnClient != null && obfsVpnClient.isStarted()) { - obfsVpnClient.stop(); - obfsVpnClient = null; - } VpnStatus.updateStateString("NOPROCESS", "VPN STOPPED", R.string.state_noprocess, ConnectionStatus.LEVEL_NOTCONNECTED); } return true; @@ -369,17 +375,42 @@ public class OpenVPNService extends VpnService implements StateListener, Callbac } private void startOpenVPN() { - //TODO: investigate how connections[n] with n>0 get called during vpn setup (on connection refused?) - // Do we need to check if there's any obfs4 connection in mProfile.mConnections and start - // the dispatcher here? Can we start the dispatcher at a later point of execution, e.g. when - // connections[n], n>0 gets choosen? - Connection connection = mProfile.mConnections[0]; VpnStatus.setCurrentlyConnectingProfile(mProfile); + // Set a flag that we are starting a new VPN + mStarting = true; + // Stop the previous session by interrupting the thread. + stopOldOpenVPNProcess(); + // An old running VPN should now be exited + mStarting = false; + + // stop old running obfsvpn client + if (!stopObfsvpn()) { + VpnStatus.logError("Failed to stop already running obfsvpn client"); + endVpnService(); + return; + } + + // optionally start start obfsvpn and adapt openvpn config to the port obfsvpn is listening to + Connection.TransportType transportType = connection.getTransportType(); + if (mProfile.usePluggableTransports() && transportType.isPluggableTransport()) { + try { + obfsVpnClient = new ObfsvpnClient(((Obfs4Connection) connection).getObfs4Options()); + obfsVpnClient.start(); + int port = obfsVpnClient.getPort(); + connection.setServerPort(String.valueOf(port)); + } catch (RuntimeException e) { + e.printStackTrace(); + VpnStatus.logException(e); + endVpnService(); + return; + } + } + + // write openvpn config VpnStatus.logInfo(R.string.building_configration); VpnStatus.updateStateString("VPN_GENERATE_CONFIG", "", R.string.building_configration, ConnectionStatus.LEVEL_START); - try { mProfile.writeConfigFile(this); } catch (IOException e) { @@ -387,6 +418,7 @@ public class OpenVPNService extends VpnService implements StateListener, Callbac endVpnService(); return; } + String nativeLibraryDirectory = getApplicationInfo().nativeLibraryDir; String tmpDir; try { @@ -399,25 +431,6 @@ public class OpenVPNService extends VpnService implements StateListener, Callbac // Write OpenVPN binary String[] argv = VPNLaunchHelper.buildOpenvpnArgv(this); - - // Set a flag that we are starting a new VPN - mStarting = true; - // Stop the previous session by interrupting the thread. - - stopOldOpenVPNProcess(); - // An old running VPN should now be exited - mStarting = false; - Connection.TransportType transportType = connection.getTransportType(); - if (mProfile.usePluggableTransports() && transportType.isPluggableTransport()) { - if (obfsVpnClient != null && obfsVpnClient.isStarted()) { - obfsVpnClient.stop(); - } - obfsVpnClient = new ObfsvpnClient(((Obfs4Connection) connection).getObfs4Options()); - obfsVpnClient.start(); - Log.d(TAG, "obfsvpn client started"); - } - - // Start a new session by creating a new thread. boolean useOpenVPN3 = VpnProfile.doUseOpenVPN3(this); @@ -471,11 +484,6 @@ public class OpenVPNService extends VpnService implements StateListener, Callbac if (mOpenVPNThread != null) ((OpenVPNThread) mOpenVPNThread).setReplaceConnection(); if (mManagement.stopVPN(true)) { - if (obfsVpnClient != null && obfsVpnClient.isStarted()) { - Log.d(TAG, "-> stop obfsvpnClient"); - obfsVpnClient.stop(); - obfsVpnClient = null; - } try { Thread.sleep(1000); } catch (InterruptedException e) { diff --git a/app/src/main/java/de/blinkt/openvpn/core/connection/Obfs4Connection.java b/app/src/main/java/de/blinkt/openvpn/core/connection/Obfs4Connection.java index 0fe6bff2..e2c596ac 100644 --- a/app/src/main/java/de/blinkt/openvpn/core/connection/Obfs4Connection.java +++ b/app/src/main/java/de/blinkt/openvpn/core/connection/Obfs4Connection.java @@ -15,7 +15,7 @@ public class Obfs4Connection extends Connection { public Obfs4Connection(Obfs4Options options) { setServerName(ObfsvpnClient.IP); - setServerPort(String.valueOf(ObfsvpnClient.PORT)); + setServerPort(String.valueOf(ObfsvpnClient.DEFAULT_PORT)); setUseUdp(true); setProxyType(ProxyType.NONE); setProxyName(""); |
