make sure Tor start/stop work

- use Process instance instead of processID lookup
- reply on Tor's HALT command
This commit is contained in:
Nathan Freitas 2016-09-25 18:49:32 -04:00
parent 55e08b9de4
commit 7284a5e1fb
2 changed files with 41 additions and 42 deletions

View File

@ -156,16 +156,20 @@ public class OrbotMainActivity extends AppCompatActivity
new IntentFilter(TorServiceConstants.LOCAL_ACTION_LOG)); new IntentFilter(TorServiceConstants.LOCAL_ACTION_LOG));
} }
private void sendIntentToService(String action) { private void sendIntentToService(final String action) {
Intent torService = new Intent(this, TorService.class);
torService.setAction(action); Intent torService = new Intent(OrbotMainActivity.this, TorService.class);
startService(torService); torService.setAction(action);
startService(torService);
} }
private void stopTor() { private void stopTor() {
imgStatus.setImageResource(R.drawable.torstarting);
Intent torService = new Intent(this, TorService.class); imgStatus.setImageResource(R.drawable.torstarting);
Intent torService = new Intent(OrbotMainActivity.this, TorService.class);
stopService(torService); stopService(torService);
} }
/** /**
@ -1197,6 +1201,7 @@ public class OrbotMainActivity extends AppCompatActivity
*/ */
private void startTor() { private void startTor() {
sendIntentToService(TorServiceConstants.ACTION_START); sendIntentToService(TorServiceConstants.ACTION_START);
mTxtOrbotLog.setText("");
} }
/** /**

View File

@ -77,8 +77,6 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
import javax.net.ssl.SSLEngine;
public class TorService extends Service implements TorServiceConstants, OrbotConstants, EventHandler public class TorService extends Service implements TorServiceConstants, OrbotConstants, EventHandler
{ {
@ -89,7 +87,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
private TorControlConnection conn = null; private TorControlConnection conn = null;
private Socket torConnSocket = null; private Socket torConnSocket = null;
private int mLastProcessId = -1; private int mLastProcessId = -1;
private Process mProcPolipo;
private int mPortHTTP = HTTP_PROXY_PORT_DEFAULT; private int mPortHTTP = HTTP_PROXY_PORT_DEFAULT;
private int mPortSOCKS = SOCKS_PROXY_PORT_DEFAULT; private int mPortSOCKS = SOCKS_PROXY_PORT_DEFAULT;
@ -98,8 +96,6 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
private static final int TRANSPROXY_NOTIFY_ID = 2; private static final int TRANSPROXY_NOTIFY_ID = 2;
private static final int ERROR_NOTIFY_ID = 3; private static final int ERROR_NOTIFY_ID = 3;
private static final int HS_NOTIFY_ID = 4; private static final int HS_NOTIFY_ID = 4;
private static final int MAX_START_TRIES = 3;
private ArrayList<String> configBuffer = null; private ArrayList<String> configBuffer = null;
private ArrayList<String> resetBuffer = null; private ArrayList<String> resetBuffer = null;
@ -389,7 +385,18 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
}**/ }**/
private void stopTor() { private void stopTor ()
{
mExecutor.execute(new Runnable ()
{
public void run ()
{
stopTorAsync();
}
});
}
private void stopTorAsync () {
Log.i("TorService", "stopTor"); Log.i("TorService", "stopTor");
try { try {
sendCallbackStatus(STATUS_STOPPING); sendCallbackStatus(STATUS_STOPPING);
@ -500,7 +507,16 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
conn = null; conn = null;
} }
if (mProcPolipo != null)
{
mProcPolipo.destroy();
int exitValue = mProcPolipo.waitFor();
logNotice("Polipo exited with value: " + exitValue);
}
// 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 {
TorServiceUtils.killProcess(fileObfsclient); TorServiceUtils.killProcess(fileObfsclient);
@ -523,6 +539,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
Log.w(OrbotConstants.TAG,"could not kill tor",e); Log.w(OrbotConstants.TAG,"could not kill tor",e);
cannotKillFile = fileTor; cannotKillFile = fileTor;
} }
*/
} }
private void requestTorRereadConfig() { private void requestTorRereadConfig() {
@ -1127,40 +1144,17 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
{ {
logNotice( "Starting polipo process"); logNotice( "Starting polipo process");
int polipoProcId = TorServiceUtils.findProcessId(filePolipo.getCanonicalPath());
StringBuilder log = null; updatePolipoConfig();
int attempts = 0;
if (polipoProcId == -1)
{
log = new StringBuilder();
updatePolipoConfig();
String polipoConfigPath = new File(appBinHome, POLIPOCONFIG_ASSET_KEY).getCanonicalPath();
String cmd = (filePolipo.getCanonicalPath() + " -c " + polipoConfigPath + " &");
Process proc = exec(cmd,false); String polipoConfigPath = new File(appBinHome, POLIPOCONFIG_ASSET_KEY).getCanonicalPath();
String cmd = (filePolipo.getCanonicalPath() + " -c " + polipoConfigPath);
//wait one second to make sure it has started up mProcPolipo = exec(cmd,false);
Thread.sleep(1000);
sendCallbackLogMessage(getString(R.string.privoxy_is_running_on_port_) + mPortHTTP);
while ((polipoProcId = TorServiceUtils.findProcessId(filePolipo.getCanonicalPath())) == -1 && attempts < MAX_START_TRIES)
{
logNotice("Couldn't find Polipo process... retrying...\n" + log);
Thread.sleep(3000);
attempts++;
}
logNotice(log.toString());
}
sendCallbackLogMessage(getString(R.string.privoxy_is_running_on_port_) + mPortHTTP); logNotice("Polipo is running");
logNotice("Polipo process id=" + polipoProcId);
} }