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 :
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