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.
This commit is contained in:
Hans-Christoph Steiner 2015-06-17 12:06:23 -04:00
parent 943edf094b
commit a81c0001d6
2 changed files with 82 additions and 93 deletions

View File

@ -10,8 +10,11 @@ import android.os.Build;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import org.torproject.android.service.TorServiceConstants;
import info.guardianproject.util.Languages; import info.guardianproject.util.Languages;
import java.io.File;
import java.util.Locale; import java.util.Locale;
public class OrbotApp extends Application implements OrbotConstants public class OrbotApp extends Application implements OrbotConstants
@ -19,12 +22,32 @@ public class OrbotApp extends Application implements OrbotConstants
private Locale locale; 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 @Override
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
Prefs.setContext(this); Prefs.setContext(this);
setNewLocale(Prefs.getDefaultLocale()); 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 @Override

View File

@ -9,7 +9,6 @@ package org.torproject.android.service;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.Application;
import android.app.Notification; import android.app.Notification;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
@ -41,6 +40,7 @@ import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import org.sufficientlysecure.rootcommands.Shell; import org.sufficientlysecure.rootcommands.Shell;
import org.sufficientlysecure.rootcommands.command.SimpleCommand; import org.sufficientlysecure.rootcommands.command.SimpleCommand;
import org.torproject.android.OrbotApp;
import org.torproject.android.OrbotConstants; import org.torproject.android.OrbotConstants;
import org.torproject.android.OrbotMainActivity; import org.torproject.android.OrbotMainActivity;
import org.torproject.android.Prefs; import org.torproject.android.Prefs;
@ -107,17 +107,8 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
private ArrayList<String> configBuffer = null; private ArrayList<String> configBuffer = null;
private ArrayList<String> resetBuffer = null; private ArrayList<String> resetBuffer = null;
// private String appHome; private boolean isTorUpgradeAndConfigComplete = false;
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 File fileControlPort; private File fileControlPort;
private TorTransProxy mTransProxy; private TorTransProxy mTransProxy;
@ -170,31 +161,17 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
private boolean findExistingTorDaemon() { private boolean findExistingTorDaemon() {
if (fileTor != null) try {
{ mLastProcessId = initControlConnection(3, true);
try
{
mLastProcessId = initControlConnection(3,true); if (mLastProcessId != -1 && conn != null) {
sendCallbackLogMessage(getString(R.string.found_existing_tor_process));
if (mLastProcessId != -1 && conn != null) sendCallbackStatus(STATUS_ON);
{ return true;
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;
} }
} catch (Exception e) {
} }
else return false;
return false;
} }
/* (non-Javadoc) /* (non-Javadoc)
@ -442,7 +419,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
int hsPort = Integer.parseInt(st.nextToken().split(" ")[0]);; 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"); 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 // try these separately in case one fails, then it can try the next
File cannotKillFile = null; File cannotKillFile = null;
try { try {
killProcess(fileObfsclient); killProcess(OrbotApp.fileObfsclient);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
cannotKillFile = fileObfsclient; cannotKillFile = OrbotApp.fileObfsclient;
} }
try { try {
killProcess(fileMeekclient); killProcess(OrbotApp.fileMeekclient);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
cannotKillFile = fileMeekclient; cannotKillFile = OrbotApp.fileMeekclient;
} }
try { try {
killProcess(filePolipo); killProcess(OrbotApp.filePolipo);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
cannotKillFile = filePolipo; cannotKillFile = OrbotApp.filePolipo;
} }
try { try {
killProcess(fileTor); killProcess(OrbotApp.fileTor);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
cannotKillFile = fileTor; cannotKillFile = OrbotApp.fileTor;
} }
if (cannotKillFile != null) if (cannotKillFile != null)
throw new CannotKillException(cannotKillFile); throw new CannotKillException(cannotKillFile);
@ -551,7 +528,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
} }
// if that fails, try again using native utils // if that fails, try again using native utils
try { try {
killProcess(fileTor, "-1"); // this is -HUP killProcess(OrbotApp.fileTor, "-1"); // this is -HUP
} catch (CannotKillException e) { } catch (CannotKillException e) {
e.printStackTrace(); e.printStackTrace();
} catch (IOException e) { } catch (IOException e) {
@ -631,7 +608,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
} }
initBinariesAndDirectories(); torUpgradeAndConfig();
new Thread(new Runnable () new Thread(new Runnable ()
{ {
@ -662,33 +639,21 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
logNotice("There was an error installing Orbot binaries"); logNotice("There was an error installing Orbot binaries");
} }
Log.i("TorService", "onCreate end");
} }
private void initBinariesAndDirectories () throws Exception private void torUpgradeAndConfig() throws IOException, TimeoutException {
{ if (isTorUpgradeAndConfigComplete)
return;
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);
SharedPreferences prefs = TorServiceUtils.getSharedPrefs(getApplicationContext()); SharedPreferences prefs = TorServiceUtils.getSharedPrefs(getApplicationContext());
String version = prefs.getString(PREF_BINARY_TOR_VERSION_INSTALLED,null); String version = prefs.getString(PREF_BINARY_TOR_VERSION_INSTALLED,null);
logNotice("checking binary version: " + version); 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); logNotice("upgrading binaries to latest version: " + BINARY_TOR_VERSION);
@ -699,18 +664,19 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
} }
updateTorConfigFile (); updateTorConfigFile ();
isTorUpgradeAndConfigComplete = true;
} }
private boolean updateTorConfigFile () throws FileNotFoundException, IOException, TimeoutException private boolean updateTorConfigFile () throws FileNotFoundException, IOException, TimeoutException
{ {
SharedPreferences prefs = TorServiceUtils.getSharedPrefs(getApplicationContext()); SharedPreferences prefs = TorServiceUtils.getSharedPrefs(getApplicationContext());
TorResourceInstaller installer = new TorResourceInstaller(this, appBinHome); TorResourceInstaller installer = new TorResourceInstaller(this, OrbotApp.appBinHome);
StringBuffer extraLines = new StringBuffer(); StringBuffer extraLines = new StringBuffer();
String TORRC_CONTROLPORT_FILE_KEY = "ControlPortWriteToFile"; 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'); extraLines.append(TORRC_CONTROLPORT_FILE_KEY).append(' ').append(fileControlPort.getCanonicalPath()).append('\n');
if (Prefs.transparentTethering()) if (Prefs.transparentTethering())
@ -746,7 +712,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
debug("torrc.custom=" + extraLines.toString()); 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()); boolean success = installer.updateTorConfigCustom(fileTorRcCustom, extraLines.toString());
if (success) if (success)
@ -801,8 +767,8 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
killAllDaemons(); killAllDaemons();
try { try {
if (fileTor == null) if (!isTorUpgradeAndConfigComplete)
initBinariesAndDirectories(); torUpgradeAndConfig();
ArrayList<String> customEnv = new ArrayList<String>(); ArrayList<String> customEnv = new ArrayList<String>();
@ -810,7 +776,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
if (Prefs.useVpn() && !mIsLollipop) if (Prefs.useVpn() && !mIsLollipop)
customEnv.add("TOR_PT_PROXY=socks5://127.0.0.1:" + OrbotVpnService.mSocksProxyPort); 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); Shell shellUser = Shell.startShell(customEnv, baseDirectory);
boolean success = runTorShellCmd(shellUser); boolean success = runTorShellCmd(shellUser);
@ -862,7 +828,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
if (Prefs.useRoot()) if (Prefs.useRoot())
{ {
if (mTransProxy == null) if (mTransProxy == null)
mTransProxy = new TorTransProxy(this, fileXtables); mTransProxy = new TorTransProxy(this, OrbotApp.fileXtables);
try { try {
mTransProxy.flushTransproxyRules(this); mTransProxy.flushTransproxyRules(this);
@ -890,7 +856,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
if (mTransProxy == null) 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..."); debug ("Transparent Proxying: disabling...");
if (mTransProxy == null) if (mTransProxy == null)
mTransProxy = new TorTransProxy(this, fileXtables); mTransProxy = new TorTransProxy(this, OrbotApp.fileXtables);
mTransProxy.setTransparentProxyingAll(this, false, shell); mTransProxy.setTransparentProxyingAll(this, false, shell);
ArrayList<TorifiedApp> apps = AppManager.getApps(this, TorServiceUtils.getSharedPrefs(getApplicationContext())); ArrayList<TorifiedApp> 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 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(); updateTorConfigFile();
sendCallbackLogMessage(getString(R.string.status_starting_up)); sendCallbackLogMessage(getString(R.string.status_starting_up));
String torCmdString = fileTor.getCanonicalPath() String torCmdString = OrbotApp.fileTor.getCanonicalPath()
+ " DataDirectory " + appCacheHome.getCanonicalPath() + " DataDirectory " + OrbotApp.appCacheHome.getCanonicalPath()
+ " --defaults-torrc " + torrcPath + " --defaults-torrc " + torrcPath
+ " -f " + torrcPath + ".custom"; + " -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(); Properties props = new Properties();
@ -1056,7 +1022,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
logNotice( "Starting polipo process"); logNotice( "Starting polipo process");
int polipoProcId = TorServiceUtils.findProcessId(filePolipo.getCanonicalPath()); int polipoProcId = TorServiceUtils.findProcessId(OrbotApp.filePolipo.getCanonicalPath());
StringBuilder log = null; StringBuilder log = null;
@ -1068,15 +1034,15 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
updatePolipoConfig(); updatePolipoConfig();
String polipoConfigPath = new File(appBinHome, POLIPOCONFIG_ASSET_KEY).getCanonicalPath(); String polipoConfigPath = new File(OrbotApp.appBinHome, POLIPOCONFIG_ASSET_KEY).getCanonicalPath();
SimpleCommand cmdPolipo = new SimpleCommand(filePolipo.getCanonicalPath() + " -c " + polipoConfigPath + " &"); SimpleCommand cmdPolipo = new SimpleCommand(OrbotApp.filePolipo.getCanonicalPath() + " -c " + polipoConfigPath + " &");
shell.add(cmdPolipo); shell.add(cmdPolipo);
//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);
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); logNotice("Couldn't find Polipo process... retrying...\n" + log);
Thread.sleep(3000); Thread.sleep(3000);
@ -1138,7 +1104,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
{ {
logNotice( "SUCCESS connected to Tor control port."); 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()) if (fileCookie.exists())
{ {
@ -2113,9 +2079,9 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
if (obfsBridges) if (obfsBridges)
{ {
extraLines.append("ClientTransportPlugin obfs3 exec " + fileObfsclient.getCanonicalPath()).append('\n'); extraLines.append("ClientTransportPlugin obfs3 exec " + OrbotApp.fileObfsclient.getCanonicalPath()).append('\n');
extraLines.append("ClientTransportPlugin obfs4 exec " + fileObfsclient.getCanonicalPath()).append('\n'); extraLines.append("ClientTransportPlugin obfs4 exec " + OrbotApp.fileObfsclient.getCanonicalPath()).append('\n');
extraLines.append("ClientTransportPlugin scramblesuit exec " + fileObfsclient.getCanonicalPath()).append('\n'); extraLines.append("ClientTransportPlugin scramblesuit exec " + OrbotApp.fileObfsclient.getCanonicalPath()).append('\n');
} }
String[] bridgeListLines = bridgeList.split("\\r?\\n"); String[] bridgeListLines = bridgeList.split("\\r?\\n");
@ -2145,7 +2111,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
debug ("Using meek bridges"); debug ("Using meek bridges");
String bridgeConfig = "meek exec " + fileMeekclient.getCanonicalPath(); String bridgeConfig = "meek exec " + OrbotApp.fileMeekclient.getCanonicalPath();
extraLines.append("ClientTransportPlugin" + ' ' + bridgeConfig).append('\n'); extraLines.append("ClientTransportPlugin" + ' ' + bridgeConfig).append('\n');
String[] meekBridge = String[] meekBridge =
@ -2181,14 +2147,14 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
if (entranceNodes.length() > 0 || exitNodes.length() > 0 || excludeNodes.length() > 0) if (entranceNodes.length() > 0 || exitNodes.length() > 0 || excludeNodes.length() > 0)
{ {
//only apply GeoIP if you need it //only apply GeoIP if you need it
File fileGeoIP = new File(appBinHome,GEOIP_ASSET_KEY); File fileGeoIP = new File(OrbotApp.appBinHome, GEOIP_ASSET_KEY);
File fileGeoIP6 = new File(appBinHome,GEOIP6_ASSET_KEY); File fileGeoIP6 = new File(OrbotApp.appBinHome, GEOIP6_ASSET_KEY);
try try
{ {
if ((!fileGeoIP.exists())) if ((!fileGeoIP.exists()))
{ {
TorResourceInstaller installer = new TorResourceInstaller(this, appBinHome); TorResourceInstaller installer = new TorResourceInstaller(this, OrbotApp.appBinHome);
boolean success = installer.installGeoIP(); boolean success = installer.installGeoIP();
} }
@ -2284,7 +2250,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
hsPort = Integer.parseInt(hsPortConfig.split(" ")[0]); 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); debug("Adding hidden service on port: " + hsPortConfig);
@ -2324,7 +2290,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
//using Google DNS for now as the public DNS server //using Google DNS for now as the public DNS server
private String writeDNSFile () throws IOException 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)); PrintWriter bw = new PrintWriter(new FileWriter(file));
bw.println("nameserver 8.8.8.8"); bw.println("nameserver 8.8.8.8");