From 1b98611dd83169bc754fa96a12962552de9896e3 Mon Sep 17 00:00:00 2001 From: Arne Schwabe Date: Sun, 16 Jun 2013 22:43:16 +0200 Subject: Fix typos, move socket initializing to OpenVPN 2.x specific OpenVPNManagementThread --HG-- rename : src/de/blinkt/openvpn/core/OpenVPNMangement.java => src/de/blinkt/openvpn/core/OpenVPNManagement.java --- .../blinkt/openvpn/core/DeviceStateReceiver.java | 4 +- src/de/blinkt/openvpn/core/OpenVPNManagement.java | 14 ++++ src/de/blinkt/openvpn/core/OpenVPNMangement.java | 14 ---- .../openvpn/core/OpenVpnManagementThread.java | 52 +++++++++++-- src/de/blinkt/openvpn/core/OpenVpnService.java | 87 +++++++--------------- 5 files changed, 90 insertions(+), 81 deletions(-) create mode 100644 src/de/blinkt/openvpn/core/OpenVPNManagement.java delete mode 100644 src/de/blinkt/openvpn/core/OpenVPNMangement.java (limited to 'src') diff --git a/src/de/blinkt/openvpn/core/DeviceStateReceiver.java b/src/de/blinkt/openvpn/core/DeviceStateReceiver.java index 67ca0f26..d5029b07 100644 --- a/src/de/blinkt/openvpn/core/DeviceStateReceiver.java +++ b/src/de/blinkt/openvpn/core/DeviceStateReceiver.java @@ -15,7 +15,7 @@ import java.util.LinkedList; public class DeviceStateReceiver extends BroadcastReceiver implements ByteCountListener { private int lastNetwork=-1; - private OpenVPNMangement mManangement; + private OpenVPNManagement mManangement; // Window time in s private final int TRAFFIC_WINDOW = 60; @@ -74,7 +74,7 @@ public class DeviceStateReceiver extends BroadcastReceiver implements ByteCountL - public DeviceStateReceiver(OpenVPNMangement magnagement) { + public DeviceStateReceiver(OpenVPNManagement magnagement) { super(); mManangement = magnagement; } diff --git a/src/de/blinkt/openvpn/core/OpenVPNManagement.java b/src/de/blinkt/openvpn/core/OpenVPNManagement.java new file mode 100644 index 00000000..5b8fc074 --- /dev/null +++ b/src/de/blinkt/openvpn/core/OpenVPNManagement.java @@ -0,0 +1,14 @@ +package de.blinkt.openvpn.core; + +public interface OpenVPNManagement { + int mBytecountinterval=2; + + void reconnect(); + + void pause(); + + void resume(); + + boolean stopVPN(); + +} diff --git a/src/de/blinkt/openvpn/core/OpenVPNMangement.java b/src/de/blinkt/openvpn/core/OpenVPNMangement.java deleted file mode 100644 index a1334ac2..00000000 --- a/src/de/blinkt/openvpn/core/OpenVPNMangement.java +++ /dev/null @@ -1,14 +0,0 @@ -package de.blinkt.openvpn.core; - -public interface OpenVPNMangement { - int mBytecountinterval=2; - - void reconnect(); - - void pause(); - - void resume(); - - boolean stopVPN(); - -} diff --git a/src/de/blinkt/openvpn/core/OpenVpnManagementThread.java b/src/de/blinkt/openvpn/core/OpenVpnManagementThread.java index 5b465973..13bebc8a 100644 --- a/src/de/blinkt/openvpn/core/OpenVpnManagementThread.java +++ b/src/de/blinkt/openvpn/core/OpenVpnManagementThread.java @@ -11,9 +11,11 @@ import java.util.LinkedList; import java.util.Locale; import java.util.Vector; +import android.content.Context; import android.content.SharedPreferences; import android.net.LocalServerSocket; import android.net.LocalSocket; +import android.net.LocalSocketAddress; import android.os.ParcelFileDescriptor; import android.preference.PreferenceManager; import android.util.Log; @@ -21,23 +23,23 @@ import de.blinkt.openvpn.R; import de.blinkt.openvpn.VpnProfile; import de.blinkt.openvpn.core.OpenVPN.ConnectionStatus; -public class OpenVpnManagementThread implements Runnable, OpenVPNMangement { +public class OpenVpnManagementThread implements Runnable, OpenVPNManagement { private static final String TAG = "openvpn"; private LocalSocket mSocket; private VpnProfile mProfile; private OpenVpnService mOpenVPNService; private LinkedList mFDList=new LinkedList(); - private LocalServerSocket mServerSocket; + private LocalServerSocket mServerSocket; private boolean mReleaseHold=true; private boolean mWaitingForRelease=false; private long mLastHoldRelease=0; private static Vector active=new Vector(); + private LocalSocket mServerSocketLocal; - public OpenVpnManagementThread(VpnProfile profile, LocalServerSocket mgmtsocket, OpenVpnService openVpnService) { + public OpenVpnManagementThread(VpnProfile profile, OpenVpnService openVpnService) { mProfile = profile; - mServerSocket = mgmtsocket; mOpenVPNService = openVpnService; @@ -48,7 +50,42 @@ public class OpenVpnManagementThread implements Runnable, OpenVPNMangement { } - static { + public boolean openManagementInterface(Context c) { + // Could take a while to open connection + int tries=8; + + String socketName = (c.getCacheDir().getAbsolutePath() + "/" + "mgmtsocket"); + // The mServerSocketLocal is transferred to the LocalServerSocket, ignore warning + + mServerSocketLocal = new LocalSocket(); + + while(tries > 0 && !mServerSocketLocal.isConnected()) { + try { + mServerSocketLocal.bind(new LocalSocketAddress(socketName, + LocalSocketAddress.Namespace.FILESYSTEM)); + } catch (IOException e) { + // wait 300 ms before retrying + try { Thread.sleep(300); + } catch (InterruptedException e1) {} + + } + tries--; + } + + try { + + mServerSocket = new LocalServerSocket(mServerSocketLocal.getFileDescriptor()); + return true; + } catch (IOException e) { + e.printStackTrace(); + } + return false; + + + } + + + static { System.loadLibrary("opvpnutil"); } @@ -77,6 +114,11 @@ public class OpenVpnManagementThread implements Runnable, OpenVPNMangement { // Wait for a client to connect mSocket= mServerSocket.accept(); InputStream instream = mSocket.getInputStream(); + // Close the management socket after client connected + + mServerSocket.close(); + // Closing one of the two sockets also closes the other + //mServerSocketLocal.close(); while(true) { int numbytesread = instream.read(buffer); diff --git a/src/de/blinkt/openvpn/core/OpenVpnService.java b/src/de/blinkt/openvpn/core/OpenVpnService.java index 853389fd..74ae7245 100644 --- a/src/de/blinkt/openvpn/core/OpenVpnService.java +++ b/src/de/blinkt/openvpn/core/OpenVpnService.java @@ -20,7 +20,6 @@ import de.blinkt.openvpn.core.OpenVPN.ByteCountListener; import de.blinkt.openvpn.core.OpenVPN.ConnectionStatus; import de.blinkt.openvpn.core.OpenVPN.StateListener; -import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.HashMap; @@ -63,12 +62,12 @@ public class OpenVpnService extends VpnService implements StateListener, Callbac public static final int PROTECT_FD = 0; - private static boolean mNotificationalwaysVisible=false; + private static boolean mNotificationAlwaysVisible =false; private final IBinder mBinder = new LocalBinder(); private boolean mOvpn3; - private Thread mSocketManagerThread; - private OpenVPNMangement mManagement; + + private OpenVPNManagement mManagement; public class LocalBinder extends Binder { public OpenVpnService getService() { @@ -103,9 +102,9 @@ public class OpenVpnService extends VpnService implements StateListener, Callbac unregisterDeviceStateReceiver(); ProfileManager.setConntectedVpnProfileDisconnected(this); if(!mStarting) { - stopForeground(!mNotificationalwaysVisible); + stopForeground(!mNotificationAlwaysVisible); - if( !mNotificationalwaysVisible) { + if( !mNotificationAlwaysVisible) { stopSelf(); OpenVPN.removeStateListener(this); } @@ -207,39 +206,7 @@ public class OpenVpnService extends VpnService implements StateListener, Callbac } - - private LocalServerSocket openManagmentInterface(int tries) { - // Could take a while to open connection - String socketname = (getCacheDir().getAbsolutePath() + "/" + "mgmtsocket"); - // The sock is transfered to the LocalServerSocket, ignore warning - @SuppressWarnings("resource") - LocalSocket sock = new LocalSocket(); - - while(tries > 0 && !sock.isConnected()) { - try { - sock.bind(new LocalSocketAddress(socketname, - LocalSocketAddress.Namespace.FILESYSTEM)); - } catch (IOException e) { - // wait 300 ms before retrying - try { Thread.sleep(300); - } catch (InterruptedException e1) {} - - } - tries--; - } - - try { - LocalServerSocket lss = new LocalServerSocket(sock.getFileDescriptor()); - return lss; - } catch (IOException e) { - e.printStackTrace(); - } - return null; - - - } - - synchronized void registerDeviceStateReceiver(OpenVPNMangement magnagement) { + synchronized void registerDeviceStateReceiver(OpenVPNManagement magnagement) { // Registers BroadcastReceiver to track network connection changes. IntentFilter filter = new IntentFilter(); filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); @@ -269,7 +236,7 @@ public class OpenVpnService extends VpnService implements StateListener, Callbac public int onStartCommand(Intent intent, int flags, int startId) { if(intent != null && intent.getBooleanExtra(ALWAYS_SHOW_NOTIFICATION, false)) - mNotificationalwaysVisible=true; + mNotificationAlwaysVisible =true; OpenVPN.addStateListener(this); OpenVPN.addByteCountListener(this); @@ -316,21 +283,21 @@ public class OpenVpnService extends VpnService implements StateListener, Callbac mStarting=false; - // Open the Management Interface - if(!mOvpn3) { - LocalServerSocket mgmtsocket = openManagmentInterface(8); + // Open the Management Interface + if (!mOvpn3) { - if(mgmtsocket!=null) { - // start a Thread that handles incoming messages of the managment socket - OpenVpnManagementThread ovpnmgmthread = new OpenVpnManagementThread(mProfile,mgmtsocket,this); - mSocketManagerThread = new Thread(ovpnmgmthread,"OpenVPNMgmtThread"); - mSocketManagerThread.start(); - mManagement= ovpnmgmthread; - OpenVPN.logInfo("started Socket Thread"); - } - } + // start a Thread that handles incoming messages of the managment socket + OpenVpnManagementThread ovpnManagementThread = new OpenVpnManagementThread(mProfile, this); + if (ovpnManagementThread.openManagementInterface(this)) { + + Thread mSocketManagerThread = new Thread(ovpnManagementThread, "OpenVPNManagementThread"); + mSocketManagerThread.start(); + mManagement = ovpnManagementThread; + OpenVPN.logInfo("started Socket Thread"); + } + } - // Start a new session by creating a new thread. + // Start a new session by creating a new thread. SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); mOvpn3 = prefs.getBoolean("ovpn3", false); @@ -339,7 +306,7 @@ public class OpenVpnService extends VpnService implements StateListener, Callbac Runnable processThread; if(mOvpn3) { - OpenVPNMangement mOpenVPN3 = instantiateOpenVPN3Core(); + OpenVPNManagement mOpenVPN3 = instantiateOpenVPN3Core(); processThread = (Runnable) mOpenVPN3; mManagement = mOpenVPN3; @@ -363,7 +330,7 @@ public class OpenVpnService extends VpnService implements StateListener, Callbac return START_NOT_STICKY; } - private OpenVPNMangement instantiateOpenVPN3Core() { + private OpenVPNManagement instantiateOpenVPN3Core() { return null; } @@ -560,7 +527,7 @@ public class OpenVpnService extends VpnService implements StateListener, Callbac // If the process is not running, ignore any state, // Notification should be invisible in this state doSendBroadcast(state, level); - if(mProcessThread==null && !mNotificationalwaysVisible) + if(mProcessThread==null && !mNotificationAlwaysVisible) return; // Display byte count only after being connected @@ -599,11 +566,11 @@ public class OpenVpnService extends VpnService implements StateListener, Callbac if(mDisplayBytecount) { String netstat = String.format(getString(R.string.statusline_bytecount), humanReadableByteCount(in, false), - humanReadableByteCount(diffin/OpenVPNMangement.mBytecountinterval, true), + humanReadableByteCount(diffin/ OpenVPNManagement.mBytecountinterval, true), humanReadableByteCount(out, false), - humanReadableByteCount(diffout/OpenVPNMangement.mBytecountinterval, true)); + humanReadableByteCount(diffout/ OpenVPNManagement.mBytecountinterval, true)); - boolean lowpriority = !mNotificationalwaysVisible; + boolean lowpriority = !mNotificationAlwaysVisible; showNotification(netstat,null,lowpriority,mConnecttime, LEVEL_CONNECTED); } @@ -636,7 +603,7 @@ public class OpenVpnService extends VpnService implements StateListener, Callbac } } - public OpenVPNMangement getManagement() { + public OpenVPNManagement getManagement() { return mManagement; } } -- cgit v1.2.3