add ability to check if configured SOCKS port is available

Samsung devices like to use 9050 (Since their hardware model is i9050!). Tor likes to use port 9050. This new code looks to see if port 9050 is available, and if not, change the setting to 'auto' so Tor can find another port.
This commit is contained in:
Nathan Freitas 2016-01-15 23:43:54 -05:00
parent b4ca30b811
commit 6ffea9e38f
3 changed files with 45 additions and 91 deletions

View File

@ -629,6 +629,14 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
if (socksPortPref.indexOf(':')!=-1) if (socksPortPref.indexOf(':')!=-1)
socksPortPref = socksPortPref.split(":")[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("SOCKSPort ").append(socksPortPref).append('\n');
extraLines.append("SafeSocks 0").append('\n'); extraLines.append("SafeSocks 0").append('\n');
extraLines.append("TestSocks 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 = st.nextToken().split(":")[1];
confSocks = confSocks.substring(0,confSocks.length()-1); confSocks = confSocks.substring(0,confSocks.length()-1);
mPortSOCKS = Integer.parseInt(confSocks); 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<String> socksLines = new ArrayList<String>();
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<String> confLines = new ArrayList<String>();
confLines.add("TransPort " + transPort);
conn.setConf(confLines);
sendCallbackLogMessage("Local TransProxy port: " + transPort);
}
catch (Exception e)
{
sendCallbackLogMessage("ERROR setting TransProxy port to: " + transPort);
}
try
{
ArrayList<String> confLines = new ArrayList<String>();
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); 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 intent = new Intent(TorService.this, OrbotVpnService.class);
intent.setAction("start"); intent.setAction("start");
intent.putExtra("torSocks", mPortSOCKS);
if (!mIsLollipop) if (!mIsLollipop)
intent.putExtra("proxyPort",OrbotVpnService.mSocksProxyPort); 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())
{ {
extraLines.append("GeoIPFile" + ' ' + fileGeoIP.getCanonicalPath()).append('\n');
//only apply GeoIP if you need it extraLines.append("GeoIPv6File" + ' ' + fileGeoIP6.getCanonicalPath()).append('\n');
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');
} }
if (!TextUtils.isEmpty(entranceNodes)) if (!TextUtils.isEmpty(entranceNodes))
extraLines.append("EntryNodes" + ' ' + entranceNodes).append('\n'); extraLines.append("EntryNodes" + ' ' + entranceNodes).append('\n');

View File

@ -7,6 +7,9 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.net.ConnectException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import org.sufficientlysecure.rootcommands.Shell; 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;
}
}
} }

View File

@ -65,8 +65,11 @@ public class OrbotVpnService extends VpnService implements Handler.Callback {
private String mSessionName = "OrbotVPN"; private String mSessionName = "OrbotVPN";
private ParcelFileDescriptor mInterface; private ParcelFileDescriptor mInterface;
private int mTorSocks = TorServiceConstants.SOCKS_PROXY_PORT_DEFAULT;
public static int mSocksProxyPort = -1; public static int mSocksProxyPort = -1;
private ProxyServer mSocksProxyServer; private ProxyServer mSocksProxyServer;
private final static int VPN_MTU = 1500; 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!"); Log.d(TAG,"starting OrbotVPNService service!");
mTorSocks = intent.getIntExtra("torSocks", TorServiceConstants.SOCKS_PROXY_PORT_DEFAULT);
mSocksProxyPort = intent.getIntExtra("proxyPort", 0); mSocksProxyPort = intent.getIntExtra("proxyPort", 0);
// The handler is only used to show messages. // 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 defaultRoute = "0.0.0.0";
final String localSocks = localhost + ':' 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 String localDNS = virtualGateway + ':' + "8091";//String.valueOf(TorServiceConstants.TOR_DNS_PORT_DEFAULT);
final boolean localDnsTransparentProxy = true; final boolean localDnsTransparentProxy = true;