[tor-commits] [orbot/master] switch to native TorService

n8fr8 at torproject.org n8fr8 at torproject.org
Wed Dec 22 21:55:07 UTC 2021


commit c229771b7fbb633842d865f11154ad2884669e44
Author: Hans-Christoph Steiner <hans at eds.org>
Date:   Fri Oct 25 22:38:06 2019 +0200

    switch to native TorService
    
    Signed-off-by: Hans-Christoph Steiner <hans at eds.org>
---
 app-mini/build.gradle                              |   2 +-
 app/build.gradle                                   |   5 +-
 orbotservice/build.gradle                          |   6 +-
 .../torproject/android/service/OrbotService.java   | 408 ++++++---------------
 .../android/service/TorServiceConstants.java       |  10 -
 .../service/util/CustomTorResourceInstaller.java   |  72 +---
 6 files changed, 133 insertions(+), 370 deletions(-)

diff --git a/app-mini/build.gradle b/app-mini/build.gradle
index dd1914d0..5617f1a0 100644
--- a/app-mini/build.gradle
+++ b/app-mini/build.gradle
@@ -100,8 +100,8 @@ android {
         exclude 'lib/arm64-v8a/obfs4proxy.so'
         exclude 'lib/armeabi-v7a/obfs4proxy.so'
         exclude 'lib/armeabi/obfs4proxy.so'
+        exclude 'META-INF/androidx.localbroadcastmanager_localbroadcastmanager.version'
     }
-
 }
 
 
diff --git a/app/build.gradle b/app/build.gradle
index 70349b22..8d5e3ea7 100644
--- a/app/build.gradle
+++ b/app/build.gradle
@@ -110,8 +110,11 @@ android {
             universalApk true
         }
     }
-}
 
