cleanup notifications; fixes for binary checking

This commit is contained in:
Nathan Freitas 2012-01-16 22:56:50 -05:00
parent d35fb1bda2
commit 512f3d6dbd
1 changed files with 115 additions and 115 deletions

View File

@ -55,6 +55,10 @@ public class TorService extends Service implements TorServiceConstants, TorConst
private static TorService _torInstance; private static TorService _torInstance;
private static final int NOTIFY_ID = 1; private static final int NOTIFY_ID = 1;
private static final int TRANSPROXY_NOTIFY_ID = 2;
private static final int ERROR_NOTIFY_ID = 3;
private static final int HS_NOTIFY_ID = 3;
private static final int MAX_START_TRIES = 3; private static final int MAX_START_TRIES = 3;
@ -66,15 +70,13 @@ public class TorService extends Service implements TorServiceConstants, TorConst
private File appBinHome; private File appBinHome;
private File appDataHome; private File appDataHome;
private String torBinaryPath; private File fileTor;
private String privoxyPath; private File filePrivoxy;
/** Called when the activity is first created. */ /** Called when the activity is first created. */
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
logMessage("serviced created");
} }
public static void logMessage(String msg) public static void logMessage(String msg)
@ -92,7 +94,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
private boolean findExistingProc () private boolean findExistingProc ()
{ {
int procId = TorServiceUtils.findProcessId(torBinaryPath); int procId = TorServiceUtils.findProcessId(fileTor.getAbsolutePath());
if (procId != -1) if (procId != -1)
{ {
@ -200,7 +202,27 @@ public class TorService extends Service implements TorServiceConstants, TorConst
public void onRebind(Intent intent) { public void onRebind(Intent intent) {
super.onRebind(intent); super.onRebind(intent);
initTorPaths();
if (!fileTor.exists())
{
new Thread ()
{
public void run ()
{
try {
checkTorBinaries (false);
} catch (Exception e) {
logNotice("unable to find tor binaries: " + e.getMessage());
showToolbarNotification(getString(R.string.error_installing_binares), ERROR_NOTIFY_ID, R.drawable.tornotificationerr, -1);
Log.e(TAG, "error checking tor binaries", e);
}
}
}.start();
}
} }
@ -212,7 +234,9 @@ public class TorService extends Service implements TorServiceConstants, TorConst
_torInstance = this; _torInstance = this;
Thread thread = new Thread () initTorPaths();
new Thread ()
{ {
public void run () public void run ()
@ -222,14 +246,14 @@ public class TorService extends Service implements TorServiceConstants, TorConst
} catch (Exception e) { } catch (Exception e) {
logNotice("unable to find tor binaries: " + e.getMessage()); logNotice("unable to find tor binaries: " + e.getMessage());
showToolbarNotification(getString(R.string.error_installing_binares), NOTIFY_ID, R.drawable.tornotificationerr, -1); showToolbarNotification(getString(R.string.error_installing_binares), ERROR_NOTIFY_ID, R.drawable.tornotificationerr, -1);
Log.e(TAG, "error checking tor binaries", e); Log.e(TAG, "error checking tor binaries", e);
} }
} }
}; }.start();
thread.start();
if (intent.getAction()!=null && intent.getAction().equals("onboot")) if (intent.getAction()!=null && intent.getAction().equals("onboot"))
{ {
@ -260,7 +284,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
catch (Exception e) catch (Exception e)
{ {
currentStatus = STATUS_OFF; currentStatus = STATUS_OFF;
this.showToolbarNotification(getString(R.string.status_disabled), NOTIFY_ID, R.drawable.tornotificationerr, -1); this.showToolbarNotification(getString(R.string.status_disabled), ERROR_NOTIFY_ID, R.drawable.tornotificationerr, -1);
Log.d(TAG,"Unable to start Tor: " + e.getMessage(),e); Log.d(TAG,"Unable to start Tor: " + e.getMessage(),e);
} }
} }
@ -290,7 +314,6 @@ public class TorService extends Service implements TorServiceConstants, TorConst
clearNotifications(); clearNotifications();
//showToolbarNotification (getString(R.string.status_disabled),NOTIFY_ID,R.drawable.tornotificationoff);
sendCallbackStatusMessage(getString(R.string.status_disabled)); sendCallbackStatusMessage(getString(R.string.status_disabled));
disableTransparentProxy(); disableTransparentProxy();
@ -344,7 +367,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
{ {
try { try {
String onionHostname = Utils.readString(new FileInputStream(file)); String onionHostname = Utils.readString(new FileInputStream(file));
showToolbarNotification(getString(R.string.hidden_service_on) + ' ' + onionHostname, NOTIFY_ID, R.drawable.tornotification, Notification.FLAG_ONGOING_EVENT); showToolbarNotification(getString(R.string.hidden_service_on) + ' ' + onionHostname, HS_NOTIFY_ID, R.drawable.tornotification, Notification.FLAG_ONGOING_EVENT);
Editor pEdit = prefs.edit(); Editor pEdit = prefs.edit();
pEdit.putString("pref_hs_hostname",onionHostname); pEdit.putString("pref_hs_hostname",onionHostname);
pEdit.commit(); pEdit.commit();
@ -352,13 +375,13 @@ public class TorService extends Service implements TorServiceConstants, TorConst
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
logException("unable to read onion hostname file",e); logException("unable to read onion hostname file",e);
showToolbarNotification(getString(R.string.unable_to_read_hidden_service_name), NOTIFY_ID, R.drawable.tornotificationerr, -1); showToolbarNotification(getString(R.string.unable_to_read_hidden_service_name), ERROR_NOTIFY_ID, R.drawable.tornotificationerr, -1);
return; return;
} }
} }
else else
{ {
showToolbarNotification(getString(R.string.unable_to_read_hidden_service_name), NOTIFY_ID, R.drawable.tornotificationerr, -1); showToolbarNotification(getString(R.string.unable_to_read_hidden_service_name), HS_NOTIFY_ID, R.drawable.tornotificationerr, -1);
} }
@ -392,7 +415,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
conn = null; conn = null;
} }
while ((procId = TorServiceUtils.findProcessId(torBinaryPath)) != -1) while ((procId = TorServiceUtils.findProcessId(fileTor.getAbsolutePath())) != -1)
{ {
logNotice("Found Tor PID=" + procId + " - killing now..."); logNotice("Found Tor PID=" + procId + " - killing now...");
@ -403,7 +426,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
catch (Exception e){} catch (Exception e){}
} }
while ((procId = TorServiceUtils.findProcessId(privoxyPath)) != -1) while ((procId = TorServiceUtils.findProcessId(filePrivoxy.getAbsolutePath())) != -1)
{ {
logNotice("Found Privoxy PID=" + procId + " - killing now..."); logNotice("Found Privoxy PID=" + procId + " - killing now...");
@ -427,19 +450,24 @@ public class TorService extends Service implements TorServiceConstants, TorConst
} }
} }
private void initTorPaths ()
private boolean checkTorBinaries (boolean forceInstall) throws Exception
{ {
//android.os.Debug.waitForDebugger();
//check and install iptables
TorBinaryInstaller.assertIpTablesBinaries(this, true);
appBinHome = getDir("bin",0); appBinHome = getDir("bin",0);
appDataHome = getCacheDir(); appDataHome = getCacheDir();
File fileTor = new File(appBinHome, TOR_BINARY_ASSET_KEY); fileTor = new File(appBinHome, TOR_BINARY_ASSET_KEY);
File filePrivoxy = new File(appBinHome, PRIVOXY_ASSET_KEY); filePrivoxy = new File(appBinHome, PRIVOXY_ASSET_KEY);
}
public boolean checkTorBinaries (boolean forceInstall) throws Exception
{
//check and install iptables
TorBinaryInstaller.assertIpTablesBinaries(this, true);
initTorPaths();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String currTorBinary = prefs.getString(TorServiceConstants.PREF_BINARY_TOR_VERSION_INSTALLED, null); String currTorBinary = prefs.getString(TorServiceConstants.PREF_BINARY_TOR_VERSION_INSTALLED, null);
@ -495,8 +523,6 @@ public class TorService extends Service implements TorServiceConstants, TorConst
//showToolbarNotification(getString(R.string.status_install_success), NOTIFY_ID, R.drawable.tornotification); //showToolbarNotification(getString(R.string.status_install_success), NOTIFY_ID, R.drawable.tornotification);
torBinaryPath = fileTor.getAbsolutePath();
privoxyPath = filePrivoxy.getAbsolutePath();
} }
else else
{ {
@ -509,24 +535,15 @@ public class TorService extends Service implements TorServiceConstants, TorConst
} }
} }
else
{
logNotice("Found Tor binary: " + torBinaryPath);
logNotice("Found Privoxy binary: " + privoxyPath);
torBinaryPath = fileTor.getAbsolutePath();
privoxyPath = filePrivoxy.getAbsolutePath();
}
StringBuilder log = new StringBuilder (); StringBuilder log = new StringBuilder ();
logNotice("(re)Setting permission on Tor binary"); logNotice("(re)Setting permission on Tor binary");
String[] cmd1 = {SHELL_CMD_CHMOD + ' ' + CHMOD_EXE_VALUE + ' ' + torBinaryPath}; String[] cmd1 = {SHELL_CMD_CHMOD + ' ' + CHMOD_EXE_VALUE + ' ' + fileTor.getAbsolutePath()};
TorServiceUtils.doShellCommand(cmd1, log, false, true); TorServiceUtils.doShellCommand(cmd1, log, false, true);
logNotice("(re)Setting permission on Privoxy binary"); logNotice("(re)Setting permission on Privoxy binary");
String[] cmd2 = {SHELL_CMD_CHMOD + ' ' + CHMOD_EXE_VALUE + ' ' + privoxyPath}; String[] cmd2 = {SHELL_CMD_CHMOD + ' ' + CHMOD_EXE_VALUE + ' ' + filePrivoxy.getAbsolutePath()};
TorServiceUtils.doShellCommand(cmd2, log, false, true); TorServiceUtils.doShellCommand(cmd2, log, false, true);
@ -535,7 +552,9 @@ public class TorService extends Service implements TorServiceConstants, TorConst
public void initTor () throws Exception public void initTor () throws Exception
{ {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
ENABLE_DEBUG_LOG = prefs.getBoolean("pref_enable_logging",false);
Log.i(TAG,"debug logging:" + ENABLE_DEBUG_LOG);
currentStatus = STATUS_CONNECTING; currentStatus = STATUS_CONNECTING;
@ -593,7 +612,8 @@ public class TorService extends Service implements TorServiceConstants, TorConst
if(transProxyPortFallback) if(transProxyPortFallback)
{ {
showAlert(getString(R.string.status), getString(R.string.setting_up_port_based_transparent_proxying_));
showToolbarNotification(getString(R.string.setting_up_port_based_transparent_proxying_), TRANSPROXY_NOTIFY_ID, R.drawable.tornotification, -1);
StringTokenizer st = new StringTokenizer(portProxyList, ","); StringTokenizer st = new StringTokenizer(portProxyList, ",");
int status = code; int status = code;
while (st.hasMoreTokens()) while (st.hasMoreTokens())
@ -607,12 +627,14 @@ public class TorService extends Service implements TorServiceConstants, TorConst
{ {
if(transProxyAll) if(transProxyAll)
{ {
showAlert(getString(R.string.status), getString(R.string.setting_up_full_transparent_proxying_)); showToolbarNotification(getString(R.string.setting_up_full_transparent_proxying_), TRANSPROXY_NOTIFY_ID, R.drawable.tornotification, -1);
code = ttProxy.setTransparentProxyingAll(this); code = ttProxy.setTransparentProxyingAll(this);
} }
else else
{ {
showAlert(getString(R.string.status), getString(R.string.setting_up_app_based_transparent_proxying_)); showToolbarNotification(getString(R.string.setting_up_app_based_transparent_proxying_), TRANSPROXY_NOTIFY_ID, R.drawable.tornotification, -1);
code = ttProxy.setTransparentProxyingByApp(this,AppManager.getApps(this)); code = ttProxy.setTransparentProxyingByApp(this,AppManager.getApps(this));
} }
@ -622,13 +644,11 @@ public class TorService extends Service implements TorServiceConstants, TorConst
if (code == 0) if (code == 0)
{ {
showAlert(getString(R.string.status), getString(R.string.transparent_proxying_enabled)); showToolbarNotification(getString(R.string.transparent_proxying_enabled), TRANSPROXY_NOTIFY_ID, R.drawable.tornotification, -1);
if (transProxyTethering) if (transProxyTethering)
{ {
showAlert(getString(R.string.status), getString(R.string.transproxy_enabled_for_tethering_)); showToolbarNotification(getString(R.string.transproxy_enabled_for_tethering_), TRANSPROXY_NOTIFY_ID, R.drawable.tornotification, -1);
ttProxy.enableTetheringRules(this); ttProxy.enableTetheringRules(this);
@ -636,7 +656,8 @@ public class TorService extends Service implements TorServiceConstants, TorConst
} }
else else
{ {
showAlert(getString(R.string.status), getString(R.string.warning_error_starting_transparent_proxying_)); showToolbarNotification(getString(R.string.warning_error_starting_transparent_proxying_), TRANSPROXY_NOTIFY_ID, R.drawable.tornotification, -1);
} }
return true; return true;
@ -666,7 +687,8 @@ public class TorService extends Service implements TorServiceConstants, TorConst
new TorTransProxy().flushIptables(this); new TorTransProxy().flushIptables(this);
showAlert(getString(R.string.status), getString(R.string.transproxy_rules_cleared)); showToolbarNotification(getString(R.string.transproxy_rules_cleared), TRANSPROXY_NOTIFY_ID, R.drawable.tornotification, -1);
return true; return true;
} }
@ -689,7 +711,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
torrcPath = new File(appBinHome, TORRC_TETHER_KEY).getAbsolutePath(); torrcPath = new File(appBinHome, TORRC_TETHER_KEY).getAbsolutePath();
} }
String[] torCmd = {torBinaryPath + " DataDirectory " + appDataHome.getAbsolutePath() + " -f " + torrcPath + " || exit\n"}; String[] torCmd = {fileTor.getAbsolutePath() + " DataDirectory " + appDataHome.getAbsolutePath() + " -f " + torrcPath + " || exit\n"};
boolean runAsRootFalse = false; boolean runAsRootFalse = false;
boolean waitForProcess = false; boolean waitForProcess = false;
@ -710,18 +732,19 @@ public class TorService extends Service implements TorServiceConstants, TorConst
Thread.sleep(torRetryWaitTimeMS); Thread.sleep(torRetryWaitTimeMS);
procId = TorServiceUtils.findProcessId(torBinaryPath); procId = TorServiceUtils.findProcessId(fileTor.getAbsolutePath());
logNotice("got tor proc id: " + procId); logNotice("got tor proc id: " + procId);
if (procId == -1) if (procId == -1)
{ {
sendCallbackStatusMessage(getString(R.string.couldn_t_start_tor_process_) + log.toString()); sendCallbackStatusMessage(getString(R.string.couldn_t_start_tor_process_));
Thread.sleep(torRetryWaitTimeMS); Thread.sleep(torRetryWaitTimeMS);
attempts++; attempts++;
} }
if (waitForProcess)
logNotice(log.toString()); logNotice(log.toString());
} }
@ -747,7 +770,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
logNotice( "Starting privoxy process"); logNotice( "Starting privoxy process");
int privoxyProcId = TorServiceUtils.findProcessId(privoxyPath); int privoxyProcId = TorServiceUtils.findProcessId(filePrivoxy.getAbsolutePath());
StringBuilder log = null; StringBuilder log = null;
@ -760,7 +783,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
String privoxyConfigPath = new File(appBinHome, PRIVOXYCONFIG_ASSET_KEY).getAbsolutePath(); String privoxyConfigPath = new File(appBinHome, PRIVOXYCONFIG_ASSET_KEY).getAbsolutePath();
String[] cmds = String[] cmds =
{ privoxyPath + " " + privoxyConfigPath + " &" }; { filePrivoxy.getAbsolutePath() + " " + privoxyConfigPath + " &" };
logNotice (cmds[0]); logNotice (cmds[0]);
@ -772,7 +795,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
//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 ((privoxyProcId = TorServiceUtils.findProcessId(privoxyPath)) == -1 && attempts < MAX_START_TRIES) while ((privoxyProcId = TorServiceUtils.findProcessId(filePrivoxy.getAbsolutePath())) == -1 && attempts < MAX_START_TRIES)
{ {
logNotice("Couldn't find Privoxy process... retrying...\n" + log); logNotice("Couldn't find Privoxy process... retrying...\n" + log);
Thread.sleep(3000); Thread.sleep(3000);
@ -981,12 +1004,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
} }
private void showAlert(String title, String msg) //showToolbarNotification(msg, TRANSPROXY_NOTIFY_ID, R.drawable.tornotification);
{
//showToolbarNotification(msg, NOTIFY_ID, R.drawable.tornotification);
sendCallbackStatusMessage(msg);
}
public void newDescriptors(List<String> orList) { public void newDescriptors(List<String> orList) {
@ -1072,35 +1090,17 @@ public class TorService extends Service implements TorServiceConstants, TorConst
} }
public IBinder onBind(Intent intent) { public IBinder onBind(Intent intent) {
// Select the interface to return. If your service only implements
// a single interface, you can just return it here without checking
// the Intent.
_torInstance = this; _torInstance = this;
initTorPaths();
/*
try
{
checkTorBinaries(false);
}
catch (Exception e)
{
logNotice("unable to find tor binaries: " + e.getMessage());
showToolbarNotification(e.getMessage(), NOTIFY_ID, R.drawable.tornotificationerr);
Log.d(TAG,"Unable to check for Tor binaries",e);
return null;
}*/
findExistingProc (); findExistingProc ();
if (ITorService.class.getName().equals(intent.getAction())) { if (ITorService.class.getName().equals(intent.getAction())) {
return mBinder; return mBinder;
} }
else
return null; return null;
} }
@ -1346,7 +1346,6 @@ public class TorService extends Service implements TorServiceConstants, TorConst
private boolean applyPreferences () throws RemoteException private boolean applyPreferences () throws RemoteException
{ {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
ENABLE_DEBUG_LOG = prefs.getBoolean("pref_enable_logging",false); ENABLE_DEBUG_LOG = prefs.getBoolean("pref_enable_logging",false);
Log.i(TAG,"debug logging:" + ENABLE_DEBUG_LOG); Log.i(TAG,"debug logging:" + ENABLE_DEBUG_LOG);
@ -1393,9 +1392,9 @@ public class TorService extends Service implements TorServiceConstants, TorConst
if (bridgeList == null || bridgeList.length() == 0) if (bridgeList == null || bridgeList.length() == 0)
{ {
String msgBridge = getString(R.string.bridge_requires_ip) +
showAlert(getString(R.string.bridge_error),getString(R.string.bridge_requires_ip) + getString(R.string.send_email_for_bridges);
getString(R.string.send_email_for_bridges)); showToolbarNotification(msgBridge, ERROR_NOTIFY_ID, R.drawable.tornotification, -1);
return false; return false;
@ -1445,7 +1444,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
} }
catch (Exception e) catch (Exception e)
{ {
showAlert(getString(R.string.error),getString(R.string.your_reachableaddresses_settings_caused_an_exception_)); showToolbarNotification (getString(R.string.your_reachableaddresses_settings_caused_an_exception_),ERROR_NOTIFY_ID,R.drawable.tornotificationon, Notification.FLAG_ONGOING_EVENT);
return false; return false;
} }
@ -1471,7 +1470,8 @@ public class TorService extends Service implements TorServiceConstants, TorConst
} }
catch (Exception e) catch (Exception e)
{ {
showAlert(getString(R.string.error),getString(R.string.your_relay_settings_caused_an_exception_)); showToolbarNotification (getString(R.string.your_relay_settings_caused_an_exception_),ERROR_NOTIFY_ID,R.drawable.tornotificationon, Notification.FLAG_ONGOING_EVENT);
return false; return false;
} }