2

After the last update of Android System Webview (30th of May, 2018) proxy settings stopped working for me and proxy is just not working anymore for a webview. In other browsers with proxy it's the same effect, proxy not setting, blocked websites not opening, ip not changing. Returned error is either host not resolver or connection failed. My device is Nexus 5X, Android 8.1.0. Did anybody else face the same issue?

I'm using this for setting proxy:

private static boolean setProxyKKPlus(WebView webView, String host, int port, String applicationClassName) {
        Log.d(LOG_TAG, "Setting proxy with >= 4.4 API.");

        Context appContext = webView.getContext().getApplicationContext();
        System.setProperty("http.proxyHost", host);
        System.setProperty("http.proxyPort", port + "");
        System.setProperty("https.proxyHost", host);
        System.setProperty("https.proxyPort", port + "");
        try {
            Class applictionCls = Class.forName(applicationClassName);
            Field loadedApkField = applictionCls.getField("mLoadedApk");
            loadedApkField.setAccessible(true);
            Object loadedApk = loadedApkField.get(appContext);
            Class loadedApkCls = Class.forName("android.app.LoadedApk");
            Field receiversField = loadedApkCls.getDeclaredField("mReceivers");
            receiversField.setAccessible(true);
            ArrayMap receivers = (ArrayMap) receiversField.get(loadedApk);
            for (Object receiverMap : receivers.values()) {
                for (Object rec : ((ArrayMap) receiverMap).keySet()) {
                    Class clazz = rec.getClass();
                    if (clazz.getName().contains("ProxyChangeListener")) {
                        Method onReceiveMethod = clazz.getDeclaredMethod("onReceive", Context.class, Intent.class);
                        Intent intent = new Intent("android.intent.action.PROXY_CHANGE");

                        onReceiveMethod.invoke(rec, appContext, intent);
                    }
                }
            }

            Log.d(LOG_TAG, "Setting proxy with >= 4.4 API successful!");
            return true;
        } catch (Exception e) {
            StringWriter sw = new StringWriter();
            e.printStackTrace(new PrintWriter(sw));
            String exceptionAsString = sw.toString();
            Log.v(LOG_TAG, e.getMessage());
            Log.v(LOG_TAG, exceptionAsString);
        } 
        return false;
    }
1

2 Answers 2

1

This code work in my environment (Chrome 67 + Android 7.0)

for (Object receiverMap : receivers.values()) {
    for (Object rec : ((ArrayMap) receiverMap).keySet()) {
        Class clazz = rec.getClass();

        boolean targetReceiverFound = false;
        if (clazz.getName().contains("ProxyChangeListener")) {
            targetReceiverFound = true;
        } else {
            final Field[] obfuscatedFields = clazz.getDeclaredFields();
            for (Field f : obfuscatedFields) {
                if (f.getType().getName().contains("ProxyChangeListener")) {
                    targetReceiverFound = true;
                    break;
                }
            }
        }

        if (targetReceiverFound) {
            // invoke onReceive() here
        }
    }
}
0

To expand on the answer provided by @Anmerris, the following is a complete code set that works for me. I have tested it with API 19 through 27 using WebView 30.0.0.0 through 67.0.3396.87.

public static void setProxy(Context context, String proxyHost, String proxyPort) {
    // Set the proxy values
    System.setProperty("http.proxyHost", proxyHost);
    System.setProperty("http.proxyPort", proxyPort);
    System.setProperty("https.proxyHost", proxyHost);
    System.setProperty("https.proxyPort", proxyPort);

    // Use reflection to apply the new proxy values.
    try {
        // Get the application and APK classes.  Suppress the lint warning that reflection may not always work in the future and on all devices.
        Class applicationClass = Class.forName("android.app.Application");
        @SuppressLint("PrivateApi") Class loadedApkClass = Class.forName("android.app.LoadedApk");

        // Get the declared fields.  Suppress the lint warning that `mLoadedApk` cannot be resolved.
        @SuppressWarnings("JavaReflectionMemberAccess") Field mLoadedApkField = applicationClass.getDeclaredField("mLoadedApk");
        Field mReceiversField = loadedApkClass.getDeclaredField("mReceivers");

        // Allow the values to be changed.
        mLoadedApkField.setAccessible(true);
        mReceiversField.setAccessible(true);

        // Get the APK object.
        Object mLoadedApkObject = mLoadedApkField.get(context);

        // Get an array map of the receivers.
        ArrayMap receivers = (ArrayMap) mReceiversField.get(mLoadedApkObject);

        // Set the proxy.
        for (Object receiverMap : receivers.values()) {
            for (Object receiver : ((ArrayMap) receiverMap).keySet()) {
                // `Class<?>`, which is an `unbounded wildcard parameterized type`, must be used instead of `Class`, which is a `raw type`.  Otherwise, `receiveClass.getDeclaredMethod` is unhappy.
                Class<?> receiverClass = receiver.getClass();

                // Get the declared fields.
                final Field[] declaredFieldArray = receiverClass.getDeclaredFields();

                // Set the proxy for each field that is a `ProxyChangeListener`.
                for (Field field : declaredFieldArray) {
                    if (field.getType().getName().contains("ProxyChangeListener")) {
                        Method onReceiveMethod = receiverClass.getDeclaredMethod("onReceive", Context.class, Intent.class);
                        Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
                        onReceiveMethod.invoke(receiver, context, intent);
                    }
                }
            }
        }
    } catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException | NoSuchMethodException | InvocationTargetException exception) {
        Log.d("enableProxyThroughOrbot", "Exception: " + exception);
    }
}
2
  • 1
    how can we add password in it ?
    – Ahmad
    Jun 13, 2022 at 10:40
  • I am not aware of any method for adding authentication using the above method, but if you are using the newer ProxyConfig.Builder and you have an HTTP proxy you can use the method described at stackoverflow.com/questions/68756786/… Jun 14, 2022 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.