From 78e7af4372cc9dc5f5484fccd7814d1a95d728cc Mon Sep 17 00:00:00 2001 From: Nathan Freitas Date: Thu, 17 Apr 2014 11:34:47 -0400 Subject: [PATCH] check if needed ports are available dont' reapply transproxy on network reconnect --- .../android/service/TorService.java | 41 +++++++++++++++++-- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/src/org/torproject/android/service/TorService.java b/src/org/torproject/android/service/TorService.java index 441bd1ed..48eb5801 100644 --- a/src/org/torproject/android/service/TorService.java +++ b/src/org/torproject/android/service/TorService.java @@ -15,6 +15,8 @@ import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; +import java.net.InetSocketAddress; +import java.net.ServerSocket; import java.net.Socket; import java.util.ArrayList; import java.util.Arrays; @@ -121,8 +123,10 @@ public class TorService extends Service implements TorServiceConstants, TorConst if (ENABLE_DEBUG_LOG) { Log.e(TAG,msg,e); - sendCallbackLogMessage(msg); } + + sendCallbackLogMessage(msg); + } @@ -615,6 +619,14 @@ public class TorService extends Service implements TorServiceConstants, TorConst public void initTor () throws Exception { + boolean portsAvail = checkPortsAvailable(); + + if (!portsAvail) + { + logNotice("Another app is blocking Tor from starting"); + return; + } + try { initBinaries(); @@ -650,6 +662,31 @@ public class TorService extends Service implements TorServiceConstants, TorConst //checkAddressAndCountry(); } + private boolean checkPortsAvailable () + { + int[] ports = {9050,9051,8118}; + + for (int port: ports) + { + try + { + logNotice("checking local port is available: " + port); + + ServerSocket ss = new ServerSocket(); + ss.bind(new InetSocketAddress(IP_LOCALHOST,port)); + ss.close(); + } + catch (Exception e) + { + logException ("Tor socket is not available",e); + return false; + } + } + + return true; + + } + /* * activate means whether to apply the users preferences * or clear them out @@ -1576,8 +1613,6 @@ public class TorService extends Service implements TorServiceConstants, TorConst logNotice("Network connectivity is good. Waking Tor up..."); showToolbarNotification(getString(R.string.status_activated),NOTIFY_ID,R.drawable.ic_stat_tor,-1,prefPersistNotifications); - if (mHasRoot && mEnableTransparentProxy) - enableTransparentProxy(mTransProxyAll, mTransProxyTethering); } } catch (Exception e) {