diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/de/blinkt/openvpn/ConfigParser.java | 232 | ||||
-rw-r--r-- | src/de/blinkt/openvpn/Settings_Authentication.java | 13 | ||||
-rw-r--r-- | src/de/blinkt/openvpn/VpnProfile.java | 21 |
3 files changed, 262 insertions, 4 deletions
diff --git a/src/de/blinkt/openvpn/ConfigParser.java b/src/de/blinkt/openvpn/ConfigParser.java new file mode 100644 index 00000000..8497330f --- /dev/null +++ b/src/de/blinkt/openvpn/ConfigParser.java @@ -0,0 +1,232 @@ +package de.blinkt.openvpn; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.util.HashMap; +import java.util.Vector; + +//! Openvpn Config FIle Parser, probably not 100% accurate but close enough + +// And rember, this is valid :) +// --<foo> +// bar +// </bar> +public class ConfigParser { + + + private HashMap<String,Vector<String>> options = new HashMap<String, Vector<String>>(); + private void parseConfig(String filename) throws IOException, ConfigParseError { + + + FileReader fr = new FileReader(filename); + BufferedReader br =new BufferedReader(fr); + + int lineno=0; + + while (true){ + String line = br.readLine(); + if(line==null) + break; + lineno++; + System.out.print("LINE:"); + System.out.println(line); + Vector<String> args = parseline(line); + if(args.size() ==0) + continue; + + + + if(args.get(0).startsWith("--")) + args.set(0, args.get(0).substring(2)); + + checkinlinefile(args,br); + + options.put(args.get(0), args); + } + } + + private void checkinlinefile(Vector<String> args, BufferedReader br) throws IOException, ConfigParseError { + String arg0 = args.get(0); + // CHeck for <foo> + if(arg0.startsWith("<") && arg0.endsWith(">")) { + String argname = arg0.substring(1, arg0.length()-1); + String inlinefile = ""; + + String endtag = String.format("</%s>",argname); + do { + String line = br.readLine(); + if(line==null){ + throw new ConfigParseError(String.format("No endtag </%s> for starttag <%s> found",argname,argname)); + } + if(line.equals(endtag)) + break; + else { + inlinefile+=line; + inlinefile+= "\n"; + } + } while(true); + + args.clear(); + args.add(argname); + args.add(inlinefile); + } + + } + + enum linestate { + initial, + readin_single_quote + , reading_quoted, reading_unquoted, done} + + private boolean space(char c) { + // I really hope nobody is using zero bytes inside his/her config file + // to sperate parameter but here we go: + return Character.isSpace(c) || c == '\0'; + + } + + public class ConfigParseError extends Exception { + private static final long serialVersionUID = -60L; + + public ConfigParseError(String msg) { + super(msg); + } + } + + + // adapted openvpn's parse function to java + private Vector<String> parseline(String line) throws ConfigParseError { + Vector<String> parameters = new Vector<String>(); + + if (line.length()==0) + return parameters; + + + linestate state = linestate.initial; + boolean backslash = false; + char out=0; + + int pos=0; + String currentarg=""; + + do { + // Emulate the c parsing ... + char in; + if(pos < line.length()) + in = line.charAt(pos); + else + in = '\0'; + + if (!backslash && in == '\\' && state != linestate.readin_single_quote) + { + backslash = true; + } + else + { + if (state == linestate.initial) + { + if (!space (in)) + { + if (in == ';' || in == '#') /* comment */ + break; + if (!backslash && in == '\"') + state = linestate.reading_quoted; + else if (!backslash && in == '\'') + state = linestate.readin_single_quote; + else + { + out = in; + state = linestate.reading_unquoted; + } + } + } + else if (state == linestate.reading_unquoted) + { + if (!backslash && space (in)) + state = linestate.done; + else + out = in; + } + else if (state == linestate.reading_quoted) + { + if (!backslash && in == '\"') + state = linestate.done; + else + out = in; + } + else if (state == linestate.readin_single_quote) + { + if (in == '\'') + state = linestate.done; + else + out = in; + } + + if (state == linestate.done) + { + /* ASSERT (parm_len > 0); */ + state = linestate.initial; + parameters.add(currentarg); + currentarg = ""; + out =0; + } + + if (backslash && out!=0) + { + if (!(out == '\\' || out == '\"' || space (out))) + { + throw new ConfigParseError("Options warning: Bad backslash ('\\') usage"); + } + } + backslash = false; + } + + /* store parameter character */ + if (out!=0) + { + currentarg+=out; + } + } while (pos++ < line.length()); + + return parameters; + } + + void convertProfile() throws ConfigParseError{ + VpnProfile newprofile = new VpnProfile("converted Profile"); + // Pull, client, tls-client + + if(options.containsKey("client") || options.containsKey("pull")) { + newprofile.mUsePull=true; + options.remove("pull"); + options.remove("client"); + } + + if(options.containsKey("secret")){ + newprofile.mAuthenticationType=VpnProfile.TYPE_STATICKEYS; + options.remove("secret"); + } + + if(options.containsKey("redirect-gateway")) { + options.remove("redirect-gateway"); + newprofile.mUseDefaultRoute=true; + } else { + newprofile.mUseDefaultRoute=true; + } + + Vector<String> mode = options.get("mode"); + if (mode != null){ + options.remove("mode"); + if(mode.size() != 2) + throw new ConfigParseError("--mode has more than one parameter"); + if(!mode.get(1).equals("p2p")) + throw new ConfigParseError("Invalid mode for --mode specified"); + } + + } + +} + + + + diff --git a/src/de/blinkt/openvpn/Settings_Authentication.java b/src/de/blinkt/openvpn/Settings_Authentication.java index 57d99417..6d21ca6a 100644 --- a/src/de/blinkt/openvpn/Settings_Authentication.java +++ b/src/de/blinkt/openvpn/Settings_Authentication.java @@ -24,6 +24,7 @@ public class Settings_Authentication extends PreferenceFragment implements OnPre private ListPreference mTLSAuthDirection; private Preference mTLSAuthFile; private SwitchPreference mUseTLSAuth; + private EditTextPreference mCipher; @Override public void onCreate(Bundle savedInstanceState) { @@ -45,6 +46,8 @@ public class Settings_Authentication extends PreferenceFragment implements OnPre mProfile = ProfileManager.get(profileUUID); mTLSAuthFile.setOnPreferenceClickListener(this); + mCipher =(EditTextPreference) findPreference("cipher"); + loadSettings(); } @@ -59,6 +62,8 @@ public class Settings_Authentication extends PreferenceFragment implements OnPre mUseTLSAuth.setChecked(mProfile.mUseTLSAuth); mTLSAuthFile.setSummary(mProfile.mTLSAuthFilename); mTLSAuthDirection.setValue(mProfile.mTLSAuthDirection); + mCipher.setText(mProfile.mCipher); + onPreferenceChange(mCipher, mProfile.mCipher); } private void saveSettings() { @@ -76,6 +81,12 @@ public class Settings_Authentication extends PreferenceFragment implements OnPre mProfile.mTLSAuthDirection=null; else mProfile.mTLSAuthDirection = mTLSAuthDirection.getValue().toString(); + + if(mCipher.getText()==null) + mProfile.mCipher=null; + else + mProfile.mCipher = mCipher.getText(); + } @Override @@ -91,6 +102,8 @@ public class Settings_Authentication extends PreferenceFragment implements OnPre preference.setSummary(mProfile.mServerName); else preference.setSummary((String)newValue); + } else if (preference == mCipher) { + preference.setSummary((CharSequence) newValue); } return true; } diff --git a/src/de/blinkt/openvpn/VpnProfile.java b/src/de/blinkt/openvpn/VpnProfile.java index 26cf50bd..1b855f5d 100644 --- a/src/de/blinkt/openvpn/VpnProfile.java +++ b/src/de/blinkt/openvpn/VpnProfile.java @@ -90,10 +90,13 @@ public class VpnProfile implements Serializable{ public boolean mUseCustomConfig=false; public String mCustomConfigOptions=""; public String mVerb="1"; + public String mCipher=""; - public static String openVpnEscape(String unescape) { - String escapedString = unescape.replace("\\", "\\\\"); + public static String openVpnEscape(String unescaped) { + if(unescaped==null) + return null; + String escapedString = unescaped.replace("\\", "\\\\"); escapedString = escapedString.replace("\"","\\\""); escapedString = escapedString.replace("\n","\\n"); return '"' + escapedString + '"'; @@ -269,6 +272,9 @@ public class VpnProfile implements Serializable{ cfg += "remote-cert-tls server\n"; + if(nonNull(mCipher)){ + cfg += "cipher " + mCipher + "\n"; + } // Obscure Settings dialog @@ -291,6 +297,13 @@ public class VpnProfile implements Serializable{ return cfg; } + private boolean nonNull(String val) { + if(val == null || val.equals("")) + return false; + else + return true; + } + private Collection<String> getCustomRoutes() { Vector<String> cidrRoutes=new Vector<String>(); if(mCustomRoutes==null) { @@ -343,8 +356,8 @@ public class VpnProfile implements Serializable{ Vector<String> args = new Vector<String>(); // Add fixed paramenters - //args.add(cacheDir.getAbsolutePath() +"/" +"openvpn"); - args.add(cacheDir.getAbsolutePath() +"/" +"minivpn"); + //args.add("/data/data/de.blinkt.openvpn/lib/openvpn"); + args.add(cacheDir.getAbsolutePath() +"/" +"openvpn"); args.add("--config"); args.add(cacheDir.getAbsolutePath() + "/" + OVPNCONFIGFILE); |