From ef264f51e7b57e771ce4339261d2bb918580ecc9 Mon Sep 17 00:00:00 2001 From: Arne Schwabe Date: Sat, 5 May 2012 02:33:46 +0200 Subject: Version 0.4.7 with more route checking and fix of the tmp-dir bug :( closes issue #6 --- src/de/blinkt/openvpn/LaunchVPN.java | 5 +- src/de/blinkt/openvpn/LogWindow.java | 7 +- src/de/blinkt/openvpn/OpenVPN.java | 141 ++++++++++++--------- src/de/blinkt/openvpn/OpenVPNThread.java | 2 + src/de/blinkt/openvpn/OpenVpnManagementThread.java | 14 +- src/de/blinkt/openvpn/OpenVpnService.java | 73 ++++++++++- 6 files changed, 167 insertions(+), 75 deletions(-) (limited to 'src/de/blinkt') diff --git a/src/de/blinkt/openvpn/LaunchVPN.java b/src/de/blinkt/openvpn/LaunchVPN.java index d74834c..2e25f7a 100644 --- a/src/de/blinkt/openvpn/LaunchVPN.java +++ b/src/de/blinkt/openvpn/LaunchVPN.java @@ -29,6 +29,8 @@ import android.content.Intent; import android.net.VpnService; import android.os.Bundle; import android.os.Parcelable; +import android.text.InputType; +import android.text.method.PasswordTransformationMethod; import android.view.View; import android.view.inputmethod.EditorInfo; import android.widget.AdapterView; @@ -207,7 +209,8 @@ public class LaunchVPN extends ListActivity implements OnItemClickListener { final EditText entry = new EditText(this); entry.setSingleLine(); - entry.setInputType(EditorInfo.TYPE_TEXT_VARIATION_PASSWORD); + entry.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); + entry.setTransformationMethod(new PasswordTransformationMethod()); AlertDialog.Builder dialog = new AlertDialog.Builder(this); dialog.setTitle("Need " + type); diff --git a/src/de/blinkt/openvpn/LogWindow.java b/src/de/blinkt/openvpn/LogWindow.java index 001dbf1..65e8c66 100644 --- a/src/de/blinkt/openvpn/LogWindow.java +++ b/src/de/blinkt/openvpn/LogWindow.java @@ -23,6 +23,8 @@ import android.widget.TextView; import de.blinkt.openvpn.OpenVPN.LogListener; public class LogWindow extends ListActivity { + private String[] mBconfig=null; + class LogWindowListAdapter implements ListAdapter,LogListener, Callback { @@ -36,6 +38,7 @@ public class LogWindow extends ListActivity { private Vector observers=new Vector(); + public LogWindowListAdapter() { initLogBuffer(); @@ -159,7 +162,6 @@ public class LogWindow extends ListActivity { OpenVPN.logMessage(0,"","Log cleared."); mHandler.sendEmptyMessage(MESSAGE_CLEARLOG); } - } @@ -185,6 +187,9 @@ public class LogWindow extends ListActivity { builder.setNegativeButton(android.R.string.no, null); builder.show(); return true; + } else if(item.getItemId()==R.id.info) { + if(mBconfig==null) + OpenVPN.triggerLogBuilderConfig(); } return super.onOptionsItemSelected(item); diff --git a/src/de/blinkt/openvpn/OpenVPN.java b/src/de/blinkt/openvpn/OpenVPN.java index daa2971..a5f60da 100644 --- a/src/de/blinkt/openvpn/OpenVPN.java +++ b/src/de/blinkt/openvpn/OpenVPN.java @@ -10,77 +10,79 @@ public class OpenVPN { private static final int MAXLOGENTRIES = 500; public static native int startOpenVPNThread(); public static native int startOpenVPNThreadArgs(String argv[]); - private static final String TAG = "OpenVpn"; + private static final String TAG = "OpenVpn"; + - public static LinkedList logbuffer = new LinkedList(); private static int counter=0; private static Vector logListener=new Vector(); - + private static String[] mBconfig=null; + public interface LogListener { void newLog(String logmessage); } - static { - System.loadLibrary("crypto"); - System.loadLibrary("ssl"); - System.loadLibrary("lzo"); - System.loadLibrary("openvpn"); - } - - static void addRoute(String dest,String mask, String gw) { - Log.i("openvpn" ,"Got Routing information " + dest + " " + mask + " " + gw ); - mOpenVpnService.addRoute(dest,mask); - } - - synchronized static void logMessage(int level,String prefix, String message) - { - logbuffer.addLast(prefix + " " + message); - if(logbuffer.size()>MAXLOGENTRIES) - logbuffer.removeFirst(); - - // The garbage collector does not collect the String from native - // but kills me for logging 100 messages with too many references :( - // Force GC how and then to kill loose ends - if(counter++ % 50==0) - System.gc(); - - for (LogListener ll : logListener) { + static { + System.loadLibrary("crypto"); + System.loadLibrary("ssl"); + System.loadLibrary("lzo"); + System.loadLibrary("openvpn"); + } + + static void addRoute(String dest,String mask, String gw) { + Log.i("openvpn" ,"Got Routing information " + dest + " " + mask + " " + gw ); + mOpenVpnService.addRoute(dest,mask); + } + + synchronized static void logMessage(int level,String prefix, String message) + { + logbuffer.addLast(prefix + " " + message); + if(logbuffer.size()>MAXLOGENTRIES) + logbuffer.removeFirst(); + + // The garbage collector does not collect the String from native + // but kills me for logging 100 messages with too many references :( + // Force GC how and then to kill loose ends + if(counter++ % 50==0) { + System.gc(); + } + + for (LogListener ll : logListener) { ll.newLog(prefix + " " + message); } - - } - - synchronized static void clearLog() { - logbuffer.clear(); - } - - synchronized static void addLogListener(LogListener ll){ - logListener.add(ll); - } - - synchronized static void removeLogListener(LogListener ll) { - logListener.remove(ll); - } - - - static void addInterfaceInfo(int mtu, String local, String netmask) - { - Log.i("openvpn","Got interface info M" + mtu + " L: " + local + "NM: " + netmask); - mOpenVpnService.setLocalIP(local,netmask); - } - - static void addDns(String dns) { - Log.i("openvpn","Got DNS Server: " + dns); - mOpenVpnService.addDNS(dns); - } - - - static void addDomain(String domain) { - Log.i("openvpn","Got DNS Domain: " + domain); - mOpenVpnService.setDomain(domain); - } + + } + + synchronized static void clearLog() { + logbuffer.clear(); + } + + synchronized static void addLogListener(LogListener ll){ + logListener.add(ll); + } + + synchronized static void removeLogListener(LogListener ll) { + logListener.remove(ll); + } + + + static void addInterfaceInfo(int mtu, String local, String netmask) + { + Log.i("openvpn","Got interface info M" + mtu + " L: " + local + "NM: " + netmask); + mOpenVpnService.setLocalIP(local,netmask); + } + + static void addDns(String dns) { + Log.i("openvpn","Got DNS Server: " + dns); + mOpenVpnService.addDNS(dns); + } + + + static void addDomain(String domain) { + Log.i("openvpn","Got DNS Domain: " + domain); + mOpenVpnService.setDomain(domain); + } public static void setCallback(OpenVpnService openVpnService) { @@ -96,19 +98,32 @@ public class OpenVPN { Log.e("openvpn","Error protecting socket "+ sockfd); return p; } - + public static int openTunDevice() { Log.d(TAG,"Opening tun device"); return mOpenVpnService.openTun(); } //! Dummy method being called to force loading of JNI Libraries public static void foo() { } - + synchronized public static String[] getlogbuffer() { - + // The stoned way of java to return an array from a vector // brought to you by eclipse auto complete return (String[]) logbuffer.toArray(new String[logbuffer.size()]); } + public static void logBuilderConfig(String[] bconfig) { + mBconfig =bconfig; + } + public static void triggerLogBuilderConfig() { + if(mBconfig==null) { + logMessage(0, "", "No active interface"); + } else { + for (String item : mBconfig) { + logMessage(0, "", item); + } + } + + } } diff --git a/src/de/blinkt/openvpn/OpenVPNThread.java b/src/de/blinkt/openvpn/OpenVPNThread.java index 124179e..a5b3e5e 100644 --- a/src/de/blinkt/openvpn/OpenVPNThread.java +++ b/src/de/blinkt/openvpn/OpenVPNThread.java @@ -55,6 +55,8 @@ public class OpenVPNThread implements Runnable { mService.getHandler().sendEmptyMessage(R.string.disconnected); + // Not a good place to do it, but will do + OpenVPN.logBuilderConfig(null); Log.i(TAG, "Exiting"); } } diff --git a/src/de/blinkt/openvpn/OpenVpnManagementThread.java b/src/de/blinkt/openvpn/OpenVpnManagementThread.java index f05f58d..2421b28 100644 --- a/src/de/blinkt/openvpn/OpenVpnManagementThread.java +++ b/src/de/blinkt/openvpn/OpenVpnManagementThread.java @@ -105,13 +105,14 @@ private static Vector active=new Vector active=new Vector> 24,(newip & 0xff0000) >> 16, (newip & 0xff00) >> 8 ,newip & 0xff); + return true; + } else { + return false; + } + } } @Override public void onRevoke() { - mSocketManager.managmentCommand("signal SIGINT\n"); + OpenVpnManagementThread.stopOpenVPN(); mServiceThread=null; stopSelf(); }; @@ -128,8 +150,12 @@ public class OpenVpnService extends VpnService implements Handler.Callback { } // Stop the previous session by interrupting the thread. - if (mSocketManager != null) { - mSocketManager.managmentCommand("signal SIGINT\n"); + if(OpenVpnManagementThread.stopOpenVPN()){ + // an old was asked to exit, wait 2s + try { + Thread.sleep(2000); + } catch (InterruptedException e) { + } } if (mServiceThread!=null) { @@ -210,13 +236,25 @@ public class OpenVpnService extends VpnService implements Handler.Callback { if(mDomain!=null) builder.addSearchDomain(mDomain); - + String bconfig[] = new String[5]; + + bconfig[0]= getString(R.string.last_openvpn_tun_config); + bconfig[1] = String.format(getString(R.string.local_ip_info,mLocalIP.mIp,mLocalIP.len)); + 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))); + + + OpenVPN.logBuilderConfig(bconfig); + mDnslist.clear(); mRoutes.clear(); builder.setSession(mProfile.mName + " - " + mLocalIP); + + // Let the configure Button show the Log Intent intent = new Intent(getBaseContext(),LogWindow.class); PendingIntent startLW = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0); @@ -233,6 +271,23 @@ public class OpenVpnService extends VpnService implements Handler.Callback { } + + // Ugly, but java has no such method + private String joinString(Vector vec) { + String ret = ""; + if(vec.size() > 0); + ret = vec.get(0).toString(); + for(int i=1;i < vec.size();i++) { + ret = ret + ", " + vec.get(i).toString(); + } + return ret; + } + + + + + + public void addDNS(String dns) { mDnslist.add(dns); } @@ -246,7 +301,15 @@ public class OpenVpnService extends VpnService implements Handler.Callback { public void addRoute(String dest, String mask) { - mRoutes.add(new CIDRIP(dest, 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))); + } + + if(route.normalise()) + OpenVPN.logMessage(0, "", String.format(getString(R.string.route_not_netip,dest,route.len,route.mIp))); + + mRoutes.add(route); } -- cgit v1.2.3