[tbb-commits] [Git][tpo/applications/tor-browser][tor-browser-115.7.0esr-13.5-1] fixup! Bug 42247: Android helpers for the TorProvider

Pier Angelo Vendrame (@pierov) git at gitlab.torproject.org
Thu Jan 25 16:44:47 UTC 2024



Pier Angelo Vendrame pushed to branch tor-browser-115.7.0esr-13.5-1 at The Tor Project / Applications / Tor Browser


Commits:
308d6a64 by Pier Angelo Vendrame at 2024-01-25T17:44:22+01:00
fixup! Bug 42247: Android helpers for the TorProvider

Bug 42368: Do not use API26+ java.nio features

- - - - -


1 changed file:

- mobile/android/geckoview/src/main/java/org/mozilla/geckoview/TorIntegrationAndroid.java


Changes:

=====================================
mobile/android/geckoview/src/main/java/org/mozilla/geckoview/TorIntegrationAndroid.java
=====================================
@@ -16,15 +16,10 @@ import androidx.annotation.Nullable;
 
 import java.io.BufferedReader;
 import java.io.File;
+import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.nio.file.Paths;
-import java.nio.file.StandardCopyOption;
-import java.nio.file.attribute.PosixFilePermission;
-import java.nio.file.attribute.PosixFilePermissions;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -69,9 +64,9 @@ public class TorIntegrationAndroid implements BundleEventListener {
     private static final String COOKIE_AUTH_FILE = "/auth-file";
 
     private final String mLibraryDir;
-    private final Path mCacheDir;
+    private final String mCacheDir;
     private final String mIpcDirectory;
-    private final String mDataDir;
+    private final File mDataDir;
 
     private TorProcess mTorProcess = null;
     /**
@@ -91,9 +86,9 @@ public class TorIntegrationAndroid implements BundleEventListener {
 
     /* package */ TorIntegrationAndroid(Context context) {
         mLibraryDir = context.getApplicationInfo().nativeLibraryDir;
-        mCacheDir = context.getCacheDir().toPath();
+        mCacheDir = context.getCacheDir().getAbsolutePath();
         mIpcDirectory = mCacheDir + "/tor-private";
-        mDataDir = context.getDataDir().getAbsolutePath() + "/tor";
+        mDataDir = new File(context.getFilesDir(), "tor");
         registerListener();
     }
 
@@ -260,7 +255,7 @@ public class TorIntegrationAndroid implements BundleEventListener {
             args.add("CookieAuthFile");
             args.add(ipcDir + COOKIE_AUTH_FILE);
             args.add("DataDirectory");
-            args.add(mDataDir);
+            args.add(mDataDir.getAbsolutePath());
             boolean copied = true;
             try {
                 copyAndUseConfigFile("--defaults-torrc", "torrc-defaults", args);
@@ -322,15 +317,19 @@ public class TorIntegrationAndroid implements BundleEventListener {
 
         private void cleanIpcDirectory() {
             File directory = new File(TorIntegrationAndroid.this.mIpcDirectory);
-            if (!Files.isDirectory(directory.toPath())) {
+            if (!directory.isDirectory()) {
                 if (!directory.mkdirs()) {
                     Log.e(TAG, "Failed to create the IPC directory.");
                     return;
                 }
                 try {
-                    Set<PosixFilePermission> chmod = PosixFilePermissions.fromString("rwx------");
-                    Files.setPosixFilePermissions(directory.toPath(), chmod);
-                } catch (IOException e) {
+                    directory.setReadable(false, false);
+                    directory.setReadable(true, true);
+                    directory.setWritable(false, false);
+                    directory.setWritable(true, true);
+                    directory.setExecutable(false, false);
+                    directory.setExecutable(true, true);
+                } catch (SecurityException e) {
                     Log.e(TAG, "Could not set the permissions to the IPC directory.", e);
                 }
                 return;
@@ -347,15 +346,46 @@ public class TorIntegrationAndroid implements BundleEventListener {
         }
 
         private void copyAndUseConfigFile(String option, String name, ArrayList<String> args) throws IOException {
-            final Path path = Paths.get(mCacheDir.toFile().getAbsolutePath(), name);
-            if (!mCopiedConfigFiles || !path.toFile().exists()) {
-                final Context context = GeckoAppShell.getApplicationContext();
-                final InputStream in = context.getAssets().open("common/" + name);
-                Files.copy(in, path, StandardCopyOption.REPLACE_EXISTING);
+            File file = copyConfigFile(name);
+            args.add(option);
+            args.add(file.getAbsolutePath());
+        }
+
+        private File copyConfigFile(String name) throws IOException {
+            final File file = new File(mCacheDir, name);
+            if (mCopiedConfigFiles && file.exists()) {
+                return file;
+            }
+
+            final Context context = GeckoAppShell.getApplicationContext();
+            final InputStream in = context.getAssets().open("common/" + name);
+            // Files.copy is API 26+, so use java.io and a loop for now.
+            FileOutputStream out = null;
+            try {
+                out = new FileOutputStream(file);
+            } catch (IOException e) {
                 in.close();
+                throw e;
             }
-            args.add(option);
-            args.add(path.toString());
+            try {
+                byte buffer[] = new byte[4096];
+                int read;
+                while ((read = in.read(buffer)) >= 0) {
+                    out.write(buffer, 0, read);
+                }
+            } finally {
+                try {
+                    in.close();
+                } catch (IOException e) {
+                    Log.w(TAG, "Cannot close the input stream for " + name);
+                }
+                try {
+                    out.close();
+                } catch (IOException e) {
+                    Log.w(TAG, "Cannot close the output stream for " + name);
+                }
+            }
+            return file;
         }
 
         public void shutdown() {
@@ -406,9 +436,10 @@ public class TorIntegrationAndroid implements BundleEventListener {
             setName("meek-" + id);
             final ProcessBuilder builder = new ProcessBuilder(mLibraryDir + "/libObfs4proxy.so");
             {
+                File ptStateDir = new File(mDataDir, "pt_state");
                 final Map<String, String> env = builder.environment();
                 env.put("TOR_PT_MANAGED_TRANSPORT_VER", "1");
-                env.put("TOR_PT_STATE_LOCATION", mDataDir + "/pt_state");
+                env.put("TOR_PT_STATE_LOCATION", ptStateDir.getAbsolutePath());
                 env.put("TOR_PT_EXIT_ON_STDIN_CLOSE", "1");
                 env.put("TOR_PT_CLIENT_TRANSPORTS", TRANSPORT);
             }



View it on GitLab: https://gitlab.torproject.org/tpo/applications/tor-browser/-/commit/308d6a6487eecfa9927fe52cdb3c0372588bb015

-- 
View it on GitLab: https://gitlab.torproject.org/tpo/applications/tor-browser/-/commit/308d6a6487eecfa9927fe52cdb3c0372588bb015
You're receiving this email because of your account on gitlab.torproject.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.torproject.org/pipermail/tbb-commits/attachments/20240125/07d4b3b5/attachment-0001.htm>


More information about the tbb-commits mailing list