run Tor from link to keep compatibility with OrbotHelper class

some apps use our OrbotHelper.java class and are expecting the
Tor binary process to be at /data/data/org.torproject.android/app_bin/tor
so we link that path to the lib/libtor.so path
This commit is contained in:
Nathan Freitas 2013-12-27 21:43:55 -05:00
parent 17f7d46e5c
commit 3069fb5af4
2 changed files with 97 additions and 53 deletions

View File

@ -80,7 +80,9 @@ public class TorService extends Service implements TorServiceConstants, TorConst
private File appCacheHome; private File appCacheHome;
private File appLibsHome; private File appLibsHome;
private File fileTor; private File fileTorOrig;
private File fileTorLink;
private File filePrivoxy; private File filePrivoxy;
private File fileObfsProxy; private File fileObfsProxy;
private File fileTorRc; private File fileTorRc;
@ -117,41 +119,40 @@ public class TorService extends Service implements TorServiceConstants, TorConst
} }
private boolean findExistingProc () private boolean findExistingProc ()
{ {
int procId = TorServiceUtils.findProcessId(fileTor.getAbsolutePath()); try
{
if (procId != -1) if (fileTorLink == null)
{ initTorPathLink();
logNotice("Found existing Tor process");
int procId = TorServiceUtils.findProcessId(fileTorLink.getAbsolutePath());
sendCallbackLogMessage (getString(R.string.found_existing_tor_process));
if (procId != -1)
try { {
currentStatus = STATUS_CONNECTING; logNotice("Found existing Tor process");
initControlConnection(); sendCallbackLogMessage (getString(R.string.found_existing_tor_process));
processSettingsImpl(); currentStatus = STATUS_CONNECTING;
currentStatus = STATUS_ON; initControlConnection();
return true; processSettingsImpl();
} catch (RuntimeException e) { currentStatus = STATUS_ON;
Log.d(TAG,"Unable to connect to existing Tor instance,",e);
currentStatus = STATUS_OFF; return true;
} catch (Exception e) { }
Log.d(TAG,"Unable to connect to existing Tor instance,",e);
currentStatus = STATUS_OFF; return false;
}
catch (Exception e)
} {
} Log.e(TAG,"error finding proc",e);
return false;
return false; }
} }
@ -229,10 +230,17 @@ public class TorService extends Service implements TorServiceConstants, TorConst
public void onRebind(Intent intent) { public void onRebind(Intent intent) {
super.onRebind(intent); super.onRebind(intent);
initTorPaths(); try
{
sendCallbackLogMessage("Welcome back, Carter!"); initTorPaths();
sendCallbackLogMessage("Welcome back, Carter!");
}
catch (Exception e)
{
Log.e(TAG,"unable to init Tor",e);
throw new RuntimeException("Unable to init Tor");
}
} }
@ -243,8 +251,16 @@ public class TorService extends Service implements TorServiceConstants, TorConst
_torInstance = this; _torInstance = this;
initTorPaths(); try
{
initTorPaths();
}
catch (Exception e)
{
Log.e(TAG,"error setting up Tor",e);
throw new RuntimeException("Unable to start Tor");
}
IntentFilter mNetworkStateFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); IntentFilter mNetworkStateFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(mNetworkStateReceiver , mNetworkStateFilter); registerReceiver(mNetworkStateReceiver , mNetworkStateFilter);
@ -419,6 +435,20 @@ public class TorService extends Service implements TorServiceConstants, TorConst
return null; return null;
} }
private void initTorPathLink () throws Exception
{
fileTorLink = new File(appBinHome,"tor");
StringBuilder log = new StringBuilder();
String[] cmdDel = { "rm " + fileTorLink.getAbsolutePath() };
TorServiceUtils.doShellCommand(cmdDel,log, false, false);
String[] cmd = { SHELL_CMD_LINK + ' ' + fileTorOrig.getAbsolutePath() + ' ' + fileTorLink.getAbsolutePath() };
TorServiceUtils.doShellCommand(cmd,log, false, false);
logNotice("link command output: " + log.toString());
}
private void killTorProcess () throws Exception private void killTorProcess () throws Exception
{ {
@ -444,7 +474,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
int killDelayMs = 300; int killDelayMs = 300;
while ((procId = TorServiceUtils.findProcessId(fileTor.getAbsolutePath())) != -1) while ((procId = TorServiceUtils.findProcessId(fileTorLink.getAbsolutePath())) != -1)
{ {
logNotice("Found Tor PID=" + procId + " - killing now..."); logNotice("Found Tor PID=" + procId + " - killing now...");
@ -490,16 +520,18 @@ public class TorService extends Service implements TorServiceConstants, TorConst
} }
} }
private void initTorPaths () private void initTorPaths () throws IOException
{ {
appBinHome = getDir("bin",Application.MODE_PRIVATE); appBinHome = getDir("bin",Application.MODE_PRIVATE);
appCacheHome = getDir("data",Application.MODE_PRIVATE); appCacheHome = getDir("data",Application.MODE_PRIVATE);
appLibsHome = new File(getApplicationInfo().nativeLibraryDir); appLibsHome = new File(getApplicationInfo().nativeLibraryDir);
fileTor = new File(appLibsHome, TOR_BINARY_ASSET_KEY); fileTorOrig = new File(appLibsHome, TOR_BINARY_ASSET_KEY);
if (fileTor.exists()) if (fileTorOrig.exists())
logNotice ("Tor binary exists: " + fileTor.getAbsolutePath()); {
logNotice ("Tor binary exists: " + fileTorOrig.getAbsolutePath());
}
else else
throw new RuntimeException("Tor binary not installed"); throw new RuntimeException("Tor binary not installed");
@ -587,6 +619,8 @@ public class TorService extends Service implements TorServiceConstants, TorConst
sendCallbackStatusMessage(getString(R.string.status_starting_up)); sendCallbackStatusMessage(getString(R.string.status_starting_up));
initTorPathLink ();
killTorProcess (); killTorProcess ();
try { try {
@ -688,6 +722,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
private void runTorShellCmd() throws Exception private void runTorShellCmd() throws Exception
{ {
SharedPreferences prefs =getSharedPrefs(getApplicationContext()); SharedPreferences prefs =getSharedPrefs(getApplicationContext());
StringBuilder log = new StringBuilder(); StringBuilder log = new StringBuilder();
@ -703,7 +738,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
String[] torCmd = { String[] torCmd = {
"export HOME=" + appBinHome.getAbsolutePath(), "export HOME=" + appBinHome.getAbsolutePath(),
fileTor.getAbsolutePath() + " DataDirectory " + appCacheHome.getAbsolutePath() + " -f " + torrcPath + " || exit\n" fileTorLink.getAbsolutePath() + " DataDirectory " + appCacheHome.getAbsolutePath() + " -f " + torrcPath + " || exit\n"
}; };
boolean runAsRootFalse = false; boolean runAsRootFalse = false;
@ -724,12 +759,12 @@ public class TorService extends Service implements TorServiceConstants, TorConst
Thread.sleep(torRetryWaitTimeMS); Thread.sleep(torRetryWaitTimeMS);
procId = TorServiceUtils.findProcessId(fileTor.getAbsolutePath()); procId = TorServiceUtils.findProcessId(fileTorLink.getAbsolutePath());
if (procId == -1) if (procId == -1)
{ {
Thread.sleep(torRetryWaitTimeMS); Thread.sleep(torRetryWaitTimeMS);
procId = TorServiceUtils.findProcessId(fileTor.getAbsolutePath()); procId = TorServiceUtils.findProcessId(fileTorLink.getAbsolutePath());
attempts++; attempts++;
} }
else else
@ -1193,9 +1228,16 @@ public class TorService extends Service implements TorServiceConstants, TorConst
_torInstance = this; _torInstance = this;
initTorPaths(); try
findExistingProc (); {
initTorPaths();
findExistingProc ();
}
catch (Exception e)
{
Log.e(TAG,"error onBind",e);
}
if (ITorService.class.getName().equals(intent.getAction())) { if (ITorService.class.getName().equals(intent.getAction())) {
return mBinder; return mBinder;
} }
@ -1243,7 +1285,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
processSettingsImpl (); processSettingsImpl ();
} catch (RemoteException e) { } catch (Exception e) {
logException ("error applying mPrefs",e); logException ("error applying mPrefs",e);
} }
@ -1528,7 +1570,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
} }
}; };
private boolean processSettingsImpl () throws RemoteException private boolean processSettingsImpl () throws RemoteException, IOException
{ {
SharedPreferences prefs = getSharedPrefs(getApplicationContext()); SharedPreferences prefs = getSharedPrefs(getApplicationContext());

View File

@ -38,6 +38,8 @@ public interface TorServiceConstants {
public final static String SHELL_CMD_RM = "rm"; public final static String SHELL_CMD_RM = "rm";
public final static String SHELL_CMD_PS = "ps"; public final static String SHELL_CMD_PS = "ps";
public final static String SHELL_CMD_PIDOF = "pidof"; public final static String SHELL_CMD_PIDOF = "pidof";
public final static String SHELL_CMD_LINK = "ln -s";
public final static String CHMOD_EXE_VALUE = "700"; public final static String CHMOD_EXE_VALUE = "700";