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
ControlListenAddress 127.0.0.1
ControlPort 9051
ControlPortWriteToFile /data/data/org.torproject.android/app_bin/control.txt
ControlPort auto
CookieAuthentication 1
TransPort 9040
TransListenAddress 127.0.0.1

View File

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

View File

@ -8,10 +8,12 @@
package org.torproject.android.service;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
@ -491,7 +493,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
conn = null;
}
killProcess(fileTor);
killProcess(filePolipo);
killProcess(fileObfsclient);
@ -552,26 +554,26 @@ public class TorService extends Service implements TorServiceConstants, TorConst
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);
TorResourceInstaller installer = new TorResourceInstaller(this, appBinHome);
boolean success = installer.installResources();
success = installer.installResources();
if (success)
prefs.edit().putString(PREF_BINARY_TOR_VERSION_INSTALLED,BINARY_TOR_VERSION).commit();
}
else if (!fileTorRc.exists())
{
stopTor();
logNotice("upgrading binaries to latest version: " + BINARY_TOR_VERSION);
TorResourceInstaller installer = new TorResourceInstaller(this, appBinHome);
boolean success = installer.installResources();
success = installer.installResources();
if (success)
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);
Thread.sleep(torRetryWaitTimeMS);
//now try to connect
@ -785,11 +786,13 @@ public class TorService extends Service implements TorServiceConstants, TorConst
logNotice("Tor started; process id=" + mLastProcessId);
processSettingsImpl();
startTorMinder ();
}
shell.close();
startTorMinder ();
}
private void runPolipoShellCmd () throws Exception
@ -869,63 +872,69 @@ public class TorService extends Service implements TorServiceConstants, TorConst
}
else
{
while (conn == null && i++ < maxAttempts)
{
try
{
logNotice( "Connecting to control port: " + TOR_CONTROL_PORT);
torConnSocket = new Socket(IP_LOCALHOST, TOR_CONTROL_PORT);
torConnSocket.setSoTimeout(CONTROL_SOCKET_TIMEOUT);
int controlPort = getControlPort();
logNotice( "Connecting to control port: " + controlPort);
conn = TorControlConnection.getConnection(torConnSocket);
if (controlPort != -1)
{
torConnSocket = new Socket(IP_LOCALHOST, controlPort);
torConnSocket.setSoTimeout(CONTROL_SOCKET_TIMEOUT);
if (ENABLE_DEBUG_LOG)
{
conn.setDebugging(System.out);
conn = TorControlConnection.getConnection(torConnSocket);
}
if (ENABLE_DEBUG_LOG)
{
conn.setDebugging(System.out);
logNotice( "SUCCESS connected to Tor control port");
}
File fileCookie = new File(appCacheHome, TOR_CONTROL_COOKIE);
logNotice( "SUCCESS connected to Tor control port");
if (fileCookie.exists())
{
byte[] cookie = new byte[(int)fileCookie.length()];
DataInputStream fis = new DataInputStream(new FileInputStream(fileCookie));
fis.read(cookie);
fis.close();
conn.authenticate(cookie);
File fileCookie = new File(appCacheHome, TOR_CONTROL_COOKIE);
logNotice( "SUCCESS - authenticated to control port");
if (fileCookie.exists())
{
byte[] cookie = new byte[(int)fileCookie.length()];
DataInputStream fis = new DataInputStream(new FileInputStream(fileCookie));
fis.read(cookie);
fis.close();
conn.authenticate(cookie);
sendCallbackStatusMessage(getString(R.string.tor_process_starting) + ' ' + getString(R.string.tor_process_complete));
logNotice( "SUCCESS - authenticated to control port");
addEventHandler();
sendCallbackStatusMessage(getString(R.string.tor_process_starting) + ' ' + getString(R.string.tor_process_complete));
String torProcId = conn.getInfo("process/pid");
addEventHandler();
return Integer.parseInt(torProcId);
String torProcId = conn.getInfo("process/pid");
}
else
{
logNotice ("Tor authentication cookie does not exist yet; trying again...");
conn = null;
return Integer.parseInt(torProcId);
}
else
{
logNotice ("Tor authentication cookie does not exist yet");
conn = null;
}
}
}
}
catch (Exception ce)
{
conn = null;
logNotice( "Error connecting to Tor local control port");
//Log.d(TAG,"Attempt: Error connecting to control port: " + ce.getLocalizedMessage(),ce);
logException( "Error connecting to Tor local control port",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
{
@ -1046,6 +1084,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
catch (Exception e)
{
stopTor();
logException("Unable to start Tor: " + e.toString(),e);
currentStatus = STATUS_OFF;
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) {
logException("Error in Tor heartbeat checker",e1);
}
}

View File

@ -53,7 +53,6 @@ public interface TorServiceConstants {
//what is says!
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 TOR_TRANSPROXY_PORT = 9040;
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 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";
//obfsproxy