How to Set SOCKS proxy for webview

2.8k views Asked by At

I need to set webview to use SOCKS proxy. I have been researching this issue from days now. I see that webview gives no public interface to set PROXY. There have been examples that use Java reflection to set http proxy.

as described here :

WebView android proxy

But this discussion is about setting http/https proxy.

For setting socks proxy I used the following approach :

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

        Context appContext = webView.getContext().getApplicationContext();
        String user = NativeSDK.getProxyUser();
        String pass = NativeSDK.getProxyPass();

        System.setProperty("socksProxyHost", host);
        System.setProperty("socksProxyPort", port + "");
        System.setProperty("java.net.socks.username", user);
        System.setProperty("java.net.socks.password", pass);
        Log.d(TAG,"socks proxy set");



        try{
            Class applictionCls = Class.forName("android.app.Application");
            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(Proxy.PROXY_CHANGE_ACTION);
                        onReceiveMethod.invoke(rec, appContext, intent);
                    }
                }
            }

            Log.d(TAG, "Setting proxy with >= 4.4 API successful!");
            return true;
        }

    catch (all exceptions) {

        Log.d(TAG, "Setting proxy with >= 4.4 API NOT successful!");
        return false;
    }

But this does not seem to really work : I get this error in my Logs from ProxyChangeListener :

E/ProxyChangeListener: Using no proxy configuration due to exception:java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.os.Bundle.get(java.lang.String)' on a null object reference

Now the strange part is : after this code when I enter the url in webview, the request is read by the socket listening on the port above - the proxy port. ( I believe that indicates that the request is being proxied) But the request that I read from the socket looks like : \x05\x01

Now as I understand: (From wiki)

The initial greeting from the client is

field 1: SOCKS version number (must be 0x05 for this version) field 2: number of authentication methods supported, 1 byte field 3: authentication methods, variable length, 1 byte per method supported

So is this SOCKS5 client request. But then where is the info about authentication methods ?

Can someone help me setting SOCKS proxy for webview

2

There are 2 answers

0
Jeff Gehlbach On

I came across this post while chasing down a different problem related to HTTP proxies. I can't offer any SOCKS-specific help, but the NullPointerException you're getting is one I had to work past.

You need to set an extra named "proxy" in the intent containing an android.net.ProxyInfo object, which you can build using the following static method:

https://developer.android.com/reference/android/net/ProxyInfo.html#buildDirectProxy(java.lang.String,%20int)

You should be able to adapt the code changes in the following pull request -- again I don't know how this would change for the SOCKS case, but in the hope that it's useful:

https://github.com/velazcod/Tinfoil-Facebook/pull/95/files

0
diyism On