From 21c3bfb3c55a26f80914c52537360a9de390f99e Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Tue, 9 Jun 2015 14:27:39 -0400 Subject: [PATCH] use "SIGNAL HUP" to request Tor re-read its config The tor daemon supports both "SIGNAL HUP" via its control port or the UNIX signal `kill -HUP` via the terminal as a way to trigger the tor daemon to reload its config. This is needed for new bridges and hidden services. It is not necessary to restart everything to add those. https://stem.torproject.org/faq.html#how-do-i-reload-my-torrc --- .../torproject/android/OrbotMainActivity.java | 42 ++++--------------- .../android/service/TorService.java | 31 ++++++++++++-- .../android/service/TorServiceConstants.java | 1 + 3 files changed, 36 insertions(+), 38 deletions(-) diff --git a/src/org/torproject/android/OrbotMainActivity.java b/src/org/torproject/android/OrbotMainActivity.java index 7b0bb311..e588e9c0 100644 --- a/src/org/torproject/android/OrbotMainActivity.java +++ b/src/org/torproject/android/OrbotMainActivity.java @@ -447,7 +447,7 @@ public class OrbotMainActivity extends Activity implements OrbotConstants, OnLon //can happen on exit/shutdown } } - + private void doTorCheck () { @@ -479,13 +479,11 @@ public class OrbotMainActivity extends Activity implements OrbotConstants, OnLon if (onionHostname == null || onionHostname.length() == 0) { - stopTor(); - startTor(); - + requestTorRereadConfig(); + new Thread () { - - - public void run () + + public void run () { String onionHostname = mPrefs.getString("pref_hs_hostname",""); @@ -945,37 +943,13 @@ public class OrbotMainActivity extends Activity implements OrbotConstants, OnLon String bridgeList = Prefs.getBridgesList(); if (bridgeList != null && bridgeList.length() > 0) { - restartTor (); + requestTorRereadConfig (); } - } } - private void restartTor () - { - try - { - //do auto restart - stopTor (); - - mStatusUpdateHandler.postDelayed(new Runnable () { - public void run () - { - try - { - startTor(); - } - catch (Exception e) - { - Log.e(TAG,"can't start orbot",e); - } - } - }, 2000); - } - catch (Exception e) - { - Log.e(TAG,"can't stop orbot",e); - } + private void requestTorRereadConfig() { + sendIntentToService (TorServiceConstants.CMD_START); } public void promptStartVpnService () diff --git a/src/org/torproject/android/service/TorService.java b/src/org/torproject/android/service/TorService.java index 5884f62d..516b56e7 100644 --- a/src/org/torproject/android/service/TorService.java +++ b/src/org/torproject/android/service/TorService.java @@ -344,6 +344,8 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon if (action.equals(CMD_START)) { startTor(); // stopTor() is called when the Service is destroyed + } else if (action.equals(CMD_SIGNAL_HUP)) { + requestTorRereadConfig(); } else if (action.equals(CMD_NEWNYM)) { newIdentity(); } else if (action.equals(CMD_FLUSH)) { @@ -534,7 +536,28 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon } } + private void requestTorRereadConfig() { + try { + conn.signal("HUP"); + return; + } catch (IOException e) { + e.printStackTrace(); + } + // if that fails, try again using native utils + try { + killProcess(fileTor, "-1"); // this is -HUP + } catch (CannotKillException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + private void killProcess(File fileProcBin) throws IOException, CannotKillException { + killProcess(fileProcBin, "-9"); // this is -KILL + } + + private void killProcess(File fileProcBin, String signal) throws IOException, CannotKillException { int procId = -1; int killAttempts = 0; @@ -554,10 +577,10 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon } else { shell = Shell.startShell(); } - shell.add(new SimpleCommand("busybox killall " + fileProcBin.getName())); - shell.add(new SimpleCommand("toolbox kill -9 " + pidString)); - shell.add(new SimpleCommand("busybox kill -9 " + pidString)); - shell.add(new SimpleCommand("kill -9 " + pidString)); + shell.add(new SimpleCommand("busybox killall " + signal + " " + fileProcBin.getName())); + shell.add(new SimpleCommand("toolbox kill " + signal + " " + pidString)); + shell.add(new SimpleCommand("busybox kill " + signal + " " + pidString)); + shell.add(new SimpleCommand("kill " + signal + " " + pidString)); try { Thread.sleep(1000); } catch (InterruptedException e) { diff --git a/src/org/torproject/android/service/TorServiceConstants.java b/src/org/torproject/android/service/TorServiceConstants.java index 7a5a14a5..745d74f7 100644 --- a/src/org/torproject/android/service/TorServiceConstants.java +++ b/src/org/torproject/android/service/TorServiceConstants.java @@ -84,6 +84,7 @@ public interface TorServiceConstants { public static final int LOG_MSG = 4; public static final String CMD_START = "start"; + public static final String CMD_SIGNAL_HUP = "signal_hup"; public static final String CMD_FLUSH = "flush"; public static final String CMD_NEWNYM = "newnym"; public static final String CMD_VPN = "vpn";