在Android系统webview (2018年5月30日)的最后一次更新之后,代理设置对我不再起作用,并且代理不再对Webview起作用。在其他使用代理的浏览器中也有同样的效果,代理没有设置,阻止的网站不打开,ip不变。返回的错误是主机未解析或连接失败。我的设备是Nexus 5X,Android 8.1.0。其他人也面临同样的问题吗?
我用这个来设置代理:
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;
}发布于 2018-06-29 09:58:09
这段代码适用于我的环境(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
}
}
}发布于 2018-07-07 21:38:43
为了扩展@Anmerris提供的答案,以下是适用于我的完整代码集。我已经使用WebView 30.0.0.0到67.0.3396.87使用API19到27进行了测试。
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);
}
}https://stackoverflow.com/questions/50667178
复制相似问题