multiple fixes for tor start including "auto" control port

also try to kill hung tor process
This commit is contained in:
Nathan Freitas 2014-06-09 18:35:08 -04:00
parent 022436515d
commit 22938c9ed9
4 changed files with 97 additions and 62 deletions

View File

@ -1,6 +1,6 @@
Log notice stdout Log notice stdout
ControlListenAddress 127.0.0.1 ControlPortWriteToFile /data/data/org.torproject.android/app_bin/control.txt
ControlPort 9051 ControlPort auto
CookieAuthentication 1 CookieAuthentication 1
TransPort 9040 TransPort 9040
TransListenAddress 127.0.0.1 TransListenAddress 127.0.0.1

View File

@ -652,15 +652,11 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic
if (showWizard) if (showWizard)
{ {
Editor pEdit = mPrefs.edit(); Editor pEdit = mPrefs.edit();
pEdit.putBoolean("show_wizard",false); pEdit.putBoolean("show_wizard",false);
pEdit.commit(); pEdit.commit();
startWizard(); startWizard();
} }
} }

View File

@ -8,10 +8,12 @@
package org.torproject.android.service; package org.torproject.android.service;
import java.io.BufferedReader;
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
@ -491,7 +493,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
conn = null; conn = null;
} }
killProcess(fileTor);
killProcess(filePolipo); killProcess(filePolipo);
killProcess(fileObfsclient); killProcess(fileObfsclient);
@ -552,26 +554,26 @@ public class TorService extends Service implements TorServiceConstants, TorConst
logNotice("checking binary version: " + version); logNotice("checking binary version: " + version);
if (version == null || (!version.equals(BINARY_TOR_VERSION)))
{
stopTor(); stopTor();
TorResourceInstaller installer = new TorResourceInstaller(this, appBinHome);
boolean success = installer.installTorrc();
if (version == null || (!version.equals(BINARY_TOR_VERSION)) || (!fileTor.exists()))
{
logNotice("upgrading binaries to latest version: " + BINARY_TOR_VERSION); logNotice("upgrading binaries to latest version: " + BINARY_TOR_VERSION);
TorResourceInstaller installer = new TorResourceInstaller(this, appBinHome); success = installer.installResources();
boolean success = installer.installResources();
if (success) if (success)
prefs.edit().putString(PREF_BINARY_TOR_VERSION_INSTALLED,BINARY_TOR_VERSION).commit(); prefs.edit().putString(PREF_BINARY_TOR_VERSION_INSTALLED,BINARY_TOR_VERSION).commit();
} }
else if (!fileTorRc.exists()) else if (!fileTorRc.exists())
{ {
stopTor();
logNotice("upgrading binaries to latest version: " + BINARY_TOR_VERSION); logNotice("upgrading binaries to latest version: " + BINARY_TOR_VERSION);
TorResourceInstaller installer = new TorResourceInstaller(this, appBinHome); success = installer.installResources();
boolean success = installer.installResources();
if (success) if (success)
prefs.edit().putString(PREF_BINARY_TOR_VERSION_INSTALLED,BINARY_TOR_VERSION).commit(); prefs.edit().putString(PREF_BINARY_TOR_VERSION_INSTALLED,BINARY_TOR_VERSION).commit();
@ -763,7 +765,6 @@ public class TorService extends Service implements TorServiceConstants, TorConst
shell.add(cmdTor); shell.add(cmdTor);
Thread.sleep(torRetryWaitTimeMS); Thread.sleep(torRetryWaitTimeMS);
//now try to connect //now try to connect
@ -785,11 +786,13 @@ public class TorService extends Service implements TorServiceConstants, TorConst
logNotice("Tor started; process id=" + mLastProcessId); logNotice("Tor started; process id=" + mLastProcessId);
processSettingsImpl(); processSettingsImpl();
startTorMinder ();
} }
shell.close(); shell.close();
startTorMinder ();
} }
private void runPolipoShellCmd () throws Exception private void runPolipoShellCmd () throws Exception
@ -869,14 +872,17 @@ public class TorService extends Service implements TorServiceConstants, TorConst
} }
else else
{ {
while (conn == null && i++ < maxAttempts) while (conn == null && i++ < maxAttempts)
{ {
try try
{ {
logNotice( "Connecting to control port: " + TOR_CONTROL_PORT);
torConnSocket = new Socket(IP_LOCALHOST, TOR_CONTROL_PORT); int controlPort = getControlPort();
logNotice( "Connecting to control port: " + controlPort);
if (controlPort != -1)
{
torConnSocket = new Socket(IP_LOCALHOST, controlPort);
torConnSocket.setSoTimeout(CONTROL_SOCKET_TIMEOUT); torConnSocket.setSoTimeout(CONTROL_SOCKET_TIMEOUT);
conn = TorControlConnection.getConnection(torConnSocket); conn = TorControlConnection.getConnection(torConnSocket);
@ -912,20 +918,23 @@ public class TorService extends Service implements TorServiceConstants, TorConst
} }
else else
{ {
logNotice ("Tor authentication cookie does not exist yet; trying again..."); logNotice ("Tor authentication cookie does not exist yet");
conn = null; conn = null;
} }
} }
}
catch (Exception ce) catch (Exception ce)
{ {
conn = null; conn = null;
logNotice( "Error connecting to Tor local control port"); logException( "Error connecting to Tor local control port",ce);
//Log.d(TAG,"Attempt: Error connecting to control port: " + ce.getLocalizedMessage(),ce);
} }
try {
logNotice("waiting...");
Thread.sleep(5000); }
catch (Exception e){}
} }
} }
@ -934,6 +943,35 @@ public class TorService extends Service implements TorServiceConstants, TorConst
} }
private int getControlPort ()
{
try
{
File fileControl = new File(appBinHome,"control.txt");
logNotice("Reading control port config file: " + fileControl.getAbsolutePath());
BufferedReader bufferedReader = new BufferedReader(new FileReader(fileControl));
String line = bufferedReader.readLine();
//PORT=127.0.0.1:45051
if (line != null)
{
String[] lineParts = line.split(":");
return Integer.parseInt(lineParts[1]);
}
}
catch (Exception e)
{
logException("unable to get control port",e);
}
return -1;
}
private void checkAddressAndCountry () throws IOException private void checkAddressAndCountry () throws IOException
{ {
@ -1046,6 +1084,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
catch (Exception e) catch (Exception e)
{ {
stopTor();
logException("Unable to start Tor: " + e.toString(),e); logException("Unable to start Tor: " + e.toString(),e);
currentStatus = STATUS_OFF; currentStatus = STATUS_OFF;
showToolbarNotification(getString(R.string.unable_to_start_tor) + ": " + e.getMessage(), ERROR_NOTIFY_ID, R.drawable.ic_stat_notifyerr, false); showToolbarNotification(getString(R.string.unable_to_start_tor) + ": " + e.getMessage(), ERROR_NOTIFY_ID, R.drawable.ic_stat_notifyerr, false);
@ -2103,6 +2142,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
} catch (Exception e1) { } catch (Exception e1) {
logException("Error in Tor heartbeat checker",e1); logException("Error in Tor heartbeat checker",e1);
} }
} }

