fixed and cleaned up file path issues with binary installs and execution

This commit is contained in:
Nathan Freitas 2011-07-25 16:12:41 -04:00
parent 45bfee56e0
commit 7a1c0f3224
2 changed files with 75 additions and 158 deletions

View File

@ -20,147 +20,75 @@ import android.util.Log;
public class TorBinaryInstaller implements TorServiceConstants { public class TorBinaryInstaller implements TorServiceConstants {
String installPath; File installFolder;
String apkPath;
Context context; Context context;
public TorBinaryInstaller (Context context, String installPath, String apkPath) public TorBinaryInstaller (Context context, File installFolder)
{ {
this.installPath = installPath; this.installFolder = installFolder;
this.apkPath = apkPath;
this.context = context; this.context = context;
} }
/*
* Start the binary installation if the file doesn't exist or is forced
*/
public void start (boolean force)
{
boolean torBinaryExists = new File(installPath + TOR_BINARY_ASSET_KEY).exists();
Log.d(TAG,"Tor binary exists=" + torBinaryExists);
boolean privoxyBinaryExists = new File(installPath + PRIVOXY_ASSET_KEY).exists();
Log.d(TAG,"Privoxy binary exists=" + privoxyBinaryExists);
if (!(torBinaryExists && privoxyBinaryExists) || force)
installFromRaw ();
}
// //
/* /*
* Extract the Tor binary from the APK file using ZIP * Extract the Tor binary from the APK file using ZIP
*/ */
private void installFromRaw () public boolean installFromRaw ()
{ {
boolean result = false;
InputStream is = context.getResources().openRawResource(R.raw.toraa);
streamToFile(is,installPath + TOR_BINARY_ASSET_KEY, false);
is = context.getResources().openRawResource(R.raw.torab);
streamToFile(is,installPath + TOR_BINARY_ASSET_KEY, true);
is = context.getResources().openRawResource(R.raw.torac);
streamToFile(is,installPath + TOR_BINARY_ASSET_KEY, true);
is = context.getResources().openRawResource(R.raw.torad);
streamToFile(is,installPath + TOR_BINARY_ASSET_KEY, true);
is = context.getResources().openRawResource(R.raw.torrc);
streamToFile(is,installPath + TORRC_ASSET_KEY, false);
is = context.getResources().openRawResource(R.raw.privoxy);
streamToFile(is,installPath + PRIVOXY_ASSET_KEY, false);
is = context.getResources().openRawResource(R.raw.privoxy_config);
streamToFile(is,installPath + PRIVOXYCONFIG_ASSET_KEY, false);
Log.d(TAG,"SUCCESS: installed tor, privoxy binaries from raw");
}
/*
private void installFromZip ()
{
try try
{ {
InputStream is;
ZipFile zip = new ZipFile(apkPath); is = context.getResources().openRawResource(R.raw.toraa);
streamToFile(is,installFolder, TOR_BINARY_ASSET_KEY, false);
ZipEntry zipen = zip.getEntry(ASSETS_BASE + TOR_BINARY_ASSET_KEY); is = context.getResources().openRawResource(R.raw.torab);
streamToFile(zip.getInputStream(zipen),installPath + TOR_BINARY_ASSET_KEY, false); streamToFile(is,installFolder, TOR_BINARY_ASSET_KEY, true);
zipen = zip.getEntry(ASSETS_BASE + TORRC_ASSET_KEY); is = context.getResources().openRawResource(R.raw.torac);
streamToFile(zip.getInputStream(zipen),installPath + TORRC_ASSET_KEY); streamToFile(is,installFolder, TOR_BINARY_ASSET_KEY, true);
zipen = zip.getEntry(ASSETS_BASE + PRIVOXY_ASSET_KEY); is = context.getResources().openRawResource(R.raw.torad);
streamToFile(zip.getInputStream(zipen),installPath + PRIVOXY_ASSET_KEY); streamToFile(is,installFolder, TOR_BINARY_ASSET_KEY, true);
zipen = zip.getEntry(ASSETS_BASE + PRIVOXYCONFIG_ASSET_KEY); is = context.getResources().openRawResource(R.raw.torrc);
streamToFile(zip.getInputStream(zipen),installPath + PRIVOXYCONFIG_ASSET_KEY); streamToFile(is,installFolder, TORRC_ASSET_KEY, false);
zipen = zip.getEntry(ASSETS_BASE + PRIVOXYCONFIG_ASSET_KEY); is = context.getResources().openRawResource(R.raw.privoxy);
streamToFile(zip.getInputStream(zipen),installPath + PRIVOXYCONFIG_ASSET_KEY); streamToFile(is,installFolder, PRIVOXY_ASSET_KEY, false);
is = context.getResources().openRawResource(R.raw.privoxy_config);
zip.close(); streamToFile(is,installFolder, PRIVOXYCONFIG_ASSET_KEY, false);
Log.d(TAG,"SUCCESS: unzipped tor, privoxy, iptables binaries from apk");
} }
catch (IOException ioe) catch (IOException ioe)
{ {
Log.d(TAG,"FAIL: unable to unzip binaries from apk",ioe); Log.e(TAG, "unable to install tor binaries from raw", ioe);
return false;
}
return true;
} }
}
*/
/* /*
* Write the inputstream contents to the file * Write the inputstream contents to the file
*/ */
private static void streamToFile(InputStream stm, String targetFilename, boolean append) private static boolean streamToFile(InputStream stm, File folder, String targetFilename, boolean append) throws IOException
{ {
FileOutputStream stmOut = null;
byte[] buffer = new byte[FILE_WRITE_BUFFER_SIZE]; byte[] buffer = new byte[FILE_WRITE_BUFFER_SIZE];
int bytecount; int bytecount;
File outFile = new File(folder, targetFilename);
File outFile = new File(targetFilename); FileOutputStream stmOut = new FileOutputStream(outFile, append);
try {
if (!append)
outFile.createNewFile();
stmOut = new FileOutputStream(outFile);
}
catch (java.io.IOException e)
{
Log.d(TAG,"Error opening output file " + targetFilename,e);
return;
}
try
{
while ((bytecount = stm.read(buffer)) > 0) while ((bytecount = stm.read(buffer)) > 0)
@ -172,17 +100,8 @@ public class TorBinaryInstaller implements TorServiceConstants {
stmOut.close(); stmOut.close();
}
catch (java.io.IOException e) return true;
{
Log.d(TAG,"Error writing output file '" + targetFilename + "': " + e.toString());
return;
}
} }

View File

@ -60,16 +60,13 @@ public class TorService extends Service implements TorServiceConstants, Runnable
private ArrayList<String> resetBuffer = null; private ArrayList<String> resetBuffer = null;
private String appHome; // private String appHome;
private String appBinHome; private File appBinHome;
private String appDataHome; private File appDataHome;
private String torBinaryPath; private String torBinaryPath;
private String privoxyPath; private String privoxyPath;
private boolean hasRoot = false;
/** Called when the activity is first created. */ /** Called when the activity is first created. */
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
@ -419,36 +416,34 @@ public class TorService extends Service implements TorServiceConstants, Runnable
//check and install iptables //check and install iptables
IptablesManager.assertBinaries(this, true); IptablesManager.assertBinaries(this, true);
File fileInstall = getDir("bin/",0);
appHome = fileInstall.getAbsolutePath(); appBinHome = getDir("bin",0);
appDataHome = getCacheDir().getAbsolutePath() + '/'; appDataHome = getCacheDir();
logNotice( "appHome=" + appHome);
// logNotice( "appHome=" + appHome);
File fileTor = new File(appBinHome, TOR_BINARY_ASSET_KEY);
File filePrivoxy = new File(appBinHome, PRIVOXY_ASSET_KEY);
torBinaryPath = appBinHome + TOR_BINARY_ASSET_KEY;
privoxyPath = appBinHome + PRIVOXY_ASSET_KEY;
logNotice( "checking Tor binaries"); logNotice( "checking Tor binaries");
boolean torBinaryExists = new File(torBinaryPath).exists(); if (!(fileTor.exists() && filePrivoxy.exists()))
boolean privoxyBinaryExists = new File(privoxyPath).exists();
if (!(torBinaryExists && privoxyBinaryExists))
{ {
killTorProcess (); killTorProcess ();
TorBinaryInstaller installer = new TorBinaryInstaller(this, appBinHome, appBinHome); TorBinaryInstaller installer = new TorBinaryInstaller(this, appBinHome);
installer.start(true); boolean success = installer.installFromRaw();
torBinaryExists = new File(torBinaryPath).exists(); if (success)
privoxyBinaryExists = new File(privoxyPath).exists();
if (torBinaryExists && privoxyBinaryExists)
{ {
logNotice(getString(R.string.status_install_success)); logNotice(getString(R.string.status_install_success));
showToolbarNotification(getString(R.string.status_install_success), NOTIFY_ID, R.drawable.tornotification); showToolbarNotification(getString(R.string.status_install_success), NOTIFY_ID, R.drawable.tornotification);
torBinaryPath = fileTor.getAbsolutePath();
privoxyPath = filePrivoxy.getAbsolutePath();
} }
else else
{ {
@ -466,6 +461,9 @@ public class TorService extends Service implements TorServiceConstants, Runnable
logNotice("Found Tor binary: " + torBinaryPath); logNotice("Found Tor binary: " + torBinaryPath);
logNotice("Found Privoxy binary: " + privoxyPath); logNotice("Found Privoxy binary: " + privoxyPath);
torBinaryPath = fileTor.getAbsolutePath();
privoxyPath = filePrivoxy.getAbsolutePath();
} }
StringBuilder log = new StringBuilder (); StringBuilder log = new StringBuilder ();
@ -627,9 +625,9 @@ public class TorService extends Service implements TorServiceConstants, Runnable
StringBuilder log = new StringBuilder(); StringBuilder log = new StringBuilder();
String torrcPath = appBinHome + TORRC_ASSET_KEY; String torrcPath = new File(appBinHome, TORRC_ASSET_KEY).getAbsolutePath();
String[] torCmd = {torBinaryPath + " DataDirectory " + appDataHome + " -f " + torrcPath + " || exit\n"}; String[] torCmd = {torBinaryPath + " DataDirectory " + appDataHome.getAbsolutePath() + " -f " + torrcPath + " || exit\n"};
boolean runAsRootFalse = false; boolean runAsRootFalse = false;
boolean waitForProcess = false; boolean waitForProcess = false;
@ -697,7 +695,7 @@ public class TorService extends Service implements TorServiceConstants, Runnable
{ {
log = new StringBuilder(); log = new StringBuilder();
String privoxyConfigPath = appBinHome + PRIVOXYCONFIG_ASSET_KEY; String privoxyConfigPath = new File(appBinHome, PRIVOXYCONFIG_ASSET_KEY).getAbsolutePath();
String[] cmds = String[] cmds =
{ privoxyPath + " " + privoxyConfigPath + " &" }; { privoxyPath + " " + privoxyConfigPath + " &" };
@ -762,7 +760,7 @@ public class TorService extends Service implements TorServiceConstants, Runnable
logNotice( "SUCCESS connected to control port"); logNotice( "SUCCESS connected to control port");
String torAuthCookie = appDataHome + TOR_CONTROL_COOKIE; String torAuthCookie = new File(appDataHome, TOR_CONTROL_COOKIE).getAbsolutePath();
File fileCookie = new File(torAuthCookie); File fileCookie = new File(torAuthCookie);
@ -1406,7 +1404,7 @@ public class TorService extends Service implements TorServiceConstants, Runnable
if (enableHiddenServices) if (enableHiddenServices)
{ {
mBinder.updateConfiguration("HiddenServiceDir",appDataHome, false); mBinder.updateConfiguration("HiddenServiceDir",appDataHome.getAbsolutePath(), false);
String hsPorts = prefs.getString("pref_hs_ports",""); String hsPorts = prefs.getString("pref_hs_ports","");