From a81c0001d6cfb764ba6e1cd2fca6e78c6487c5f3 Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Wed, 17 Jun 2015 12:06:23 -0400 Subject: [PATCH] init file path variables (tor, polipo, etc) when the app starts These file path variables can be set at the very start, OrbotApp.onCreate() and they will not change during the lifetime of the app, so represent them as globally accessible static variables. This is needed for things like OrbotMainActivity detecting whether the tor daemon is still running, even though TorService is not. --- src/org/torproject/android/OrbotApp.java | 23 +++ .../android/service/TorService.java | 152 +++++++----------- 2 files changed, 82 insertions(+), 93 deletions(-) diff --git a/src/org/torproject/android/OrbotApp.java b/src/org/torproject/android/OrbotApp.java index c6f47971..bbc25092 100644 --- a/src/org/torproject/android/OrbotApp.java +++ b/src/org/torproject/android/OrbotApp.java @@ -10,8 +10,11 @@ import android.os.Build; import android.text.TextUtils; import android.util.Log; +import org.torproject.android.service.TorServiceConstants; + import info.guardianproject.util.Languages; +import java.io.File; import java.util.Locale; public class OrbotApp extends Application implements OrbotConstants @@ -19,12 +22,32 @@ public class OrbotApp extends Application implements OrbotConstants private Locale locale; + public static File appBinHome; + public static File appCacheHome; + + public static File fileTor; + public static File filePolipo; + public static File fileObfsclient; + public static File fileMeekclient; + public static File fileXtables; + public static File fileTorRc; + @Override public void onCreate() { super.onCreate(); Prefs.setContext(this); setNewLocale(Prefs.getDefaultLocale()); + + appBinHome = getDir(TorServiceConstants.DIRECTORY_TOR_BINARY,Application.MODE_PRIVATE); + appCacheHome = getDir(TorServiceConstants.DIRECTORY_TOR_DATA,Application.MODE_PRIVATE); + + fileTor= new File(appBinHome, TorServiceConstants.TOR_ASSET_KEY); + filePolipo = new File(appBinHome, TorServiceConstants.POLIPO_ASSET_KEY); + fileObfsclient = new File(appBinHome, TorServiceConstants.OBFSCLIENT_ASSET_KEY); + fileMeekclient = new File(appBinHome, TorServiceConstants.MEEK_ASSET_KEY); + fileXtables = new File(appBinHome, TorServiceConstants.IPTABLES_ASSET_KEY); + fileTorRc = new File(appBinHome, TorServiceConstants.TORRC_ASSET_KEY); } @Override diff --git a/src/org/torproject/android/service/TorService.java b/src/org/torproject/android/service/TorService.java index 7830e5ef..464382ee 100644 --- a/src/org/torproject/android/service/TorService.java +++ b/src/org/torproject/android/service/TorService.java @@ -9,7 +9,6 @@ package org.torproject.android.service; import android.annotation.SuppressLint; -import android.app.Application; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; @@ -41,6 +40,7 @@ import org.json.JSONArray; import org.json.JSONObject; import org.sufficientlysecure.rootcommands.Shell; import org.sufficientlysecure.rootcommands.command.SimpleCommand; +import org.torproject.android.OrbotApp; import org.torproject.android.OrbotConstants; import org.torproject.android.OrbotMainActivity; import org.torproject.android.Prefs; @@ -106,18 +106,9 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon private ArrayList configBuffer = null; private ArrayList resetBuffer = null; - - // private String appHome; - private File appBinHome; - private File appCacheHome; - - private File fileTor; - private File filePolipo; - private File fileObfsclient; - private File fileMeekclient; - private File fileXtables; - - private File fileTorRc; + + private boolean isTorUpgradeAndConfigComplete = false; + private File fileControlPort; private TorTransProxy mTransProxy; @@ -170,31 +161,17 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon private boolean findExistingTorDaemon() { - if (fileTor != null) - { - try - { - - mLastProcessId = initControlConnection(3,true); - - if (mLastProcessId != -1 && conn != null) - { - sendCallbackLogMessage (getString(R.string.found_existing_tor_process)); - sendCallbackStatus(STATUS_ON); - return true; - } - - - return false; - } - catch (Exception e) - { - //Log.e(TAG,"error finding proc",e); - return false; + try { + mLastProcessId = initControlConnection(3, true); + + if (mLastProcessId != -1 && conn != null) { + sendCallbackLogMessage(getString(R.string.found_existing_tor_process)); + sendCallbackStatus(STATUS_ON); + return true; } + } catch (Exception e) { } - else - return false; + return false; } /* (non-Javadoc) @@ -442,7 +419,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon int hsPort = Integer.parseInt(st.nextToken().split(" ")[0]);; - File fileDir = new File(appCacheHome, "hs" + hsPort); + File fileDir = new File(OrbotApp.appCacheHome, "hs" + hsPort); File file = new File(fileDir, "hostname"); @@ -507,28 +484,28 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon // try these separately in case one fails, then it can try the next File cannotKillFile = null; try { - killProcess(fileObfsclient); + killProcess(OrbotApp.fileObfsclient); } catch (IOException e) { e.printStackTrace(); - cannotKillFile = fileObfsclient; + cannotKillFile = OrbotApp.fileObfsclient; } try { - killProcess(fileMeekclient); + killProcess(OrbotApp.fileMeekclient); } catch (IOException e) { e.printStackTrace(); - cannotKillFile = fileMeekclient; + cannotKillFile = OrbotApp.fileMeekclient; } try { - killProcess(filePolipo); + killProcess(OrbotApp.filePolipo); } catch (IOException e) { e.printStackTrace(); - cannotKillFile = filePolipo; + cannotKillFile = OrbotApp.filePolipo; } try { - killProcess(fileTor); + killProcess(OrbotApp.fileTor); } catch (IOException e) { e.printStackTrace(); - cannotKillFile = fileTor; + cannotKillFile = OrbotApp.fileTor; } if (cannotKillFile != null) throw new CannotKillException(cannotKillFile); @@ -551,7 +528,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon } // if that fails, try again using native utils try { - killProcess(fileTor, "-1"); // this is -HUP + killProcess(OrbotApp.fileTor, "-1"); // this is -HUP } catch (CannotKillException e) { e.printStackTrace(); } catch (IOException e) { @@ -631,7 +608,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon } - initBinariesAndDirectories(); + torUpgradeAndConfig(); new Thread(new Runnable () { @@ -662,33 +639,21 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon logNotice("There was an error installing Orbot binaries"); } - + Log.i("TorService", "onCreate end"); } - private void initBinariesAndDirectories () throws Exception - { - - if (appBinHome == null) - appBinHome = getDir(DIRECTORY_TOR_BINARY,Application.MODE_PRIVATE); - - if (appCacheHome == null) - appCacheHome = getDir(DIRECTORY_TOR_DATA,Application.MODE_PRIVATE); - - fileTor= new File(appBinHome, TOR_ASSET_KEY); - filePolipo = new File(appBinHome, POLIPO_ASSET_KEY); - fileObfsclient = new File(appBinHome, OBFSCLIENT_ASSET_KEY); - fileMeekclient = new File(appBinHome, MEEK_ASSET_KEY); - fileTorRc = new File(appBinHome, TORRC_ASSET_KEY); - fileXtables = new File(appBinHome, IPTABLES_ASSET_KEY); + private void torUpgradeAndConfig() throws IOException, TimeoutException { + if (isTorUpgradeAndConfigComplete) + return; SharedPreferences prefs = TorServiceUtils.getSharedPrefs(getApplicationContext()); String version = prefs.getString(PREF_BINARY_TOR_VERSION_INSTALLED,null); logNotice("checking binary version: " + version); - TorResourceInstaller installer = new TorResourceInstaller(this, appBinHome); + TorResourceInstaller installer = new TorResourceInstaller(this, OrbotApp.appBinHome); - if (version == null || (!version.equals(BINARY_TOR_VERSION)) || (!fileTor.exists())) + if (version == null || (!version.equals(BINARY_TOR_VERSION)) || (!OrbotApp.fileTor.exists())) { logNotice("upgrading binaries to latest version: " + BINARY_TOR_VERSION); @@ -699,18 +664,19 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon } updateTorConfigFile (); + isTorUpgradeAndConfigComplete = true; } private boolean updateTorConfigFile () throws FileNotFoundException, IOException, TimeoutException { SharedPreferences prefs = TorServiceUtils.getSharedPrefs(getApplicationContext()); - TorResourceInstaller installer = new TorResourceInstaller(this, appBinHome); + TorResourceInstaller installer = new TorResourceInstaller(this, OrbotApp.appBinHome); StringBuffer extraLines = new StringBuffer(); String TORRC_CONTROLPORT_FILE_KEY = "ControlPortWriteToFile"; - fileControlPort = new File(appBinHome,"control.txt"); + fileControlPort = new File(OrbotApp.appBinHome, "control.txt"); extraLines.append(TORRC_CONTROLPORT_FILE_KEY).append(' ').append(fileControlPort.getCanonicalPath()).append('\n'); if (Prefs.transparentTethering()) @@ -746,7 +712,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon debug("torrc.custom=" + extraLines.toString()); - File fileTorRcCustom = new File(fileTorRc.getAbsolutePath() + ".custom"); + File fileTorRcCustom = new File(OrbotApp.fileTorRc.getAbsolutePath() + ".custom"); boolean success = installer.updateTorConfigCustom(fileTorRcCustom, extraLines.toString()); if (success) @@ -801,8 +767,8 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon killAllDaemons(); try { - if (fileTor == null) - initBinariesAndDirectories(); + if (!isTorUpgradeAndConfigComplete) + torUpgradeAndConfig(); ArrayList customEnv = new ArrayList(); @@ -810,7 +776,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon if (Prefs.useVpn() && !mIsLollipop) customEnv.add("TOR_PT_PROXY=socks5://127.0.0.1:" + OrbotVpnService.mSocksProxyPort); - String baseDirectory = fileTor.getParent(); + String baseDirectory = OrbotApp.fileTor.getParent(); Shell shellUser = Shell.startShell(customEnv, baseDirectory); boolean success = runTorShellCmd(shellUser); @@ -862,7 +828,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon if (Prefs.useRoot()) { if (mTransProxy == null) - mTransProxy = new TorTransProxy(this, fileXtables); + mTransProxy = new TorTransProxy(this, OrbotApp.fileXtables); try { mTransProxy.flushTransproxyRules(this); @@ -890,7 +856,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon if (mTransProxy == null) { - mTransProxy = new TorTransProxy(this, fileXtables); + mTransProxy = new TorTransProxy(this, OrbotApp.fileXtables); } @@ -963,7 +929,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon debug ("Transparent Proxying: disabling..."); if (mTransProxy == null) - mTransProxy = new TorTransProxy(this, fileXtables); + mTransProxy = new TorTransProxy(this, OrbotApp.fileXtables); mTransProxy.setTransparentProxyingAll(this, false, shell); ArrayList apps = AppManager.getApps(this, TorServiceUtils.getSharedPrefs(getApplicationContext())); @@ -975,14 +941,14 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon private boolean runTorShellCmd(final Shell shell) throws Exception { - String torrcPath = new File(appBinHome, TORRC_ASSET_KEY).getCanonicalPath(); + String torrcPath = new File(OrbotApp.appBinHome, TORRC_ASSET_KEY).getCanonicalPath(); updateTorConfigFile(); sendCallbackLogMessage(getString(R.string.status_starting_up)); - String torCmdString = fileTor.getCanonicalPath() - + " DataDirectory " + appCacheHome.getCanonicalPath() + String torCmdString = OrbotApp.fileTor.getCanonicalPath() + + " DataDirectory " + OrbotApp.appCacheHome.getCanonicalPath() + " --defaults-torrc " + torrcPath + " -f " + torrcPath + ".custom"; @@ -1037,7 +1003,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon { - File file = new File(appBinHome, POLIPOCONFIG_ASSET_KEY); + File file = new File(OrbotApp.appBinHome, POLIPOCONFIG_ASSET_KEY); Properties props = new Properties(); @@ -1056,7 +1022,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon logNotice( "Starting polipo process"); - int polipoProcId = TorServiceUtils.findProcessId(filePolipo.getCanonicalPath()); + int polipoProcId = TorServiceUtils.findProcessId(OrbotApp.filePolipo.getCanonicalPath()); StringBuilder log = null; @@ -1068,15 +1034,15 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon updatePolipoConfig(); - String polipoConfigPath = new File(appBinHome, POLIPOCONFIG_ASSET_KEY).getCanonicalPath(); - SimpleCommand cmdPolipo = new SimpleCommand(filePolipo.getCanonicalPath() + " -c " + polipoConfigPath + " &"); + String polipoConfigPath = new File(OrbotApp.appBinHome, POLIPOCONFIG_ASSET_KEY).getCanonicalPath(); + SimpleCommand cmdPolipo = new SimpleCommand(OrbotApp.filePolipo.getCanonicalPath() + " -c " + polipoConfigPath + " &"); shell.add(cmdPolipo); //wait one second to make sure it has started up Thread.sleep(1000); - while ((polipoProcId = TorServiceUtils.findProcessId(filePolipo.getCanonicalPath())) == -1 && attempts < MAX_START_TRIES) + while ((polipoProcId = TorServiceUtils.findProcessId(OrbotApp.filePolipo.getCanonicalPath())) == -1 && attempts < MAX_START_TRIES) { logNotice("Couldn't find Polipo process... retrying...\n" + log); Thread.sleep(3000); @@ -1138,7 +1104,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon { logNotice( "SUCCESS connected to Tor control port."); - File fileCookie = new File(appCacheHome, TOR_CONTROL_COOKIE); + File fileCookie = new File(OrbotApp.appCacheHome, TOR_CONTROL_COOKIE); if (fileCookie.exists()) { @@ -2113,9 +2079,9 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon if (obfsBridges) { - extraLines.append("ClientTransportPlugin obfs3 exec " + fileObfsclient.getCanonicalPath()).append('\n'); - extraLines.append("ClientTransportPlugin obfs4 exec " + fileObfsclient.getCanonicalPath()).append('\n'); - extraLines.append("ClientTransportPlugin scramblesuit exec " + fileObfsclient.getCanonicalPath()).append('\n'); + extraLines.append("ClientTransportPlugin obfs3 exec " + OrbotApp.fileObfsclient.getCanonicalPath()).append('\n'); + extraLines.append("ClientTransportPlugin obfs4 exec " + OrbotApp.fileObfsclient.getCanonicalPath()).append('\n'); + extraLines.append("ClientTransportPlugin scramblesuit exec " + OrbotApp.fileObfsclient.getCanonicalPath()).append('\n'); } String[] bridgeListLines = bridgeList.split("\\r?\\n"); @@ -2145,7 +2111,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon debug ("Using meek bridges"); - String bridgeConfig = "meek exec " + fileMeekclient.getCanonicalPath(); + String bridgeConfig = "meek exec " + OrbotApp.fileMeekclient.getCanonicalPath(); extraLines.append("ClientTransportPlugin" + ' ' + bridgeConfig).append('\n'); String[] meekBridge = @@ -2181,14 +2147,14 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon if (entranceNodes.length() > 0 || exitNodes.length() > 0 || excludeNodes.length() > 0) { //only apply GeoIP if you need it - File fileGeoIP = new File(appBinHome,GEOIP_ASSET_KEY); - File fileGeoIP6 = new File(appBinHome,GEOIP6_ASSET_KEY); + File fileGeoIP = new File(OrbotApp.appBinHome, GEOIP_ASSET_KEY); + File fileGeoIP6 = new File(OrbotApp.appBinHome, GEOIP6_ASSET_KEY); try { if ((!fileGeoIP.exists())) { - TorResourceInstaller installer = new TorResourceInstaller(this, appBinHome); + TorResourceInstaller installer = new TorResourceInstaller(this, OrbotApp.appBinHome); boolean success = installer.installGeoIP(); } @@ -2284,7 +2250,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon hsPort = Integer.parseInt(hsPortConfig.split(" ")[0]); - String hsDirPath = new File(appCacheHome,"hs" + hsPort).getCanonicalPath(); + String hsDirPath = new File(OrbotApp.appCacheHome,"hs" + hsPort).getCanonicalPath(); debug("Adding hidden service on port: " + hsPortConfig); @@ -2324,8 +2290,8 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon //using Google DNS for now as the public DNS server private String writeDNSFile () throws IOException { - File file = new File(appBinHome,"resolv.conf"); - + File file = new File(OrbotApp.appBinHome, "resolv.conf"); + PrintWriter bw = new PrintWriter(new FileWriter(file)); bw.println("nameserver 8.8.8.8"); bw.println("nameserver 8.8.4.4");