From bbf93681fe1383c06edc47c89b50d444df57e56b Mon Sep 17 00:00:00 2001 From: Arne Schwabe Date: Sun, 12 Aug 2012 18:52:38 +0200 Subject: Implement starting a VPN on boot. (closes issue #62) --- AndroidManifest.xml | 24 +++++++++------ res/values-de/strings.xml | 2 ++ res/values/strings.xml | 2 ++ res/xml/general_settings.xml | 6 ++++ src/de/blinkt/openvpn/LaunchVPN.java | 19 +++++------- src/de/blinkt/openvpn/LogWindow.java | 3 ++ src/de/blinkt/openvpn/OnBootReceiver.java | 33 ++++++++++++++++++++ src/de/blinkt/openvpn/OpenVpnManagementThread.java | 1 - src/de/blinkt/openvpn/OpenVpnService.java | 4 +-- src/de/blinkt/openvpn/ProfileManager.java | 36 ++++++++++++++++++++++ src/de/blinkt/openvpn/VpnProfile.java | 4 +++ 11 files changed, 110 insertions(+), 24 deletions(-) create mode 100644 src/de/blinkt/openvpn/OnBootReceiver.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 25c3fbdf..f63e6ffe 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -22,8 +22,9 @@ - + + @@ -31,8 +32,9 @@ android:icon="@drawable/icon" android:label="@string/app" > - + @@ -53,8 +55,12 @@ + + + + + - @@ -63,19 +69,19 @@ + - - + + - - + @@ -84,7 +90,7 @@ android:pathPattern=".*\\.ovpn" android:scheme="content" /> - + diff --git a/res/values-de/strings.xml b/res/values-de/strings.xml index 8884b9e2..f5c9c1f8 100644 --- a/res/values-de/strings.xml +++ b/res/values-de/strings.xml @@ -196,4 +196,6 @@ Importiere die PKCS12 Datei, die in der Konfiguration angegeben ist, in den Android Keystore Benutze System Proxies Benutze die System weiten Einstellungen für HTTP/HTTPS Proxies beim Verbinden. + Openvpn wird bei einem Neustart des Telefon das beim herrunterfahren/neu starten aktive VPN weider verbinden. Bitte lesen Sie die FAQ "Warnung beim Verbinden" FAQ bevor Sie diese Option verwenden. + Nach Neustart verbinden diff --git a/res/values/strings.xml b/res/values/strings.xml index 3627f30a..d18c9563 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -242,4 +242,6 @@ Use system proxy Use the system wide configuration for HTTP/HTTPS proxies to connect. You can <a href=\"https://www.paypal.com/cgi-bin/webscr?hosted_button_id=R2M6ZP9AF25LS&amp;cmd=_s-xclick\">donate with PayPal</a> + OpenVPN will reconnect a VPN if it was active on system reboot/shutdown. Please read the Connection warning FAQ before using this option. + Reconnect on reboot diff --git a/res/xml/general_settings.xml b/res/xml/general_settings.xml index 5a2be01d..3013963d 100644 --- a/res/xml/general_settings.xml +++ b/res/xml/general_settings.xml @@ -21,6 +21,11 @@ android:key="usesystemproxy" android:summary="@string/use_system_proxy_summary" android:title="@string/use_system_proxy" /> + + \ No newline at end of file diff --git a/src/de/blinkt/openvpn/LaunchVPN.java b/src/de/blinkt/openvpn/LaunchVPN.java index bfc6256e..a2a0de3f 100644 --- a/src/de/blinkt/openvpn/LaunchVPN.java +++ b/src/de/blinkt/openvpn/LaunchVPN.java @@ -75,12 +75,14 @@ public class LaunchVPN extends ListActivity implements OnItemClickListener { static final String EXTRA_KEY = "de.blinkt.openvpn.shortcutProfileUUID"; static final String EXTRA_NAME = "de.blinkt.openvpn.shortcutProfileName"; + public static final String EXTRA_HIDELOG = "de.blinkt.openvpn.showNoLogWindow";; private static final int START_VPN_PROFILE= 70; + private ProfileManager mPM; private VpnProfile mSelectedProfile; - + private boolean mhideLog=false; private boolean mCmfixed=false; @@ -102,10 +104,12 @@ public class LaunchVPN extends ListActivity implements OnItemClickListener { // If the intent is a request to create a shortcut, we'll do that and exit + if(Intent.ACTION_MAIN.equals(action)) { // we got called to be the starting point, most likely a shortcut String shortcutUUID = intent.getStringExtra( EXTRA_KEY); String shortcutName = intent.getStringExtra( EXTRA_NAME); + mhideLog = intent.getBooleanExtra(EXTRA_HIDELOG, false); VpnProfile profileToConnect = ProfileManager.get(shortcutUUID); if(shortcutName != null && profileToConnect ==null) @@ -122,18 +126,9 @@ public class LaunchVPN extends ListActivity implements OnItemClickListener { mSelectedProfile = profileToConnect; launchVPN(); - - } else if (Intent.ACTION_CREATE_SHORTCUT.equals(action)) { createListView(); } - - - - - - - } private void createListView() { @@ -318,7 +313,8 @@ public class LaunchVPN extends ListActivity implements OnItemClickListener { } else { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); boolean showlogwindow = prefs.getBoolean("showlogwindow", true); - if(showlogwindow) + + if(!mhideLog && showlogwindow) showLogWindow(); new startOpenVpnThread().start(); } @@ -372,7 +368,6 @@ public class LaunchVPN extends ListActivity implements OnItemClickListener { } - if (intent != null) { // Start the query try { diff --git a/src/de/blinkt/openvpn/LogWindow.java b/src/de/blinkt/openvpn/LogWindow.java index ae5277cd..6cc257a5 100644 --- a/src/de/blinkt/openvpn/LogWindow.java +++ b/src/de/blinkt/openvpn/LogWindow.java @@ -58,6 +58,8 @@ public class LogWindow extends ListActivity implements StateListener { OpenVPN.addLogListener(this); } + + private void initLogBuffer() { myEntries.clear(); for (LogItem litem : OpenVPN.getlogbuffer()) { @@ -207,6 +209,7 @@ public class LogWindow extends ListActivity implements StateListener { @Override public void onClick(DialogInterface dialog, int which) { + ProfileManager.onBootDelete(getApplicationContext()); OpenVpnManagementThread.stopOpenVPN(); } }); diff --git a/src/de/blinkt/openvpn/OnBootReceiver.java b/src/de/blinkt/openvpn/OnBootReceiver.java new file mode 100644 index 00000000..032501b6 --- /dev/null +++ b/src/de/blinkt/openvpn/OnBootReceiver.java @@ -0,0 +1,33 @@ +package de.blinkt.openvpn; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; + + +public class OnBootReceiver extends BroadcastReceiver { + + // Debug: am broadcast -a android.intent.action.BOOT_COMPLETED + @Override + public void onReceive(Context context, Intent intent) { + + final String action = intent.getAction(); + + if(Intent.ACTION_BOOT_COMPLETED.equals(action)) { + VpnProfile bootProfile = ProfileManager.getOnBootProfile(context); + if(bootProfile != null) { + lauchVPN(bootProfile, context); + } + } + } + + void lauchVPN(VpnProfile profile,Context context) { + Intent startVpnIntent = new Intent(Intent.ACTION_MAIN); + startVpnIntent.setClass(context, LaunchVPN.class); + startVpnIntent.putExtra(LaunchVPN.EXTRA_KEY,profile.getUUIDString()); + startVpnIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + startVpnIntent.putExtra(LaunchVPN.EXTRA_HIDELOG, true); + + context.startActivity(startVpnIntent); + } +} diff --git a/src/de/blinkt/openvpn/OpenVpnManagementThread.java b/src/de/blinkt/openvpn/OpenVpnManagementThread.java index c42e7516..4e7729ec 100644 --- a/src/de/blinkt/openvpn/OpenVpnManagementThread.java +++ b/src/de/blinkt/openvpn/OpenVpnManagementThread.java @@ -18,7 +18,6 @@ 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; diff --git a/src/de/blinkt/openvpn/OpenVpnService.java b/src/de/blinkt/openvpn/OpenVpnService.java index f56e873b..01def8eb 100644 --- a/src/de/blinkt/openvpn/OpenVpnService.java +++ b/src/de/blinkt/openvpn/OpenVpnService.java @@ -67,6 +67,7 @@ public class OpenVpnService extends VpnService implements StateListener { OpenVpnManagementThread.stopOpenVPN(); mServiceThread=null; stopSelf(); + ProfileManager.onBootDelete(this); }; private void hideNotification() { @@ -200,9 +201,8 @@ public class OpenVpnService extends VpnService implements StateListener { mServiceThread = new Thread(serviceThread, "OpenVPNServiceThread"); mServiceThread.start(); + ProfileManager.setOnBootProfile(this, mProfile); - - return START_NOT_STICKY; } diff --git a/src/de/blinkt/openvpn/ProfileManager.java b/src/de/blinkt/openvpn/ProfileManager.java index b1321b97..5d498c67 100644 --- a/src/de/blinkt/openvpn/ProfileManager.java +++ b/src/de/blinkt/openvpn/ProfileManager.java @@ -14,12 +14,17 @@ import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; +import android.preference.PreferenceManager; public class ProfileManager { private static final String PREFS_NAME = "VPNList"; + private static final String ONBOOTPROFILE = "onBootProfile"; + + + private static ProfileManager instance; private HashMap profiles=new HashMap(); @@ -47,6 +52,37 @@ public class ProfileManager { return instance; } + public static void onBootDelete(Context c) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c); + Editor prefsedit = prefs.edit(); + prefsedit.putString(ONBOOTPROFILE, null); + prefsedit.apply(); + + } + + public static void setOnBootProfile(Context c, VpnProfile bootprofile) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c); + Editor prefsedit = prefs.edit(); + + prefsedit.putString(ONBOOTPROFILE, bootprofile.getUUIDString()); + prefsedit.apply(); + + } + + public static VpnProfile getOnBootProfile(Context c) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c); + + boolean useStartOnBoot = prefs.getBoolean("restartvpnonboot", false); + + + String mBootProfileUUID = prefs.getString(ONBOOTPROFILE,null); + if(useStartOnBoot && mBootProfileUUID!=null) + return get(c, mBootProfileUUID); + else + return null; + } + + public Collection getProfiles() { diff --git a/src/de/blinkt/openvpn/VpnProfile.java b/src/de/blinkt/openvpn/VpnProfile.java index a6dfe053..024874a4 100644 --- a/src/de/blinkt/openvpn/VpnProfile.java +++ b/src/de/blinkt/openvpn/VpnProfile.java @@ -24,6 +24,7 @@ import org.spongycastle.util.io.pem.PemWriter; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; +import android.content.SharedPreferences.Editor; import android.content.pm.ApplicationInfo; import android.preference.PreferenceManager; import android.security.KeyChain; @@ -710,6 +711,9 @@ public class VpnProfile implements Serializable{ public PrivateKey getKeystoreKey() { return mPrivateKey; } + + + } -- cgit v1.2.3