diff options
Diffstat (limited to 'src/de')
-rw-r--r-- | src/de/blinkt/openvpn/core/DeviceStateReceiver.java | 4 | ||||
-rw-r--r-- | src/de/blinkt/openvpn/core/OpenVPNManagement.java (renamed from src/de/blinkt/openvpn/core/OpenVPNMangement.java) | 2 | ||||
-rw-r--r-- | src/de/blinkt/openvpn/core/OpenVpnManagementThread.java | 52 | ||||
-rw-r--r-- | src/de/blinkt/openvpn/core/OpenVpnService.java | 87 |
4 files changed, 77 insertions, 68 deletions
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/OpenVPNMangement.java b/src/de/blinkt/openvpn/core/OpenVPNManagement.java index a1334ac2..5b8fc074 100644 --- a/src/de/blinkt/openvpn/core/OpenVPNMangement.java +++ b/src/de/blinkt/openvpn/core/OpenVPNManagement.java @@ -1,6 +1,6 @@ package de.blinkt.openvpn.core; -public interface OpenVPNMangement { +public interface OpenVPNManagement { int mBytecountinterval=2; void reconnect(); 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<FileDescriptor> mFDList=new LinkedList<FileDescriptor>();
- private LocalServerSocket mServerSocket;
+ private LocalServerSocket mServerSocket;
private boolean mReleaseHold=true;
private boolean mWaitingForRelease=false;
private long mLastHoldRelease=0;
private static Vector<OpenVpnManagementThread> active=new Vector<OpenVpnManagementThread>();
+ 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; } } |