fix for reconnecting to exiting Tor process after service restart

This commit is contained in:
Nathan Freitas 2014-09-03 22:45:31 -04:00
parent a0b32ba068
commit 9c4c3496f1
3 changed files with 185 additions and 74 deletions

View File

@ -590,6 +590,14 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic
{ {
try { try {
startTor(); startTor();
Intent nResult = new Intent();
//nResult.putExtra("socks", ); //TODO respond with socks, transport, dns, etc
setResult(RESULT_OK,nResult);
} catch (RemoteException e) { } catch (RemoteException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
@ -631,6 +639,8 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic
pEdit.putBoolean(TorConstants.PREF_BRIDGES_ENABLED,true); pEdit.putBoolean(TorConstants.PREF_BRIDGES_ENABLED,true);
pEdit.commit(); pEdit.commit();
setResult(RESULT_OK);
} }
} }
} }
@ -821,6 +831,9 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic
torStatus = mService.getStatus(); torStatus = mService.getStatus();
if (torStatus == 0) //make sure we don't have a tor process already running
mService.checkAndInit();
handleIntents(); handleIntents();
} catch (RemoteException e) { } catch (RemoteException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block

View File

@ -11,6 +11,12 @@ interface ITorService {
**/ **/
int getStatus(); int getStatus();
/**
* check for exiting Tor process
**/
boolean checkAndInit ();
/** /**
* The profile value is the start/stop state for Tor * The profile value is the start/stop state for Tor
**/ **/

View File

@ -177,7 +177,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
try try
{ {
mLastProcessId = initControlConnection(3); mLastProcessId = initControlConnection(3,true);
if (mLastProcessId != -1 && conn != null) if (mLastProcessId != -1 && conn != null)
{ {
@ -570,7 +570,11 @@ public class TorService extends Service implements TorServiceConstants, TorConst
@Override @Override
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
initialize();
}
private void initialize()
{
try try
{ {
initBinariesAndDirectories(); initBinariesAndDirectories();
@ -656,11 +660,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
String TORRC_CONTROLPORT_FILE_KEY = "ControlPortWriteToFile"; String TORRC_CONTROLPORT_FILE_KEY = "ControlPortWriteToFile";
fileControlPort = new File(appBinHome,"control.txt"); fileControlPort = new File(appBinHome,"control.txt");
extraLines.append(TORRC_CONTROLPORT_FILE_KEY).append(' ').append(fileControlPort.getCanonicalPath()).append('\n'); extraLines.append(TORRC_CONTROLPORT_FILE_KEY).append(' ').append(fileControlPort.getCanonicalPath()).append('\n');
String transPort = prefs.getString("pref_transport", TorServiceConstants.TOR_TRANSPROXY_PORT_DEFAULT+"");
String dnsPort = prefs.getString("pref_dnsport", TorServiceConstants.TOR_DNS_PORT_DEFAULT+"");
if (mTransProxyTethering) if (mTransProxyTethering)
{ {
extraLines.append("TransListenAddress 0.0.0.0").append('\n'); extraLines.append("TransListenAddress 0.0.0.0").append('\n');
@ -676,8 +676,8 @@ public class TorService extends Service implements TorServiceConstants, TorConst
extraLines.append("TestSocks 0").append('\n'); extraLines.append("TestSocks 0").append('\n');
extraLines.append("WarnUnsafeSocks 1").append('\n'); extraLines.append("WarnUnsafeSocks 1").append('\n');
extraLines.append("TransPort ").append(transPort).append('\n'); extraLines.append("TransPort ").append("auto").append('\n');
extraLines.append("DNSPort ").append(dnsPort).append('\n'); extraLines.append("DNSPort ").append("auto").append('\n');
extraLines.append("VirtualAddrNetwork 10.192.0.0/10").append('\n'); extraLines.append("VirtualAddrNetwork 10.192.0.0/10").append('\n');
extraLines.append("AutomapHostsOnResolve 1").append('\n'); extraLines.append("AutomapHostsOnResolve 1").append('\n');
@ -741,7 +741,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
} }
public void initTor () throws Exception public void startTor () throws Exception
{ {
currentStatus = STATUS_CONNECTING; currentStatus = STATUS_CONNECTING;
@ -965,9 +965,8 @@ public class TorService extends Service implements TorServiceConstants, TorConst
return false; return false;
} }
//now try to connect //now try to connect
mLastProcessId = initControlConnection (100); mLastProcessId = initControlConnection (100,false);
if (mLastProcessId == -1) if (mLastProcessId == -1)
{ {
@ -1052,7 +1051,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
} }
private int initControlConnection (int maxTries) throws Exception, RuntimeException private int initControlConnection (int maxTries, boolean isReconnect) throws Exception, RuntimeException
{ {
int i = 0; int i = 0;
int controlPort = -1; int controlPort = -1;
@ -1076,46 +1075,83 @@ public class TorService extends Service implements TorServiceConstants, TorConst
conn = new TorControlConnection(torConnSocket); conn = new TorControlConnection(torConnSocket);
conn.launchThread(true);//is daemon conn.launchThread(true);//is daemon
logNotice( "SUCCESS connected to Tor control port."); break;
}
File fileCookie = new File(appCacheHome, TOR_CONTROL_COOKIE);
}
if (fileCookie.exists()) catch (Exception ce)
{ {
byte[] cookie = new byte[(int)fileCookie.length()]; conn = null;
DataInputStream fis = new DataInputStream(new FileInputStream(fileCookie)); logException( "Error connecting to Tor local control port: " + ce.getMessage(),ce);
fis.read(cookie);
fis.close(); }
conn.authenticate(cookie);
logNotice( "SUCCESS - authenticated to control port."); try {
logNotice("waiting...");
sendCallbackStatusMessage(getString(R.string.tor_process_starting) + ' ' + getString(R.string.tor_process_complete)); Thread.sleep(1000); }
catch (Exception e){}
addEventHandler();
}
String torProcId = conn.getInfo("process/pid");
if (conn != null)
//remove this for now until we can make a clean way to share logs from internal storage {
/** logNotice( "SUCCESS connected to Tor control port.");
if (ENABLE_DEBUG_LOG)
{ File fileCookie = new File(appCacheHome, TOR_CONTROL_COOKIE);
File fileLog2 = new File(getFilesDir(),"orbot-tor-log.txt");
fileLog2.setReadable(true); if (fileCookie.exists())
conn.setConf("Log", "debug file " + fileLog2.getCanonicalPath()); {
}*/ byte[] cookie = new byte[(int)fileCookie.length()];
DataInputStream fis = new DataInputStream(new FileInputStream(fileCookie));
currentStatus = STATUS_CONNECTING; fis.read(cookie);
fis.close();
String confSocks = conn.getInfo("net/listeners/socks"); conn.authenticate(cookie);
StringTokenizer st = new StringTokenizer(confSocks," ");
logNotice( "SUCCESS - authenticated to control port.");
sendCallbackStatusMessage(getString(R.string.tor_process_starting) + ' ' + getString(R.string.tor_process_complete));
addEventHandler();
String torProcId = conn.getInfo("process/pid");
//remove this for now until we can make a clean way to share logs from internal storage
/**
if (ENABLE_DEBUG_LOG)
{
File fileLog2 = new File(getFilesDir(),"orbot-tor-log.txt");
fileLog2.setReadable(true);
conn.setConf("Log", "debug file " + fileLog2.getCanonicalPath());
}*/
currentStatus = STATUS_CONNECTING;
String confSocks = conn.getInfo("net/listeners/socks");
StringTokenizer st = new StringTokenizer(confSocks," ");
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
{
confSocks = st.nextToken().split(":")[1];
confSocks = confSocks.substring(0,confSocks.length()-1);
mPortSOCKS = Integer.parseInt(confSocks);
SharedPreferences prefs = TorServiceUtils.getSharedPrefs(getApplicationContext()); SharedPreferences prefs = TorServiceUtils.getSharedPrefs(getApplicationContext());
String socksPortPref = prefs.getString(TorConstants.PREF_SOCKS, TorServiceConstants.PORT_SOCKS_DEFAULT); String socksPortPref = prefs.getString(TorConstants.PREF_SOCKS, TorServiceConstants.PORT_SOCKS_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 try
{ {
@ -1126,7 +1162,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
ArrayList<String> socksLines = new ArrayList<String>(); ArrayList<String> socksLines = new ArrayList<String>();
socksLines.add("SOCKSPort " + mPortSOCKS); socksLines.add("SOCKSPort " + mPortSOCKS);
socksLines.add("SOCKSPort " + socksPortPref); socksLines.add("SOCKSPort " + socksPortPref);
conn.setConf(socksLines); conn.setConf(socksLines);
mPortSOCKS = newSocksPort; mPortSOCKS = newSocksPort;
@ -1136,37 +1172,69 @@ public class TorService extends Service implements TorServiceConstants, TorConst
} }
catch (Exception e) catch (Exception e)
{ {
//sendCallbackLogMessage("ERROR adding SOCKS on port: " + socksPortPref); sendCallbackLogMessage("Error setting TransProxy port to: " + socksPortPref);
sendCallbackLogMessage("Local SOCKS port: " + socksPortPref);
}
try
{
int newPort = Integer.parseInt(transPort);
ServerSocket ss = new ServerSocket(newPort);
ss.close();
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);
} }
return Integer.parseInt(torProcId); try
{
int newPort = Integer.parseInt(dnsPort);
ServerSocket ss = new ServerSocket(newPort);
ss.close();
ArrayList<String> confLines = new ArrayList<String>();
confLines.add("DNSPort " + dnsPort);
conn.setConf(confLines);
sendCallbackLogMessage("Local DNSPort port: " + transPort);
}
catch (Exception e)
{
sendCallbackLogMessage("ERROR setting DNSport to: " + dnsPort);
}
} }
else
{ return Integer.parseInt(torProcId);
logNotice ("Tor authentication cookie does not exist yet");
conn = null;
}
}
} }
catch (Exception ce) else
{ {
conn = null; logNotice ("Tor authentication cookie does not exist yet");
logException( "Error connecting to Tor local control port: " + ce.getMessage(),ce); conn = null;
}
} }
try {
logNotice("waiting...");
Thread.sleep(1000); }
catch (Exception e){}
}
return -1; return -1;
} }
@ -1307,7 +1375,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
try try
{ {
initTor(); startTor();
} }
catch (Exception e) catch (Exception e)
@ -1614,6 +1682,26 @@ public class TorService extends Service implements TorServiceConstants, TorConst
return mBinder; return mBinder;
} }
public boolean checkAndInitImpl ()
{
if (fileTor != null)
{
try {
if (TorServiceUtils.findProcessId(fileTor.getCanonicalPath()) != -1)
{
initialize();
return true;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return false;
}
/** /**
* The IRemoteInterface is defined through IDL * The IRemoteInterface is defined through IDL
*/ */
@ -1624,6 +1712,10 @@ public class TorService extends Service implements TorServiceConstants, TorConst
} }
public boolean checkAndInit () {
return checkAndInitImpl();
}
public void setProfile (final int profileNew) public void setProfile (final int profileNew)
{ {