如何在智能手机上制作代理服务器?

时间:2017-12-20 14:48:49

标签: java android proxy

我想在我的设备上启动代理服务器。它可以在所有网络上打开并在wifi和蜂窝连接期间工作。我使用this项目作为示例,它工作不稳定。在客户端应用程序中,我使用此代码连接到webview中的代理:

public class ProxySettings {

static final int PROXY_CHANGED = 193;

private static Object getDeclaredField(Object obj, String name) 
    throws SecurityException, NoSuchFieldException, 
    IllegalArgumentException, IllegalAccessException {

        Field f = obj.getClass().getDeclaredField(name);
        f.setAccessible(true);
        Object out = f.get(obj);
        return out;
    }

public static Object getRequestQueue(Context ctx) throws Exception {

    Object ret = null;
    Class networkClass = Class.forName("android.webkit.Network");
    if (networkClass != null) {
        Object networkObj = invokeMethod(networkClass, "getInstance", 
            new Object[] { ctx },
                Context.class);
        if (networkObj != null) {
            ret = getDeclaredField(networkObj, "mRequestQueue");
        }
    }
    return ret;
}

private static Object invokeMethod(Object object, String methodName, 
    Object[] params, Class... types) throws Exception {

        Object out = null;
        Class c = object instanceof Class ? (Class) object : 
            object.getClass();
    if (types != null) {
        Method method = c.getMethod(methodName, types);
        out = method.invoke(object, params);
    } else {
        Method method = c.getMethod(methodName);
        out = method.invoke(object);
    }
    return out;
}

public static void resetProxy(Context ctx) throws Exception {

    Object requestQueueObject = getRequestQueue(ctx);
    if (requestQueueObject != null) {
        setDeclaredField(requestQueueObject, "mProxyHost", null);
    }
}

private static void setDeclaredField(Object obj, String name, Object 
    value) throws SecurityException, NoSuchFieldException, 
        IllegalArgumentException, IllegalAccessException {

           Field f = obj.getClass().getDeclaredField(name);
           f.setAccessible(true);
           f.set(obj, value);
}

public static boolean setProxy(Context ctx, String host, int port) {

    boolean ret = false;
    setSystemProperties(host, port);

    try {

        Log.e("MEGATAG", "ProxySettings.setProxy try");

        if (Build.VERSION.SDK_INT < 14) {

            Log.e("MEGATAG", "ProxySettings.setProxy try if");

            Object requestQueueObject = getRequestQueue(ctx);
            if (requestQueueObject != null) {

                HttpHost httpHost = new HttpHost(host, port, "http");

                setDeclaredField(requestQueueObject, "mProxyHost", 
                    httpHost);
                ret = true;
            }

        } else {
            ret = setICSProxy(host, port);
        }
    } catch (Exception e) {
    }
    return ret;
}

private static boolean setICSProxy(String host, int port) throws 
    ClassNotFoundException, NoSuchMethodException, 
    IllegalArgumentException, InstantiationException,
    IllegalAccessException, InvocationTargetException {

        Log.e("MEGATAG", "ProxySettings.setICSProxy");

        Class webViewCoreClass = 
            Class.forName("android.webkit.WebViewCore");
        Class proxyPropertiesClass = 
            Class.forName("android.net.ProxyProperties");
       if (webViewCoreClass != null && proxyPropertiesClass != null){

        Method m = webViewCoreClass
            .getDeclaredMethod("sendStaticMessage", Integer
            .TYPE,Object.class);
        Constructor c = proxyPropertiesClass
            .getConstructor(String.class, Integer.TYPE,String.class);
        m.setAccessible(true);
        c.setAccessible(true);
        Object properties = c.newInstance(host, port, null);
        m.invoke(null, PROXY_CHANGED, properties);
        Log.e("MEGATAG", "setICSProxy true");
        return true;
    }

    Log.e("MEGATAG", "ProxySettings.setICSProxy else -> false");
    return false;

}

private static void setSystemProperties(String host, int port) {

    Log.e("MEGATAG", "ProxySettings.setSystemProperties");

    System.setProperty("http.proxyHost", host);
    System.setProperty("http.proxyPort", port + "");

    System.setProperty("https.proxyHost", host);
    System.setProperty("https.proxyPort", port + "");

}
}

如果我认为它工作不稳定,因为端口阻塞,当我连接时。我添加了方法,关闭我的套接字连接以取消阻止端口,它不起作用。也许任何人都有这个问题并解决了它,并且可以帮助我。

1 个答案:

答案 0 :(得分:0)

我在this链接后面找到了答案。希望它可以帮助任何人。