+    packagingOptions {
+        exclude 'META-INF/androidx.localbroadcastmanager_localbroadcastmanager.version'
+    }
+}
 
 dependencies {
     implementation project(':orbotservice')
diff --git a/orbotservice/build.gradle b/orbotservice/build.gradle
index f5ef1906..0d6b162e 100644
--- a/orbotservice/build.gradle
+++ b/orbotservice/build.gradle
@@ -41,11 +41,13 @@ android {
         lintConfig file("../lint.xml")
     }
 
+    packagingOptions {
+        exclude 'META-INF/androidx.localbroadcastmanager_localbroadcastmanager.version'
+    }
 }
 
 dependencies {
-
-    implementation 'org.torproject:tor-android-binary:0.4.4.6'
+    implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0'
 
     /**
     implementation 'info.pluggabletransports.aptds:apt-dispatch-library:1.0.9'
diff --git a/orbotservice/src/main/java/org/torproject/android/service/OrbotService.java b/orbotservice/src/main/java/org/torproject/android/service/OrbotService.java
index 6808f088..396881d3 100644
--- a/orbotservice/src/main/java/org/torproject/android/service/OrbotService.java
+++ b/orbotservice/src/main/java/org/torproject/android/service/OrbotService.java
@@ -15,12 +15,14 @@ import android.app.NotificationManager;
 import android.app.PendingIntent;
 import android.app.Service;
 import android.content.BroadcastReceiver;
+import android.content.ComponentName;
 import android.content.ContentResolver;
 import android.content.ContentValues;
 import android.content.Context;
 import android.content.ContextWrapper;
 import android.content.Intent;
 import android.content.IntentFilter;
+import android.content.ServiceConnection;
 import android.content.SharedPreferences;
 import android.content.pm.PackageManager;
 import android.database.Cursor;
@@ -28,21 +30,16 @@ import android.net.Uri;
 import android.net.VpnService;
 import android.os.Build;
 import android.os.Handler;
+import android.os.HandlerThread;
 import android.os.IBinder;
+import android.os.Looper;
 import android.provider.BaseColumns;
 import android.text.TextUtils;
 import android.util.Log;
 
-import androidx.annotation.RequiresApi;
-import androidx.core.app.NotificationCompat;
-import androidx.localbroadcastmanager.content.LocalBroadcastManager;
-
-import com.jaredrummler.android.shell.CommandResult;
-
 import net.freehaven.tor.control.TorControlCommands;
 import net.freehaven.tor.control.TorControlConnection;
 
-import org.torproject.android.service.util.CustomShell;
 import org.torproject.android.service.util.CustomTorResourceInstaller;
 import org.torproject.android.service.util.DummyActivity;
 import org.torproject.android.service.util.Prefs;
@@ -50,21 +47,19 @@ import org.torproject.android.service.util.TorServiceUtils;
 import org.torproject.android.service.util.Utils;
 import org.torproject.android.service.vpn.OrbotVpnManager;
 import org.torproject.android.service.vpn.VpnPrefs;
+import org.torproject.jni.TorService;
 
 import java.io.BufferedReader;
 import java.io.ByteArrayOutputStream;
-import java.io.DataInputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
-import java.io.FileReader;
 import java.io.FileWriter;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.PrintStream;
 import java.io.PrintWriter;
-import java.net.Socket;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
@@ -78,12 +73,15 @@ import java.util.concurrent.Executors;
 import java.util.concurrent.TimeoutException;
 
 import IPtProxy.IPtProxy;
+import androidx.annotation.RequiresApi;
+import androidx.core.app.NotificationCompat;
+import androidx.localbroadcastmanager.content.LocalBroadcastManager;
 
 public class OrbotService extends VpnService implements TorServiceConstants, OrbotConstants {
 
-    public final static String BINARY_TOR_VERSION = org.torproject.android.binary.TorServiceConstants.BINARY_TOR_VERSION;
+    public final static String BINARY_TOR_VERSION = TorService.VERSION_NAME;
+
     static final int NOTIFY_ID = 1;
-    private final static int CONTROL_SOCKET_TIMEOUT = 60000;
     private static final int ERROR_NOTIFY_ID = 3;
     private static final int HS_NOTIFY_ID = 4;
     private static final Uri V2_HS_CONTENT_URI = Uri.parse("content://org.torproject.android.ui.hiddenservices.providers/hs");
@@ -121,14 +119,13 @@ public class OrbotService extends VpnService implements TorServiceConstants, Orb
             V3ClientAuth.HASH,
             V3ClientAuth.ENABLED
     };
+
     public static int mPortSOCKS = -1;
     public static int mPortHTTP = -1;
     public static int mPortDns = TOR_DNS_PORT_DEFAULT;
     public static int mPortTrans = TOR_TRANSPROXY_PORT_DEFAULT;
     public static File appBinHome;
     public static File appCacheHome;
-    public static File fileTor;
-    public static File fileTorRc;
     private final ExecutorService mExecutor = Executors.newCachedThreadPool();
     boolean mIsLollipop = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
     TorEventHandler mEventHandler;
@@ -139,8 +136,9 @@ public class OrbotService extends VpnService implements TorServiceConstants, Orb
     ActionBroadcastReceiver mActionBroadcastReceiver;
     private String mCurrentStatus = STATUS_OFF;
     private TorControlConnection conn = null;
-    private int mLastProcessId = -1;
-    private File fileControlPort, filePid;
+    private ServiceConnection torServiceConnection;
+    private TorService torService;
+    private boolean shouldUnbindTorService;
     private NotificationManager mNotificationManager = null;
     private NotificationCompat.Builder mNotifyBuilder;
     private boolean mNotificationShowing = false;
@@ -187,22 +185,6 @@ public class OrbotService extends VpnService implements TorServiceConstants, Orb
         showToolbarNotification(getString(R.string.status_activated), NOTIFY_ID, R.drawable.ic_stat_tor);
     }
 
-    private boolean findExistingTorDaemon() {
-        try {
-            mLastProcessId = initControlConnection(1, true);
-
-            if (mLastProcessId != -1 && conn != null) {
-                sendCallbackLogMessage(getString(R.string.found_existing_tor_process));
-                sendCallbackStatus(STATUS_ON);
-                showConnectedToTorNetworkNotification();
-                return true;
-            }
-        } catch (Exception e) {
-            debug("Error finding existing tor daemon: " + e);
-        }
-        return false;
-    }
-
     @Override
     public void onLowMemory() {
         super.onLowMemory();
@@ -283,7 +265,7 @@ public class OrbotService extends VpnService implements TorServiceConstants, Orb
         showToolbarNotification("", NOTIFY_ID, R.drawable.ic_stat_tor);
 
         if (intent != null)
-            exec(new IncomingIntentRouter(intent));
+            mExecutor.execute(new IncomingIntentRouter(intent));
         else
             Log.d(OrbotConstants.TAG, "Got null onStartCommand() intent");
 
@@ -342,6 +324,13 @@ public class OrbotService extends VpnService implements TorServiceConstants, Orb
         }).start();
     }
 
+    private void stopTorOnError(String message) {
+        stopTorAsync();
+        showToolbarNotification(
+                getString(R.string.unable_to_start_tor) + ": " + message,
+                ERROR_NOTIFY_ID, R.drawable.ic_stat_notifyerr);
+    }
+
     private static boolean useIPtObfsMeekProxy() {
         String bridgeList = Prefs.getBridgesList();
         return bridgeList.contains("obfs") || bridgeList.contains("meek");
@@ -438,6 +427,11 @@ public class OrbotService extends VpnService implements TorServiceConstants, Orb
                     Log.d(OrbotConstants.TAG, "error shutting down Tor via connection", e);
                 }
 
+                if (shouldUnbindTorService) {
+                    unbindService(torServiceConnection);
+                    shouldUnbindTorService = false;
+                }
+
                 conn = null;
                 break;
             }
@@ -491,10 +485,6 @@ public class OrbotService extends VpnService implements TorServiceConstants, Orb
             if (!appCacheHome.exists())
                 appCacheHome.mkdirs();
 
-            fileTorRc = new File(appBinHome, TORRC_ASSET_KEY);
-            fileControlPort = new File(getFilesDir(), TOR_CONTROL_PORT_FILE);
-            filePid = new File(getFilesDir(), TOR_PID_FILE);
-
             mHSBasePath = new File(getFilesDir().getAbsolutePath(), TorServiceConstants.HIDDEN_SERVICES_DIR);
             if (!mHSBasePath.isDirectory())
                 mHSBasePath.mkdirs();
@@ -525,20 +515,11 @@ public class OrbotService extends VpnService implements TorServiceConstants, Orb
             if (Build.VERSION.SDK_INT >= 26)
                 createNotificationChannel();
 
-            torUpgradeAndConfig();
+            CustomTorResourceInstaller installer = new CustomTorResourceInstaller(this, appBinHome);
+            installer.installGeoIP();
 
             pluggableTransportInstall();
 
-            new Thread(() -> {
-                try {
-                    findExistingTorDaemon();
-                } catch (Exception e) {
-                    Log.e(OrbotConstants.TAG, "error onBind", e);
-                    logNotice("error finding exiting process: " + e.toString());
-                }
-
-            }).start();
-
             mVpnManager = new OrbotVpnManager(this);
 
             loadCdnFronts(this);
@@ -568,37 +549,12 @@ public class OrbotService extends VpnService implements TorServiceConstants, Orb
         return false;
     }
 
-    private boolean torUpgradeAndConfig() throws IOException, TimeoutException {
-
-        SharedPreferences prefs = Prefs.getSharedPrefs(getApplicationContext());
-        String version = prefs.getString(PREF_BINARY_TOR_VERSION_INSTALLED, null);
-
-        logNotice("checking binary version: " + version);
-
-        CustomTorResourceInstaller installer = new CustomTorResourceInstaller(this, appBinHome);
-        logNotice("upgrading binaries to latest version: " + BINARY_TOR_VERSION);
-
-        fileTor = installer.installResources();
-
-        if (fileTor != null && fileTor.canExecute()) {
-            prefs.edit().putString(PREF_BINARY_TOR_VERSION_INSTALLED, BINARY_TOR_VERSION).apply();
-
-            fileTorRc = new File(appBinHome, "torrc");//installer.getTorrcFile();
-            return fileTorRc.exists();
-        }
-
-        return false;
-    }
-
     private File updateTorrcCustomFile() throws IOException, TimeoutException {
         SharedPreferences prefs = Prefs.getSharedPrefs(getApplicationContext());
 
         StringBuffer extraLines = new StringBuffer();
 
         extraLines.append("\n");
-        extraLines.append("ControlPortWriteToFile").append(' ').append(fileControlPort.getCanonicalPath()).append('\n');
-
-        extraLines.append("PidFile").append(' ').append(filePid.getCanonicalPath()).append('\n');
 
         extraLines.append("RunAsDaemon 0").append('\n');
         extraLines.append("AvoidDiskWrites 0").append('\n');
@@ -692,15 +648,9 @@ public class OrbotService extends VpnService implements TorServiceConstants, Orb
 
         debug("torrc.custom=" + extraLines.toString());
 
-        File fileTorRcCustom = new File(fileTorRc.getAbsolutePath() + ".custom");
-        boolean success = updateTorConfigCustom(fileTorRcCustom, extraLines.toString());
-
-        if (success && fileTorRcCustom.exists()) {
-            logNotice("success.");
-            return fileTorRcCustom;
-        } else
-            return null;
-
+        File fileTorRcCustom = TorService.getTorrc(this);
+        updateTorConfigCustom(fileTorRcCustom, extraLines.toString());
+        return fileTorRcCustom;
     }
 
     private String checkPortOrAuto(String portString) {
@@ -771,36 +721,17 @@ public class OrbotService extends VpnService implements TorServiceConstants, Orb
                 // these states should probably be handled better
                 sendCallbackLogMessage("Ignoring start request, currently " + mCurrentStatus);
                 return;
-            } else if (mCurrentStatus.equals(STATUS_ON) && (mLastProcessId != -1)) {
+            } else if (mCurrentStatus.equals(STATUS_ON)) {
                 showConnectedToTorNetworkNotification();
                 sendCallbackLogMessage("Ignoring start request, already started.");
-                // setTorNetworkEnabled (true);
-
                 return;
             }
 
             sendCallbackStatus(STATUS_STARTING);
 
-            try {
-                if (conn != null) {
-                    String torProcId = conn.getInfo("process/pid");
-                    if (!TextUtils.isEmpty(torProcId))
-                        mLastProcessId = Integer.parseInt(torProcId);
-                } else {
-                    if (fileControlPort != null && fileControlPort.exists())
-                        findExistingTorDaemon();
-
-                }
-            } catch (Exception e) {
-            }
-
             // make sure there are no stray daemons running
             stopTorDaemon(false);
 
-            SharedPreferences prefs = Prefs.getSharedPrefs(getApplicationContext());
-            String version = prefs.getString(PREF_BINARY_TOR_VERSION_INSTALLED, null);
-            logNotice("checking binary version: " + version);
-
             showToolbarNotification(getString(R.string.status_starting_up), NOTIFY_ID, R.drawable.ic_stat_tor);
             //sendCallbackLogMessage(getString(R.string.status_starting_up));
             //logNotice(getString(R.string.status_starting_up));
@@ -812,27 +743,20 @@ public class OrbotService extends VpnService implements TorServiceConstants, Orb
                     customEnv.add("TOR_PT_PROXY=socks5://" + OrbotVpnManager.sSocksProxyLocalhost + ":" + OrbotVpnManager.sSocksProxyServerPort);
                 }
 
-            boolean success = runTorShellCmd();
-
-            if (success) {
-                try {
-                    updateLegacyV2OnionNames();
-                } catch (SecurityException se) {
-                    logNotice("unable to upload legacy v2 onion names");
-                }
-                try {
-                    updateV3OnionNames();
-                } catch (SecurityException se) {
-                    logNotice("unable to upload v3 onion names");
-                }
+            runTorShellCmd();
+            try {
+                updateLegacyV2OnionNames();
+            } catch (SecurityException se) {
+                logNotice("unable to upload legacy v2 onion names");
+            }
+            try {
+                updateV3OnionNames();
+            } catch (SecurityException se) {
+                logNotice("unable to upload v3 onion names");
             }
-
         } catch (Exception e) {
             logException("Unable to start Tor: " + e.toString(), e);
-            stopTorAsync();
-            showToolbarNotification(
-                    getString(R.string.unable_to_start_tor) + ": " + e.getMessage(),
-                    ERROR_NOTIFY_ID, R.drawable.ic_stat_notifyerr);
+            stopTorOnError(e.getLocalizedMessage());
         }
     }
 
@@ -913,131 +837,80 @@ public class OrbotService extends VpnService implements TorServiceConstants, Orb
         }
     }
 
-    private boolean runTorShellCmd() throws Exception {
-        File fileTorrcCustom = updateTorrcCustomFile();
-
-        //make sure Tor exists and we can execute it
-        if (fileTor == null || (!fileTor.exists()) || (!fileTor.canExecute()))
-            return false;
-
-        if ((!fileTorRc.exists()) || (!fileTorRc.canRead()))
-            return false;
+    private void runTorShellCmd() throws Exception {
+        updateTorConfigCustom(TorService.getDefaultsTorrc(this),
+                "DNSPort 0\n" +
+                "TransPort 0\n" +
+                "DisableNetwork 1\n");
 
+        File fileTorrcCustom = updateTorrcCustomFile();
         if ((!fileTorrcCustom.exists()) || (!fileTorrcCustom.canRead()))
-            return false;
+            return;
 
         sendCallbackLogMessage(getString(R.string.status_starting_up));
 
-        String torCmdString = fileTor.getAbsolutePath()
-                + " DataDirectory " + appCacheHome.getAbsolutePath()
-                + " --defaults-torrc " + fileTorRc.getAbsolutePath()
-                + " -f " + fileTorrcCustom.getAbsolutePath();
-
-        int exitCode;
-
-        try {
-            exitCode = exec(torCmdString + " --verify-config", true);
-        } catch (Exception e) {
-            logNotice("Tor configuration did not verify: " + e.getMessage());
-            return false;
-        }
-
-        if (exitCode == 0) {
-            logNotice("Tor configuration VERIFIED.");
-            try {
-                exitCode = exec(torCmdString, false);
-            } catch (Exception e) {
-                logNotice("Tor was unable to start: " + e.getMessage());
-                throw new Exception("Tor was unable to start: " + e.getMessage());
+        torServiceConnection = new ServiceConnection() {
+            @Override
+            public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
+                torService = ((TorService.LocalBinder) iBinder).getService();
+                conn = torService.getTorControlConnection();
+                addEventHandler();
             }
 
-            if (exitCode != 0) {
-                logNotice("Tor did not start. Exit:" + exitCode);
-                return false;
+            @Override
+            public void onServiceDisconnected(ComponentName componentName) {
+                conn = null;
+                torService = null;
             }
 
-            //now try to connect
-            mLastProcessId = initControlConnection(10, false);
+            @Override
+            public void onNullBinding(ComponentName componentName) {
+                stopTorOnError("Tor was unable to start: " + "onNullBinding");
+                conn = null;
+                torService = null;
+            }
 
-            if (mLastProcessId == -1) {
-                logNotice(getString(R.string.couldn_t_start_tor_process_) + "; exit=" + exitCode);
-                throw new Exception(getString(R.string.couldn_t_start_tor_process_) + "; exit=" + exitCode);
-            } else {
-                logNotice("Tor started; process id=" + mLastProcessId);
+            @Override
+            public void onBindingDied(ComponentName componentName) {
+                stopTorOnError("Tor was unable to start: " + "onBindingDied");
+                conn = null;
+                torService = null;
+            }
+        };
+
+        BroadcastReceiver receiver = new BroadcastReceiver() {
+            @Override
+            public void onReceive(Context context, Intent intent) {
+                if (TorService.ACTION_STATUS.equals(intent.getAction())
+                        && TorService.STATUS_ON.equals(intent.getStringExtra(TorService.EXTRA_STATUS))) {
+                    initControlConnection();
+                    unregisterReceiver(this);
+                }
             }
+        };
+        // run the BroadcastReceiver in its own thread
+        HandlerThread handlerThread = new HandlerThread(receiver.getClass().getSimpleName());
+        handlerThread.start();
+        Looper looper = handlerThread.getLooper();
+        Handler handler = new Handler(looper);
+        registerReceiver(receiver, new IntentFilter(TorService.ACTION_STATUS), null, handler);
+
+        Intent serviceIntent = new Intent(this, TorService.class);
+        if (Build.VERSION.SDK_INT < 29) {
+            shouldUnbindTorService = bindService(serviceIntent, torServiceConnection, BIND_AUTO_CREATE);
+        } else {
+            shouldUnbindTorService = bindService(serviceIntent, BIND_AUTO_CREATE, mExecutor, torServiceConnection);
         }
-
-        return true;
     }
 
     protected void exec(Runnable runn) {
         mExecutor.execute(runn);
     }
 
-    private int exec(String cmd, boolean wait) throws Exception {
-        HashMap<String, String> mapEnv = new HashMap<>();
-        mapEnv.put("HOME", appBinHome.getAbsolutePath());
-
-        CommandResult result = CustomShell.run("sh", wait, mapEnv, cmd);
-        debug("executing: " + cmd);
-        debug("stdout: " + result.getStdout());
-        debug("stderr: " + result.getStderr());
-
-        return result.exitCode;
-    }
-
-    private int initControlConnection(int maxTries, boolean isReconnect) throws Exception {
-        int controlPort = -1;
-        int attempt = 0;
-
-        logNotice(getString(R.string.waiting_for_control_port));
-
-        while (conn == null && attempt++ < maxTries && (!mCurrentStatus.equals(STATUS_OFF))) {
-            try {
-                controlPort = getControlPort();
-                if (controlPort != -1) {
-                    logNotice(getString(R.string.connecting_to_control_port) + controlPort);
-                    break;
-                }
-
-            } catch (Exception ce) {
-                conn = null;
-                //    logException( "Error connecting to Tor local control port: " + ce.getMessage(),ce);
-            }
-
-            try {
-                //    logNotice("waiting...");
-                Thread.sleep(2000);
-            } catch (Exception e) {
-            }
-        }
-
-        if (controlPort != -1) {
-            Socket torConnSocket = new Socket(IP_LOCALHOST, controlPort);
-            torConnSocket.setSoTimeout(CONTROL_SOCKET_TIMEOUT);
-            conn = new TorControlConnection(torConnSocket);
-            conn.launchThread(true);//is daemon
-        }
-
+    private void initControlConnection() {
         if (conn != null) {
             logNotice("SUCCESS connected to Tor control port.");
-
-            File fileCookie = new File(appCacheHome, TOR_CONTROL_COOKIE);
-
-            if (fileCookie.exists()) {
-                logNotice("adding control port event handler");
-
-                conn.setEventHandler(mEventHandler);
-
-                logNotice("SUCCESS added control port event handler");
-                byte[] cookie = new byte[(int) fileCookie.length()];
-                DataInputStream fis = new DataInputStream(new FileInputStream(fileCookie));
-                fis.read(cookie);
-                fis.close();
-                conn.authenticate(cookie);
-
-                logNotice("SUCCESS - authenticated to control port.");
-
+            try {
                 //       conn.setEvents(Arrays.asList(new String[]{"DEBUG","STATUS_CLIENT","STATUS_GENERAL","BW"}));
 
                 if (Prefs.useDebugLogging())
@@ -1047,8 +920,6 @@ public class OrbotService extends VpnService implements TorServiceConstants, Orb
 
                 //  sendCallbackLogMessage(getString(R.string.tor_process_starting) + ' ' + getString(R.string.tor_process_complete));
 
-                String torProcId = conn.getInfo("process/pid");
-
                 String confSocks = conn.getInfo("net/listeners/socks");
                 StringTokenizer st = new StringTokenizer(confSocks, " ");
 
@@ -1081,76 +952,12 @@ public class OrbotService extends VpnService implements TorServiceConstants, Orb
                 }
 
                 sendCallbackPorts(mPortSOCKS, mPortHTTP, mPortDns, mPortTrans);
-                setTorNetworkEnabled(true);
-
-                return Integer.parseInt(torProcId);
 
-            } else {
-                logNotice("Tor authentication cookie does not exist yet");
+            } catch (IOException e) {
+                e.printStackTrace();
+                stopTorOnError(e.getLocalizedMessage());
                 conn = null;
-
-            }
-        }
-
-        throw new Exception("Tor control port could not be found");
-    }
-
-    private int getControlPort() {
-        int result = -1;
-
-        try {
-            if (fileControlPort.exists()) {
-                debug("Reading control port config file: " + fileControlPort.getCanonicalPath());
-                BufferedReader bufferedReader = new BufferedReader(new FileReader(fileControlPort));
-                String line = bufferedReader.readLine();
-
-                if (line != null) {
-                    String[] lineParts = line.split(":");
-                    result = Integer.parseInt(lineParts[1]);
-                }
-
-
-                bufferedReader.close();
-
-                //store last valid control port
-                SharedPreferences prefs = Prefs.getSharedPrefs(getApplicationContext());
-                prefs.edit().putInt("controlport", result).apply();
-            } else {
-                debug("Control Port config file does not yet exist (waiting for tor): " + fileControlPort.getCanonicalPath());
             }
-        } catch (FileNotFoundException e) {
-            debug("unable to get control port; file not found");
-        } catch (Exception e) {
-            debug("unable to read control port config file");
-        }
-
-        return result;
-    }
-
-    public String getInfo(String key) {
-        try {
-            if (conn != null) {
-                return conn.getInfo(key);
-            }
-        } catch (Exception ioe) {
-            //    Log.e(TAG,"Unable to get Tor information",ioe);
-            logNotice("Unable to get Tor information" + ioe.getMessage());
-        }
-        return null;
-    }
-
-    public void setTorNetworkEnabled(final boolean isEnabled) throws IOException {
-        if (conn != null) { // it is possible to not have a connection yet, and someone might try to newnym
-            new Thread() {
-                public void run() {
-                    try {
-                        final String newValue = isEnabled ? "0" : "1";
-                        conn.setConf("DisableNetwork", newValue);
-                    } catch (Exception ioe) {
-                        debug("error requesting newnym: " + ioe.getLocalizedMessage());
-                    }
-                }
-            }.start();
         }
     }
 
@@ -1161,6 +968,19 @@ public class OrbotService extends VpnService implements TorServiceConstants, Orb
             } catch (IOException e) {
                 debug("error send active: " + e.getLocalizedMessage());
             }
+	}
+    }
+
+    public void addEventHandler() {
+        try {
+            logNotice("adding control port event handler");
+            conn.setEventHandler(mEventHandler);
+            conn.setEvents(Arrays.asList("ORCONN", "CIRC", "NOTICE", "WARN", "ERR", "BW"));
+            logNotice("SUCCESS added control port event handler");
+        } catch (IOException e) {
+            e.printStackTrace();
+            stopTorOnError(e.getLocalizedMessage());
+            conn = null;
         }
     }
 
diff --git a/orbotservice/src/main/java/org/torproject/android/service/TorServiceConstants.java b/orbotservice/src/main/java/org/torproject/android/service/TorServiceConstants.java
index e69d8ba6..d2dc4877 100644
--- a/orbotservice/src/main/java/org/torproject/android/service/TorServiceConstants.java
+++ b/orbotservice/src/main/java/org/torproject/android/service/TorServiceConstants.java
@@ -9,14 +9,6 @@ public interface TorServiceConstants {
 
     String DIRECTORY_TOR_DATA = "tordata";
 
-    String TOR_CONTROL_PORT_FILE = "control.txt";
-    String TOR_PID_FILE = "torpid";
-
-    //torrc (tor config file)
-    String TORRC_ASSET_KEY = "torrc";
-
-    String TOR_CONTROL_COOKIE = "control_auth_cookie";
-
     //geoip data file asset key
     String GEOIP_ASSET_KEY = "geoip";
     String GEOIP6_ASSET_KEY = "geoip6";
@@ -106,8 +98,6 @@ public interface TorServiceConstants {
     String CMD_SET_EXIT = "setexit";
     String CMD_ACTIVE = "ACTIVE";
 
-    String PREF_BINARY_TOR_VERSION_INSTALLED = "BINARY_TOR_VERSION_INSTALLED";
-
     //obfsproxy
     String OBFSCLIENT_ASSET_KEY = "obfs4proxy";
 
diff --git a/orbotservice/src/main/java/org/torproject/android/service/util/CustomTorResourceInstaller.java b/orbotservice/src/main/java/org/torproject/android/service/util/CustomTorResourceInstaller.java
index 8bec9ccc..b39b6ae7 100644
--- a/orbotservice/src/main/java/org/torproject/android/service/util/CustomTorResourceInstaller.java
+++ b/orbotservice/src/main/java/org/torproject/android/service/util/CustomTorResourceInstaller.java
@@ -4,27 +4,27 @@ import android.content.Context;
 import android.content.pm.ApplicationInfo;
 import android.util.Log;
 
-import org.torproject.android.binary.TorServiceConstants;
-
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.util.concurrent.TimeoutException;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipInputStream;
 
-public class CustomTorResourceInstaller implements TorServiceConstants {
+public class CustomTorResourceInstaller {
+
+    private static final String TAG = "CustomTorResourceInstaller";
 
+    String COMMON_ASSET_KEY = "common/";
+
+    //geoip data file asset key
+    String GEOIP_ASSET_KEY = "geoip";
+    String GEOIP6_ASSET_KEY = "geoip6";
 
     private File installFolder;
     private Context context;
 
-    private File fileTorrc;
-    private File fileTor;
-
     public CustomTorResourceInstaller(Context context, File installFolder) {
         this.installFolder = installFolder;
         this.context = context;
@@ -40,7 +40,7 @@ public class CustomTorResourceInstaller implements TorServiceConstants {
      * Write the inputstream contents to the file
      */
     private static boolean streamToFile(InputStream stm, File outFile, boolean append, boolean zip) throws IOException {
-        byte[] buffer = new byte[FILE_WRITE_BUFFER_SIZE];
+        byte[] buffer = new byte[1024];
 
         int bytecount;
 
@@ -101,64 +101,12 @@ public class CustomTorResourceInstaller implements TorServiceConstants {
     //
     /*
      * Extract the Tor resources from the APK file using ZIP
-     *
-     * @File path to the Tor executable
      */
-    public File installResources() throws IOException, TimeoutException {
-
-        fileTor = new File(installFolder, TOR_ASSET_KEY);
-
+    public void installGeoIP() throws IOException {
         if (!installFolder.exists())
             installFolder.mkdirs();
-
-        installGeoIP();
-        fileTorrc = assetToFile(COMMON_ASSET_KEY + TORRC_ASSET_KEY, TORRC_ASSET_KEY, false, false);
-
-        File fileNativeDir = new File(getNativeLibraryDir(context));
-        fileTor = new File(fileNativeDir, TOR_ASSET_KEY + ".so");
-
-        if (fileTor.exists()) {
-            if (fileTor.canExecute())
-                return fileTor;
-            else {
-                setExecutable(fileTor);
-
-                if (fileTor.canExecute())
-                    return fileTor;
-            }
-        }
-
-        File fileTorBin = new File(installFolder, TOR_BINARY_KEY);
-
-        //it exists but we can't execute it, so copy it to a new path
-        if (fileTor.exists()) {
-            InputStream is = new FileInputStream(fileTor);
-            streamToFile(is, fileTorBin, false, true);
-            setExecutable(fileTorBin);
-
-            if (fileTorBin.exists() && fileTorBin.canExecute())
-                return fileTorBin;
-        }
-
-        //let's try another approach
-        fileTor = CustomNativeLoader.loadNativeBinary(context, TOR_ASSET_KEY, fileTorBin);
-
-        if (fileTor != null && fileTor.exists())
-            setExecutable(fileTor);
-
-        if (fileTor != null && fileTor.exists() && fileTor.canExecute())
-            return fileTor;
-
-        return null;
-    }
-
-    private boolean installGeoIP() throws IOException {
-
         assetToFile(COMMON_ASSET_KEY + GEOIP_ASSET_KEY, GEOIP_ASSET_KEY, false, false);
-
         assetToFile(COMMON_ASSET_KEY + GEOIP6_ASSET_KEY, GEOIP6_ASSET_KEY, false, false);
-
-        return true;
     }
 
     /*





More information about the tor-commits mailing list