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 {
String installPath;
String apkPath;
File installFolder;
Context context;
public TorBinaryInstaller (Context context, String installPath, String apkPath)
public TorBinaryInstaller (Context context, File installFolder)
{
this.installPath = installPath;
this.apkPath = apkPath;
this.installFolder = installFolder;
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
*/
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
{
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);
streamToFile(zip.getInputStream(zipen),installPath + TOR_BINARY_ASSET_KEY, false);
is = context.getResources().openRawResource(R.raw.torab);
streamToFile(is,installFolder, TOR_BINARY_ASSET_KEY, true);
zipen = zip.getEntry(ASSETS_BASE + TORRC_ASSET_KEY);
streamToFile(zip.getInputStream(zipen),installPath + TORRC_ASSET_KEY);
is = context.getResources().openRawResource(R.raw.torac);
streamToFile(is,installFolder, TOR_BINARY_ASSET_KEY, true);
zipen = zip.getEntry(ASSETS_BASE + PRIVOXY_ASSET_KEY);
streamToFile(zip.getInputStream(zipen),installPath + PRIVOXY_ASSET_KEY);
is = context.getResources().openRawResource(R.raw.torad);
streamToFile(is,installFolder, TOR_BINARY_ASSET_KEY, true);
zipen = zip.getEntry(ASSETS_BASE + PRIVOXYCONFIG_ASSET_KEY);
streamToFile(zip.getInputStream(zipen),installPath + PRIVOXYCONFIG_ASSET_KEY);
is = context.getResources().openRawResource(R.raw.torrc);
streamToFile(is,installFolder, TORRC_ASSET_KEY, false);
zipen = zip.getEntry(ASSETS_BASE + PRIVOXYCONFIG_ASSET_KEY);
streamToFile(zip.getInputStream(zipen),installPath + PRIVOXYCONFIG_ASSET_KEY);
is = context.getResources().openRawResource(R.raw.privoxy);
streamToFile(is,installFolder, PRIVOXY_ASSET_KEY, false);
zip.close();
Log.d(TAG,"SUCCESS: unzipped tor, privoxy, iptables binaries from apk");
is = context.getResources().openRawResource(R.raw.privoxy_config);
streamToFile(is,installFolder, PRIVOXYCONFIG_ASSET_KEY, false);
}
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
*/
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];
int bytecount;
File outFile = new File(folder, targetFilename);
File outFile = new File(targetFilename);
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
{
FileOutputStream stmOut = new FileOutputStream(outFile, append);
while ((bytecount = stm.read(buffer)) > 0)
@ -172,17 +100,8 @@ public class TorBinaryInstaller implements TorServiceConstants {
stmOut.close();
}
catch (java.io.IOException e)
{
Log.d(TAG,"Error writing output file '" + targetFilename + "': " + e.toString());
return;
}
return true;
}

View File

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