Android系统中,当你建立WiFi连接时,可以选择代理服务器的类型,可以是手动,也可以选择自动配置。当你选择自动配置可,它就要求你输入pac文件的路径。pac(Proxy auto-config)文件是一个JAVA脚本,包含一个js函数FindProxyForURL(url, host), 这个函数返回的是一个字符串,这个字符串就决定了url/host是直接连接还是通过代理服务器进行连接。配置相当灵活。
- 例子(来自:https://en.wikipedia.org/wiki/Proxy_auto-config)
所有的请求都通过代理服务器proxy.example.com:8080进行连接,如果连接失败,则直接进行连接做第二次尝试:
function FindProxyForURL(url, host) { return "PROXY proxy.example.com:8080; DIRECT"; }
更复杂的配置:
function FindProxyForURL(url, host) { // our local URLs from the domains below example.com don't need a proxy: if (shExpMatch(host, "*.example.com")) { return "DIRECT"; } // URLs within this network are accessed through // port 8080 on fastproxy.example.com: if (isInNet(host, "10.0.0.0", "255.255.248.0")) { return "PROXY fastproxy.example.com:8080"; } // All other requests go through port 8080 of proxy.example.com. // should that fail to respond, go directly to the WWW: return "PROXY proxy.example.com:8080; DIRECT"; }
- 实现
在Android-5.1.1_r15中的实现,引用:external/chromium-libpac/src/proxy_resolver_v8.cc
Notes on the javascript environment: For the majority of the PAC utility functions, we use the same code as Firefox. See the javascript library that proxy_resolver_scipt.h pulls in. In addition, we implement a subset of Microsoft's extensions to PAC. - myIpAddressEx() - dnsResolveEx() - isResolvableEx() - isInNetEx() - sortIpAddressList() It is worth noting that the original PAC specification does not describe the return values on failure. Consequently, there are compatibility differences between browsers on what to return on failure, which are illustrated below: --------------------+-------------+-------------------+-------------- | Firefox3 | InternetExplorer8 | --> Us <--- --------------------+-------------+-------------------+-------------- myIpAddress() | "127.0.0.1" | ??? | "127.0.0.1" dnsResolve() | null | false | null myIpAddressEx() | N/A | "" | "" sortIpAddressList() | N/A | false | false dnsResolveEx() | N/A | "" | "" isInNetEx() | N/A | false | false --------------------+-------------+-------------------+-------------- TODO: The cell above reading ??? means I didn't test it. Another difference is in how dnsResolve() and myIpAddress() are implemented -- whether they should restrict to IPv4 results, or include both IPv4 and IPv6. The following table illustrates the differences: --------------------+-------------+-------------------+-------------- | Firefox3 | InternetExplorer8 | --> Us <--- --------------------+-------------+-------------------+-------------- myIpAddress() | IPv4/IPv6 | IPv4 | IPv4 dnsResolve() | IPv4/IPv6 | IPv4 | IPv4 isResolvable() | IPv4/IPv6 | IPv4 | IPv4 myIpAddressEx() | N/A | IPv4/IPv6 | IPv4/IPv6 dnsResolveEx() | N/A | IPv4/IPv6 | IPv4/IPv6 sortIpAddressList() | N/A | IPv4/IPv6 | IPv4/IPv6 isResolvableEx() | N/A | IPv4/IPv6 | IPv4/IPv6 isInNetEx() | N/A | IPv4/IPv6 | IPv4/IPv6 --------------------+-------------+-------------------+--------------
- 相关代码
在android-5.1.1_r15项目中,相关的代码分布在:
- external/chromium-libpac/
- frameworks/base/packages/services/PacProcessor
- frameworks/base/core/java/android/net/PacProxySelector.java
- frameworks/base/services/core/java/com/android/server/connectivity/PacManager.java
- frameworks/base/core/java/android/net/Proxy.java
- packages/apps/Settings/src/com/android/settings/ProxySelector.java
- packages/apps/Settings/src/com/android/settings/wifi/WifiConfigController.java
当你连接wifi网络或者在已经连接的wifi网络进行管理(管理网络设定)时,可以设置代理服务器。
在查找相关代码时,无意间发现这么一个有意思的App: ProxyHandler - 一个简单的http proxy server ->@<-
相关代码在:frameworks/base/packages/services/Proxy/
- 相关的参考文档:
- https://en.wikipedia.org/wiki/Proxy_auto-config
- http://blogs.msdn.com/b/wndp/archive/2006/07/13/ipv6-pac-extensions-v0-9.aspx
大哥,demo给一个啊