From ecc89f37fd4c70865229aa8784cd191e9bf07895 Mon Sep 17 00:00:00 2001 From: Arne Schwabe Date: Sun, 12 Aug 2012 15:01:08 +0200 Subject: Keep Openvpn in hold whiteout network (closes issue #63) --- src/de/blinkt/openvpn/NetworkSateReceiver.java | 8 +++- src/de/blinkt/openvpn/OpenVpnManagementThread.java | 56 +++++++++++++++++----- 2 files changed, 51 insertions(+), 13 deletions(-) (limited to 'src/de/blinkt') diff --git a/src/de/blinkt/openvpn/NetworkSateReceiver.java b/src/de/blinkt/openvpn/NetworkSateReceiver.java index 6584e0fa..a649bd65 100644 --- a/src/de/blinkt/openvpn/NetworkSateReceiver.java +++ b/src/de/blinkt/openvpn/NetworkSateReceiver.java @@ -44,11 +44,17 @@ public class NetworkSateReceiver extends BroadcastReceiver { if(networkInfo!=null && networkInfo.getState() == State.CONNECTED) { int newnet = networkInfo.getType(); - if(sendusr1 && lastNetwork!=-1 && (lastNetwork!=newnet)) + if(sendusr1 && lastNetwork!=newnet) mManangement.reconnect(); lastNetwork = newnet; + } else if (networkInfo==null) { + // Not connected, stop openvpn, set last connected network to no network + lastNetwork=-1; + if(sendusr1) + mManangement.signalusr1(); } + if(!netstatestring.equals(lastStateMsg)) OpenVPN.logInfo(R.string.netstatus, netstatestring); lastStateMsg=netstatestring; diff --git a/src/de/blinkt/openvpn/OpenVpnManagementThread.java b/src/de/blinkt/openvpn/OpenVpnManagementThread.java index 3f0d8439..c42e7516 100644 --- a/src/de/blinkt/openvpn/OpenVpnManagementThread.java +++ b/src/de/blinkt/openvpn/OpenVpnManagementThread.java @@ -18,10 +18,13 @@ import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; +import android.content.Context; +import android.content.SharedPreferences; import android.net.LocalServerSocket; import android.net.LocalSocket; import android.os.Build; import android.os.ParcelFileDescriptor; +import android.preference.PreferenceManager; import android.util.Base64; import android.util.Log; @@ -36,7 +39,9 @@ public class OpenVpnManagementThread implements Runnable { private long mLastIn=0; private long mLastOut=0; private String mCurrentstate; - private LocalServerSocket mServerSocket; + private LocalServerSocket mServerSocket; + private boolean mReleaseHold=true; + private boolean mWaitingForRelease=false; private static Vector active=new Vector(); @@ -47,6 +52,13 @@ public class OpenVpnManagementThread implements Runnable { mProfile = profile; mServerSocket = mgmtsocket; mOpenVPNService = openVpnService; + + + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(openVpnService); + boolean managemeNetworkState = prefs.getBoolean("netchangereconnect", true); + if(managemeNetworkState) + mReleaseHold=false; + } static { @@ -54,12 +66,13 @@ public class OpenVpnManagementThread implements Runnable { } public void managmentCommand(String cmd) { - //Log.d("openvpn", "mgmt cmd" + mSocket + " " +cmd + " " ); - try { - mSocket.getOutputStream().write(cmd.getBytes()); - mSocket.getOutputStream().flush(); - } catch (IOException e) { - // Ignore socket stack traces + if(mSocket!=null) { + try { + mSocket.getOutputStream().write(cmd.getBytes()); + mSocket.getOutputStream().flush(); + } catch (IOException e) { + // Ignore socket stack traces + } } } @@ -173,9 +186,10 @@ public class OpenVpnManagementThread implements Runnable { }else if (cmd.equals("PASSWORD")) { processPWCommand(argument); } else if (cmd.equals("HOLD")) { - managmentCommand("hold release\n"); - managmentCommand("bytecount " + mBytecountinterval + "\n"); - managmentCommand("state on\n"); + if(mReleaseHold) + releaseHoldCmd(); + else + mWaitingForRelease=true; } else if (cmd.equals("NEED-OK")) { processNeedCommand(argument); } else if (cmd.equals("BYTECOUNT")){ @@ -203,6 +217,19 @@ public class OpenVpnManagementThread implements Runnable { OpenVPN.logMessage(0, "MGMT:", "Got unrecognized line from management:" + command); } } + private void releaseHoldCmd() { + mWaitingForRelease=false; + managmentCommand("hold release\n"); + managmentCommand("bytecount " + mBytecountinterval + "\n"); + managmentCommand("state on\n"); + } + + public void releaseHold() { + if(mWaitingForRelease) + releaseHoldCmd(); + else + mReleaseHold=true; + } private void processProxyCMD(String argument) { SocketAddress proxyaddr = ProxyDetection.detectProxy(mProfile); @@ -413,10 +440,15 @@ public class OpenVpnManagementThread implements Runnable { return sendCMD; } + public void signalusr1() { + if(!mWaitingForRelease) + managmentCommand("signal SIGUSR1\n"); + mReleaseHold=false; + } public void reconnect() { - managmentCommand("signal SIGUSR1\n"); - + signalusr1(); + releaseHold(); } private void processSignCommand(String b64data) { -- cgit v1.2.3