From e2f4f5f1f2f656e340e79a2b7b25175ea80ebf2b Mon Sep 17 00:00:00 2001 From: Nathan Freitas Date: Tue, 12 Aug 2014 02:02:48 -0400 Subject: [PATCH 01/23] don't make these final, because the user might change them --- src/org/torproject/android/service/TorServiceConstants.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/org/torproject/android/service/TorServiceConstants.java b/src/org/torproject/android/service/TorServiceConstants.java index bcbede25..c65c9a5d 100644 --- a/src/org/torproject/android/service/TorServiceConstants.java +++ b/src/org/torproject/android/service/TorServiceConstants.java @@ -48,10 +48,10 @@ public interface TorServiceConstants { public final static int FILE_WRITE_BUFFER_SIZE = 1024; //HTTP Proxy server port - public final static int PORT_HTTP = 8118; //just like Privoxy! + public static int PORT_HTTP = 8118; //just like Privoxy! //Socks port client connects to, server is the Tor binary - public final static String PORT_SOCKS_DEFAULT = "9050"; + public static String PORT_SOCKS_DEFAULT = "9050";//it can be auto, 0 or a port //what is says! public final static String IP_LOCALHOST = "127.0.0.1"; From 3a82421b63ef811113ecd5840bd783d88eb8453c Mon Sep 17 00:00:00 2001 From: Nathan Freitas Date: Tue, 12 Aug 2014 02:03:22 -0400 Subject: [PATCH 02/23] no need for asynctask - just grab the status directly --- src/org/torproject/android/Orbot.java | 202 +++++++++++--------------- 1 file changed, 82 insertions(+), 120 deletions(-) diff --git a/src/org/torproject/android/Orbot.java b/src/org/torproject/android/Orbot.java index 678b98df..efb1049d 100644 --- a/src/org/torproject/android/Orbot.java +++ b/src/org/torproject/android/Orbot.java @@ -32,7 +32,6 @@ import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.content.res.Configuration; import android.net.Uri; -import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; import android.os.IBinder; @@ -831,131 +830,94 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic aDialog.setCanceledOnTouchOutside(true); } - /* - * Set the state of the running/not running graphic and label - * this all needs to be looked at w/ the shift to progressDialog - */ - public void updateStatus (String torServiceMsg) + private void updateStatus (String torServiceMsg) { - new updateStatusAsync().execute(torServiceMsg); - } - - private class updateStatusAsync extends AsyncTask { - - String mTorServiceMsg = null; - - @Override - protected Integer doInBackground(String... params) { - - mTorServiceMsg = params[0]; - int newTorStatus = TorServiceConstants.STATUS_OFF; - try - { - if (mService != null) - return new Integer(mService.getStatus()); - - } - catch (Exception e) - { - //error - Log.d(TAG,"error in update status",e); - } - - return newTorStatus; - - } - @Override - protected void onPostExecute(Integer result) { - - updateUI(result.intValue()); - - super.onPostExecute(result); - } - - private void updateUI (int newTorStatus) - { - - //now update the layout_main UI based on the status - if (imgStatus != null) + int newTorStatus = torStatus; + + if (mService != null) + try {newTorStatus = mService.getStatus();} + catch (RemoteException e){} + + //now update the layout_main UI based on the status + if (imgStatus != null) + { + + if (newTorStatus == TorServiceConstants.STATUS_ON) { - - if (newTorStatus == TorServiceConstants.STATUS_ON) - { - - imgStatus.setImageResource(R.drawable.toron); - - String lblMsg = getString(R.string.status_activated); - lblStatus.setText(lblMsg); - - if (mItemOnOff != null) - mItemOnOff.setTitle(R.string.menu_stop); - - - if (mTorServiceMsg != null && mTorServiceMsg.length() > 0) - { - appendLogTextAndScroll(mTorServiceMsg); - } - - boolean showFirstTime = mPrefs.getBoolean("connect_first_time",true); - - if (showFirstTime) - { - - Editor pEdit = mPrefs.edit(); - - pEdit.putBoolean("connect_first_time",false); - - pEdit.commit(); - - showAlert(getString(R.string.status_activated),getString(R.string.connect_first_time),true); - - } - - - if (autoStartFromIntent) - { - setResult(RESULT_OK); - finish(); - } - - } - else if (newTorStatus == TorServiceConstants.STATUS_CONNECTING) - { - - imgStatus.setImageResource(R.drawable.torstarting); - - if (mItemOnOff != null) - mItemOnOff.setTitle(R.string.menu_stop); - - if (lblStatus != null && mTorServiceMsg != null) - if (mTorServiceMsg.indexOf('%')!=-1) - lblStatus.setText(mTorServiceMsg); - - appendLogTextAndScroll(mTorServiceMsg); - - - } - else if (newTorStatus == TorServiceConstants.STATUS_OFF) - { - imgStatus.setImageResource(R.drawable.toroff); - lblStatus.setText(getString(R.string.status_disabled) + "\n" + getString(R.string.press_to_start)); - - if (mItemOnOff != null) - mItemOnOff.setTitle(R.string.menu_start); - - } - } - - + imgStatus.setImageResource(R.drawable.toron); + + String lblMsg = getString(R.string.status_activated); + lblStatus.setText(lblMsg); - torStatus = newTorStatus; - - } + if (mItemOnOff != null) + mItemOnOff.setTitle(R.string.menu_stop); + + + if (torServiceMsg != null && torServiceMsg.length() > 0) + { + appendLogTextAndScroll(torServiceMsg); + } + + boolean showFirstTime = mPrefs.getBoolean("connect_first_time",true); + + if (showFirstTime) + { + + Editor pEdit = mPrefs.edit(); + + pEdit.putBoolean("connect_first_time",false); + + pEdit.commit(); + + showAlert(getString(R.string.status_activated),getString(R.string.connect_first_time),true); + + } + + + if (autoStartFromIntent) + { + setResult(RESULT_OK); + finish(); + } + + } + else if (newTorStatus == TorServiceConstants.STATUS_CONNECTING) + { + + imgStatus.setImageResource(R.drawable.torstarting); + + if (mItemOnOff != null) + mItemOnOff.setTitle(R.string.menu_stop); + + + if (lblStatus != null && torServiceMsg != null) + if (torServiceMsg.indexOf('%')!=-1) + lblStatus.setText(torServiceMsg); + + appendLogTextAndScroll(torServiceMsg); + + + } + else if (newTorStatus == TorServiceConstants.STATUS_OFF) + { + imgStatus.setImageResource(R.drawable.toroff); + lblStatus.setText(getString(R.string.status_disabled) + "\n" + getString(R.string.press_to_start)); + + if (mItemOnOff != null) + mItemOnOff.setTitle(R.string.menu_start); + + } + } + + + + torStatus = newTorStatus; + + } - } // guess what? this start's Tor! actually no it just requests via the local ITorService to the remote TorService instance // to start Tor @@ -1209,7 +1171,7 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic handleIntents(); - updateStatus(""); + updateStatus(""); } catch (RemoteException e) { // In this case the service has crashed before we could even From ea37b44fa3ab9143e210f5ee6881691fe6b54dcf Mon Sep 17 00:00:00 2001 From: Nathan Freitas Date: Tue, 12 Aug 2014 02:03:43 -0400 Subject: [PATCH 03/23] create new thread to just for existing process dont' rely on executor there yet --- src/org/torproject/android/TorConstants.java | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/org/torproject/android/TorConstants.java b/src/org/torproject/android/TorConstants.java index 1a9fcdbe..9b0966eb 100644 --- a/src/org/torproject/android/TorConstants.java +++ b/src/org/torproject/android/TorConstants.java @@ -19,12 +19,6 @@ public interface TorConstants { public final static String NEWLINE = "\n"; - /* - public final static String TORRC_DEFAULT = - "SocksPort 9050\nSocksListenAddress 127.0.0.1\nSafeSocks 1\nDNSPort 5400\nLog debug syslog\nDataDirectory /data/data/org.torproject.android/cache\n" - + "ControlPort 9051\nCookieAuthentication 1\nRelayBandwidthRate 20 KBytes\nRelayBandwidthBurst 20 KBytes\nAutomapHostsOnResolve 1\nTransPort 9040\n"; - */ - // public final static String INTENT_TOR_SERVICE = "org.torproject.android.service.TOR_SERVICE"; public final static String HANDLER_TOR_MSG = "torServiceMsg"; From 3698e4610515bf230788d410a158759a016904b9 Mon Sep 17 00:00:00 2001 From: Nathan Freitas Date: Tue, 12 Aug 2014 02:04:10 -0400 Subject: [PATCH 04/23] create new thread for connecting to existing process don't rely on executor yet, as we need it to happen fast --- src/org/torproject/android/service/TorService.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/org/torproject/android/service/TorService.java b/src/org/torproject/android/service/TorService.java index 46d37a13..f22d8371 100644 --- a/src/org/torproject/android/service/TorService.java +++ b/src/org/torproject/android/service/TorService.java @@ -575,7 +575,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst logNotice("There was an error installing Orbot binaries"); } - mExecutor.execute(new Runnable () + new Thread(new Runnable () { public void run () { @@ -589,7 +589,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst } } - }); + }).start(); } @@ -651,7 +651,6 @@ public class TorService extends Service implements TorServiceConstants, TorConst String transPort = prefs.getString("pref_transport", TorServiceConstants.TOR_TRANSPROXY_PORT_DEFAULT+""); String dnsPort = prefs.getString("pref_dnsport", TorServiceConstants.TOR_DNS_PORT_DEFAULT+""); - if (mTransProxyTethering) { extraLines.append("TransListenAddress 0.0.0.0").append('\n'); From 9cc5f98e92dbf234f8cd6fe760b8e2bd24b6bf5f Mon Sep 17 00:00:00 2001 From: Nathan Freitas Date: Tue, 12 Aug 2014 02:04:36 -0400 Subject: [PATCH 05/23] don't obfuscate class names; we just need to remove unused classes --- procfg.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/procfg.txt b/procfg.txt index b9dab406..7d2b51f6 100644 --- a/procfg.txt +++ b/procfg.txt @@ -2,6 +2,7 @@ -dontusemixedcaseclassnames -dontskipnonpubliclibraryclasses -dontpreverify +-dontobfuscate -verbose -optimizations !code/simplification/arithmetic,!field/*,!class/merging/* From b307d510f8a6455bc44c2eb21f88cd291ad12a25 Mon Sep 17 00:00:00 2001 From: Nathan Freitas Date: Thu, 21 Aug 2014 09:45:13 -0400 Subject: [PATCH 06/23] make default SOCKs port auto, and THEN try to set specified port - also remove exit IP lookup loop bad code --- .../android/service/TorService.java | 170 ++++++++++++------ .../android/service/TorServiceConstants.java | 3 +- 2 files changed, 116 insertions(+), 57 deletions(-) diff --git a/src/org/torproject/android/service/TorService.java b/src/org/torproject/android/service/TorService.java index f22d8371..6709cad0 100644 --- a/src/org/torproject/android/service/TorService.java +++ b/src/org/torproject/android/service/TorService.java @@ -23,6 +23,7 @@ import java.io.PrintStream; import java.io.PrintWriter; import java.net.InetSocketAddress; import java.net.Proxy; +import java.net.ServerSocket; import java.net.Socket; import java.net.URL; import java.net.URLConnection; @@ -75,6 +76,7 @@ import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat.Builder; import android.util.Log; import android.widget.RemoteViews; +import android.widget.Toast; public class TorService extends Service implements TorServiceConstants, TorConstants, EventHandler { @@ -89,6 +91,11 @@ public class TorService extends Service implements TorServiceConstants, TorConst private Socket torConnSocket = null; private int mLastProcessId = -1; + + private int mPortHTTP = 8118; + private int mPortSOCKS = 9050; + + private static final int NOTIFY_ID = 1; private static final int TRANSPROXY_NOTIFY_ID = 2; private static final int ERROR_NOTIFY_ID = 3; @@ -646,7 +653,6 @@ public class TorService extends Service implements TorServiceConstants, TorConst fileControlPort = new File(appBinHome,"control.txt"); extraLines.append(TORRC_CONTROLPORT_FILE_KEY).append(' ').append(fileControlPort.getCanonicalPath()).append('\n'); - String socksPort = prefs.getString(TorConstants.PREF_SOCKS, TorServiceConstants.PORT_SOCKS_DEFAULT); String transPort = prefs.getString("pref_transport", TorServiceConstants.TOR_TRANSPROXY_PORT_DEFAULT+""); String dnsPort = prefs.getString("pref_dnsport", TorServiceConstants.TOR_DNS_PORT_DEFAULT+""); @@ -659,12 +665,9 @@ public class TorService extends Service implements TorServiceConstants, TorConst } extraLines.append("RunAsDaemon 1").append('\n'); - extraLines.append("AvoidDiskWrites 1").append('\n'); - - extraLines.append("CircuitStreamTimeout 120").append('\n'); - extraLines.append("SOCKSPort ").append(socksPort).append('\n'); + extraLines.append("SOCKSPort ").append("auto").append('\n'); extraLines.append("SafeSocks 0").append('\n'); extraLines.append("TestSocks 0").append('\n'); extraLines.append("WarnUnsafeSocks 1").append('\n'); @@ -674,7 +677,6 @@ public class TorService extends Service implements TorServiceConstants, TorConst extraLines.append("VirtualAddrNetwork 10.192.0.0/10").append('\n'); extraLines.append("AutomapHostsOnResolve 1").append('\n'); - extraLines.append(prefs.getString("pref_custom_torrc", "")); logNotice("updating torrc custom configuration..."); @@ -700,7 +702,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst if (!fileBin.canExecute()) { logNotice("(re)Setting permission on binary: " + fileBin.getCanonicalPath()); - //Shell shell = Shell.startShell(new ArrayList(), appBinHome.getCanonicalPath()); + Shell shell = Shell.startShell(); shell.add(new SimpleCommand("chmod " + CHMOD_EXE_VALUE + ' ' + fileBin.getCanonicalPath())).waitForFinish(); @@ -740,7 +742,6 @@ public class TorService extends Service implements TorServiceConstants, TorConst currentStatus = STATUS_CONNECTING; - enableBinExec(fileTor); enableBinExec(filePolipo); enableBinExec(fileObfsclient); @@ -759,9 +760,28 @@ public class TorService extends Service implements TorServiceConstants, TorConst getHiddenServiceHostname (); - //checkAddressAndCountry(); } - + + private boolean flushTransparentProxyRules () throws Exception + { + + if (mHasRoot) + { + if (mTransProxy == null) + { + mTransProxy = new TorTransProxy(this, fileXtables); + + } + + mTransProxy.flushTransproxyRules(this); + + return true; + } + else + { + return false; + } + } /* * activate means whether to apply the users preferences @@ -777,7 +797,6 @@ public class TorService extends Service implements TorServiceConstants, TorConst mTransProxy = new TorTransProxy(this, fileXtables); } - SharedPreferences prefs = TorServiceUtils.getSharedPrefs(getApplicationContext()); @@ -925,8 +944,6 @@ public class TorService extends Service implements TorServiceConstants, TorConst private void updatePolipoConfig () throws FileNotFoundException, IOException { - SharedPreferences prefs = TorServiceUtils.getSharedPrefs(getApplicationContext()); - String socksPort = prefs.getString(TorConstants.PREF_SOCKS, TorServiceConstants.PORT_SOCKS_DEFAULT); File file = new File(appBinHome, POLIPOCONFIG_ASSET_KEY); @@ -934,8 +951,8 @@ public class TorService extends Service implements TorServiceConstants, TorConst props.load(new FileReader(file)); - props.put("socksParentProxy", "\"localhost:" + socksPort + "\""); - props.put("proxyPort","8118"); + props.put("socksParentProxy", "\"localhost:" + mPortSOCKS + "\""); + props.put("proxyPort",mPortHTTP+""); props.store(new FileWriter(file), "updated"); @@ -987,17 +1004,6 @@ public class TorService extends Service implements TorServiceConstants, TorConst } - /* - public String generateHashPassword () - { - - PasswordDigest d = PasswordDigest.generateDigest(); - byte[] s = d.getSecret(); // pass this to authenticate - String h = d.getHashedPassword(); // pass this to the Tor on startup. - - return null; - }*/ - private synchronized int initControlConnection (int maxTries) throws Exception, RuntimeException { int i = 0; @@ -1019,12 +1025,10 @@ public class TorService extends Service implements TorServiceConstants, TorConst torConnSocket = new Socket(IP_LOCALHOST, controlPort); torConnSocket.setSoTimeout(CONTROL_SOCKET_TIMEOUT); - boolean oo = true; - conn = new TorControlConnection(torConnSocket); conn.launchThread(true);//is daemon - logNotice( "SUCCESS connected to Tor control port"); + logNotice( "SUCCESS connected to Tor control port."); File fileCookie = new File(appCacheHome, TOR_CONTROL_COOKIE); @@ -1036,7 +1040,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst fis.close(); conn.authenticate(cookie); - logNotice( "SUCCESS - authenticated to control port"); + logNotice( "SUCCESS - authenticated to control port."); sendCallbackStatusMessage(getString(R.string.tor_process_starting) + ' ' + getString(R.string.tor_process_complete)); @@ -1046,22 +1050,48 @@ public class TorService extends Service implements TorServiceConstants, TorConst if (ENABLE_DEBUG_LOG) { - //File fileLog = new File(getFilesDir(),"orbot-control-log.txt"); - //PrintWriter pr = new PrintWriter(new FileWriter(fileLog,true)); - //conn.setDebugging(pr); - File fileLog2 = new File(getFilesDir(),"orbot-tor-log.txt"); + fileLog2.setReadable(true); conn.setConf("Log", "debug file " + fileLog2.getCanonicalPath()); - } - String state = conn.getInfo("dormant"); - if (state != null && Integer.parseInt(state) == 0) - currentStatus = STATUS_ON; - else - currentStatus = STATUS_CONNECTING; + currentStatus = STATUS_CONNECTING; + String confSocks = conn.getInfo("net/listeners/socks"); + StringTokenizer st = new StringTokenizer(confSocks," "); + + confSocks = st.nextToken().split(":")[1]; + confSocks = confSocks.substring(0,confSocks.length()-1); + mPortSOCKS = Integer.parseInt(confSocks); + + SharedPreferences prefs = TorServiceUtils.getSharedPrefs(getApplicationContext()); + String socksPortPref = prefs.getString(TorConstants.PREF_SOCKS, TorServiceConstants.PORT_SOCKS_DEFAULT); + + try + { + int newSocksPort = Integer.parseInt(socksPortPref); + ServerSocket ss = new ServerSocket(newSocksPort); + ss.close(); + + ArrayList socksLines = new ArrayList(); + socksLines.add("SOCKSPort " + mPortSOCKS); + socksLines.add("SOCKSPort " + socksPortPref); + + conn.setConf(socksLines); + + mPortSOCKS = newSocksPort; + + sendCallbackLogMessage("Local SOCKS port: " + socksPortPref); + + } + catch (Exception e) + { + //sendCallbackLogMessage("ERROR adding SOCKS on port: " + socksPortPref); + sendCallbackLogMessage("Local SOCKS port: " + socksPortPref); + + } + return Integer.parseInt(torProcId); } @@ -1200,7 +1230,15 @@ public class TorService extends Service implements TorServiceConstants, TorConst * Returns the port number that the HTTP proxy is running on */ public int getHTTPPort() throws RemoteException { - return TorServiceConstants.PORT_HTTP; + return mPortHTTP; + } + + + /** + * Returns the port number that the HTTP proxy is running on + */ + public int getSOCKSPort() throws RemoteException { + return mPortSOCKS; } @@ -1274,7 +1312,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst sb.append("): "); sb.append(status); - logNotice(sb.toString()); + debug(sb.toString()); } @@ -1390,8 +1428,23 @@ public class TorService extends Service implements TorServiceConstants, TorConst sb.append (" > "); } - logNotice(sb.toString()); - + if (ENABLE_DEBUG_LOG) + debug(sb.toString()); + else if(status.equals("BUILT")) + { + + if (currentStatus == STATUS_CONNECTING) + currentStatus = STATUS_ON; + + logNotice(sb.toString()); + + } + else if (status.equals("CLOSED")) + { + logNotice(sb.toString()); + + } + if (mShowExpandedNotifications) { //get IP from last nodename @@ -1407,15 +1460,6 @@ public class TorService extends Service implements TorServiceConstants, TorConst { hmBuiltNodes.remove(node.id); - //how check the IP's of any other nodes we have - for (String nodeId : hmBuiltNodes.keySet()) - { - node = hmBuiltNodes.get(nodeId); - - if (node.ipAddress == null) - mExecutor.execute(new ExternalIPFetcher(node)); - - } } } @@ -1456,7 +1500,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst if (conn != null) { try { - //String nodeDetails = conn.getInfo("ns/id/"+nodes[0].id); + Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 8118)); URLConnection conn = new URL(ONIONOO_BASE_URL + mNode.id).openConnection(proxy); @@ -1683,8 +1727,6 @@ public class TorService extends Service implements TorServiceConstants, TorConst conn.signal("NEWNYM"); - //checkAddressAndCountry(); - } catch (Exception ioe){ debug("error requesting newnym: " + ioe.getLocalizedMessage()); @@ -1772,11 +1814,27 @@ public class TorService extends Service implements TorServiceConstants, TorConst } } + @Override public long[] getBandwidth() throws RemoteException { long[] bw = {lastRead,lastWritten,mTotalTrafficRead,mTotalTrafficWritten}; return bw; } + + @Override + public boolean flushTransProxy () throws RemoteException { + + try + { + return flushTransparentProxyRules(); + } + catch (Exception e) + { + Log.e(TAG,"error in transproxy",e); + return false; + } + + } }; private ArrayList mStatusBuffer = new ArrayList(); diff --git a/src/org/torproject/android/service/TorServiceConstants.java b/src/org/torproject/android/service/TorServiceConstants.java index c65c9a5d..19a99c89 100644 --- a/src/org/torproject/android/service/TorServiceConstants.java +++ b/src/org/torproject/android/service/TorServiceConstants.java @@ -51,7 +51,8 @@ public interface TorServiceConstants { public static int PORT_HTTP = 8118; //just like Privoxy! //Socks port client connects to, server is the Tor binary - public static String PORT_SOCKS_DEFAULT = "9050";//it can be auto, 0 or a port + public static String PORT_SOCKS_DEFAULT = "9050"; + //what is says! public final static String IP_LOCALHOST = "127.0.0.1"; From 79a32e69c8d78f64fcd678f575a3aa570b90a597 Mon Sep 17 00:00:00 2001 From: Nathan Freitas Date: Thu, 21 Aug 2014 09:46:00 -0400 Subject: [PATCH 07/23] make sure control connection is not null on rebind - also tune app conflict checker code for Samsung Link --- src/org/torproject/android/Orbot.java | 92 +++++++++++++++++---------- 1 file changed, 60 insertions(+), 32 deletions(-) diff --git a/src/org/torproject/android/Orbot.java b/src/org/torproject/android/Orbot.java index efb1049d..0de689db 100644 --- a/src/org/torproject/android/Orbot.java +++ b/src/org/torproject/android/Orbot.java @@ -297,17 +297,21 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic SharedPreferences sprefs = TorServiceUtils.getSharedPrefs(getApplicationContext()); boolean showAppConflict = sprefs.getBoolean("pref_show_conflict",true); - - String[] badApps = {"com.sec.msc.nts.android.proxy"}; + + String[] badApps = {"com.sec.msc.nts.android.proxy|com.sec.msc.nts.android.proxy","com.sec.pcw|Samsung Link"}; for (String badApp : badApps) { - if (appInstalledOrNot(badApp)) + String[] badAppParts = badApp.split("|"); + + if (appInstalledOrNot(badAppParts[0])) { + String msg = getString(R.string.please_disable_this_app_in_android_settings_apps_if_you_are_having_problems_with_orbot_) + badAppParts[1]; + if (showAppConflict) - showAlert(getString(R.string.app_conflict),getString(R.string.please_disable_this_app_in_android_settings_apps_if_you_are_having_problems_with_orbot_) + badApp,true); + showAlert(getString(R.string.app_conflict),msg,true); - appendLogTextAndScroll(getString(R.string.please_disable_this_app_in_android_settings_apps_if_you_are_having_problems_with_orbot_) + badApp); + appendLogTextAndScroll(msg); } } @@ -422,19 +426,7 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic //terminology but also making sure there are clear distinctions in control stopTor(); - if (mConnection != null) - unbindService(mConnection); - - //perhaps this should be referenced as INTENT_TOR_SERVICE as in startService - stopService(new Intent(this,TorService.class)); - - //clears all notifications from the status bar - NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); - mNotificationManager.cancelAll(); - - mConnection = null; - mService = null; - + onDestroy(); } catch (RemoteException e) { Log.w(TAG, e); @@ -647,6 +639,8 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic } + setIntent(null); + updateStatus (""); } @@ -768,11 +762,51 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic + + @Override + protected void onActivityResult(int request, int response, Intent data) { + super.onActivityResult(request, response, data); + + if (request == 1 && response == RESULT_OK) + { + if (data != null && data.getBooleanExtra("transproxywipe", false)) + { + try { + + boolean result = mService.flushTransProxy(); + + if (result) + { + + Toast.makeText(this, "Transparent proxy rules flushed!", Toast.LENGTH_SHORT).show(); + + } + else + { + + Toast.makeText(this, "You do not have ROOT access enabled", Toast.LENGTH_SHORT).show(); + + } + } catch (RemoteException e) { + e.printStackTrace(); + } + } + } + } + + + @Override protected void onResume() { super.onResume(); - if (mService != null) + setLocale(); + + if (mService == null) + { + startService(); + } + else { try { @@ -781,17 +815,16 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic if (torStatus != TorServiceConstants.STATUS_ON) mService.processSettings(); - setLocale(); handleIntents(); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } - } - - updateStatus(""); + updateStatus(""); + } + } AlertDialog aDialog = null; @@ -1143,7 +1176,7 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic // we should use this to activity monitor unbind so that we don't have to call // bindService() a million times - private ServiceConnection mConnection = new ServiceConnection() { + private final ServiceConnection mConnection = new ServiceConnection() { public void onServiceConnected(ComponentName className, IBinder service) { @@ -1154,7 +1187,6 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic // representation of that from the raw service object. mService = ITorService.Stub.asInterface(service); - // We want to monitor the service for as long as we are // connected to it. try { @@ -1163,10 +1195,7 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic if (autoStartFromIntent) { - - startTor(); - - + startTor(); } handleIntents(); @@ -1180,9 +1209,7 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic // so there is no need to do anything here. Log.d(TAG,"error registering callback to service",e); } - - } @@ -1195,6 +1222,8 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic Log.d(TAG,"service was disconnected"); } + + }; private void setLocale () @@ -1220,7 +1249,6 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic if (mConnection != null && mService != null) { unbindService(mConnection); - mConnection = null; mService = null; } } From 25bc4c0a6fa8b7c46933f86ff1af142f5cf7559b Mon Sep 17 00:00:00 2001 From: Nathan Freitas Date: Thu, 21 Aug 2014 09:46:36 -0400 Subject: [PATCH 08/23] add new flush transproxy interface method --- src/org/torproject/android/service/ITorService.aidl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/org/torproject/android/service/ITorService.aidl b/src/org/torproject/android/service/ITorService.aidl index 2d57c9b0..07b08a0d 100644 --- a/src/org/torproject/android/service/ITorService.aidl +++ b/src/org/torproject/android/service/ITorService.aidl @@ -52,4 +52,6 @@ interface ITorService { String[] getLog (); long[] getBandwidth (); + + boolean flushTransProxy (); } From 68495c31184202c44ef34cbcf84efdb48ee21860 Mon Sep 17 00:00:00 2001 From: Nathan Freitas Date: Thu, 21 Aug 2014 09:47:01 -0400 Subject: [PATCH 09/23] make delete iptables rules process match add for specific behavior no more drop all output tables, etc --- .../android/service/TorTransProxy.java | 65 +++++++++++-------- 1 file changed, 39 insertions(+), 26 deletions(-) diff --git a/src/org/torproject/android/service/TorTransProxy.java b/src/org/torproject/android/service/TorTransProxy.java index f0117e13..b1e21331 100644 --- a/src/org/torproject/android/service/TorTransProxy.java +++ b/src/org/torproject/android/service/TorTransProxy.java @@ -29,8 +29,7 @@ public class TorTransProxy implements TorServiceConstants { public TorTransProxy (TorService torService, File fileXTables) { mTorService = torService; - mFileXtables = fileXTables; - + mFileXtables = fileXTables; } public void setTransProxyPort (int transProxyPort) @@ -373,7 +372,7 @@ public class TorTransProxy implements TorServiceConstants { logMessage("enabling transproxy for app: " + tApp.getUsername() + " (" + tApp.getUid() + ")"); - dropAllIPv6Traffic(context, tApp.getUid()); + dropAllIPv6Traffic(context, tApp.getUid(),enableRule); script = new StringBuilder(); @@ -532,9 +531,15 @@ public class TorTransProxy implements TorServiceConstants { } - public int dropAllIPv6Traffic (Context context, int appUid) throws Exception + public int dropAllIPv6Traffic (Context context, int appUid, boolean enableDrop) throws Exception { + String action = " -A "; + String chain = "OUTPUT"; + + if (!enableDrop) + action = " -D "; + String ip6tablesPath = getIp6TablesPath(context); Shell shell = Shell.startRootShell(); @@ -543,7 +548,8 @@ public class TorTransProxy implements TorServiceConstants { script = new StringBuilder(); script.append(ip6tablesPath); - script.append(" -A OUTPUT"); + script.append(action); + script.append(chain); if (appUid != -1) { @@ -560,13 +566,13 @@ public class TorTransProxy implements TorServiceConstants { return lastExit; } + /* public int clearAllIPv6Filters (Context context) throws Exception { String ip6tablesPath = getIp6TablesPath(context); Shell shell = Shell.startRootShell(); - StringBuilder script; script = new StringBuilder(); @@ -578,6 +584,29 @@ public class TorTransProxy implements TorServiceConstants { shell.close(); return lastExit; + }*/ + + public int flushTransproxyRules (Context context) throws Exception + { + int exit = -1; + String ipTablesPath = getIpTablesPath(context); + + StringBuilder script = new StringBuilder(); + script.append(ipTablesPath); + script.append(" -t nat"); + script.append(" -F "); + + Shell shell = Shell.startRootShell(); + executeCommand (shell, script.toString()); + + script = new StringBuilder(); + script.append(ipTablesPath); + script.append(" -t filter"); + script.append(" -F "); + + dropAllIPv6Traffic(context,-1,false); + + return exit; } public int setTransparentProxyingAll(Context context, boolean enable) throws Exception @@ -589,10 +618,7 @@ public class TorTransProxy implements TorServiceConstants { if (!enable) action = " -D "; - if (enable) - dropAllIPv6Traffic(context,-1); - else - clearAllIPv6Filters(context); + dropAllIPv6Traffic(context,-1,enable); String ipTablesPath = getIpTablesPath(context); @@ -698,13 +724,13 @@ public class TorTransProxy implements TorServiceConstants { executeCommand (shell, script.toString()); script = new StringBuilder(); - //allow access to local SOCKS port + //allow access to local HTTP port script.append(ipTablesPath); script.append(" -t filter"); script.append(action).append(srcChainName); script.append(" -p tcp"); script.append(" -m tcp"); - script.append(" --dport ").append(PORT_SOCKS_DEFAULT); + script.append(" --dport ").append(mTorService.getHTTPPort()); script.append(" -j ACCEPT"); executeCommand (shell, script.toString()); @@ -716,7 +742,7 @@ public class TorTransProxy implements TorServiceConstants { script.append(action).append(srcChainName); script.append(" -p tcp"); script.append(" -m tcp"); - script.append(" --dport ").append(PORT_HTTP); + script.append(" --dport ").append(mTorService.getSOCKSPort()); script.append(" -j ACCEPT"); executeCommand (shell, script.toString()); @@ -734,19 +760,6 @@ public class TorTransProxy implements TorServiceConstants { executeCommand (shell, script.toString()); script = new StringBuilder(); - //allow access to local DNS port - script.append(ipTablesPath); - script.append(" -t filter"); - script.append(action).append(srcChainName); - script.append(" -p udp"); - script.append(" -m udp"); - script.append(" --dport ").append(mDNSPort); - script.append(" -j ACCEPT"); - - executeCommand (shell, script.toString()); - script = new StringBuilder(); - - // Reject all other packets script.append(ipTablesPath); script.append(" -t filter"); From c78a8225d68871786acd13ccba3979f6d3d95fbe Mon Sep 17 00:00:00 2001 From: Nathan Freitas Date: Thu, 21 Aug 2014 09:47:33 -0400 Subject: [PATCH 10/23] add transproxy flush option to settings --- .../android/settings/SettingsPreferences.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/org/torproject/android/settings/SettingsPreferences.java b/src/org/torproject/android/settings/SettingsPreferences.java index 57d3fc4d..9a8891dd 100644 --- a/src/org/torproject/android/settings/SettingsPreferences.java +++ b/src/org/torproject/android/settings/SettingsPreferences.java @@ -28,6 +28,8 @@ public class SettingsPreferences private CheckBoxPreference prefCBTransProxy = null; private CheckBoxPreference prefcBTransProxyAll = null; + private Preference prefTransProxyFlush = null; + private Preference prefTransProxyApps = null; private CheckBoxPreference prefHiddenServices = null; private CheckBoxPreference prefRequestRoot = null; @@ -37,6 +39,8 @@ public class SettingsPreferences private final static int HIDDEN_SERVICE_PREF_IDX = 6; private final static int TRANSPROXY_GROUP_IDX = 1; + private final static int DEBUG_GROUP_IDX = 8; + protected void onCreate(Bundle savedInstanceState) { @@ -74,6 +78,23 @@ public class SettingsPreferences prefCBTransProxy = ((CheckBoxPreference)((PreferenceCategory)this.getPreferenceScreen().getPreference(TRANSPROXY_GROUP_IDX)).getPreference(0)); prefcBTransProxyAll = (CheckBoxPreference)((PreferenceCategory)this.getPreferenceScreen().getPreference(TRANSPROXY_GROUP_IDX)).getPreference(1); + + prefTransProxyFlush = (Preference)((PreferenceCategory)this.getPreferenceScreen().getPreference(DEBUG_GROUP_IDX)).getPreference(8); + prefTransProxyFlush.setOnPreferenceClickListener(new OnPreferenceClickListener() { + + @Override + public boolean onPreferenceClick(Preference arg0) { + + Intent data = new Intent(); + data.putExtra("transproxywipe", true); + setResult(RESULT_OK,data); + + finish(); + return false; + } + + }); + prefTransProxyApps = ((PreferenceCategory)this.getPreferenceScreen().getPreference(TRANSPROXY_GROUP_IDX)).getPreference(2); From 186e74b5dc5e97b584ee0da863eee4e95ca7b0ab Mon Sep 17 00:00:00 2001 From: Nathan Freitas Date: Thu, 21 Aug 2014 09:47:51 -0400 Subject: [PATCH 11/23] add new transproxy flush pref and strings --- res/values/strings.xml | 3 +++ res/xml/preferences.xml | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/res/values/strings.xml b/res/values/strings.xml index fa4a7319..8e1df637 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -291,4 +291,7 @@ Transproxy Auto-Refresh Re-apply Transproxy rules when the network state changes + Transproxy FORCE REMOVE + Tap here to flush all transproxy network rules NOW + diff --git a/res/xml/preferences.xml b/res/xml/preferences.xml index 95d8f84c..15f1c32a 100644 --- a/res/xml/preferences.xml +++ b/res/xml/preferences.xml @@ -263,6 +263,11 @@ android:summary="@string/pref_transproxy_refresh_summary" android:enabled="true" android:title="@string/pref_transproxy_refresh_title"/> +