From 1852cde041851b0529ef50a110a74a029bd97b02 Mon Sep 17 00:00:00 2001 From: Nathan Freitas Date: Wed, 4 Feb 2015 14:55:57 -0500 Subject: [PATCH] enable local DNS listen on 10.0.0.1 for VPN service also add support for stopping VPNBuilder instance --- .../android/service/TorService.java | 27 ++++++++- .../android/vpn/OrbotVpnService.java | 55 +++++++++++++------ 2 files changed, 62 insertions(+), 20 deletions(-) diff --git a/src/org/torproject/android/service/TorService.java b/src/org/torproject/android/service/TorService.java index a20a2276..b2879752 100644 --- a/src/org/torproject/android/service/TorService.java +++ b/src/org/torproject/android/service/TorService.java @@ -141,6 +141,8 @@ public class TorService extends Service implements TorServiceConstants, TorConst private boolean mTransProxyTethering = false; private boolean mTransProxyNetworkRefresh = false; + private boolean mUseVPN = false; + private ExecutorService mExecutor = Executors.newFixedThreadPool(1); public void debug(String msg) @@ -368,7 +370,6 @@ public class TorService extends Service implements TorServiceConstants, TorConst if (action!=null){ if(action.equals(Intent.ACTION_BOOT_COMPLETED)||action.equals(CMD_START)){ - clearVpnProxy(); setTorProfile(STATUS_ON); }else if (action.equals(CMD_STOP)){ setTorProfile(STATUS_OFF); @@ -381,7 +382,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst flushTransparentProxyRules(); }else if (action.equals(CMD_UPDATE)){ processSettings(); - }else if (action.equals(CMD_VPN)){ + }else if (action.equals(CMD_VPN)){ enableVpnProxy(); } else if (action.equals(CMD_VPN_CLEAR)){ @@ -1422,6 +1423,10 @@ public class TorService extends Service implements TorServiceConstants, TorConst public void enableVpnProxy () { + debug ("enabling VPN Proxy"); + + mUseVPN = true; + SharedPreferences prefs = TorServiceUtils.getSharedPrefs(getApplicationContext()); Editor ePrefs = prefs.edit(); @@ -1434,12 +1439,18 @@ public class TorService extends Service implements TorServiceConstants, TorConst processSettings(); Intent intent = new Intent(TorService.this, OrbotVpnService.class); + intent.setAction("start"); startService(intent); + } public void clearVpnProxy () - { + { + debug ("clearing VPN Proxy"); + + mUseVPN = false; + SharedPreferences prefs = TorServiceUtils.getSharedPrefs(getApplicationContext()); Editor ePrefs = prefs.edit(); ePrefs.remove("pref_proxy_type"); @@ -1449,6 +1460,10 @@ public class TorService extends Service implements TorServiceConstants, TorConst ePrefs.remove("pref_proxy_password"); ePrefs.commit(); processSettings(); + + Intent intent = new Intent(TorService.this, OrbotVpnService.class); + intent.setAction("stop"); + startService(intent); } @@ -2314,6 +2329,12 @@ public class TorService extends Service implements TorServiceConstants, TorConst updateConfiguration("HiddenServiceDir","", false); } + + if (mUseVPN) + { + updateConfiguration("DNSListenAddress","10.0.0.1:" + TorServiceConstants.TOR_DNS_PORT_DEFAULT,false); + } + saveConfiguration(); diff --git a/src/org/torproject/android/vpn/OrbotVpnService.java b/src/org/torproject/android/vpn/OrbotVpnService.java index 315ace8d..c53d288a 100644 --- a/src/org/torproject/android/vpn/OrbotVpnService.java +++ b/src/org/torproject/android/vpn/OrbotVpnService.java @@ -41,7 +41,7 @@ import com.runjva.sourceforge.jsocks.server.ServerAuthenticatorNone; @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) public class OrbotVpnService extends VpnService implements Handler.Callback { - private static final String TAG = "DrobotVpnService"; + private static final String TAG = "OrbotVpnService"; private PendingIntent mConfigureIntent; @@ -60,20 +60,32 @@ public class OrbotVpnService extends VpnService implements Handler.Callback { @Override public int onStartCommand(Intent intent, int flags, int startId) { - // The handler is only used to show messages. - if (mHandler == null) { - mHandler = new Handler(this); - } - - // Stop the previous session by interrupting the thread. - if (mThreadVPN == null || (!mThreadVPN.isAlive())) - { - enableAppRouting (); - setupTun2Socks(); - } + String action = intent.getAction(); + + if (action.equals("start")) + { + Log.d(TAG,"starting OrbotVPNService service!"); + + // The handler is only used to show messages. + if (mHandler == null) { + mHandler = new Handler(this); + } + + // Stop the previous session by interrupting the thread. + if (mThreadVPN == null || (!mThreadVPN.isAlive())) + { + enableAppRouting (); + setupTun2Socks(); + } + } + else if (action.equals("stop")) + { + stopVPN(); + mHandler.postDelayed(new Runnable () { public void run () { stopSelf(); }}, 1000); + } - return START_STICKY; + return START_NOT_STICKY; } private void enableAppRouting () @@ -113,12 +125,21 @@ public class OrbotVpnService extends VpnService implements Handler.Callback { @Override public void onDestroy() { + stopVPN(); + + } + + private void stopVPN () + { if (mProxyServer != null){ mProxyServer.stop(); + mProxyServer = null; } if (mInterface != null){ try { + Log.d(TAG,"closing interface, destroying VPN interface"); mInterface.close(); + mInterface = null; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); @@ -154,14 +175,14 @@ public class OrbotVpnService extends VpnService implements Handler.Callback { // (i.e., Farsi and Arabic).^M Locale.setDefault(new Locale("en")); - String localhost = InetAddress.getLocalHost().getHostAddress(); + //String localhost = InetAddress.getLocalHost().getHostAddress(); String vpnName = "OrbotVPN"; String virtualGateway = "10.0.0.1"; String virtualIP = "10.0.0.2"; String virtualNetMask = "255.255.255.0"; - String localSocks = localhost + ':' + TorServiceConstants.PORT_SOCKS_DEFAULT; - String localDNS = localhost + ':' + TorServiceConstants.TOR_DNS_PORT_DEFAULT; + String localSocks = "127.0.0.1" + ':' + TorServiceConstants.PORT_SOCKS_DEFAULT; + String localDNS = "10.0.0.1" + ':' + TorServiceConstants.TOR_DNS_PORT_DEFAULT; Builder builder = new Builder(); @@ -170,7 +191,7 @@ public class OrbotVpnService extends VpnService implements Handler.Callback { builder.addAddress(virtualGateway,28); builder.setSession(vpnName); builder.addRoute("0.0.0.0",0); - builder.addDnsServer("8.8.8.8"); + // builder.addDnsServer("8.8.8.8"); // Create a new interface using the builder and save the parameters. mInterface = builder.setSession(mSessionName)