使用自动代理配置脚本和 PAC (优化)

JavaScript 区分大小写

代理脚本使用 JavaScript 语言。 JavaScript 区分大小写。 因此,大 if 写子句永远不会变为 true,而其他参数使用小写形式。 Internet Explorer函数之前将变量 host url FindProxyForURL 转换为小写形式。

对于 ,此条件不为 true WinHTTP。 这是因为 将 WinHTTP 和 host 直接 url 传递给 函数。

因此,在 PAC 文件中检查的参数应在评估之前在 PAC 内转换:

JavaScript

host = host.toLowerCase();

使用 IPv6

如果要使用和处理 IPv6 地址,Internet Explorer支持这些地址,因为 Internet Explorer 包含在每个当前支持的 Windows 版本 (中以及自 Windows Vista) 以来的 WinHTTP 中。 但是,在这种情况下,您必须使用”Ex isInNetEx() “函数 (如) ,如以下文章所述:

IPv6 感知代理帮助程序 API 定义

有关实现的示例 myIpAddressEx ,请参阅 “myIpAddress”函数在Internet Explorer 9。

测试 PAC 文件

如果脚本包含任何语法错误 (例如,语句中缺少”) if “字符,) 脚本不会运行。 若要最大程度地减少错误,请考虑使用运行语法检查的脚本编辑器。 通过使用 Visual Studio,您可以在编辑期间将 PAC 文件的扩展名重命名为”.js”,但在将其上载到 webserver 之前将其重命名回”.pac”。

 备注

从Windows 10开始,你不能再使用基于文件的 PAC 文件。 有关详细信息,请参阅以下文章:

使用测试Autoprox.exe

有时,即使无法访问网站,也不得不测试 PAC 文件。 为此,可以使用Autoprox.exe 命令行工具

如果在命令中打开该工具而不使用其他参数,则返回以下包含帮助的输出:

控制台

C:\temp>autoprox
Help for AUTOPROX.EXE
Version : 2.44 (12/16/2019)
Usage : AUTOPROX -a  (calling DetectAutoProxyUrl and saving wpad.dat file in temporary file if success)
Usage : AUTOPROX -n  (calling DetectAutoProxyUrl with PROXY_AUTO_DETECT_TYPE_DNS_A only and saving wpad.dat file in temporary file if success)
Usage : AUTOPROX  [-o] [-d] [-v] [-u:url] [-p:Path to autoproxy file] [-i:IP address]
      -o: calls InternetInitializeAutoProxyDll with helper functions implemented in AUTOPROX
       -i:IP Address: calls InternetInitializeAutoProxyDll with helper functions implemented in AUTOPROX and using provided IP Address
       -v: verbose output for helper functions
For debugging: -d plus HKEY_CURRENT_USER\Software\Microsoft\Windows Script\Settings\JITDebug=1
AUTOPROX -u:url: calling DetectAutoProxyUrl and using autoproxy file to find the proxy for the url
AUTOPROX -u:url -p:path: using the autoproxy file/url from the path to find proxy for the url
Example: autoprox http://www.microsoft.com -> calling DetectAutoProxyUrl and using WPAD if found
Example: autoprox -o -u:http://www.microsoft.com -p:c:\inetpub\wwwroot\wpad.dat
Example: autoprox -u:http://www.microsoft.com -p:http://proxy/wpad.dat
Example: autoprox -d -u:http://www.microsoft.com -p:http://proxy/wpad.dat

下面是它使用我们的示例时的输出:

控制台

C:\temp>autoprox -u:https://us.msn.com -p:c:\temp\sample.pac
Searching proxy for url : https://us.msn.com
Searching proxy using file : c:\temp\sample.pac
The Winsock 2.2 dll was found okay
Calling InternetInitializeAutoProxyDll with c:\temp\sample.pac
        Calling InternetGetProxyInfo for url https://us.msn.com and host us.msn.com
        Proxy returned for url https://us.msn.com is:
PROXY myproxy:80;

错误处理Autoprox.exe

如果 PAC 文件包含语法错误,则会收到以下消息:

