summaryrefslogtreecommitdiff
path: root/src/de/blinkt/openvpn/OpenVpnService.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/de/blinkt/openvpn/OpenVpnService.java')
-rw-r--r--src/de/blinkt/openvpn/OpenVpnService.java86
1 files changed, 50 insertions, 36 deletions
diff --git a/src/de/blinkt/openvpn/OpenVpnService.java b/src/de/blinkt/openvpn/OpenVpnService.java
index 675e44b9..d5d75589 100644
--- a/src/de/blinkt/openvpn/OpenVpnService.java
+++ b/src/de/blinkt/openvpn/OpenVpnService.java
@@ -62,9 +62,6 @@ public class OpenVpnService extends VpnService implements StateListener, Callbac
private CIDRIP mLocalIP=null;
- private OpenVpnManagementThread mManagementThread;
-
- private Thread mSocketManagerThread;
private int mMtu;
private String mLocalIPv6=null;
private NetworkSateReceiver mNetworkStateReceiver;
@@ -90,6 +87,8 @@ public class OpenVpnService extends VpnService implements StateListener, Callbac
private final IBinder mBinder = new LocalBinder();
private boolean mOvpn3;
private OpenVPNThreadv3 mOpenVPN3;
+ private Thread mSocketManagerThread;
+ private OpenVPNMangement mManagement;
public class LocalBinder extends Binder {
public OpenVpnService getService() {
@@ -109,10 +108,7 @@ public class OpenVpnService extends VpnService implements StateListener, Callbac
@Override
public void onRevoke() {
- if(!mOvpn3)
- OpenVpnManagementThread.stopOpenVPN();
- else
- mOpenVPN3.stopVPN();
+ mManagement.stopVPN();
endVpnService();
}
@@ -126,6 +122,7 @@ public class OpenVpnService extends VpnService implements StateListener, Callbac
OpenVPN.logBuilderConfig(null);
OpenVPN.removeStateListener(this);
OpenVPN.removeByteCountListener(this);
+ unregisterNetworkStateReceiver();
ProfileManager.setConntectedVpnProfileDisconnected(this);
if(!mStarting) {
stopForeground(!mNotificationalwaysVisible);
@@ -180,9 +177,9 @@ public class OpenVpnService extends VpnService implements StateListener, Callbac
// PRIORITY_MIN == -2
setpriority.invoke(nbuilder, -2 );
- Method setUsesChronometer = nbuilder.getClass().getMethod("setPriority", boolean.class);
+ Method setUsesChronometer = nbuilder.getClass().getMethod("setUsesChronometer", boolean.class);
setUsesChronometer.invoke(nbuilder,true);
-
+
/* PendingIntent cancelconnet=null;
nbuilder.addAction(android.R.drawable.ic_menu_close_clear_cancel,
@@ -240,14 +237,20 @@ public class OpenVpnService extends VpnService implements StateListener, Callbac
}
- void registerNetworkStateReceiver() {
+ void registerNetworkStateReceiver(OpenVPNMangement magnagement) {
// Registers BroadcastReceiver to track network connection changes.
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
- mNetworkStateReceiver = new NetworkSateReceiver(mManagementThread);
+ mNetworkStateReceiver = new NetworkSateReceiver(magnagement);
this.registerReceiver(mNetworkStateReceiver, filter);
}
-
+ void unregisterNetworkStateReceiver() {
+ if(mNetworkStateReceiver!=null)
+ this.unregisterReceiver(mNetworkStateReceiver);
+ mNetworkStateReceiver=null;
+ }
+
+
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
@@ -274,19 +277,27 @@ public class OpenVpnService extends VpnService implements StateListener, Callbac
showNotification("Starting VPN " + mProfile.mName,"Starting VPN " + mProfile.mName, false,0,LEVEL_NOTCONNECTED);
-
-
// Set a flag that we are starting a new VPN
mStarting=true;
// Stop the previous session by interrupting the thread.
- if(OpenVpnManagementThread.stopOpenVPN()){
- // an old was asked to exit, wait 2s
+ if(mManagement!=null && mManagement.stopVPN())
+ // an old was asked to exit, wait 1s
+ try {
+ Thread.sleep(1000);
+ } catch (InterruptedException e) {
+ }
+
+
+ if(mOpenVPN3!=null) {
+ mOpenVPN3.stopVPN();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
+
}
+
if (mProcessThread!=null) {
mProcessThread.interrupt();
try {
@@ -299,18 +310,19 @@ public class OpenVpnService extends VpnService implements StateListener, Callbac
// Open the Management Interface
- LocalServerSocket mgmtsocket = openManagmentInterface(8);
-
- if(mgmtsocket!=null) {
- // start a Thread that handles incoming messages of the managment socket
- mManagementThread=new OpenVpnManagementThread(mProfile,mgmtsocket,this);
- mSocketManagerThread = new Thread(mManagementThread,"OpenVPNMgmtThread");
- mSocketManagerThread.start();
- OpenVPN.logInfo("started Socket Thread");
- registerNetworkStateReceiver();
+ if(!mOvpn3) {
+ LocalServerSocket mgmtsocket = openManagmentInterface(8);
+
+ 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 new session by creating a new thread.
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
@@ -318,19 +330,21 @@ public class OpenVpnService extends VpnService implements StateListener, Callbac
Runnable processThread;
if(mOvpn3) {
-
mOpenVPN3 = new OpenVPNThreadv3(this,mProfile);
-
processThread = mOpenVPN3;
+ mManagement = mOpenVPN3;
+
} else {
-
processThread = new OpenVPNThread(this, argv,nativelibdir);
-
}
+
mProcessThread = new Thread(processThread, "OpenVPNProcessThread");
mProcessThread.start();
+ registerNetworkStateReceiver(mManagement);
+
+
ProfileManager.setConnectedVpnProfile(this, mProfile);
return START_NOT_STICKY;
@@ -339,7 +353,7 @@ public class OpenVpnService extends VpnService implements StateListener, Callbac
@Override
public void onDestroy() {
if (mProcessThread != null) {
- mManagementThread.managmentCommand("signal SIGINT\n");
+ mManagement.stopVPN();
mProcessThread.interrupt();
}
@@ -500,13 +514,13 @@ public class OpenVpnService extends VpnService implements StateListener, Callbac
public void setMtu(int mtu) {
mMtu=mtu;
}
-
+
public void setLocalIP(CIDRIP cdrip)
{
mLocalIP=cdrip;
}
-
-
+
+
public void setLocalIP(String local, String netmask,int mtu, String mode) {
mLocalIP = new CIDRIP(local, netmask);
mMtu = mtu;
@@ -597,7 +611,7 @@ public class OpenVpnService extends VpnService implements StateListener, Callbac
}
}
- public OpenVpnManagementThread getManagementThread() {
- return mManagementThread;
+ public OpenVPNMangement getManagement() {
+ return mManagement;
}
}