From 9bf8568681550b21afc05f1f3e337242038b716a Mon Sep 17 00:00:00 2001 From: Nathan Freitas Date: Thu, 9 Sep 2010 22:09:01 +0000 Subject: [PATCH] added LOG_OUTPUT_TO_DEBUG variable for compile time on/off of log output to Android stdout svn:r23157 --- .../android/service/TorService.java | 133 +++++++++--------- .../android/service/TorServiceConstants.java | 2 + .../android/service/TorServiceUtils.java | 9 +- .../android/service/TorTransProxy.java | 41 +++--- 4 files changed, 94 insertions(+), 91 deletions(-) diff --git a/src/org/torproject/android/service/TorService.java b/src/org/torproject/android/service/TorService.java index 14018c3e..08130071 100644 --- a/src/org/torproject/android/service/TorService.java +++ b/src/org/torproject/android/service/TorService.java @@ -61,8 +61,7 @@ public class TorService extends Service implements TorServiceConstants, Runnable public void onCreate() { super.onCreate(); - Log.d(TAG,"TorService: onCreate"); - + } @@ -73,7 +72,7 @@ public class TorService extends Service implements TorServiceConstants, Runnable if (procId != -1) { - Log.d(TAG,"Found existing Tor process"); + logNotice("Found existing Tor process"); sendCallbackLogMessage ("found existing Tor process..."); @@ -111,7 +110,7 @@ public class TorService extends Service implements TorServiceConstants, Runnable public void onLowMemory() { super.onLowMemory(); - Log.d(TAG, "Low Memory Called"); + logNotice( "Low Memory Warning!"); } @@ -121,7 +120,7 @@ public class TorService extends Service implements TorServiceConstants, Runnable */ public boolean onUnbind(Intent intent) { - Log.d(TAG, "onUnbind Called: " + intent.getAction()); + // logNotice( "onUnbind Called: " + intent.getAction()); isBound = false; @@ -213,9 +212,6 @@ public class TorService extends Service implements TorServiceConstants, Runnable // Unregister all callbacks. mCallbacks.kill(); - - Log.d(TAG,"onDestroy called"); - stopTor(); } @@ -277,7 +273,6 @@ public class TorService extends Service implements TorServiceConstants, Runnable { try { logNotice("sending SHUTDOWN signal to Tor process"); - // conn.shutdownTor(arg0) conn.signal("SHUTDOWN"); } catch (Exception e) { Log.d(TAG,"error shutting down Tor via connection",e); @@ -305,7 +300,7 @@ public class TorService extends Service implements TorServiceConstants, Runnable while (procId != -1) { - Log.d(TAG,"Found Privoxy PID=" + procId + " - killing now..."); + logNotice("Found Privoxy PID=" + procId + " - killing now..."); String[] cmd = { SHELL_CMD_KILL + ' ' + procId + "" }; TorServiceUtils.doShellCommand(cmd,log, false, false); @@ -317,10 +312,13 @@ public class TorService extends Service implements TorServiceConstants, Runnable private void logNotice (String msg) { - - Log.d(TAG, msg); - sendCallbackLogMessage(msg); - + if (msg != null && msg.trim().length() > 0) + { + if (LOG_OUTPUT_TO_DEBUG) + Log.d(TAG, msg); + + sendCallbackLogMessage(msg); + } } private String findAPK () @@ -333,7 +331,7 @@ public class TorService extends Service implements TorServiceConstants, Runnable int MAX_TRIES = 10; String buildPath = apkBase + TOR_APP_USERNAME + APK_EXT; - Log.d(TAG, "Checking APK location: " + buildPath); + logNotice("Checking APK location: " + buildPath); File fileApk = new File(buildPath); @@ -345,7 +343,7 @@ public class TorService extends Service implements TorServiceConstants, Runnable buildPath = apkBase + TOR_APP_USERNAME + '-' + i + APK_EXT; fileApk = new File(buildPath); - Log.d(TAG, "Checking APK location: " + buildPath); + logNotice( "Checking APK location: " + buildPath); if (fileApk.exists()) return fileApk.getAbsolutePath(); @@ -357,7 +355,7 @@ public class TorService extends Service implements TorServiceConstants, Runnable buildPath = apkBaseExt + pkgFile; fileApk = new File(buildPath); - Log.d(TAG, "Checking external storage APK location: " + buildPath); + logNotice( "Checking external storage APK location: " + buildPath); if (fileApk.exists()) return fileApk.getAbsolutePath(); @@ -367,7 +365,7 @@ public class TorService extends Service implements TorServiceConstants, Runnable buildPath = apkBaseExt + '-' + i + pkgFile; fileApk = new File(buildPath); - Log.d(TAG, "Checking external storage APK location: " + buildPath); + logNotice( "Checking external storage APK location: " + buildPath); if (fileApk.exists()) return fileApk.getAbsolutePath(); @@ -381,16 +379,16 @@ public class TorService extends Service implements TorServiceConstants, Runnable { - Log.d(TAG,"checking Tor binaries"); + logNotice( "checking Tor binaries"); //appHome = getApplicationContext().getFilesDir().getAbsolutePath(); appHome = "/data/data/" + TOR_APP_USERNAME + "/"; - Log.d(TAG,"appHome=" + appHome); + logNotice( "appHome=" + appHome); String apkPath = findAPK(); - Log.d(TAG,"found apk at: " + apkPath); + logNotice( "found apk at: " + apkPath); boolean apkExists = new File(apkPath).exists(); @@ -522,7 +520,7 @@ public class TorService extends Service implements TorServiceConstants, Runnable String[] torCmd = {torBinaryPath + " -f " + torrcPath + " || exit\n"}; TorServiceUtils.doShellCommand(torCmd, log, false, false); - Log.d(TAG,"Starting tor process: " + torCmd[0]); + logNotice( "Starting tor process: " + torCmd[0]); Thread.sleep(1000); int procId = TorServiceUtils.findProcessId(torBinaryPath); @@ -568,7 +566,7 @@ public class TorService extends Service implements TorServiceConstants, Runnable private void runPrivoxyShellCmd () throws Exception { - Log.d(TAG,"Starting privoxy process"); + logNotice( "Starting privoxy process"); int privoxyProcId = TorServiceUtils.findProcessId(privoxyPath); @@ -630,7 +628,7 @@ public class TorService extends Service implements TorServiceConstants, Runnable { try { - Log.d(TAG,"Connecting to control port: " + TOR_CONTROL_PORT); + logNotice( "Connecting to control port: " + TOR_CONTROL_PORT); String baseMessage = getString(R.string.tor_process_connecting); sendCallbackStatusMessage(baseMessage); @@ -641,7 +639,7 @@ public class TorService extends Service implements TorServiceConstants, Runnable sendCallbackStatusMessage(getString(R.string.tor_process_connecting_step2)); - Log.d(TAG,"SUCCESS connected to control port"); + logNotice( "SUCCESS connected to control port"); String torAuthCookie = appHome + "data/control_auth_cookie"; @@ -650,7 +648,7 @@ public class TorService extends Service implements TorServiceConstants, Runnable new FileInputStream(new File(torAuthCookie)).read(cookie); conn.authenticate(cookie); - Log.d(TAG,"SUCCESS authenticated to control port"); + logNotice( "SUCCESS authenticated to control port"); sendCallbackStatusMessage(getString(R.string.tor_process_connecting_step2) + getString(R.string.tor_process_connecting_step3)); @@ -722,7 +720,7 @@ public class TorService extends Service implements TorServiceConstants, Runnable // We extend NullEventHandler so that we don't need to provide empty // implementations for all the events we don't care about. // ... - Log.d(TAG,"adding control port event handler"); + logNotice( "adding control port event handler"); conn.setEventHandler(this); @@ -731,7 +729,7 @@ public class TorService extends Service implements TorServiceConstants, Runnable // conn.setEvents(Arrays.asList(new String[]{ // "DEBUG", "INFO", "NOTICE", "WARN", "ERR"})); - Log.d(TAG,"SUCCESS added control port event handler"); + logNotice( "SUCCESS added control port event handler"); @@ -783,7 +781,7 @@ public class TorService extends Service implements TorServiceConstants, Runnable public void message(String severity, String msg) { - Log.d(TAG, "[Tor Control Port] " + severity + ": " + msg); + logNotice( "[Tor Control Port] " + severity + ": " + msg); if (msg.indexOf(TOR_CONTROL_PORT_MSG_BOOTSTRAP_DONE)!=-1) { @@ -813,40 +811,46 @@ public class TorService extends Service implements TorServiceConstants, Runnable public void orConnStatus(String status, String orName) { - StringBuilder sb = new StringBuilder(); - sb.append("orConnStatus ("); - sb.append((orName) ); - sb.append("): "); - sb.append(status); - - logNotice(sb.toString()); + if (LOG_OUTPUT_TO_DEBUG) + { + StringBuilder sb = new StringBuilder(); + sb.append("orConnStatus ("); + sb.append((orName) ); + sb.append("): "); + sb.append(status); + + logNotice(sb.toString()); + } } public void streamStatus(String status, String streamID, String target) { - StringBuilder sb = new StringBuilder(); - sb.append("StreamStatus ("); - sb.append((streamID)); - sb.append("): "); - sb.append(status); - - logNotice(sb.toString()); - - + if (LOG_OUTPUT_TO_DEBUG) + { + StringBuilder sb = new StringBuilder(); + sb.append("StreamStatus ("); + sb.append((streamID)); + sb.append("): "); + sb.append(status); + + logNotice(sb.toString()); + } } public void unrecognized(String type, String msg) { - StringBuilder sb = new StringBuilder(); - sb.append("Message ("); - sb.append(type); - sb.append("): "); - sb.append(msg); - - logNotice(sb.toString()); - + if (LOG_OUTPUT_TO_DEBUG) + { + StringBuilder sb = new StringBuilder(); + sb.append("Message ("); + sb.append(type); + sb.append("): "); + sb.append(msg); + + logNotice(sb.toString()); + } } @@ -865,17 +869,18 @@ public class TorService extends Service implements TorServiceConstants, Runnable public void circuitStatus(String status, String circID, String path) { - /* - StringBuilder sb = new StringBuilder(); - sb.append("Circuit ("); - sb.append((circID)); - sb.append("): "); - sb.append(status); - sb.append("; "); - sb.append(path); - - logNotice(sb.toString()); - */ + if (LOG_OUTPUT_TO_DEBUG) + { + StringBuilder sb = new StringBuilder(); + sb.append("Circuit ("); + sb.append((circID)); + sb.append("): "); + sb.append(status); + sb.append("; "); + sb.append(path); + + logNotice(sb.toString()); + } } diff --git a/src/org/torproject/android/service/TorServiceConstants.java b/src/org/torproject/android/service/TorServiceConstants.java index b0a5bb54..1f0eb1e0 100644 --- a/src/org/torproject/android/service/TorServiceConstants.java +++ b/src/org/torproject/android/service/TorServiceConstants.java @@ -6,6 +6,8 @@ public interface TorServiceConstants { public final static String TAG = "ORBOT"; + public static boolean LOG_OUTPUT_TO_DEBUG = false; + public final static String TOR_APP_USERNAME = "org.torproject.android"; public final static String ASSETS_BASE = "assets/"; diff --git a/src/org/torproject/android/service/TorServiceUtils.java b/src/org/torproject/android/service/TorServiceUtils.java index 2be13fe9..6329fd24 100644 --- a/src/org/torproject/android/service/TorServiceUtils.java +++ b/src/org/torproject/android/service/TorServiceUtils.java @@ -12,6 +12,11 @@ import android.util.Log; public class TorServiceUtils implements TorServiceConstants { + private static void logNotice (String msg) + { + if (LOG_OUTPUT_TO_DEBUG) + Log.d(TAG, msg); + } public static int findProcessId(String command) { @@ -112,7 +117,7 @@ public class TorServiceUtils implements TorServiceConstants { public static int doShellCommand(String[] cmds, StringBuilder log, boolean runAsRoot, boolean waitFor) throws Exception { - Log.d(TAG,"executing shell cmds: " + cmds[0] + "; runAsRoot=" + runAsRoot); + logNotice("executing shell cmds: " + cmds[0] + "; runAsRoot=" + runAsRoot); Process proc = null; @@ -163,7 +168,7 @@ public class TorServiceUtils implements TorServiceConstants { log.append(exitCode); log.append("\n"); - Log.d(TAG,"command process exit value: " + exitCode); + logNotice("command process exit value: " + exitCode); } diff --git a/src/org/torproject/android/service/TorTransProxy.java b/src/org/torproject/android/service/TorTransProxy.java index 044a3b52..f46190a9 100644 --- a/src/org/torproject/android/service/TorTransProxy.java +++ b/src/org/torproject/android/service/TorTransProxy.java @@ -5,27 +5,17 @@ import org.torproject.android.TorifiedApp; import android.content.Context; import android.util.Log; -public class TorTransProxy { +public class TorTransProxy implements TorServiceConstants { private final static String TAG = TorServiceConstants.TAG; - - + //private static String BASE_DIR = "/data/data/" + TorServiceConstants.TOR_APP_USERNAME + "/"; - - /* - private final static String CMD_NAT_FLUSH = "iptables -t nat -F || exit\n"; - private final static String CMD_FILTER_FLUSH = "iptables -t filter -F || exit\n"; - - private final static String CMD_DNS_PROXYING_ADD = "iptables -t nat -A PREROUTING -p udp --dport 53 -j DNAT --to 127.0.0.1:5400 || exit\n"; - - private final static String IPTABLES_ADD = " -A "; - */ - - //private final static String CMD_DNS_PROXYING_DELETE = "iptables -t nat -D PREROUTING -p udp --dport 53 -j DNAT --to 127.0.0.1:5400 || exit\n"; - // - just calling a system wide flush of iptables rules - //private final static String IPTABLES_DELETE = " -D "; //not deleting manually anymore - just calling a system wide flush of iptables rules - // private final static String IPTABLES_DROP_ALL = " -j DROP "; - + + 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 @@ -48,7 +38,8 @@ public class TorTransProxy { } catch (Exception e) { Log.w(TAG,"Error checking for root access: " + e.getMessage() ,e); } - Log.w(TAG, "Could not acquire root access: " + log.toString()); + + logNotice("Could not acquire root access: " + log.toString()); return false; } @@ -67,7 +58,7 @@ public class TorTransProxy { String[] cmd = {"iptables -v"}; int code = TorServiceUtils.doShellCommand(cmd, log, true, true); String msg = log.toString(); - Log.d(TAG,cmd[0] + ";errCode=" + code + ";resp=" + msg); + logNotice(cmd[0] + ";errCode=" + code + ";resp=" + msg); String out = log.toString(); @@ -85,7 +76,7 @@ public class TorTransProxy { Log.w(TAG,"Error checking iptables version: " + e.getMessage() ,e); } - Log.w(TAG, "Could not acquire check iptables: " + log.toString()); + logNotice("Could not acquire check iptables: " + log.toString()); return null; } @@ -146,7 +137,7 @@ public class TorTransProxy { String[] cmd = {script.toString()}; code = TorServiceUtils.doShellCommand(cmd, res, true, true); String msg = res.toString(); - Log.d(TAG,cmd[0] + ";errCode=" + code + ";resp=" + msg); + logNotice(cmd[0] + ";errCode=" + code + ";resp=" + msg); return code; @@ -163,7 +154,7 @@ public class TorTransProxy { String baseDir = findBaseDir(); String iptablesVersion = getIPTablesVersion(); - Log.d(TAG, "iptables version: " + iptablesVersion); + logNotice( "iptables version: " + iptablesVersion); boolean ipTablesOld = false; if (iptablesVersion != null && iptablesVersion.startsWith("1.3")){ @@ -208,7 +199,7 @@ public class TorTransProxy { continue; } - Log.d(TAG,"enabling transproxy for app: " + apps[i].getUsername() + "(" + apps[i].getUid() + ")"); + logNotice("enabling transproxy for app: " + apps[i].getUsername() + "(" + apps[i].getUid() + ")"); //TCP script.append(baseDir); @@ -285,7 +276,7 @@ public class TorTransProxy { String[] cmdAdd = {script.toString()}; code = TorServiceUtils.doShellCommand(cmdAdd, res, true, true); String msg = res.toString(); - Log.d(TAG,cmdAdd[0] + ";errCode=" + code + ";resp=" + msg); + logNotice(cmdAdd[0] + ";errCode=" + code + ";resp=" + msg); return code; }