From b6a9b48e779d687271ef9e43c4a2a059acf79f37 Mon Sep 17 00:00:00 2001 From: Nathan Freitas Date: Tue, 8 Apr 2014 11:37:45 -0400 Subject: [PATCH] ensure existing binaries are removed before install problems may be caused by soft links or old bins --- .../android/service/TorResourceInstaller.java | 24 +++- .../android/service/TorService.java | 21 +++- .../android/service/TorServiceConstants.java | 4 +- .../android/service/TorServiceUtils.java | 106 ------------------ 4 files changed, 38 insertions(+), 117 deletions(-) diff --git a/src/org/torproject/android/service/TorResourceInstaller.java b/src/org/torproject/android/service/TorResourceInstaller.java index b1a85a77..41e899c5 100644 --- a/src/org/torproject/android/service/TorResourceInstaller.java +++ b/src/org/torproject/android/service/TorResourceInstaller.java @@ -3,19 +3,21 @@ package org.torproject.android.service; -import java.io.BufferedReader; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; -import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.util.ArrayList; +import java.util.concurrent.TimeoutException; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; +import org.sufficientlysecure.rootcommands.Shell; +import org.sufficientlysecure.rootcommands.command.SimpleCommand; import org.torproject.android.R; import org.torproject.android.TorConstants; @@ -39,7 +41,7 @@ public class TorResourceInstaller implements TorServiceConstants { /* * Extract the Tor resources from the APK file using ZIP */ - public boolean installResources () throws IOException, FileNotFoundException + public boolean installResources () throws IOException, FileNotFoundException, TimeoutException { InputStream is; @@ -48,32 +50,48 @@ public class TorResourceInstaller implements TorServiceConstants { if (!installFolder.exists()) installFolder.mkdirs(); + Shell shell = Shell.startShell(new ArrayList(),installFolder.getCanonicalPath()); + is = context.getResources().openRawResource(R.raw.torrc); outFile = new File(installFolder, TORRC_ASSET_KEY); + if (outFile.exists()) + shell.add(new SimpleCommand("rm " + outFile.getCanonicalPath())).waitForFinish(); streamToFile(is,outFile, false, false); is = context.getResources().openRawResource(R.raw.torrctether); outFile = new File(installFolder, TORRC_TETHER_KEY); + if (outFile.exists()) + shell.add(new SimpleCommand("rm " + outFile.getCanonicalPath())).waitForFinish(); streamToFile(is, outFile, false, false); is = context.getResources().openRawResource(R.raw.privoxy_config); outFile = new File(installFolder, PRIVOXYCONFIG_ASSET_KEY); + if (outFile.exists()) + shell.add(new SimpleCommand("rm " + outFile.getCanonicalPath())).waitForFinish(); streamToFile(is,outFile, false, false); is = context.getResources().openRawResource(R.raw.tor); outFile = new File(installFolder, TOR_ASSET_KEY); + if (outFile.exists()) + shell.add(new SimpleCommand("rm " + outFile.getCanonicalPath())).waitForFinish(); streamToFile(is,outFile, false, true); is = context.getResources().openRawResource(R.raw.privoxy); outFile = new File(installFolder, PRIVOXY_ASSET_KEY); + if (outFile.exists()) + shell.add(new SimpleCommand("rm " + outFile.getCanonicalPath())).waitForFinish(); streamToFile(is,outFile, false, true); is = context.getResources().openRawResource(R.raw.obfsproxy); outFile = new File(installFolder, OBFSPROXY_ASSET_KEY); + if (outFile.exists()) + shell.add(new SimpleCommand("rm " + outFile.getCanonicalPath())).waitForFinish(); streamToFile(is,outFile, false, true); is = context.getResources().openRawResource(R.raw.xtables); outFile = new File(installFolder, IPTABLES_ASSET_KEY); + if (outFile.exists()) + shell.add(new SimpleCommand("rm " + outFile.getCanonicalPath())).waitForFinish(); streamToFile(is,outFile, false, true); return true; diff --git a/src/org/torproject/android/service/TorService.java b/src/org/torproject/android/service/TorService.java index fc49df15..471d35bb 100644 --- a/src/org/torproject/android/service/TorService.java +++ b/src/org/torproject/android/service/TorService.java @@ -25,7 +25,6 @@ import net.freehaven.tor.control.ConfigEntry; import net.freehaven.tor.control.EventHandler; import net.freehaven.tor.control.TorControlConnection; -import org.sufficientlysecure.rootcommands.RootCommands; import org.sufficientlysecure.rootcommands.Shell; import org.sufficientlysecure.rootcommands.Toolbox; import org.sufficientlysecure.rootcommands.command.SimpleCommand; @@ -55,7 +54,6 @@ import android.os.RemoteException; import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat.Builder; import android.util.Log; -import android.widget.Toast; public class TorService extends Service implements TorServiceConstants, TorConstants, Runnable, EventHandler { @@ -528,7 +526,8 @@ public class TorService extends Service implements TorServiceConstants, TorConst TorResourceInstaller installer = new TorResourceInstaller(this, appBinHome); boolean success = installer.installResources(); - prefs.edit().putString(PREF_BINARY_TOR_VERSION_INSTALLED,BINARY_TOR_VERSION).commit(); + if (success) + prefs.edit().putString(PREF_BINARY_TOR_VERSION_INSTALLED,BINARY_TOR_VERSION).commit(); } else if (!fileTorRc.exists()) { @@ -537,7 +536,8 @@ public class TorService extends Service implements TorServiceConstants, TorConst TorResourceInstaller installer = new TorResourceInstaller(this, appBinHome); boolean success = installer.installResources(); - prefs.edit().putString(PREF_BINARY_TOR_VERSION_INSTALLED,BINARY_TOR_VERSION).commit(); + if (success) + prefs.edit().putString(PREF_BINARY_TOR_VERSION_INSTALLED,BINARY_TOR_VERSION).commit(); } @@ -556,7 +556,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst if (!fileBin.canExecute()) { logNotice("(re)Setting permission on binary: " + fileBin.getCanonicalPath()); - Shell shell = Shell.startShell(new ArrayList(), appBinHome.getAbsolutePath()); + Shell shell = Shell.startShell(new ArrayList(), appBinHome.getCanonicalPath()); shell.add(new SimpleCommand("chmod " + CHMOD_EXE_VALUE + ' ' + fileBin.getCanonicalPath())).waitForFinish(); @@ -588,7 +588,16 @@ public class TorService extends Service implements TorServiceConstants, TorConst public void initTor () throws Exception { - initBinaries(); + try + { + initBinaries(); + } + catch (IOException e) + { + logNotice("There was a problem installing the Tor binaries: " + e.getLocalizedMessage()); + Log.d(TAG,"error installing binaries",e); + return; + } updateSettings (); diff --git a/src/org/torproject/android/service/TorServiceConstants.java b/src/org/torproject/android/service/TorServiceConstants.java index db73e70e..b1c99456 100644 --- a/src/org/torproject/android/service/TorServiceConstants.java +++ b/src/org/torproject/android/service/TorServiceConstants.java @@ -46,7 +46,7 @@ public interface TorServiceConstants { public final static String CHMOD_EXE_VALUE = "770"; - public final static int FILE_WRITE_BUFFER_SIZE = 2048; + public final static int FILE_WRITE_BUFFER_SIZE = 1024; //HTTP Proxy server port public final static int PORT_HTTP = 8118; //just like Privoxy! @@ -80,7 +80,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.21-openssl1.0.1g"; + public static final String BINARY_TOR_VERSION = "0.2.4.21-openssl1.0.1g-installfix"; public static final String BINARY_PRIVOXY_VERSION = "3.0.12"; public static final String PREF_BINARY_TOR_VERSION_INSTALLED = "BINARY_TOR_VERSION_INTALLED"; public static final String PREF_BINARY_PRIVOXY_VERSION_INSTALLED = "BINARY_PRIVOXY_VERSION_INTALLED"; diff --git a/src/org/torproject/android/service/TorServiceUtils.java b/src/org/torproject/android/service/TorServiceUtils.java index e5cf25e0..96a2c8af 100644 --- a/src/org/torproject/android/service/TorServiceUtils.java +++ b/src/org/torproject/android/service/TorServiceUtils.java @@ -117,110 +117,4 @@ public class TorServiceUtils implements TorServiceConstants { return procId; } - - /** - public static int doShellCommand(String[] cmds, StringBuilder log, boolean runAsRoot, boolean waitFor) throws Exception - { - - Process proc = null; - int exitCode = -1; - - if (runAsRoot) - proc = Runtime.getRuntime().exec("su"); - else - proc = Runtime.getRuntime().exec("sh"); - - OutputStreamWriter out = new OutputStreamWriter(proc.getOutputStream()); - - for (int i = 0; i < cmds.length; i++) - { - if (TorService.ENABLE_DEBUG_LOG) - Log.d(TorService.TAG,"executing shell cmd: " + cmds[i] + "; runAsRoot=" + runAsRoot + ";waitFor=" + waitFor); - - out.write(cmds[i]); - out.write("\n"); - } - - out.flush(); - out.write("exit\n"); - out.flush(); - - if (waitFor) - { - - final char buf[] = new char[10]; - - // Consume the "stdout" - InputStreamReader reader = new InputStreamReader(proc.getInputStream()); - int read=0; - while ((read=reader.read(buf)) != -1) { - if (log != null) log.append(buf, 0, read); - } - - // Consume the "stderr" - reader = new InputStreamReader(proc.getErrorStream()); - read=0; - while ((read=reader.read(buf)) != -1) { - if (log != null) log.append(buf, 0, read); - } - - exitCode = proc.waitFor(); - - } - - - return exitCode; - - } - - public static int doShellCommand(String cmd, StringBuilder log, boolean runAsRoot, boolean waitFor) throws Exception - { - - Process proc = null; - int exitCode = -1; - - if (runAsRoot) - proc = Runtime.getRuntime().exec("su"); - else - proc = Runtime.getRuntime().exec("sh"); - - OutputStreamWriter out = new OutputStreamWriter(proc.getOutputStream()); - - // TorService.logMessage("executing shell cmd: " + cmds[i] + "; runAsRoot=" + runAsRoot + ";waitFor=" + waitFor); - - out.write(cmd); - out.write("\n"); - - - out.flush(); - out.write("exit\n"); - out.flush(); - - if (waitFor) - { - - final char buf[] = new char[10]; - - // Consume the "stdout" - InputStreamReader reader = new InputStreamReader(proc.getInputStream()); - int read=0; - while ((read=reader.read(buf)) != -1) { - if (log != null) log.append(buf, 0, read); - } - - // Consume the "stderr" - reader = new InputStreamReader(proc.getErrorStream()); - read=0; - while ((read=reader.read(buf)) != -1) { - if (log != null) log.append(buf, 0, read); - } - - exitCode = proc.waitFor(); - - } - - return exitCode; - - } - **/ }