diff --git a/src/org/torproject/android/service/TorService.java b/src/org/torproject/android/service/TorService.java index ca92dbb6..da4d0c10 100644 --- a/src/org/torproject/android/service/TorService.java +++ b/src/org/torproject/android/service/TorService.java @@ -629,6 +629,14 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon if (socksPortPref.indexOf(':')!=-1) socksPortPref = socksPortPref.split(":")[1]; + if (!socksPortPref.equalsIgnoreCase("auto")) + { + boolean isPortUsed = TorServiceUtils.isPortOpen("127.0.0.1",Integer.parseInt(socksPortPref),500); + + if (isPortUsed) //the specified port is not available, so let Tor find one instead + socksPortPref = "auto"; + } + extraLines.append("SOCKSPort ").append(socksPortPref).append('\n'); extraLines.append("SafeSocks 0").append('\n'); extraLines.append("TestSocks 0").append('\n'); @@ -1094,86 +1102,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon confSocks = st.nextToken().split(":")[1]; confSocks = confSocks.substring(0,confSocks.length()-1); mPortSOCKS = Integer.parseInt(confSocks); - - /** - if (!isReconnect) //if we are reconnected then we don't need to reset the ports - { - - SharedPreferences prefs = TorServiceUtils.getSharedPrefs(getApplicationContext()); - - String socksPortPref = prefs.getString(OrbotConstants.PREF_SOCKS, - String.valueOf(TorServiceConstants.SOCKS_PROXY_PORT_DEFAULT)); - if (socksPortPref.indexOf(':')!=-1) - socksPortPref = socksPortPref.split(":")[1]; - - String transPort = prefs.getString("pref_transport", TorServiceConstants.TOR_TRANSPROXY_PORT_DEFAULT+""); - if (transPort.indexOf(':')!=-1) - transPort = transPort.split(":")[1]; - - String dnsPort = prefs.getString("pref_dnsport", TorServiceConstants.TOR_DNS_PORT_DEFAULT+""); - if (dnsPort.indexOf(':')!=-1) - dnsPort = dnsPort.split(":")[1]; - - try - { - int newSocksPort = Integer.parseInt(socksPortPref); - - ArrayList socksLines = new ArrayList(); - socksLines.add("SOCKSPort " + mPortSOCKS); - socksLines.add("SOCKSPort " + socksPortPref); - - conn.setConf(socksLines); - - mPortSOCKS = newSocksPort; - - sendCallbackLogMessage("Local SOCKS port: " + socksPortPref); - - } - catch (Exception e) - { - sendCallbackLogMessage("Error setting TransProxy port to: " + socksPortPref); - - - } - - try - { - ArrayList confLines = new ArrayList(); - - confLines.add("TransPort " + transPort); - - conn.setConf(confLines); - - sendCallbackLogMessage("Local TransProxy port: " + transPort); - - } - catch (Exception e) - { - sendCallbackLogMessage("ERROR setting TransProxy port to: " + transPort); - - - - } - - try - { - ArrayList confLines = new ArrayList(); - - confLines.add("DNSPort " + dnsPort); - - conn.setConf(confLines); - - sendCallbackLogMessage("Local DNSPort port: " + dnsPort); - - } - catch (Exception e) - { - sendCallbackLogMessage("ERROR setting DNSport to: " + dnsPort); - - - } - }*/ - + return Integer.parseInt(torProcId); } @@ -1277,6 +1206,8 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon Intent intent = new Intent(TorService.this, OrbotVpnService.class); intent.setAction("start"); + intent.putExtra("torSocks", mPortSOCKS); + if (!mIsLollipop) intent.putExtra("proxyPort",OrbotVpnService.mSocksProxyPort); @@ -2086,18 +2017,17 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon } - if (entranceNodes.length() > 0 || exitNodes.length() > 0 || excludeNodes.length() > 0) + + //only apply GeoIP if you need it + File fileGeoIP = new File(OrbotApp.appBinHome, GEOIP_ASSET_KEY); + File fileGeoIP6 = new File(OrbotApp.appBinHome, GEOIP6_ASSET_KEY); + + if (fileGeoIP.exists()) { - - //only apply GeoIP if you need it - File fileGeoIP = new File(OrbotApp.appBinHome, GEOIP_ASSET_KEY); - File fileGeoIP6 = new File(OrbotApp.appBinHome, GEOIP6_ASSET_KEY); - - extraLines.append("GeoIPFile" + ' ' + fileGeoIP.getCanonicalPath()).append('\n'); - extraLines.append("GeoIPv6File" + ' ' + fileGeoIP6.getCanonicalPath()).append('\n'); - + extraLines.append("GeoIPFile" + ' ' + fileGeoIP.getCanonicalPath()).append('\n'); + extraLines.append("GeoIPv6File" + ' ' + fileGeoIP6.getCanonicalPath()).append('\n'); } - + if (!TextUtils.isEmpty(entranceNodes)) extraLines.append("EntryNodes" + ' ' + entranceNodes).append('\n'); diff --git a/src/org/torproject/android/service/TorServiceUtils.java b/src/org/torproject/android/service/TorServiceUtils.java index 91d48e95..b8ae024b 100644 --- a/src/org/torproject/android/service/TorServiceUtils.java +++ b/src/org/torproject/android/service/TorServiceUtils.java @@ -7,6 +7,9 @@ import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; +import java.net.ConnectException; +import java.net.InetSocketAddress; +import java.net.Socket; import java.util.StringTokenizer; import org.sufficientlysecure.rootcommands.Shell; @@ -166,5 +169,22 @@ public class TorServiceUtils implements TorServiceConstants { } } + public static boolean isPortOpen(final String ip, final int port, final int timeout) { + try { + Socket socket = new Socket(); + socket.connect(new InetSocketAddress(ip, port), timeout); + socket.close(); + return true; + } + catch(ConnectException ce){ + ce.printStackTrace(); + return false; + } + + catch (Exception ex) { + ex.printStackTrace(); + return false; + } + } } diff --git a/src/org/torproject/android/vpn/OrbotVpnService.java b/src/org/torproject/android/vpn/OrbotVpnService.java index 6cc718c0..9b039bc9 100644 --- a/src/org/torproject/android/vpn/OrbotVpnService.java +++ b/src/org/torproject/android/vpn/OrbotVpnService.java @@ -65,8 +65,11 @@ public class OrbotVpnService extends VpnService implements Handler.Callback { private String mSessionName = "OrbotVPN"; private ParcelFileDescriptor mInterface; + private int mTorSocks = TorServiceConstants.SOCKS_PROXY_PORT_DEFAULT; + public static int mSocksProxyPort = -1; private ProxyServer mSocksProxyServer; + private final static int VPN_MTU = 1500; @@ -96,6 +99,7 @@ public class OrbotVpnService extends VpnService implements Handler.Callback { { Log.d(TAG,"starting OrbotVPNService service!"); + mTorSocks = intent.getIntExtra("torSocks", TorServiceConstants.SOCKS_PROXY_PORT_DEFAULT); mSocksProxyPort = intent.getIntExtra("proxyPort", 0); // The handler is only used to show messages. @@ -277,7 +281,7 @@ public class OrbotVpnService extends VpnService implements Handler.Callback { final String defaultRoute = "0.0.0.0"; final String localSocks = localhost + ':' - + String.valueOf(TorServiceConstants.SOCKS_PROXY_PORT_DEFAULT); + + String.valueOf(mTorSocks); final String localDNS = virtualGateway + ':' + "8091";//String.valueOf(TorServiceConstants.TOR_DNS_PORT_DEFAULT); final boolean localDnsTransparentProxy = true;