improved logging and exit code reporting for transproxy

This commit is contained in:
Nathan Freitas 2014-04-09 09:12:00 -04:00
parent f8532715c0
commit 831a52a7e9
2 changed files with 78 additions and 46 deletions

View File

@ -274,15 +274,11 @@ public class TorService extends Service implements TorServiceConstants, TorConst
appBinHome = getDir(DIRECTORY_TOR_BINARY,Application.MODE_PRIVATE); appBinHome = getDir(DIRECTORY_TOR_BINARY,Application.MODE_PRIVATE);
appCacheHome = getDir(DIRECTORY_TOR_DATA,Application.MODE_PRIVATE); appCacheHome = getDir(DIRECTORY_TOR_DATA,Application.MODE_PRIVATE);
IntentFilter mNetworkStateFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); IntentFilter mNetworkStateFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(mNetworkStateReceiver , mNetworkStateFilter); registerReceiver(mNetworkStateReceiver , mNetworkStateFilter);
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (intent != null && intent.getAction()!=null && intent.getAction().equals("onboot")) if (intent != null && intent.getAction()!=null && intent.getAction().equals("onboot"))
{ {
@ -293,6 +289,20 @@ public class TorService extends Service implements TorServiceConstants, TorConst
setTorProfile(PROFILE_ON); setTorProfile(PROFILE_ON);
} }
} }
else if (intent == null)
{
try {
initBinaries();
findExistingProc () ;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// We want this service to continue running until it is explicitly // We want this service to continue running until it is explicitly
// stopped, so return sticky. // stopped, so return sticky.

View File

@ -1,7 +1,9 @@
package org.torproject.android.service; package org.torproject.android.service;
import java.io.File; import java.io.File;
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;
@ -286,6 +288,7 @@ public class TorTransProxy implements TorServiceConstants {
//reset script //reset script
Shell shell = Shell.startRootShell(); Shell shell = Shell.startRootShell();
int lastExit = -1;
//build up array of shell cmds to execute under one root context //build up array of shell cmds to execute under one root context
for (TorifiedApp tApp:apps) for (TorifiedApp tApp:apps)
@ -312,7 +315,7 @@ public class TorTransProxy implements TorServiceConstants {
script.append(" -j REDIRECT --to-ports "); script.append(" -j REDIRECT --to-ports ");
script.append(TOR_TRANSPROXY_PORT); script.append(TOR_TRANSPROXY_PORT);
shell.add(new SimpleCommand(script.toString())); executeCommand (shell, script.toString());
script = new StringBuilder(); script = new StringBuilder();
// Same for DNS // Same for DNS
@ -326,9 +329,10 @@ public class TorTransProxy implements TorServiceConstants {
script.append(" -j REDIRECT --to-ports "); script.append(" -j REDIRECT --to-ports ");
script.append(TOR_DNS_PORT); script.append(TOR_DNS_PORT);
shell.add(new SimpleCommand(script.toString())); executeCommand (shell, script.toString());
script = new StringBuilder(); script = new StringBuilder();
int[] ports = {TOR_DNS_PORT,TOR_TRANSPROXY_PORT,PORT_SOCKS,PORT_HTTP}; int[] ports = {TOR_DNS_PORT,TOR_TRANSPROXY_PORT,PORT_SOCKS,PORT_HTTP};
for (int port : ports) for (int port : ports)
@ -345,9 +349,10 @@ public class TorTransProxy implements TorServiceConstants {
script.append(port); script.append(port);
script.append(" -j ACCEPT"); script.append(" -j ACCEPT");
shell.add(new SimpleCommand(script.toString())); executeCommand (shell, script.toString());
script = new StringBuilder(); script = new StringBuilder();
} }
// Allow loopback // Allow loopback
@ -360,9 +365,10 @@ public class TorTransProxy implements TorServiceConstants {
script.append(" -o lo"); script.append(" -o lo");
script.append(" -j ACCEPT"); script.append(" -j ACCEPT");
shell.add(new SimpleCommand(script.toString())); executeCommand (shell, script.toString());
script = new StringBuilder(); script = new StringBuilder();
// Reject all other outbound TCP packets // Reject all other outbound TCP packets
script.append(ipTablesPath); script.append(ipTablesPath);
script.append(" -t filter"); script.append(" -t filter");
@ -373,9 +379,10 @@ public class TorTransProxy implements TorServiceConstants {
script.append(" ! -d 127.0.0.1"); //allow access to localhost script.append(" ! -d 127.0.0.1"); //allow access to localhost
script.append(" -j REJECT"); script.append(" -j REJECT");
shell.add(new SimpleCommand(script.toString())); executeCommand (shell, script.toString());
script = new StringBuilder(); script = new StringBuilder();
// Reject all other outbound UDP packets // Reject all other outbound UDP packets
script.append(ipTablesPath); script.append(ipTablesPath);
script.append(" -t filter"); script.append(" -t filter");
@ -386,7 +393,9 @@ public class TorTransProxy implements TorServiceConstants {
script.append(" ! -d 127.0.0.1"); //allow access to localhost script.append(" ! -d 127.0.0.1"); //allow access to localhost
script.append(" -j REJECT"); script.append(" -j REJECT");
shell.add(new SimpleCommand(script.toString())); lastExit = executeCommand (shell, script.toString());
script = new StringBuilder();
} }
} }
@ -395,9 +404,19 @@ public class TorTransProxy implements TorServiceConstants {
shell.close(); shell.close();
return 1; return lastExit;
} }
private int executeCommand (Shell shell, String cmdString) throws IOException, TimeoutException
{
SimpleCommand cmd = new SimpleCommand(cmdString);
shell.add(cmd);
cmd.waitForFinish();
logMessage(cmdString + "; exit=" + cmd.getExitCode() + ";output=" + cmd.getOutput());
return cmd.getExitCode();
}
public int enableTetheringRules (Context context) throws Exception public int enableTetheringRules (Context context) throws Exception
{ {
@ -410,6 +429,8 @@ public class TorTransProxy implements TorServiceConstants {
Shell shell = Shell.startRootShell(); Shell shell = Shell.startRootShell();
int lastExit = -1;
for (int i = 0; i < hwinterfaces.length; i++) for (int i = 0; i < hwinterfaces.length; i++)
{ {
@ -420,7 +441,9 @@ public class TorTransProxy implements TorServiceConstants {
script.append(" -p udp --dport 53 -j REDIRECT --to-ports "); script.append(" -p udp --dport 53 -j REDIRECT --to-ports ");
script.append(TOR_DNS_PORT); script.append(TOR_DNS_PORT);
shell.add(new SimpleCommand(script.toString())); executeCommand (shell, script.toString());
script = new StringBuilder();
script = new StringBuilder(); script = new StringBuilder();
script.append(ipTablesPath); script.append(ipTablesPath);
@ -429,22 +452,22 @@ public class TorTransProxy implements TorServiceConstants {
script.append(" -p tcp -j REDIRECT --to-ports "); script.append(" -p tcp -j REDIRECT --to-ports ");
script.append(TOR_TRANSPROXY_PORT); script.append(TOR_TRANSPROXY_PORT);
shell.add(new SimpleCommand(script.toString())); lastExit = executeCommand (shell, script.toString());
script = new StringBuilder();
} }
shell.close(); shell.close();
return 0; return lastExit;
} }
private void logMessage (String msg) private void logMessage (String msg)
{ {
if (mTorService != null) if (mTorService != null)
mTorService.logMessage(msg); mTorService.logMessage(msg);
else
Log.w(TorConstants.TAG,msg);
} }
public int clearTransparentProxyingAll(Context context) throws Exception public int clearTransparentProxyingAll(Context context) throws Exception
@ -463,7 +486,9 @@ public class TorTransProxy implements TorServiceConstants {
script.append(" -t nat"); script.append(" -t nat");
script.append(" -F ").append(chainName); //delete previous user-defined chain script.append(" -F ").append(chainName); //delete previous user-defined chain
shell.add(new SimpleCommand(script.toString())); executeCommand (shell, script.toString());
script = new StringBuilder();
script = new StringBuilder(); script = new StringBuilder();
@ -471,15 +496,11 @@ public class TorTransProxy implements TorServiceConstants {
script.append(" -t filter"); script.append(" -t filter");
script.append(" -F ").append(chainName); //delete previous user-defined chain script.append(" -F ").append(chainName); //delete previous user-defined chain
SimpleCommand cmd = new SimpleCommand(script.toString()); int lastExit = executeCommand (shell, script.toString());
shell.add(cmd);
int exitCode = cmd.getExitCode();
shell.close(); shell.close();
return exitCode; return lastExit;
} }
public int fixTransproxyLeak (Context context) throws Exception public int fixTransproxyLeak (Context context) throws Exception
@ -491,16 +512,20 @@ public class TorTransProxy implements TorServiceConstants {
StringBuilder script = new StringBuilder(); StringBuilder script = new StringBuilder();
script.append(ipTablesPath); script.append(ipTablesPath);
script.append(" -I OUTPUT ! -o lo ! -d 127.0.0.1 ! -s 127.0.0.1 -p tcp -m tcp --tcp-flags ACK,FIN ACK,FIN -j DROP"); script.append(" -I OUTPUT ! -o lo ! -d 127.0.0.1 ! -s 127.0.0.1 -p tcp -m tcp --tcp-flags ACK,FIN ACK,FIN -j DROP");
shell.add(new SimpleCommand(script.toString()));
executeCommand (shell, script.toString());
script = new StringBuilder();
script = new StringBuilder(); script = new StringBuilder();
script.append(ipTablesPath); script.append(ipTablesPath);
script.append(" -I OUTPUT ! -o lo ! -d 127.0.0.1 ! -s 127.0.0.1 -p tcp -m tcp --tcp-flags ACK,RST ACK,RST -j DROP"); script.append(" -I OUTPUT ! -o lo ! -d 127.0.0.1 ! -s 127.0.0.1 -p tcp -m tcp --tcp-flags ACK,RST ACK,RST -j DROP");
shell.add(new SimpleCommand(script.toString()));
int lastExit = executeCommand (shell, script.toString());
script = new StringBuilder();
shell.close(); shell.close();
return 1; return lastExit;
} }
@ -528,9 +553,10 @@ public class TorTransProxy implements TorServiceConstants {
script.append(torUid); script.append(torUid);
script.append(" -j ACCEPT"); script.append(" -j ACCEPT");
shell.add(new SimpleCommand(script.toString())); executeCommand (shell, script.toString());
script = new StringBuilder(); script = new StringBuilder();
// Set up port redirection // Set up port redirection
script.append(ipTablesPath); script.append(ipTablesPath);
script.append(" -t nat"); script.append(" -t nat");
@ -543,7 +569,7 @@ public class TorTransProxy implements TorServiceConstants {
script.append(" -j REDIRECT --to-ports "); script.append(" -j REDIRECT --to-ports ");
script.append(TOR_TRANSPROXY_PORT); script.append(TOR_TRANSPROXY_PORT);
shell.add(new SimpleCommand(script.toString())); executeCommand (shell, script.toString());
script = new StringBuilder(); script = new StringBuilder();
// Same for DNS // Same for DNS
@ -559,7 +585,7 @@ public class TorTransProxy implements TorServiceConstants {
script.append(" -j REDIRECT --to-ports "); script.append(" -j REDIRECT --to-ports ");
script.append(TOR_DNS_PORT); script.append(TOR_DNS_PORT);
shell.add(new SimpleCommand(script.toString())); executeCommand (shell, script.toString());
script = new StringBuilder(); script = new StringBuilder();
@ -591,7 +617,7 @@ public class TorTransProxy implements TorServiceConstants {
script.append(" -o lo"); script.append(" -o lo");
script.append(" -j ACCEPT"); script.append(" -j ACCEPT");
shell.add(new SimpleCommand(script.toString())); executeCommand (shell, script.toString());
script = new StringBuilder(); script = new StringBuilder();
@ -608,7 +634,7 @@ public class TorTransProxy implements TorServiceConstants {
script.append(" --log-prefix='ORBOT_DNSLEAK_PROTECTION'"); script.append(" --log-prefix='ORBOT_DNSLEAK_PROTECTION'");
script.append(" --log-uid"); script.append(" --log-uid");
shell.add(new SimpleCommand(script.toString())); executeCommand (shell, script.toString());
script = new StringBuilder(); script = new StringBuilder();
script.append(ipTablesPath); script.append(ipTablesPath);
@ -619,7 +645,7 @@ public class TorTransProxy implements TorServiceConstants {
script.append(" --log-prefix='ORBOT_TCPLEAK_PROTECTION'"); script.append(" --log-prefix='ORBOT_TCPLEAK_PROTECTION'");
script.append(" --log-uid"); script.append(" --log-uid");
shell.add(new SimpleCommand(script.toString())); executeCommand (shell, script.toString());
script = new StringBuilder(); script = new StringBuilder();
} }
@ -634,7 +660,7 @@ public class TorTransProxy implements TorServiceConstants {
script.append(" ! -d 127.0.0.1"); //allow access to localhost script.append(" ! -d 127.0.0.1"); //allow access to localhost
script.append(" -j REJECT"); script.append(" -j REJECT");
shell.add(new SimpleCommand(script.toString())); executeCommand (shell, script.toString());
script = new StringBuilder(); script = new StringBuilder();
// Reject all other outbound UDP packets // Reject all other outbound UDP packets
@ -647,16 +673,12 @@ public class TorTransProxy implements TorServiceConstants {
script.append(" ! -d 127.0.0.1"); //allow access to localhost script.append(" ! -d 127.0.0.1"); //allow access to localhost
script.append(" -j REJECT"); script.append(" -j REJECT");
SimpleCommand cmd = new SimpleCommand(script.toString()); int lastExit = executeCommand (shell, script.toString());
shell.add(cmd);
fixTransproxyLeak (context); fixTransproxyLeak (context);
int exitCode = cmd.getExitCode();
shell.close(); shell.close();
return exitCode; return lastExit;
} }