handle logging native process Exceptions closer to the source

Instead of passing Exceptions through many layers only to log them, just
log them where they are thrown.  Keeps things neater.
This commit is contained in:
Hans-Christoph Steiner 2015-06-08 20:42:18 -04:00
parent ae83f011b2
commit 12d92f48f5
2 changed files with 40 additions and 50 deletions

View File

@ -348,11 +348,12 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
* @see android.app.Service#onStart(android.content.Intent, int) * @see android.app.Service#onStart(android.content.Intent, int)
*/ */
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
if (intent != null)
new Thread (new TorStarter(intent)).start();
else
Log.d(TAG, "Got null onStartCommand() intent");
new Thread (new TorStarter(intent)).start();
return Service.START_STICKY; return Service.START_STICKY;
} }
private class TorStarter implements Runnable private class TorStarter implements Runnable
@ -364,36 +365,27 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
mIntent = intent; mIntent = intent;
} }
public void run (){ public void run() {
try{ String action = mIntent.getAction();
//if this is a start on boot launch turn tor on
if (mIntent != null){ if (action != null) {
String action = mIntent.getAction(); if (action.equals(Intent.ACTION_BOOT_COMPLETED) || action.equals(CMD_START)) {
setTorProfile(STATUS_ON);
if (action!=null){ } else if (action.equals(CMD_STOP)) {
if(action.equals(Intent.ACTION_BOOT_COMPLETED)||action.equals(CMD_START)){ setTorProfile(STATUS_OFF);
setTorProfile(STATUS_ON); } else if (action.equals(CMD_NEWNYM)) {
}else if (action.equals(CMD_STOP)){ newIdentity();
setTorProfile(STATUS_OFF); } else if (action.equals(CMD_FLUSH)) {
}else if (action.equals(CMD_NEWNYM)){ flushTransparentProxyRules();
newIdentity(); } else if (action.equals(CMD_UPDATE)) {
}else if (action.equals(CMD_FLUSH)){ processSettings();
flushTransparentProxyRules(); } else if (action.equals(CMD_VPN)) {
}else if (action.equals(CMD_UPDATE)){ enableVpnProxy();
processSettings(); } else if (action.equals(CMD_VPN_CLEAR)) {
}else if (action.equals(CMD_VPN)){ clearVpnProxy();
enableVpnProxy(); } else {
} Log.w(TAG, "unhandled TorService Intent: " + action);
else if (action.equals(CMD_VPN_CLEAR)){
clearVpnProxy();
}
}
}else{
Log.d(TAG, "Got null onStartCommand() intent");
} }
}catch (Exception e){
Log.e(TAG,"error onBind",e);
} }
} }
} }
@ -879,18 +871,18 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
shellUser.close(); shellUser.close();
} }
private boolean flushTransparentProxyRules () throws Exception private boolean flushTransparentProxyRules () {
{
if (mHasRoot) if (mHasRoot)
{ {
if (mTransProxy == null) if (mTransProxy == null)
{
mTransProxy = new TorTransProxy(this, fileXtables); mTransProxy = new TorTransProxy(this, fileXtables);
try {
mTransProxy.flushTransproxyRules(this);
} catch (Exception e) {
e.printStackTrace();
return false;
} }
mTransProxy.flushTransproxyRules(this);
return true; return true;
} }

View File

@ -3,8 +3,6 @@ package org.torproject.android.service;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.concurrent.TimeoutException;
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.OrbotConstants; import org.torproject.android.OrbotConstants;
@ -12,7 +10,6 @@ import org.torproject.android.settings.TorifiedApp;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.util.Log;
public class TorTransProxy implements TorServiceConstants { public class TorTransProxy implements TorServiceConstants {
@ -335,7 +332,7 @@ public class TorTransProxy implements TorServiceConstants {
return code; return code;
}*/ }*/
public int setTransparentProxyingByApp(Context context, ArrayList<TorifiedApp> apps, boolean enableRule, Shell shell) throws Exception public int setTransparentProxyingByApp(Context context, ArrayList<TorifiedApp> apps, boolean enableRule, Shell shell)
{ {
String ipTablesPath = getIpTablesPath(context); String ipTablesPath = getIpTablesPath(context);
@ -439,10 +436,13 @@ public class TorTransProxy implements TorServiceConstants {
return lastExit; return lastExit;
} }
private int executeCommand (Shell shell, String cmdString) throws IOException, TimeoutException private int executeCommand (Shell shell, String cmdString) {
{
SimpleCommand cmd = new SimpleCommand(cmdString); SimpleCommand cmd = new SimpleCommand(cmdString);
shell.add(cmd); try {
shell.add(cmd);
} catch (IOException e) {
e.printStackTrace();
}
int exitCode = cmd.getExitCode(); int exitCode = cmd.getExitCode();
String output = cmd.getOutput(); String output = cmd.getOutput();
@ -524,8 +524,7 @@ public class TorTransProxy implements TorServiceConstants {
} }
public int dropAllIPv6Traffic (Context context, int appUid, boolean enableDrop, Shell shell) throws Exception public int dropAllIPv6Traffic (Context context, int appUid, boolean enableDrop, Shell shell) {
{
String action = " -A "; String action = " -A ";
String chain = "OUTPUT"; String chain = "OUTPUT";
@ -575,8 +574,7 @@ public class TorTransProxy implements TorServiceConstants {
return lastExit; return lastExit;
}*/ }*/
public int flushTransproxyRules (Context context) throws Exception public int flushTransproxyRules (Context context) throws IOException {
{
int exit = -1; int exit = -1;
String ipTablesPath = getIpTablesPath(context); String ipTablesPath = getIpTablesPath(context);