From 1c8e1f9adb41d69829886538e9b2b25a9aeb7bbb Mon Sep 17 00:00:00 2001 From: Arne Schwabe Date: Thu, 27 Dec 2012 19:47:35 +0100 Subject: Make changes needed to provide an external API --- src/de/blinkt/openvpn/ConfigConverter.java | 14 +--- src/de/blinkt/openvpn/ConfigParser.java | 18 +++++- src/de/blinkt/openvpn/LaunchVPN.java | 65 +------------------ src/de/blinkt/openvpn/LogWindow.java | 2 +- src/de/blinkt/openvpn/OpenVPN.java | 4 +- src/de/blinkt/openvpn/OpenVPNThread.java | 10 +-- src/de/blinkt/openvpn/OpenVpnManagementThread.java | 7 ++ src/de/blinkt/openvpn/OpenVpnService.java | 22 +++---- src/de/blinkt/openvpn/ProfileManager.java | 8 +++ src/de/blinkt/openvpn/VPNLaunchHelper.java | 74 ++++++++++++++++++++++ src/de/blinkt/openvpn/VpnProfile.java | 2 +- 11 files changed, 131 insertions(+), 95 deletions(-) create mode 100644 src/de/blinkt/openvpn/VPNLaunchHelper.java (limited to 'src/de') diff --git a/src/de/blinkt/openvpn/ConfigConverter.java b/src/de/blinkt/openvpn/ConfigConverter.java index 40aa24e0..5065436c 100644 --- a/src/de/blinkt/openvpn/ConfigConverter.java +++ b/src/de/blinkt/openvpn/ConfigConverter.java @@ -350,17 +350,9 @@ public class ConfigConverter extends ListActivity { mResult.mPKCS12Filename = embedFile(mResult.mPKCS12Filename,true); - if(mResult.mUsername != null && !mResult.mUsername.equals("")){ - String data =embedFile(mResult.mUsername); - mResult.mUsername=null; - if(data!=null) { - data = data.replace(VpnProfile.INLINE_TAG, ""); - String[] parts = data.split("\n"); - if(parts.length >= 2) { - mResult.mUsername=parts[0]; - mResult.mPassword=parts[1]; - } - } + if(mResult.mUsername == null && mResult.mPassword != null ){ + String data =embedFile(mResult.mPassword); + ConfigParser.useEmbbedUserAuth(mResult, data); } } diff --git a/src/de/blinkt/openvpn/ConfigParser.java b/src/de/blinkt/openvpn/ConfigParser.java index cdec964e..faf0dbd9 100644 --- a/src/de/blinkt/openvpn/ConfigParser.java +++ b/src/de/blinkt/openvpn/ConfigParser.java @@ -242,7 +242,7 @@ public class ConfigParser { // This method is far too long - VpnProfile convertProfile() throws ConfigParseError{ + public VpnProfile convertProfile() throws ConfigParseError{ boolean noauthtypeset=true; VpnProfile np = new VpnProfile("converted Profile"); // Pull, client, tls-client @@ -461,7 +461,6 @@ public class ConfigParser { Vector authuser = getOption("auth-user-pass",0,1); if(authuser !=null){ - if(noauthtypeset) { np.mAuthenticationType=VpnProfile.TYPE_USERPASS; } else if(np.mAuthenticationType==VpnProfile.TYPE_CERTIFICATES) { @@ -470,7 +469,10 @@ public class ConfigParser { np.mAuthenticationType=VpnProfile.TYPE_USERPASS_KEYSTORE; } if(authuser.size()>1) { - np.mUsername=authuser.get(1); + // Set option value to password get to get canche to embed later. + np.mUsername=null; + np.mPassword=authuser.get(1); + useEmbbedUserAuth(np,authuser.get(1)); } } @@ -483,6 +485,16 @@ public class ConfigParser { return np; } + + static public void useEmbbedUserAuth(VpnProfile np,String inlinedata) + { + String data = inlinedata.replace(VpnProfile.INLINE_TAG, ""); + String[] parts = data.split("\n"); + if(parts.length >= 2) { + np.mUsername=parts[0]; + np.mPassword=parts[1]; + } + } private void checkIgnoreAndInvalidOptions(VpnProfile np) throws ConfigParseError { for(String option:unsupportedOptions) diff --git a/src/de/blinkt/openvpn/LaunchVPN.java b/src/de/blinkt/openvpn/LaunchVPN.java index bc0a4cf2..e45bee3b 100644 --- a/src/de/blinkt/openvpn/LaunchVPN.java +++ b/src/de/blinkt/openvpn/LaunchVPN.java @@ -218,55 +218,7 @@ public class LaunchVPN extends ListActivity implements OnItemClickListener { } - private boolean writeMiniVPN() { - File mvpnout = new File(getCacheDir(),VpnProfile.MINIVPN); - if (mvpnout.exists() && mvpnout.canExecute()) - return true; - - IOException e2 = null; - - try { - - - InputStream mvpn; - - try { - mvpn = getAssets().open("minivpn." + Build.CPU_ABI); - } - catch (IOException errabi) { - OpenVPN.logInfo("Failed getting assets for archicture " + Build.CPU_ABI); - e2=errabi; - mvpn = getAssets().open("minivpn." + Build.CPU_ABI2); - - } - - - FileOutputStream fout = new FileOutputStream(mvpnout); - - byte buf[]= new byte[4096]; - - int lenread = mvpn.read(buf); - while(lenread> 0) { - fout.write(buf, 0, lenread); - lenread = mvpn.read(buf); - } - fout.close(); - - if(!mvpnout.setExecutable(true)) { - OpenVPN.logMessage(0, "","Failed to set minivpn executable"); - return false; - } - - - return true; - } catch (IOException e) { - if(e2!=null) - OpenVPN.logMessage(0, "",e2.getLocalizedMessage()); - OpenVPN.logMessage(0, "",e.getLocalizedMessage()); - e.printStackTrace(); - return false; - } - } + private void askForPW(final int type) { @@ -407,22 +359,11 @@ public class LaunchVPN extends ListActivity implements OnItemClickListener { @Override public void run() { - startOpenVpn(); - } - - void startOpenVpn() { - if(!writeMiniVPN()) { - OpenVPN.logMessage(0, "", "Error writing minivpn binary"); - return; - } - OpenVPN.logMessage(0, "", getString(R.string.building_configration)); - - Intent startVPN = mSelectedProfile.prepareIntent(getBaseContext()); - if(startVPN!=null) - startService(startVPN); + VPNLaunchHelper.startOpenVpn(mSelectedProfile, getBaseContext()); finish(); } + } diff --git a/src/de/blinkt/openvpn/LogWindow.java b/src/de/blinkt/openvpn/LogWindow.java index 790e143a..ad3af278 100644 --- a/src/de/blinkt/openvpn/LogWindow.java +++ b/src/de/blinkt/openvpn/LogWindow.java @@ -296,7 +296,7 @@ public class LogWindow extends ListActivity implements StateListener { @Override protected void onStop() { super.onStop(); - OpenVPN.removeSpeedListener(this); + OpenVPN.removeStateListener(this); } @Override diff --git a/src/de/blinkt/openvpn/OpenVPN.java b/src/de/blinkt/openvpn/OpenVPN.java index 0ae681bc..2d7b7db6 100644 --- a/src/de/blinkt/openvpn/OpenVPN.java +++ b/src/de/blinkt/openvpn/OpenVPN.java @@ -125,13 +125,13 @@ public class OpenVPN { } - synchronized static void addStateListener(StateListener sl){ + public synchronized static void addStateListener(StateListener sl){ stateListener.add(sl); if(mLaststate!=null) sl.updateState(mLaststate, mLaststatemsg); } - synchronized static void removeSpeedListener(StateListener sl) { + public synchronized static void removeStateListener(StateListener sl) { stateListener.remove(sl); } diff --git a/src/de/blinkt/openvpn/OpenVPNThread.java b/src/de/blinkt/openvpn/OpenVPNThread.java index 7d58552a..3922e679 100644 --- a/src/de/blinkt/openvpn/OpenVPNThread.java +++ b/src/de/blinkt/openvpn/OpenVPNThread.java @@ -40,7 +40,8 @@ public class OpenVPNThread implements Runnable { startOpenVPNThreadArgs(mArgv); Log.i(TAG, "Giving up"); } catch (Exception e) { - Log.e(TAG, "Got " + e.toString()); + e.printStackTrace(); + Log.e(TAG, "OpenVPNThread Got " + e.toString()); } finally { int exitvalue = 0; try { @@ -105,12 +106,13 @@ public class OpenVPNThread implements Runnable { while(true) { String logline = br.readLine(); + if(logline==null) + return; + if (logline.startsWith(DUMP_PATH_STRING)) mDumpPath = logline.substring(DUMP_PATH_STRING.length()); - if(logline==null) { - return; - } + OpenVPN.logMessage(0, "P:", logline); } diff --git a/src/de/blinkt/openvpn/OpenVpnManagementThread.java b/src/de/blinkt/openvpn/OpenVpnManagementThread.java index 4e26c44b..ee458200 100644 --- a/src/de/blinkt/openvpn/OpenVpnManagementThread.java +++ b/src/de/blinkt/openvpn/OpenVpnManagementThread.java @@ -563,4 +563,11 @@ public class OpenVpnManagementThread implements Runnable { } } + public static boolean protectFD(ParcelFileDescriptor fd) { + boolean hasBeenProtected=false; + for (OpenVpnManagementThread mt : active) { + hasBeenProtected = hasBeenProtected || mt.mOpenVPNService.protect(fd.getFd()); + } + return hasBeenProtected; + } } diff --git a/src/de/blinkt/openvpn/OpenVpnService.java b/src/de/blinkt/openvpn/OpenVpnService.java index 603f86ce..f4550c13 100644 --- a/src/de/blinkt/openvpn/OpenVpnService.java +++ b/src/de/blinkt/openvpn/OpenVpnService.java @@ -120,7 +120,7 @@ public class OpenVpnService extends VpnService implements StateListener { private void jbNotificationExtras(boolean lowpriority, android.app.Notification.Builder nbuilder) { try { - if( lowpriority) { + if(lowpriority) { Method setpriority = nbuilder.getClass().getMethod("setPriority", int.class); // PRIORITY_MIN == -2 setpriority.invoke(nbuilder, -2 ); @@ -195,8 +195,8 @@ public class OpenVpnService extends VpnService implements StateListener { String prefix = getPackageName(); String[] argv = intent.getStringArrayExtra(prefix + ".ARGV"); String nativelibdir = intent.getStringExtra(prefix + ".nativelib"); - String profileUUID = intent.getStringExtra(prefix + ".profileUUID"); + mProfile = ProfileManager.get(profileUUID); showNotification("Starting VPN " + mProfile.mName,"Starting VPN " + mProfile.mName, false,0); @@ -317,12 +317,12 @@ public class OpenVpnService extends VpnService implements StateListener { String bconfig[] = new String[6]; - bconfig[0]= getString(R.string.last_openvpn_tun_config); - bconfig[1] = String.format(getString(R.string.local_ip_info,mLocalIP.mIp,mLocalIP.len,mLocalIPv6, mMtu)); - bconfig[2] = String.format(getString(R.string.dns_server_info, joinString(mDnslist))); - bconfig[3] = String.format(getString(R.string.dns_domain_info, mDomain)); - bconfig[4] = String.format(getString(R.string.routes_info, joinString(mRoutes))); - bconfig[5] = String.format(getString(R.string.routes_info6, joinString(mRoutesv6))); + bconfig[0]= getString(R.string.last_openvpn_tun_config); + bconfig[1] = getString(R.string.local_ip_info,mLocalIP.mIp,mLocalIP.len,mLocalIPv6, mMtu); + bconfig[2] = getString(R.string.dns_server_info, joinString(mDnslist)); + bconfig[3] = getString(R.string.dns_domain_info, mDomain); + bconfig[4] = getString(R.string.routes_info, joinString(mRoutes)); + bconfig[5] = getString(R.string.routes_info6, joinString(mRoutesv6)); String session = mProfile.mName; if(mLocalIP!=null) @@ -393,11 +393,11 @@ public class OpenVpnService extends VpnService implements StateListener { public void addRoute(String dest, String mask) { CIDRIP route = new CIDRIP(dest, mask); if(route.len == 32 && !mask.equals("255.255.255.255")) { - OpenVPN.logMessage(0, "", String.format(getString(R.string.route_not_cidr,dest,mask))); + OpenVPN.logMessage(0, "", getString(R.string.route_not_cidr,dest,mask)); } if(route.normalise()) - OpenVPN.logMessage(0, "", String.format(getString(R.string.route_not_netip,dest,route.len,route.mIp))); + OpenVPN.logMessage(0, "", getString(R.string.route_not_netip,dest,route.len,route.mIp)); mRoutes.add(route); } @@ -420,7 +420,7 @@ public class OpenVpnService extends VpnService implements StateListener { else mLocalIP.len=31; } else { - OpenVPN.logMessage(0, "", String.format(getString(R.string.ip_not_cidr, local,netmask,mode))); + OpenVPN.logMessage(0, "", getString(R.string.ip_not_cidr, local,netmask,mode)); } } } diff --git a/src/de/blinkt/openvpn/ProfileManager.java b/src/de/blinkt/openvpn/ProfileManager.java index 9457b53f..9f17a68e 100644 --- a/src/de/blinkt/openvpn/ProfileManager.java +++ b/src/de/blinkt/openvpn/ProfileManager.java @@ -31,9 +31,13 @@ public class ProfileManager { private static VpnProfile mLastConnectedVpn=null; private HashMap profiles=new HashMap(); + private static VpnProfile tmpprofile=null; public static VpnProfile get(String key) { + if (tmpprofile!=null && tmpprofile.getUUIDString().equals(key)) + return tmpprofile; + if(instance==null) return null; return instance.profiles.get(key); @@ -122,6 +126,10 @@ public class ProfileManager { } + public static void setTemporaryProfile(VpnProfile tmp) { + ProfileManager.tmpprofile = tmp; + } + public void saveProfile(Context context,VpnProfile profile) { // First let basic settings save its state diff --git a/src/de/blinkt/openvpn/VPNLaunchHelper.java b/src/de/blinkt/openvpn/VPNLaunchHelper.java new file mode 100644 index 00000000..8389462a --- /dev/null +++ b/src/de/blinkt/openvpn/VPNLaunchHelper.java @@ -0,0 +1,74 @@ +package de.blinkt.openvpn; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; + +import android.content.Context; +import android.content.Intent; +import android.os.Build; + +public class VPNLaunchHelper { + static private boolean writeMiniVPN(Context context) { + File mvpnout = new File(context.getCacheDir(),VpnProfile.MINIVPN); + if (mvpnout.exists() && mvpnout.canExecute()) + return true; + + IOException e2 = null; + + try { + InputStream mvpn; + + try { + mvpn = context.getAssets().open("minivpn." + Build.CPU_ABI); + } + catch (IOException errabi) { + OpenVPN.logInfo("Failed getting assets for archicture " + Build.CPU_ABI); + e2=errabi; + mvpn = context.getAssets().open("minivpn." + Build.CPU_ABI2); + + } + + + FileOutputStream fout = new FileOutputStream(mvpnout); + + byte buf[]= new byte[4096]; + + int lenread = mvpn.read(buf); + while(lenread> 0) { + fout.write(buf, 0, lenread); + lenread = mvpn.read(buf); + } + fout.close(); + + if(!mvpnout.setExecutable(true)) { + OpenVPN.logMessage(0, "","Failed to set minivpn executable"); + return false; + } + + + return true; + } catch (IOException e) { + if(e2!=null) + OpenVPN.logMessage(0, "",e2.getLocalizedMessage()); + OpenVPN.logMessage(0, "",e.getLocalizedMessage()); + e.printStackTrace(); + return false; + } + } + + + public static void startOpenVpn(VpnProfile startprofile, Context context) { + if(!writeMiniVPN(context)) { + OpenVPN.logMessage(0, "", "Error writing minivpn binary"); + return; + } + OpenVPN.logMessage(0, "", context.getString(R.string.building_configration)); + + Intent startVPN = startprofile.prepareIntent(context); + if(startVPN!=null) + context.startService(startVPN); + + } +} diff --git a/src/de/blinkt/openvpn/VpnProfile.java b/src/de/blinkt/openvpn/VpnProfile.java index 4c3c05f0..d8b22ae6 100644 --- a/src/de/blinkt/openvpn/VpnProfile.java +++ b/src/de/blinkt/openvpn/VpnProfile.java @@ -595,7 +595,7 @@ public class VpnProfile implements Serializable{ //! Return an error if somethign is wrong - int checkProfile(Context context) { + public int checkProfile(Context context) { if(mAuthenticationType==TYPE_KEYSTORE || mAuthenticationType==TYPE_USERPASS_KEYSTORE) { if(mAlias==null) return R.string.no_keystore_cert_selected; -- cgit v1.2.3