View File

@ -53,7 +53,6 @@ public interface TorServiceConstants {
//what is says! //what is says!
public final static String IP_LOCALHOST = "127.0.0.1"; public final static String IP_LOCALHOST = "127.0.0.1";
public final static int TOR_CONTROL_PORT = 9051;
public final static int UPDATE_TIMEOUT = 1000; public final static int UPDATE_TIMEOUT = 1000;
public final static int TOR_TRANSPROXY_PORT = 9040; public final static int TOR_TRANSPROXY_PORT = 9040;
public final static int STANDARD_DNS_PORT = 53; public final static int STANDARD_DNS_PORT = 53;
@ -77,7 +76,7 @@ public interface TorServiceConstants {
public static final int DISABLE_TOR_MSG = 3; public static final int DISABLE_TOR_MSG = 3;
public static final int LOG_MSG = 4; public static final int LOG_MSG = 4;
public static final String BINARY_TOR_VERSION = "0.2.4.22-openssl1.0.1h"; public static final String BINARY_TOR_VERSION = "0.2.4.22-openssl1.0.1h.2";
public static final String PREF_BINARY_TOR_VERSION_INSTALLED = "BINARY_TOR_VERSION_INSTALLED"; public static final String PREF_BINARY_TOR_VERSION_INSTALLED = "BINARY_TOR_VERSION_INSTALLED";
//obfsproxy //obfsproxy