fixed issue with force close crash on install

svn:r22685
This commit is contained in:
Nathan Freitas 2010-07-24 03:24:30 +00:00
parent a9083a3999
commit 2832e744b2
3 changed files with 139 additions and 54 deletions

View File

@ -17,8 +17,13 @@ import android.util.Log;
public class TorBinaryInstaller implements TorServiceConstants { public class TorBinaryInstaller implements TorServiceConstants {
public TorBinaryInstaller () String installPath = null;
String apkPath = null;
public TorBinaryInstaller (String installPath, String apkPath)
{ {
this.installPath = installPath;
this.apkPath = apkPath;
} }
/* /*
@ -26,10 +31,11 @@ public class TorBinaryInstaller implements TorServiceConstants {
*/ */
public void start (boolean force) public void start (boolean force)
{ {
boolean torBinaryExists = new File(TOR_BINARY_INSTALL_PATH).exists();
boolean torBinaryExists = new File(installPath + TOR_BINARY_ASSET_KEY).exists();
Log.i(TAG,"Tor binary exists=" + torBinaryExists); Log.i(TAG,"Tor binary exists=" + torBinaryExists);
boolean privoxyBinaryExists = new File(PRIVOXY_INSTALL_PATH).exists(); boolean privoxyBinaryExists = new File(installPath + PRIVOXY_ASSET_KEY).exists();
Log.i(TAG,"Privoxy binary exists=" + privoxyBinaryExists); Log.i(TAG,"Privoxy binary exists=" + privoxyBinaryExists);
if (!(torBinaryExists && privoxyBinaryExists) || force) if (!(torBinaryExists && privoxyBinaryExists) || force)
@ -45,7 +51,7 @@ public class TorBinaryInstaller implements TorServiceConstants {
try try
{ {
/*
String apkPath = APK_PATH; String apkPath = APK_PATH;
int apkIdx = 1; int apkIdx = 1;
@ -56,21 +62,23 @@ public class TorBinaryInstaller implements TorServiceConstants {
Log.i(TAG,"Could not find APK. Trying new path: " + apkPath); Log.i(TAG,"Could not find APK. Trying new path: " + apkPath);
} }
*/
ZipFile zip = new ZipFile(apkPath); ZipFile zip = new ZipFile(apkPath);
ZipEntry zipen = zip.getEntry(TOR_BINARY_ZIP_KEY); ZipEntry zipen = zip.getEntry(TOR_BINARY_ZIP_KEY);
streamToFile(zip.getInputStream(zipen),TOR_BINARY_INSTALL_PATH); streamToFile(zip.getInputStream(zipen),installPath + TOR_BINARY_ASSET_KEY);
zipen = zip.getEntry(TORRC_ZIP_KEY); zipen = zip.getEntry(TORRC_ZIP_KEY);
streamToFile(zip.getInputStream(zipen),TORRC_INSTALL_PATH); streamToFile(zip.getInputStream(zipen),installPath + TORRC_ASSET_KEY);
zipen = zip.getEntry(PRIVOXY_ZIP_KEY); zipen = zip.getEntry(PRIVOXY_ZIP_KEY);
streamToFile(zip.getInputStream(zipen),PRIVOXY_INSTALL_PATH); streamToFile(zip.getInputStream(zipen),installPath + PRIVOXY_ASSET_KEY);
zipen = zip.getEntry(PRIVOXYCONFIG_ZIP_KEY); zipen = zip.getEntry(PRIVOXYCONFIG_ZIP_KEY);
streamToFile(zip.getInputStream(zipen),PRIVOXYCONFIG_INSTALL_PATH); streamToFile(zip.getInputStream(zipen),installPath + PRIVOXYCONFIG_ASSET_KEY);
zip.close(); zip.close();

View File

@ -51,25 +51,25 @@ public class TorService extends Service implements TorServiceConstants, Runnable
private boolean hasRoot = false; private boolean hasRoot = false;
private String appHome = null;
private String torBinaryPath = null;
private String privoxyPath = null;
/** Called when the activity is first created. */ /** Called when the activity is first created. */
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
Log.i(TAG,"TorService: onCreate"); Log.i(TAG,"TorService: onCreate");
checkTorBinaries();
findExistingProc ();
_torInstance = this;
hasRoot = TorServiceUtils.hasRoot();
} }
private boolean findExistingProc () private boolean findExistingProc ()
{ {
int procId = TorServiceUtils.findProcessId(TorServiceConstants.TOR_BINARY_INSTALL_PATH); int procId = TorServiceUtils.findProcessId(torBinaryPath);
if (procId != -1) if (procId != -1)
{ {
@ -274,7 +274,7 @@ public class TorService extends Service implements TorServiceConstants, Runnable
StringBuilder log = new StringBuilder(); StringBuilder log = new StringBuilder();
int procId = TorServiceUtils.findProcessId(TorServiceConstants.TOR_BINARY_INSTALL_PATH); int procId = TorServiceUtils.findProcessId(torBinaryPath);
while (procId != -1) while (procId != -1)
{ {
@ -284,10 +284,10 @@ public class TorService extends Service implements TorServiceConstants, Runnable
String[] cmd = { SHELL_CMD_KILL + ' ' + procId + "" }; String[] cmd = { SHELL_CMD_KILL + ' ' + procId + "" };
TorServiceUtils.doShellCommand(cmd,log, false, false); TorServiceUtils.doShellCommand(cmd,log, false, false);
procId = TorServiceUtils.findProcessId(TorServiceConstants.TOR_BINARY_INSTALL_PATH); procId = TorServiceUtils.findProcessId(torBinaryPath);
} }
procId = TorServiceUtils.findProcessId(TorServiceConstants.PRIVOXY_INSTALL_PATH); procId = TorServiceUtils.findProcessId(privoxyPath);
while (procId != -1) while (procId != -1)
{ {
@ -297,7 +297,7 @@ public class TorService extends Service implements TorServiceConstants, Runnable
TorServiceUtils.doShellCommand(cmd,log, false, false); TorServiceUtils.doShellCommand(cmd,log, false, false);
procId = TorServiceUtils.findProcessId(TorServiceConstants.PRIVOXY_INSTALL_PATH); procId = TorServiceUtils.findProcessId(privoxyPath);
} }
} }
@ -311,21 +311,76 @@ public class TorService extends Service implements TorServiceConstants, Runnable
} }
private boolean checkTorBinaries () private String findAPK ()
{ {
boolean torBinaryExists = new File(TOR_BINARY_INSTALL_PATH).exists(); String apkBase = "/data/app/";
boolean privoxyBinaryExists = new File(PRIVOXY_INSTALL_PATH).exists();
String APK_EXT = ".apk";
String buildPath = apkBase + TOR_APP_USERNAME + APK_EXT;
Log.i(TAG, "Checking APK location: " + buildPath);
File fileApk = new File(buildPath);
if (fileApk.exists())
return fileApk.getAbsolutePath();
for (int i = 0; i < 10; i++)
{
buildPath = apkBase + TOR_APP_USERNAME + '-' + i + APK_EXT;
fileApk = new File(buildPath);
Log.i(TAG, "Checking APK location: " + buildPath);
if (fileApk.exists())
return fileApk.getAbsolutePath();
}
return null;
}
private boolean checkTorBinaries ()
{
//android.os.Debug.waitForDebugger();
Log.i(TAG,"checking Tor binaries");
//appHome = getApplicationContext().getFilesDir().getAbsolutePath();
appHome = "/data/data/" + TOR_APP_USERNAME + "/";
Log.i(TAG,"appHome=" + appHome);
String apkPath = findAPK();
Log.i(TAG,"found apk at: " + apkPath);
boolean apkExists = new File(apkPath).exists();
if (!apkExists)
{
Log.w(TAG,"APK file not found at: " + apkPath);
Log.w(TAG,"Binary installation aborted");
return false;
}
torBinaryPath = appHome + '/' + TOR_BINARY_ASSET_KEY;
privoxyPath = appHome + '/' + PRIVOXY_ASSET_KEY;
boolean torBinaryExists = new File(torBinaryPath).exists();
boolean privoxyBinaryExists = new File(privoxyPath).exists();
if (!(torBinaryExists && privoxyBinaryExists)) if (!(torBinaryExists && privoxyBinaryExists))
{ {
killTorProcess (); killTorProcess ();
TorBinaryInstaller installer = new TorBinaryInstaller(); TorBinaryInstaller installer = new TorBinaryInstaller(appHome, apkPath);
installer.start(true); installer.start(true);
torBinaryExists = new File(TOR_BINARY_INSTALL_PATH).exists(); torBinaryExists = new File(torBinaryPath).exists();
privoxyBinaryExists = new File(PRIVOXY_INSTALL_PATH).exists(); privoxyBinaryExists = new File(privoxyPath).exists();
if (torBinaryExists && privoxyBinaryExists) if (torBinaryExists && privoxyBinaryExists)
{ {
@ -339,7 +394,9 @@ public class TorService extends Service implements TorServiceConstants, Runnable
logNotice(getString(R.string.status_install_fail)); logNotice(getString(R.string.status_install_fail));
showAlert(getString(R.string.title_error),getString(R.string.status_install_fail)); sendCallbackMessage(getString(R.string.status_install_fail));
//showAlert(getString(R.string.title_error),getString(R.string.status_install_fail));
return false; return false;
} }
@ -349,11 +406,11 @@ public class TorService extends Service implements TorServiceConstants, Runnable
StringBuilder log = new StringBuilder (); StringBuilder log = new StringBuilder ();
logNotice("Setting permission on Tor binary"); logNotice("Setting permission on Tor binary");
String[] cmd1 = {SHELL_CMD_CHMOD + ' ' + CHMOD_EXE_VALUE + ' ' + TOR_BINARY_INSTALL_PATH}; String[] cmd1 = {SHELL_CMD_CHMOD + ' ' + CHMOD_EXE_VALUE + ' ' + torBinaryPath};
TorServiceUtils.doShellCommand(cmd1, log, false, true); TorServiceUtils.doShellCommand(cmd1, log, false, true);
logNotice("Setting permission on Privoxy binary"); logNotice("Setting permission on Privoxy binary");
String[] cmd2 = {SHELL_CMD_CHMOD + ' ' + CHMOD_EXE_VALUE + ' ' + PRIVOXY_INSTALL_PATH}; String[] cmd2 = {SHELL_CMD_CHMOD + ' ' + CHMOD_EXE_VALUE + ' ' + privoxyPath};
TorServiceUtils.doShellCommand(cmd2, log, false, true); TorServiceUtils.doShellCommand(cmd2, log, false, true);
return true; return true;
@ -363,6 +420,9 @@ public class TorService extends Service implements TorServiceConstants, Runnable
{ {
// android.os.Debug.waitForDebugger(); // android.os.Debug.waitForDebugger();
currentStatus = STATUS_CONNECTING; currentStatus = STATUS_CONNECTING;
logNotice(getString(R.string.status_starting_up)); logNotice(getString(R.string.status_starting_up));
@ -417,11 +477,13 @@ public class TorService extends Service implements TorServiceConstants, Runnable
Log.i(TAG,"Starting tor process"); Log.i(TAG,"Starting tor process");
String[] torCmd = {TOR_BINARY_INSTALL_PATH + ' ' + TOR_COMMAND_LINE_ARGS}; String torrcPath = appHome + TORRC_ASSET_KEY;
String[] torCmd = {torBinaryPath + " -f " + torrcPath + " || exit\n"};
TorServiceUtils.doShellCommand(torCmd, log, false, false); TorServiceUtils.doShellCommand(torCmd, log, false, false);
Thread.sleep(1000); Thread.sleep(1000);
int procId = TorServiceUtils.findProcessId(TorServiceConstants.TOR_BINARY_INSTALL_PATH); int procId = TorServiceUtils.findProcessId(torBinaryPath);
int attempts = 0; int attempts = 0;
@ -432,7 +494,7 @@ public class TorService extends Service implements TorServiceConstants, Runnable
logNotice(torCmd[0]); logNotice(torCmd[0]);
TorServiceUtils.doShellCommand(torCmd, log, false, false); TorServiceUtils.doShellCommand(torCmd, log, false, false);
procId = TorServiceUtils.findProcessId(TorServiceConstants.TOR_BINARY_INSTALL_PATH); procId = TorServiceUtils.findProcessId(torBinaryPath);
if (procId == -1) if (procId == -1)
{ {
@ -463,7 +525,7 @@ public class TorService extends Service implements TorServiceConstants, Runnable
private void runPrivoxyShellCmd () throws Exception private void runPrivoxyShellCmd () throws Exception
{ {
int privoxyProcId = TorServiceUtils.findProcessId(TorServiceConstants.PRIVOXY_INSTALL_PATH); int privoxyProcId = TorServiceUtils.findProcessId(privoxyPath);
StringBuilder log = null; StringBuilder log = null;
@ -473,8 +535,10 @@ public class TorService extends Service implements TorServiceConstants, Runnable
{ {
log = new StringBuilder(); log = new StringBuilder();
String privoxyConfigPath = appHome + PRIVOXYCONFIG_ASSET_KEY;
String[] cmds = String[] cmds =
{ PRIVOXY_INSTALL_PATH + " " + PRIVOXY_COMMAND_LINE_ARGS }; { privoxyPath + " " + privoxyConfigPath };
logNotice (cmds[0]); logNotice (cmds[0]);
@ -483,7 +547,7 @@ public class TorService extends Service implements TorServiceConstants, Runnable
//wait one second to make sure it has started up //wait one second to make sure it has started up
Thread.sleep(1000); Thread.sleep(1000);
privoxyProcId = TorServiceUtils.findProcessId(TorServiceConstants.PRIVOXY_INSTALL_PATH); privoxyProcId = TorServiceUtils.findProcessId(privoxyPath);
if (privoxyProcId == -1) if (privoxyProcId == -1)
{ {
@ -534,9 +598,11 @@ public class TorService extends Service implements TorServiceConstants, Runnable
Log.i(TAG,"SUCCESS connected to control port"); Log.i(TAG,"SUCCESS connected to control port");
File fileCookie = new File(TOR_CONTROL_AUTH_COOKIE); String torAuthCookie = appHome + "data/control_auth_cookie";
File fileCookie = new File(torAuthCookie);
byte[] cookie = new byte[(int)fileCookie.length()]; byte[] cookie = new byte[(int)fileCookie.length()];
new FileInputStream(new File(TOR_CONTROL_AUTH_COOKIE)).read(cookie); new FileInputStream(new File(torAuthCookie)).read(cookie);
conn.authenticate(cookie); conn.authenticate(cookie);
Log.i(TAG,"SUCCESS authenticated to control port"); Log.i(TAG,"SUCCESS authenticated to control port");
@ -772,16 +838,27 @@ public class TorService extends Service implements TorServiceConstants, Runnable
} }
private Intent launchContext = null;
public IBinder onBind(Intent intent) { public IBinder onBind(Intent intent) {
// Select the interface to return. If your service only implements // Select the interface to return. If your service only implements
// a single interface, you can just return it here without checking // a single interface, you can just return it here without checking
// the Intent. // the Intent.
if (appHome == null)
{
checkTorBinaries();
findExistingProc ();
_torInstance = this;
hasRoot = TorServiceUtils.hasRoot();
}
if (ITorService.class.getName().equals(intent.getAction())) { if (ITorService.class.getName().equals(intent.getAction())) {
return mBinder; return mBinder;
} }
return null; return null;
} }

View File

@ -9,40 +9,40 @@ public interface TorServiceConstants {
public final static String TOR_APP_USERNAME = "org.torproject.android"; public final static String TOR_APP_USERNAME = "org.torproject.android";
//home directory of Android application //home directory of Android application
public final static String TOR_HOME = "/data/data/" + TOR_APP_USERNAME + "/"; // public final static String TOR_HOME = "/data/data/" + TOR_APP_USERNAME + "/";
public final static String TOR_HOME_DATA_DIR = TOR_HOME + "data/"; //public final static String TOR_HOME_DATA_DIR = TOR_HOME + "data/";
//name of the tor C binary //name of the tor C binary
public final static String TOR_BINARY_ASSET_KEY = "tor"; public final static String TOR_BINARY_ASSET_KEY = "tor";
public final static String TOR_BINARY_INSTALL_PATH = TOR_HOME + TOR_BINARY_ASSET_KEY; //path to install the Tor binary too // public final static String TOR_BINARY_INSTALL_PATH = TOR_HOME + TOR_BINARY_ASSET_KEY; //path to install the Tor binary too
public final static String TOR_BINARY_ZIP_KEY = "assets/" + TOR_BINARY_ASSET_KEY;//key of the tor binary in the Zip file public final static String TOR_BINARY_ZIP_KEY = "assets/" + TOR_BINARY_ASSET_KEY;//key of the tor binary in the Zip file
//torrc (tor config file) //torrc (tor config file)
public final static String TORRC_ASSET_KEY = "torrc"; public final static String TORRC_ASSET_KEY = "torrc";
public final static String TORRC_INSTALL_PATH = TOR_HOME + TORRC_ASSET_KEY; //path to install torrc to within the android app data folder // public final static String TORRC_INSTALL_PATH = TOR_HOME + TORRC_ASSET_KEY; //path to install torrc to within the android app data folder
public final static String TORRC_ZIP_KEY = "assets/" + TORRC_ASSET_KEY; //key of the torrc file in the Zip file public final static String TORRC_ZIP_KEY = "assets/" + TORRC_ASSET_KEY; //key of the torrc file in the Zip file
//how to launch tor //how to launch tor
public final static String TOR_COMMAND_LINE_ARGS = "-f " + TORRC_INSTALL_PATH + " || exit\n"; // public final static String TOR_COMMAND_LINE_ARGS = "-f " + TORRC_INSTALL_PATH + " || exit\n";
//privoxy //privoxy
public final static String PRIVOXY_ASSET_KEY = "privoxy"; public final static String PRIVOXY_ASSET_KEY = "privoxy";
public final static String PRIVOXY_INSTALL_PATH = TOR_HOME + PRIVOXY_ASSET_KEY; //path to install privoxy to within the android app data folder // public final static String PRIVOXY_INSTALL_PATH = TOR_HOME + PRIVOXY_ASSET_KEY; //path to install privoxy to within the android app data folder
public final static String PRIVOXY_ZIP_KEY = "assets/" + PRIVOXY_ASSET_KEY; //key of the privoxy file in the Zip file public final static String PRIVOXY_ZIP_KEY = "assets/" + PRIVOXY_ASSET_KEY; //key of the privoxy file in the Zip file
//privoxy.config //privoxy.config
public final static String PRIVOXYCONFIG_ASSET_KEY = "privoxy.config"; public final static String PRIVOXYCONFIG_ASSET_KEY = "privoxy.config";
public final static String PRIVOXYCONFIG_INSTALL_PATH = TOR_HOME + PRIVOXYCONFIG_ASSET_KEY; //path to install privoxy to within the android app data folder // public final static String PRIVOXYCONFIG_INSTALL_PATH = TOR_HOME + PRIVOXYCONFIG_ASSET_KEY; //path to install privoxy to within the android app data folder
public final static String PRIVOXYCONFIG_ZIP_KEY = "assets/" + PRIVOXYCONFIG_ASSET_KEY; //key of the privoxy file in the Zip file public final static String PRIVOXYCONFIG_ZIP_KEY = "assets/" + PRIVOXYCONFIG_ASSET_KEY; //key of the privoxy file in the Zip file
//how to launch privoxy //how to launch privoxy
public final static String PRIVOXY_COMMAND_LINE_ARGS = ' ' + PRIVOXYCONFIG_INSTALL_PATH + " || exit\n"; // public final static String PRIVOXY_COMMAND_LINE_ARGS = ' ' + PRIVOXYCONFIG_INSTALL_PATH + " || exit\n";
//where to send the notices log //where to send the notices log
public final static String TOR_LOG_PATH = TOR_HOME + "notices.log"; // public final static String TOR_LOG_PATH = TOR_HOME + "notices.log";
//control port cookie path //control port cookie path
public final static String TOR_CONTROL_AUTH_COOKIE = TOR_HOME_DATA_DIR + "control_auth_cookie"; // public final static String TOR_CONTROL_AUTH_COOKIE = TOR_HOME_DATA_DIR + "control_auth_cookie";
//various console cmds //various console cmds
@ -56,7 +56,7 @@ public interface TorServiceConstants {
//path of the installed APK file //path of the installed APK file
public final static String APK_PATH = "/data/app/org.torproject.android.apk"; public final static String APK_PATH = "/data/app/org.torproject.android.apk";
public final static String APK_PATH_BASE = "/data/app/org.torproject.android"; public final static String APK_PATH_BASE = "/data/app";