错误:InternetGetProxyInfo 失败,错误0x3eb 1003。

完成本地测试后,您应将 PAC 文件复制到 Web 服务器,该服务器将通过其通过 HTTP 协议访问它。

示例:

JavaScript

function FindProxyForURL(url, host) {
    // NetBIOS-names
    if (isPlainHostName(host))
        return "DIRECT";
    // change to lower case, if not already been done
    host = host.toLowerCase();
    // internal DNS-suffixes
    if (shExpMatch(host, "*.corp.company.com") ||
        shExpMatch(host, "*.dns.company.com"))
        return "DIRECT";
    // Save the IP-address to variable hostIP
    var hostIP;
    var isIpV4Addr = /^(\d+.){3}\d+$/;
    if (isIpV4Addr.test(host))
        hostIP = host;
    else
        hostIP = dnsResolve(host);
    // IP could not be determined -> go to proxy
    if (hostIP == 0)
        return "PROXY myproxy:80";
    // These 3 scopes are used only internally
    if (shExpMatch(hostIP, "95.53.*") ||
        shExpMatch(hostIP, "192.168.*") ||
        shExpMatch(hostIP, "127.0.0.1"))
        return "DIRECT";
    // Eveything else goes through the proxy
    return "PROXY myproxy:80;";
}

使用 IEAK 11 的代理自动配置 (.pac) 文件

 
 

Workaround(not working in chrome)

To work around this issue, write your own custom myIpAddress function for Internet Explorer 9. For example, you can write the following function:function myIE9IpAddress()
{
var ipex = myIpAddressEx();
if (ipex === false)
{
var myip=myIpAddress();
return myip;
}
var iparray = ipex.split(“;”);
for (j=0; j < iparray.length;j++)
{
var ipaddress=iparray[j];
//IPv6 adress : skip
var parts=ipaddress.split(“::”);
var firstpart=parts[0];
if (firstpart===”[fe80″)
{
//alert(“skipping IPv6 address :”+ ipaddress);
continue;
}

//IPv4 address starting with 192.: skip
parts=ipaddress.split(“.”);
firstpart=parts[0];
if (firstpart === “192”)
{
//alert(“skipping IPv4 address starting with 192. :”+ ipaddress);
continue;
}
break;
}
return ipaddress;
}

代理自动配置文件(PAC)文件

 

https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Proxy_servers_and_tunneling/Proxy_Auto-Configuration_PAC_file#myipaddress

 

 

PAC和CHROME开启DEBUG LOG

https://gist.github.com/raghur/7aba1d879d49f30e7c833875d79d966b

 

function FindProxyForURL(url, host)
{
// firefox – Ctrl-Shift-J Chrome/Chromium: chrome://net-internals/#events
var debug = true;
var log = function() {
if (debug) {
var args = Array.prototype.slice.call(arguments);
alert(args.join(” “));
}
}
var bypassProxy = false;
log(“Checking for availability of myIpAddressEx:”, typeof(myIpAddressEx) == “function”);
// firefox doesn’t have myIpAddressEx – but it does seem to return the
// right IP
if (typeof(myIpAddressEx) != “function”) {
log(“myIpAddress returns: “, myIpAddress());
bypassProxy = myIpAddress() == ‘10.8.0.6’ || myIpAddress().startsWith(“192.168.1.”);
} else {
// chromium seems to pick the first IP – in my case, a virtualbox HOST
// only IP. So check in all IPs returned by myIpAddressEx
var ips = myIpAddressEx().split(“;”);
log(“myIpAddressEx returns”, myIpAddressEx());
for(i=0; i< ips.length; i++) {
if (ips[i] == “10.8.0.6” || ips[i].startsWith(“192.168.1.”)) {
bypassProxy = true;
break;
}
}
}
log(“Can bypass proxy: “, bypassProxy);
if (bypassProxy)
{
log (“DIRECT”)
return “DIRECT”;
}
else
{
log( “PROXY”)
return “PROXY 10.98.2.8:8080”;
}
}

发表评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注

滚动至顶部