From e5c2e1a040045781507fca48138d4bdaa672be10 Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Wed, 17 Jun 2015 11:00:48 -0400 Subject: [PATCH 1/6] when OrbotMainActivity starts, query TorService for current status If OrbotMainActivity gets killed while TorService is running, then when OrbotMainActivity starts again, it needs to get the current status from TorService to correctly represent things to the user. --- .../torproject/android/OrbotMainActivity.java | 6 +++- .../android/service/TorService.java | 28 +++++++++++++++---- .../android/service/TorServiceConstants.java | 1 + 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/org/torproject/android/OrbotMainActivity.java b/src/org/torproject/android/OrbotMainActivity.java index e13f55de..b1ea6353 100644 --- a/src/org/torproject/android/OrbotMainActivity.java +++ b/src/org/torproject/android/OrbotMainActivity.java @@ -589,7 +589,11 @@ public class OrbotMainActivity extends Activity } } } - + else if (action.equals(Intent.ACTION_MAIN)) + { + // OrbotMainActivity was restarted after being killed + sendIntentToService(TorServiceConstants.CMD_STATUS); + } updateStatus(""); setIntent(null); diff --git a/src/org/torproject/android/service/TorService.java b/src/org/torproject/android/service/TorService.java index f5fe6e1c..7830e5ef 100644 --- a/src/org/torproject/android/service/TorService.java +++ b/src/org/torproject/android/service/TorService.java @@ -16,6 +16,7 @@ import android.app.PendingIntent; import android.app.Service; import android.content.BroadcastReceiver; import android.content.Context; +import android.content.ContextWrapper; import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; @@ -346,6 +347,9 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon replyWithStatus(mIntent); startTor(); // stopTor() is called when the Service is destroyed + } else if (action.equals(CMD_STATUS)) { + Intent intent = getActionStatusIntent(mCurrentStatus); + sendBroadcastOnlyToOrbot(intent); } else if (action.equals(CMD_SIGNAL_HUP)) { requestTorRereadConfig(); } else if (action.equals(CMD_NEWNYM)) { @@ -1901,20 +1905,32 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon private void sendCallbackStatus(String currentStatus) { mCurrentStatus = currentStatus; - - Intent intent = new Intent(ACTION_STATUS); - intent.putExtra(EXTRA_STATUS, currentStatus); + Intent intent = getActionStatusIntent(currentStatus); // send for Orbot internals, using secure local broadcast - LocalBroadcastManager.getInstance(this).sendBroadcast(intent); + sendBroadcastOnlyToOrbot(intent); // send for any apps that are interested sendBroadcast(intent); } - + + /** + * Send a secure broadcast only to Orbot itself + * @see {@link ContextWrapper#sendBroadcast(Intent)} + * @see {@link LocalBroadcastManager} + */ + private boolean sendBroadcastOnlyToOrbot(Intent intent) { + return LocalBroadcastManager.getInstance(this).sendBroadcast(intent); + } + + private Intent getActionStatusIntent(String currentStatus) { + Intent intent = new Intent(ACTION_STATUS); + intent.putExtra(EXTRA_STATUS, currentStatus); + return intent; + } + /* * Another way to do this would be to use the Observer pattern by defining the * BroadcastReciever in the Android manifest. */ - private final BroadcastReceiver mNetworkStateReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { diff --git a/src/org/torproject/android/service/TorServiceConstants.java b/src/org/torproject/android/service/TorServiceConstants.java index adaa6740..fa26e107 100644 --- a/src/org/torproject/android/service/TorServiceConstants.java +++ b/src/org/torproject/android/service/TorServiceConstants.java @@ -119,6 +119,7 @@ public interface TorServiceConstants { // actions for internal command Intents public static final String CMD_SIGNAL_HUP = "signal_hup"; + public static final String CMD_STATUS = "status"; public static final String CMD_FLUSH = "flush"; public static final String CMD_NEWNYM = "newnym"; public static final String CMD_VPN = "vpn"; From c9e31020e06bf283653e5f96bbdf70fc6a50e355 Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Wed, 17 Jun 2015 11:13:35 -0400 Subject: [PATCH 2/6] prevent a status request from starting TorService If some internal bit of Orbot is requesting the status of TorService, it should not cause it to start. So only request status from TorService if it is running, otherwise keep status as OFF. the big imports change is because of the Android auto-formatter --- .../torproject/android/OrbotMainActivity.java | 43 ++++++++++++------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/src/org/torproject/android/OrbotMainActivity.java b/src/org/torproject/android/OrbotMainActivity.java index b1ea6353..d0d088ba 100644 --- a/src/org/torproject/android/OrbotMainActivity.java +++ b/src/org/torproject/android/OrbotMainActivity.java @@ -3,22 +3,10 @@ package org.torproject.android; -import java.io.UnsupportedEncodingException; -import java.net.URLDecoder; -import java.net.URLEncoder; -import java.text.NumberFormat; -import java.util.Locale; - -import org.torproject.android.service.TorService; -import org.torproject.android.service.TorServiceConstants; -import org.torproject.android.service.TorServiceUtils; -import org.torproject.android.settings.SettingsPreferences; -import org.torproject.android.ui.ImageProgressView; -import org.torproject.android.ui.PromoAppsActivity; -import org.torproject.android.ui.Rotate3dAnimation; - import android.annotation.TargetApi; import android.app.Activity; +import android.app.ActivityManager; +import android.app.ActivityManager.RunningServiceInfo; import android.app.AlertDialog; import android.app.Dialog; import android.content.BroadcastReceiver; @@ -63,6 +51,20 @@ import android.widget.ToggleButton; import com.google.zxing.integration.android.IntentIntegrator; import com.google.zxing.integration.android.IntentResult; +import org.torproject.android.service.TorService; +import org.torproject.android.service.TorServiceConstants; +import org.torproject.android.service.TorServiceUtils; +import org.torproject.android.settings.SettingsPreferences; +import org.torproject.android.ui.ImageProgressView; +import org.torproject.android.ui.PromoAppsActivity; +import org.torproject.android.ui.Rotate3dAnimation; + +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; +import java.net.URLEncoder; +import java.text.NumberFormat; +import java.util.Locale; + public class OrbotMainActivity extends Activity implements OrbotConstants, OnLongClickListener, OnTouchListener { @@ -592,7 +594,8 @@ public class OrbotMainActivity extends Activity else if (action.equals(Intent.ACTION_MAIN)) { // OrbotMainActivity was restarted after being killed - sendIntentToService(TorServiceConstants.CMD_STATUS); + if (isTorServiceRunning()) + sendIntentToService(TorServiceConstants.CMD_STATUS); } updateStatus(""); @@ -1129,6 +1132,16 @@ public class OrbotMainActivity extends Activity sendIntentToService(TorServiceConstants.ACTION_START); } + private boolean isTorServiceRunning() { + ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); + for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { + if (TorService.class.getName().equals(service.service.getClassName())) { + return true; + } + } + return false; + } + public boolean onLongClick(View view) { try { if (torStatus == TorServiceConstants.STATUS_OFF) { From adf7c09f0e78bc0ea6fc60cc6430fff7140beb95 Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Wed, 17 Jun 2015 11:15:31 -0400 Subject: [PATCH 3/6] remove delayed handling of Intents in OrbotMainActivity This is a leftover bit from the old structure, it should no longer be needed and it causes the status updates to be noticeably delayed so when OrbotMainActivity is started after being killed, it flashes OFF then ON. --- src/org/torproject/android/OrbotMainActivity.java | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/src/org/torproject/android/OrbotMainActivity.java b/src/org/torproject/android/OrbotMainActivity.java index d0d088ba..15af8b95 100644 --- a/src/org/torproject/android/OrbotMainActivity.java +++ b/src/org/torproject/android/OrbotMainActivity.java @@ -1010,18 +1010,7 @@ public class OrbotMainActivity extends Activity mBtnBridges.setChecked(Prefs.bridgesEnabled()); } - mStatusUpdateHandler.postDelayed(new Runnable () - { - public void run () - { - - handleIntents(); - - } - } - , 500); - - + handleIntents(); } AlertDialog aDialog = null; From 943edf094bfd559abc0d7359ce7728f3e322cd15 Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Wed, 17 Jun 2015 11:45:07 -0400 Subject: [PATCH 4/6] "Allow Background Starts" prefs also controls the old START_TOR Intent --- src/org/torproject/android/OrbotMainActivity.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/org/torproject/android/OrbotMainActivity.java b/src/org/torproject/android/OrbotMainActivity.java index 15af8b95..0bc0f7e9 100644 --- a/src/org/torproject/android/OrbotMainActivity.java +++ b/src/org/torproject/android/OrbotMainActivity.java @@ -554,9 +554,10 @@ public class OrbotMainActivity extends Activity .setNegativeButton("Deny", dialogClickListener).show(); return; //don't null the setIntent() as we need it later - } - else if (action.equals("org.torproject.android.START_TOR")) - { + } + else if (action.equals("org.torproject.android.START_TOR") + && Prefs.allowBackgroundStarts()) + { autoStartFromIntent = true; try { startTor(); @@ -569,7 +570,6 @@ public class OrbotMainActivity extends Activity } resultIntent.putExtra(TorServiceConstants.EXTRA_STATUS, torStatus); setResult(RESULT_OK, resultIntent); - finish(); } catch (RemoteException e) { e.printStackTrace(); } From a81c0001d6cfb764ba6e1cd2fca6e78c6487c5f3 Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Wed, 17 Jun 2015 12:06:23 -0400 Subject: [PATCH 5/6] 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"); From aa87e6712a34e6fe9629a93649aa01cee7be6d04 Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Wed, 17 Jun 2015 12:08:52 -0400 Subject: [PATCH 6/6] on start, check for running tor daemon, and if TorService stopped, then start If Orbot was killed when the tor daemon was running, the tor daemon will still be running when Orbot starts again. OrbotMainActivity then checks to see if tor daemon is running while TorService is stopped. If so, TorService is started so that the state of everything is in sync. --- .../torproject/android/OrbotMainActivity.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/org/torproject/android/OrbotMainActivity.java b/src/org/torproject/android/OrbotMainActivity.java index 0bc0f7e9..dd9427e0 100644 --- a/src/org/torproject/android/OrbotMainActivity.java +++ b/src/org/torproject/android/OrbotMainActivity.java @@ -59,6 +59,7 @@ import org.torproject.android.ui.ImageProgressView; import org.torproject.android.ui.PromoAppsActivity; import org.torproject.android.ui.Rotate3dAnimation; +import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.net.URLEncoder; @@ -1010,6 +1011,22 @@ public class OrbotMainActivity extends Activity mBtnBridges.setChecked(Prefs.bridgesEnabled()); } + /* + * Check if the tor daemon is running and if TorService is stopped. If + * so, automatically start TorService to represent it + */ + try { + if (TorServiceUtils.findProcessId(OrbotApp.fileTor.getAbsolutePath()) != -1 + && !isTorServiceRunning()) { + Log.i(TAG, "Found tor daemon without TorService: starting TorService"); + startTor(); + } + } catch (IOException e) { + e.printStackTrace(); + } catch (RemoteException e) { + e.printStackTrace(); + } + handleIntents(); }