From 294ea3e986d7d6726d5bf6f913c47ce9a966c7ee Mon Sep 17 00:00:00 2001 From: Arne Schwabe Date: Wed, 8 Aug 2012 00:37:54 +0200 Subject: Use management-client and management-signal (closes issue #65) --- src/de/blinkt/openvpn/OpenVpnManagementThread.java | 18 ++++----- src/de/blinkt/openvpn/OpenVpnService.java | 44 +++++++++++----------- src/de/blinkt/openvpn/VpnProfile.java | 4 +- 3 files changed, 34 insertions(+), 32 deletions(-) (limited to 'src/de/blinkt/openvpn') diff --git a/src/de/blinkt/openvpn/OpenVpnManagementThread.java b/src/de/blinkt/openvpn/OpenVpnManagementThread.java index 7a0beb1d..3f0d8439 100644 --- a/src/de/blinkt/openvpn/OpenVpnManagementThread.java +++ b/src/de/blinkt/openvpn/OpenVpnManagementThread.java @@ -18,6 +18,7 @@ import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; +import android.net.LocalServerSocket; import android.net.LocalSocket; import android.os.Build; import android.os.ParcelFileDescriptor; @@ -34,16 +35,17 @@ public class OpenVpnManagementThread implements Runnable { private int mBytecountinterval=2; private long mLastIn=0; private long mLastOut=0; - private String mCurrentstate; + private String mCurrentstate; + private LocalServerSocket mServerSocket; private static Vector active=new Vector(); static private native void jniclose(int fdint); static private native byte[] rsasign(byte[] input,int pkey) throws InvalidKeyException; - public OpenVpnManagementThread(VpnProfile profile, LocalSocket mgmtsocket, OpenVpnService openVpnService) { + public OpenVpnManagementThread(VpnProfile profile, LocalServerSocket mgmtsocket, OpenVpnService openVpnService) { mProfile = profile; - mSocket = mgmtsocket; + mServerSocket = mgmtsocket; mOpenVPNService = openVpnService; } @@ -67,16 +69,14 @@ public class OpenVpnManagementThread implements Runnable { Log.i(TAG, "Managment Socket Thread started"); byte [] buffer =new byte[2048]; // mSocket.setSoTimeout(5); // Setting a timeout cannot be that bad - InputStream instream = null; - try { - instream = mSocket.getInputStream(); - } catch (IOException e) { - e.printStackTrace(); - } + String pendingInput=""; active.add(this); try { + // Wait for a client to connect + mSocket= mServerSocket.accept(); + InputStream instream = mSocket.getInputStream(); while(true) { int numbytesread = instream.read(buffer); diff --git a/src/de/blinkt/openvpn/OpenVpnService.java b/src/de/blinkt/openvpn/OpenVpnService.java index c28802f6..f56e873b 100644 --- a/src/de/blinkt/openvpn/OpenVpnService.java +++ b/src/de/blinkt/openvpn/OpenVpnService.java @@ -27,6 +27,7 @@ import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; import android.net.ConnectivityManager; +import android.net.LocalServerSocket; import android.net.LocalSocket; import android.net.LocalSocketAddress; import android.net.VpnService; @@ -114,14 +115,14 @@ public class OpenVpnService extends VpnService implements StateListener { - private LocalSocket openManagmentInterface(int tries) { + private LocalServerSocket openManagmentInterface(int tries) { // Could take a while to open connection String socketname = (getCacheDir().getAbsolutePath() + "/" + "mgmtsocket"); LocalSocket sock = new LocalSocket(); while(tries > 0 && !sock.isConnected()) { try { - sock.connect(new LocalSocketAddress(socketname, + sock.bind(new LocalSocketAddress(socketname, LocalSocketAddress.Namespace.FILESYSTEM)); } catch (IOException e) { // wait 300 ms before retrying @@ -131,7 +132,16 @@ public class OpenVpnService extends VpnService implements StateListener { } tries--; } - return sock; + + try { + LocalServerSocket lss = new LocalServerSocket(sock.getFileDescriptor()); + return lss; + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return null; + } @@ -171,19 +181,19 @@ public class OpenVpnService extends VpnService implements StateListener { } catch (InterruptedException e) { } } + // Open the Management Interface + LocalServerSocket mgmtsocket = openManagmentInterface(8); - // See if there is a managment socket we can connect to and kill the process too - LocalSocket mgmtsocket = openManagmentInterface(1); if(mgmtsocket!=null) { - // Fire and forget :) - new OpenVpnManagementThread(mProfile,mgmtsocket,this).managmentCommand("signal SIGINT\n"); - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - } - //checkForRemainingMiniVpns(); + // start a Thread that handles incoming messages of the managment socket + mSocketManager = new OpenVpnManagementThread(mProfile,mgmtsocket,this); + mSocketManagerThread = new Thread(mSocketManager,"OpenVPNMgmtThread"); + mSocketManagerThread.start(); + OpenVPN.logInfo("started Socket Thread"); + registerNetworkStateReceiver(); } + // Start a new session by creating a new thread. OpenVPNThread serviceThread = new OpenVPNThread(this, argv,nativelibdir); @@ -192,16 +202,6 @@ public class OpenVpnService extends VpnService implements StateListener { - // Open the Management Interface - mgmtsocket = openManagmentInterface(8); - - if(mgmtsocket!=null) { - // start a Thread that handles incoming messages of the managment socket - mSocketManager = new OpenVpnManagementThread(mProfile,mgmtsocket,this); - mSocketManagerThread = new Thread(mSocketManager,"OpenVPNMgmtThread"); - mSocketManagerThread.start(); - registerNetworkStateReceiver(); - } return START_NOT_STICKY; } diff --git a/src/de/blinkt/openvpn/VpnProfile.java b/src/de/blinkt/openvpn/VpnProfile.java index 199f6b54..c03dff74 100644 --- a/src/de/blinkt/openvpn/VpnProfile.java +++ b/src/de/blinkt/openvpn/VpnProfile.java @@ -158,7 +158,9 @@ public class VpnProfile implements Serializable{ cfg +=cacheDir.getAbsolutePath() + "/" + "mgmtsocket"; cfg += " unix\n"; - cfg+= "management-query-passwords\n"; + cfg += "management-client\n"; + cfg += "management-signal\n"; + cfg += "management-query-passwords\n"; cfg += "management-hold\n\n"; /* tmp-dir patched out :) -- cgit v1.2.3