Verified Commit 2ad9456f authored by Pier Angelo Vendrame's avatar Pier Angelo Vendrame 🎃
Browse files

fixup! Bug 42247: Android helpers for the TorProvider

Lint with android-format.
parent 28b9a966
Loading
Loading
Loading
Loading
+35 −23
Changes for mobile/android/geckoview/src/main/java/org/mozilla/geckoview/TorIntegrationAndroid.java: 35 added lines, 23 removed lines.
Original line number Diff line number Diff line
@@ -9,11 +9,7 @@ package org.mozilla.geckoview;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

import androidx.annotation.AnyThread;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
@@ -24,14 +20,11 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.mozilla.gecko.EventDispatcher;
import org.mozilla.gecko.GeckoAppShell;
import org.mozilla.gecko.util.BundleEventListener;
import org.mozilla.gecko.util.EventCallback;
import org.mozilla.gecko.util.GeckoBundle;

import org.mozilla.geckoview.androidlegacysettings.TorLegacyAndroidSettings;

public class TorIntegrationAndroid implements BundleEventListener {
@@ -70,23 +63,26 @@ public class TorIntegrationAndroid implements BundleEventListener {
  private final File mDataDir;

  private TorProcess mTorProcess = null;

  /**
   * The first time we run a Tor process in this session, we copy some configuration files to be
   * sure we always have the latest version, but if we re-launch a tor process we do not need to
   * copy them again.
   */
  private boolean mCopiedConfigFiles = false;

  /**
     * Allow multiple proxies to be started, even though it might not actually happen.
     * The key should be positive (also 0 is not allowed).
   * Allow multiple proxies to be started, even though it might not actually happen. The key should
   * be positive (also 0 is not allowed).
   */
  private final HashMap<Integer, MeekTransport> mMeeks = new HashMap<>();

  private int mMeekCounter;

  /**
     * mSettings is a Java-side copy of the authoritative settings in the JS code.
     * It's useful to maintain as the UI may be fetching these options often and we don't watch each
     * fetch to be a passthrough to JS with marshalling/unmarshalling each time.
   * mSettings is a Java-side copy of the authoritative settings in the JS code. It's useful to
   * maintain as the UI may be fetching these options often and we don't watch each fetch to be a
   * passthrough to JS with marshalling/unmarshalling each time.
   */
  private TorSettings mSettings = null;

@@ -235,7 +231,8 @@ public class TorIntegrationAndroid implements BundleEventListener {
    String handle = message.getString("handle", "");
    if (!mTorProcess.getHandle().equals(handle)) {
      GeckoBundle bundle = new GeckoBundle(1);
            bundle.putString("error", "The requested process has not been found. It might have already been stopped.");
      bundle.putString(
          "error", "The requested process has not been found. It might have already been stopped.");
      callback.sendError(bundle);
      return;
    }
@@ -280,7 +277,8 @@ public class TorIntegrationAndroid implements BundleEventListener {
      try {
        copyAndUseConfigFile("--defaults-torrc", "torrc-defaults", args);
      } catch (IOException e) {
                Log.w(TAG, "torrc-default cannot be created, pluggable transports will not be available", e);
        Log.w(
            TAG, "torrc-default cannot be created, pluggable transports will not be available", e);
        copied = false;
      }
      // tor-browser#42607: For now we do not ship geoip databases, as we
@@ -315,7 +313,8 @@ public class TorIntegrationAndroid implements BundleEventListener {
        EventDispatcher.getInstance().dispatch(EVENT_TOR_STARTED, data);
      }
      try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(mProcess.getInputStream()));
        BufferedReader reader =
            new BufferedReader(new InputStreamReader(mProcess.getInputStream()));
        String line;
        while ((line = reader.readLine()) != null) {
          Log.i(TAG, "[tor-" + mHandle + "] " + line);
@@ -370,7 +369,8 @@ public class TorIntegrationAndroid implements BundleEventListener {
      }
    }

        private void copyAndUseConfigFile(String option, String name, ArrayList<String> args) throws IOException {
    private void copyAndUseConfigFile(String option, String name, ArrayList<String> args)
        throws IOException {
      File file = copyConfigFile(name);
      args.add(option);
      args.add(file.getAbsolutePath());
@@ -421,7 +421,10 @@ public class TorIntegrationAndroid implements BundleEventListener {
        try {
          join();
        } catch (InterruptedException e) {
                    Log.e(TAG, "Cannot join the thread for tor process " + mHandle + ", possibly already terminated", e);
          Log.e(
              TAG,
              "Cannot join the thread for tor process " + mHandle + ", possibly already terminated",
              e);
        }
      }
    }
@@ -485,8 +488,8 @@ public class TorIntegrationAndroid implements BundleEventListener {
    /**
     * Parse the standard output of the pluggable transport to find the hostname and port it is
     * listening on.
         * <p>
         * See also the specs for the IPC protocol at https://spec.torproject.org/pt-spec/ipc.html.
     *
     * <p>See also the specs for the IPC protocol at https://spec.torproject.org/pt-spec/ipc.html.
     */
    @Override
    public void run() {
@@ -504,7 +507,8 @@ public class TorIntegrationAndroid implements BundleEventListener {
          Log.d(TAG, "Meek line: " + line);
          // Split produces always at least one item
          String[] tokens = line.split(" ");
                    if ("VERSION".equals(tokens[0]) && (tokens.length != 2 || !PROTOCOL_VERSION.equals(tokens[1]))) {
          if ("VERSION".equals(tokens[0])
              && (tokens.length != 2 || !PROTOCOL_VERSION.equals(tokens[1]))) {
            error = "Bad version: " + line;
            break;
          }
@@ -576,8 +580,11 @@ public class TorIntegrationAndroid implements BundleEventListener {

  public interface BootstrapStateChangeListener {
    void onBootstrapStateChange(String state);

    void onBootstrapProgress(double progress, boolean hasWarnings);

    void onBootstrapComplete();

    void onBootstrapError(String code, String message, String phase, String reason);
  }

@@ -586,7 +593,10 @@ public class TorIntegrationAndroid implements BundleEventListener {
  }

  private @NonNull void reloadSettings() {
        EventDispatcher.getInstance().queryBundle(EVENT_SETTINGS_GET).then( new GeckoResult.OnValueListener<GeckoBundle, Void>() {
    EventDispatcher.getInstance()
        .queryBundle(EVENT_SETTINGS_GET)
        .then(
            new GeckoResult.OnValueListener<GeckoBundle, Void>() {
              public GeckoResult<Void> onValue(final GeckoBundle bundle) {
                mSettings = new TorSettings(bundle);
                return new GeckoResult<Void>();
@@ -601,7 +611,8 @@ public class TorIntegrationAndroid implements BundleEventListener {
  public void setSettings(final TorSettings settings, boolean save, boolean apply) {
    mSettings = settings;

        emitSetSettings(settings, save, apply).then(
    emitSetSettings(settings, save, apply)
        .then(
            new GeckoResult.OnValueListener<Void, Void>() {
              public GeckoResult<Void> onValue(Void v) {
                return new GeckoResult<Void>();
@@ -616,7 +627,8 @@ public class TorIntegrationAndroid implements BundleEventListener {
            });
  }

    private @NonNull GeckoResult<Void> emitSetSettings(final TorSettings settings, boolean save, boolean apply) {
  private @NonNull GeckoResult<Void> emitSetSettings(
      final TorSettings settings, boolean save, boolean apply) {
    GeckoBundle bundle = new GeckoBundle(3);
    bundle.putBoolean("save", save);
    bundle.putBoolean("apply", apply);
+23 −16
Changes for mobile/android/geckoview/src/main/java/org/mozilla/geckoview/TorSettings.java: 23 added lines, 16 removed lines.
Original line number Diff line number Diff line
package org.mozilla.geckoview;

import android.util.Log;

import org.mozilla.gecko.util.GeckoBundle;

public class TorSettings {
@@ -20,10 +19,14 @@ public class TorSettings {

    public static BridgeSource fromInt(int i) {
      switch (i) {
                case -1: return Invalid;
                case 0: return BuiltIn;
                case 1: return BridgeDB;
                case 2: return UserProvided;
        case -1:
          return Invalid;
        case 0:
          return BuiltIn;
        case 1:
          return BridgeDB;
        case 2:
          return UserProvided;
      }
      return Invalid;
    }
@@ -51,10 +54,14 @@ public class TorSettings {

    public static ProxyType fromInt(int i) {
      switch (i) {
                case -1: return Invalid;
                case 0: return Socks4;
                case 1: return Socks5;
                case 2: return HTTPS;
        case -1:
          return Invalid;
        case 0:
          return Socks4;
        case 1:
          return Socks5;
        case 2:
          return HTTPS;
      }
      return Invalid;
    }
@@ -67,7 +74,6 @@ public class TorSettings {
    MeekAzure("meek-azure"),
    Snowflake("snowflake");


    private String type;

    BridgeBuiltinType(String type) {
@@ -80,13 +86,15 @@ public class TorSettings {

    public static BridgeBuiltinType fromString(String s) {
      switch (s) {
                case "obfs4": return Obfs4;
                case "meek-azure": return MeekAzure;
                case "snowflake": return Snowflake;
        case "obfs4":
          return Obfs4;
        case "meek-azure":
          return MeekAzure;
        case "snowflake":
          return Snowflake;
      }
      return Invalid;
    }

  }

  private boolean loaded = false;
@@ -113,8 +121,7 @@ public class TorSettings {
  public boolean firewallEnabled = false;
  public int[] firewallAllowedPorts;

    public TorSettings() {
    }
  public TorSettings() {}

  public TorSettings(GeckoBundle bundle) {
    try {
+7 −10
Changes for mobile/android/geckoview/src/main/java/org/mozilla/geckoview/androidlegacysettings/Prefs.java: 7 added lines, 10 removed lines.
Original line number Diff line number Diff line
@@ -2,28 +2,27 @@ package org.mozilla.geckoview.androidlegacysettings;

import android.content.Context;
import android.content.SharedPreferences;
import org.mozilla.gecko.GeckoAppShell;

import java.util.Locale;
import org.mozilla.gecko.GeckoAppShell;

// tor-android-service utils/Prefs.java

/* package */ class Prefs {
    private final static String PREF_BRIDGES_ENABLED = "pref_bridges_enabled";
    private final static String PREF_BRIDGES_LIST = "pref_bridges_list";
  private static final String PREF_BRIDGES_ENABLED = "pref_bridges_enabled";
  private static final String PREF_BRIDGES_LIST = "pref_bridges_list";

  private static SharedPreferences prefs;

  // OrbotConstants
    private final static String PREF_TOR_SHARED_PREFS = "org.torproject.android_preferences";

  private static final String PREF_TOR_SHARED_PREFS = "org.torproject.android_preferences";

  // tor-android-service utils/TorServiceUtil.java

  private static void setContext() {
    if (prefs == null) {
            prefs = GeckoAppShell.getApplicationContext().getSharedPreferences(PREF_TOR_SHARED_PREFS,
                    Context.MODE_MULTI_PROCESS);
      prefs =
          GeckoAppShell.getApplicationContext()
              .getSharedPreferences(PREF_TOR_SHARED_PREFS, Context.MODE_MULTI_PROCESS);
    }
  }

@@ -67,6 +66,4 @@ import java.util.Locale;
    }
    return list;
  }


}
+2 −1
Changes for mobile/android/geckoview/src/main/java/org/mozilla/geckoview/androidlegacysettings/TorLegacyAndroidSettings.java: 2 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -48,7 +48,8 @@ public class TorLegacyAndroidSettings {
      switch (userDefinedBridgeList) {
        case "obfs4":
        case "snowflake":
                    settings.bridgesBuiltinType = TorSettings.BridgeBuiltinType.fromString(userDefinedBridgeList);
          settings.bridgesBuiltinType =
              TorSettings.BridgeBuiltinType.fromString(userDefinedBridgeList);
          break;
        case "meek":
          settings.bridgesBuiltinType = TorSettings.BridgeBuiltinType.MeekAzure;