Improved transproxy code for background service

svn:r24645
This commit is contained in:
Nathan Freitas 2011-04-17 06:04:27 +00:00
parent 79179e28d9
commit 0c60572ef6
6 changed files with 477 additions and 346 deletions

View File

@ -801,23 +801,29 @@ public final class Api {
boolean changed = false;
try {
// Check iptables_g1
File file = new File(ctx.getDir("bin",0), "iptables_g1");
File file = new File(ctx.getDir("bin",0), "iptables");
if ((!file.exists()) && isARMv6()) {
copyRawFile(ctx, R.raw.iptables_g1, file, "755");
changed = true;
}
// Check iptables_n1
file = new File(ctx.getDir("bin",0), "iptables_n1");
file = new File(ctx.getDir("bin",0), "iptables");
if ((!file.exists()) && (!isARMv6())) {
copyRawFile(ctx, R.raw.iptables_n1, file, "755");
changed = true;
}
// Check busybox
/*
file = new File(ctx.getDir("bin",0), "busybox_g1");
if (!file.exists()) {
copyRawFile(ctx, R.raw.busybox_g1, file, "755");
changed = true;
}
*/
if (changed) {
Toast.makeText(ctx, R.string.status_install_success, Toast.LENGTH_LONG).show();
}

View File

@ -37,6 +37,11 @@ interface ITorService {
* Set configuration
**/
boolean updateConfiguration (String name, String value, boolean saveToDisk);
/**
* Set configuration
**/
void processSettings();
/**
* Set configuration

File diff suppressed because it is too large Load Diff

View File

@ -6,8 +6,6 @@ public interface TorServiceConstants {
public final static String TAG = "ORBOT";
public static boolean LOG_OUTPUT_TO_DEBUG = true;
public final static String TOR_APP_USERNAME = "org.torproject.android";
public final static String ASSETS_BASE = "assets/";
@ -78,4 +76,9 @@ public interface TorServiceConstants {
public final static int PROFILE_OFF = -1;
public final static int PROFILE_ON = 1;
public static final int STATUS_MSG = 1;
public static final int ENABLE_TOR_MSG = 2;
public static final int DISABLE_TOR_MSG = 3;
public static final int LOG_MSG = 4;
}

View File

@ -34,22 +34,19 @@ public class TorServiceUtils implements TorServiceConstants {
} catch (IOException e) {
//this means that there is no root to be had (normally) so we won't log anything
TorService.logException("Error checking for root access",e);
}
catch (Exception e) {
Log.w(TAG,"Error checking for root access: " + e.getMessage());
TorService.logException("Error checking for root access",e);
//this means that there is no root to be had (normally)
}
logNotice("Could not acquire root permissions");
TorService.logMessage("Could not acquire root permissions");
return false;
}
private static void logNotice (String msg)
{
if (LOG_OUTPUT_TO_DEBUG)
Log.d(TAG, msg);
}
public static int findProcessId(String command)
{
@ -106,7 +103,7 @@ public class TorServiceUtils implements TorServiceConstants {
}
catch (NumberFormatException e)
{
logNotice("unable to parse process pid: " + line);
TorService.logException("unable to parse process pid: " + line,e);
}
}
@ -153,7 +150,7 @@ public class TorServiceUtils implements TorServiceConstants {
public static int doShellCommand(String[] cmds, StringBuilder log, boolean runAsRoot, boolean waitFor) throws Exception
{
logNotice("executing shell cmds: " + cmds[0] + "; runAsRoot=" + runAsRoot);
TorService.logMessage("executing shell cmds: " + cmds[0] + "; runAsRoot=" + runAsRoot);
Process proc = null;
@ -201,7 +198,7 @@ public class TorServiceUtils implements TorServiceConstants {
log.append(exitCode);
log.append("\n");
logNotice("command process exit value: " + exitCode);
TorService.logMessage("command process exit value: " + exitCode);
}

View File

@ -11,55 +11,11 @@ public class TorTransProxy implements TorServiceConstants {
private final static String TAG = TorServiceConstants.TAG;
//private static String BASE_DIR = "/data/data/" + TorServiceConstants.TOR_APP_USERNAME + "/";
private static void logNotice (String msg)
{
if (LOG_OUTPUT_TO_DEBUG)
Log.d(TAG, msg);
}
/**
* Check if we have root access
* @return boolean true if we have root
*/
/*
public static String getIPTablesVersion() {
StringBuilder log = new StringBuilder();
try {
// Run an empty script just to check root access
String[] cmd = {"iptables -v"};
int code = TorServiceUtils.doShellCommand(cmd, log, true, true);
String msg = log.toString();
logNotice(cmd[0] + ";errCode=" + code + ";resp=" + msg);
String out = log.toString();
if (out.indexOf(" v")!=-1)
{
out = out.substring(out.indexOf(" v")+2);
out = out.substring(0,out.indexOf(":"));
return out.trim();
}
} catch (Exception e) {
Log.w(TAG,"Error checking iptables version: " + e.getMessage() ,e);
}
logNotice("Could not acquire check iptables: " + log.toString());
return null;
}*/
public static int purgeIptables(Context context) throws Exception {
String ipTablesPath = new File(context.getDir("bin", 0),"iptables_n1").getAbsolutePath();
String ipTablesPath = new File(context.getDir("bin", 0),"iptables").getAbsolutePath();
final StringBuilder script = new StringBuilder();
@ -78,7 +34,8 @@ public class TorTransProxy implements TorServiceConstants {
String[] cmd = {script.toString()};
code = TorServiceUtils.doShellCommand(cmd, res, true, true);
String msg = res.toString();
logNotice(cmd[0] + ";errCode=" + code + ";resp=" + msg);
TorService.logMessage(cmd[0] + ";errCode=" + code + ";resp=" + msg);
return code;
@ -90,7 +47,7 @@ public class TorTransProxy implements TorServiceConstants {
//restoreDNSResolvConf(); //not working yet
String ipTablesPath = new File(context.getDir("bin", 0),"iptables_n1").getAbsolutePath();
String ipTablesPath = new File(context.getDir("bin", 0),"iptables").getAbsolutePath();
final StringBuilder script = new StringBuilder();
@ -171,12 +128,14 @@ public class TorTransProxy implements TorServiceConstants {
public static int setTransparentProxyingByApp(Context context, TorifiedApp[] apps, boolean forceAll) throws Exception
{
boolean runRoot = true;
boolean waitFor = true;
//android.os.Debug.waitForDebugger();
//redirectDNSResolvConf(); //not working yet
//String baseDir = context.getDir("bin", 0).getAbsolutePath() + "/";
String ipTablesPath = new File(context.getDir("bin", 0),"iptables_n1").getAbsolutePath();
String ipTablesPath = new File(context.getDir("bin", 0),"iptables").getAbsolutePath();
boolean ipTablesOld = false;
@ -202,17 +161,8 @@ public class TorTransProxy implements TorServiceConstants {
continue;
}
logNotice("enabling transproxy for app: " + apps[i].getUsername() + "(" + apps[i].getUid() + ")");
TorService.logMessage("enabling transproxy for app: " + apps[i].getUsername() + "(" + apps[i].getUid() + ")");
/*
* iptables -t nat -A OUTPUT -p tcp -m owner --uid-owner anonymous -m tcp --syn -j REDIRECT --to-ports 9040
iptables -t nat -A OUTPUT -p udp -m owner --uid-owner anonymous -m udp --dport 53 -j REDIRECT --to-ports 53
iptables -t nat -A OUTPUT -m owner --uid-owner anonymous -j DROP
*/
//iptables -t nat -A output -p tcp -m owner --uid-owner 100 -m tcp --sync -j REDIRECT --to-ports 9040
//TCP
script.append(ipTablesPath);
script.append(" -t nat");
@ -248,7 +198,6 @@ iptables -t nat -A OUTPUT -m owner --uid-owner anonymous -j DROP
script.append(" || exit\n");
//EVERYTHING ELSE - DROP!
if (ipTablesOld) //for some reason this doesn't work on iptables 1.3.7
{
script.append(ipTablesPath);
@ -288,17 +237,16 @@ iptables -t nat -A OUTPUT -m owner --uid-owner anonymous -j DROP
}
}
else
{
}
}
String[] cmdAdd = {script.toString()};
String[] cmdAdd = {script.toString()};
code = TorServiceUtils.doShellCommand(cmdAdd, res, true, true);
code = TorServiceUtils.doShellCommand(cmdAdd, res, runRoot, waitFor);
String msg = res.toString();
logNotice(cmdAdd[0] + ";errCode=" + code + ";resp=" + msg);
TorService.logMessage(cmdAdd[0] + ";errCode=" + code + ";resp=" + msg);
return code;
}
@ -311,7 +259,7 @@ iptables -t nat -A OUTPUT -m owner --uid-owner anonymous -j DROP
//redirectDNSResolvConf(); //not working yet
//String baseDir = context.getDir("bin",0).getAbsolutePath() + '/';
String ipTablesPath = new File(context.getDir("bin", 0),"iptables_n1").getAbsolutePath();
String ipTablesPath = new File(context.getDir("bin", 0),"iptables").getAbsolutePath();
boolean ipTablesOld = false;
@ -380,7 +328,7 @@ iptables -t nat -A OUTPUT -m owner --uid-owner anonymous -j DROP
String[] cmdAdd = {script.toString()};
code = TorServiceUtils.doShellCommand(cmdAdd, res, true, true);
String msg = res.toString();
logNotice(cmdAdd[0] + ";errCode=" + code + ";resp=" + msg);
TorService.logMessage(cmdAdd[0] + ";errCode=" + code + ";resp=" + msg);
return code;
}