Commit 4de66586 authored by Amogh Pradeep's avatar Amogh Pradeep Committed by Georg Koppen
Browse files

Orfox: Centralized proxy applied to AbstractCommunicator and BaseResources.

See Bug 1357997 for partial uplift.

Also:
Bug 28051 - Use our Orbot for proxying our connections

Bug 31144 - ESR68 Network Code Review
parent 44d38a64
Loading
Loading
Loading
Loading
+9 −3
Original line number Diff line number Diff line
@@ -61,6 +61,7 @@ import org.mozilla.gecko.util.GeckoBundle;
import org.mozilla.gecko.util.HardwareUtils;
import org.mozilla.gecko.util.IntentUtils;
import org.mozilla.gecko.util.PRNGFixes;
import org.mozilla.gecko.util.ProxySelector;
import org.mozilla.gecko.util.ShortcutUtils;
import org.mozilla.gecko.util.ThreadUtils;
import org.mozilla.geckoview.GeckoRuntime;
@@ -71,7 +72,7 @@ import java.io.File;
import java.io.InputStream;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URI;
import java.util.UUID;

public class GeckoApplication extends Application
@@ -861,11 +862,16 @@ public class GeckoApplication extends Application
                byte[] buf = Base64.decode(aSrc.substring(dataStart + 1), Base64.DEFAULT);
                image = BitmapUtils.decodeByteArray(buf);
            } else {
                URI uri;
                int byteRead;
                byte[] buf = new byte[4192];
                os = new ByteArrayOutputStream();
                URL url = new URL(aSrc);
                is = url.openStream();
                try {
                    uri = new URI(aSrc);
                } catch (Exception e) {
                    return;
                }
                is = ProxySelector.openConnectionWithProxy(uri).getInputStream();

                // Cannot read from same stream twice. Also, InputStream from
                // URL does not support reset. So converting to byte array.
+12 −9
Original line number Diff line number Diff line
@@ -359,16 +359,19 @@ public class CustomTabsActivity extends AppCompatActivity
    }

    private void performPendingIntent(@NonNull PendingIntent pendingIntent) {
        // Bug 31144 - Fail and return early, prevent potential proxy-bypass.
        return;

        // bug 1337771: If intent-creator haven't set data url, call send() directly won't work.
        final Intent additional = new Intent();
        if (!TextUtils.isEmpty(mCurrentUrl)) {
            additional.setData(Uri.parse(mCurrentUrl));
        }
        try {
            pendingIntent.send(this, 0, additional);
        } catch (PendingIntent.CanceledException e) {
            Log.w(LOGTAG, "Performing a canceled pending intent", e);
        }
        //final Intent additional = new Intent();
        //if (!TextUtils.isEmpty(mCurrentUrl)) {
        //    additional.setData(Uri.parse(mCurrentUrl));
        //}
        //try {
        //    pendingIntent.send(this, 0, additional);
        //} catch (PendingIntent.CanceledException e) {
        //    Log.w(LOGTAG, "Performing a canceled pending intent", e);
        //}
    }

    /**
+7 −3
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import org.mozilla.gecko.overlays.ui.ShareDialog;
import org.mozilla.gecko.menu.MenuItemSwitcherLayout;
import org.mozilla.gecko.util.IOUtils;
import org.mozilla.gecko.util.IntentUtils;
import org.mozilla.gecko.util.ProxySelector;
import org.mozilla.gecko.util.ThreadUtils;

import android.content.Context;
@@ -44,7 +45,8 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;

@@ -344,9 +346,9 @@ public class GeckoActionProvider {
                InputStream is = null;
                try {
                    final byte[] buf = new byte[2048];
                    final URL url = new URL(src);
                    final URI uri = new URI(src);
                    final String filename = URLUtil.guessFileName(src, null, type);
                    is = url.openStream();
                    is = ProxySelector.openConnectionWithProxy(uri).getInputStream();

                    final File imageFile = new File(dir, filename);
                    os = new FileOutputStream(imageFile);
@@ -362,6 +364,8 @@ public class GeckoActionProvider {
                    IOUtils.safeStreamClose(is);
                }
            }
        } catch (URISyntaxException ex) {
            // Handle this the same way as IOException
        } catch (IOException ex) {
            // If something went wrong, we'll just leave the intent un-changed
        } finally {
+35 −33
Original line number Diff line number Diff line
@@ -1810,39 +1810,41 @@ public class GeckoAppShell {

    @WrapForJNI
    private static URLConnection getConnection(final String url) {
        try {
            String spec;
            if (url.startsWith("android://")) {
                spec = url.substring(10);
            } else {
                spec = url.substring(8);
            }

            // Check if we are loading a package icon.
            try {
                if (spec.startsWith("icon/")) {
                    String[] splits = spec.split("/");
                    if (splits.length != 2) {
                        return null;
                    }
                    final String pkg = splits[1];
                    final PackageManager pm = getApplicationContext().getPackageManager();
                    final Drawable d = pm.getApplicationIcon(pkg);
                    final Bitmap bitmap = BitmapUtils.getBitmapFromDrawable(d);
                    return new BitmapConnection(bitmap);
                }
            } catch (Exception ex) {
                Log.e(LOGTAG, "error", ex);
            }

            // if the colon got stripped, put it back
            int colon = spec.indexOf(':');
            if (colon == -1 || colon > spec.indexOf('/')) {
                spec = spec.replaceFirst("/", ":/");
            }
        } catch (Exception ex) {
            return null;
        }
        // Bug 31144 - Prevent potential proxy-bypass

        //try {
        //    String spec;
        //    if (url.startsWith("android://")) {
        //        spec = url.substring(10);
        //    } else {
        //        spec = url.substring(8);
        //    }

        //    // Check if we are loading a package icon.
        //    try {
        //        if (spec.startsWith("icon/")) {
        //            String[] splits = spec.split("/");
        //            if (splits.length != 2) {
        //                return null;
        //            }
        //            final String pkg = splits[1];
        //            final PackageManager pm = getApplicationContext().getPackageManager();
        //            final Drawable d = pm.getApplicationIcon(pkg);
        //            final Bitmap bitmap = BitmapUtils.getBitmapFromDrawable(d);
        //            return new BitmapConnection(bitmap);
        //        }
        //    } catch (Exception ex) {
        //        Log.e(LOGTAG, "error", ex);
        //    }

        //    // if the colon got stripped, put it back
        //    int colon = spec.indexOf(':');
        //    if (colon == -1 || colon > spec.indexOf('/')) {
        //        spec = spec.replaceFirst("/", ":/");
        //    }
        //} catch (Exception ex) {
        //    return null;
        //}
        return null;
    }

+3 −10
Original line number Diff line number Diff line
@@ -103,16 +103,9 @@ public final class BitmapUtils {
    public static Bitmap decodeUrl(final URL url) {
        InputStream stream = null;

        try {
        if ("jar".equals(url.getProtocol())) {
            final Context context = GeckoAppShell.getApplicationContext();
            stream = GeckoJarReader.getStream(context, url.toString());
            } else {
                stream = url.openStream();
            }
        } catch (IOException e) {
            Log.w(LOGTAG, "decodeUrl: IOException downloading " + url);
            return null;
        }

        if (stream == null) {
Loading