PAC脚本语法(代理自动配置)

现在浏览器基本都支持PAC格式的代理脚本。本文仅介绍PAC脚本语法,并不解释使用方法。
参考文档:
https://en.wikipedia.org/wiki/Proxy_auto-config#DnsResolve
http://www.360doc.com/content/12/0618/16/2633_219014023.shtml

示例脚本

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";
}

主体

PAC脚本主体是一个FindProxyForURL的JS函数。函数的反回值有三类:

function FindProxyForURL(url,host)
{
  if(host=="http://www.domain.com/") return "DIRECT";
  return "PROXY myproxy:80;PROXY myotherproxy:8080;DIRECT";

}

函数

isPlainHostName(host)

判断是否是本地主机

dnsDomainIs(host,domain) localHostOrDomainIs(host,”“)

判断访问主机是否属于某个域和某个域名
示例:

function FindProxyForURL(url, host)
{
    if ((isPlainHostName(host) ||
    dnsDomainIs(host, ".company.com")) &&
    !localHostOrDomainIs(host, "http://www.company.com/") &&
    !localHostOrDomainIs(host, "home.company.com"))

        return "DIRECT";
    else
        return "PROXY proxy:80";
}

isResolvable(host)

主机名是否能被dns服务器解析

function FindProxyForURL(url, host)
{
    if (isResolvable(host))
        return "DIRECT";
    else
        return "PROXY proxy:80";
}

isInNet(host,”“,”“)

访问IP是否在某个子网内

function FindProxyForURL(url, host)
{
    if (isInNet(host, "166.111.0.0", "255.255.0.0"))
        return "DIRECT";
    else
        return "PROXY proxy:80";
}

shExpMatch(host,”“)

主机名判断

function FindProxyForURL(url, host)
{
    if (isPlainHostName(host))
        return "DIRECT";
    else if (shExpMatch(host, "*.com"))
        return "PROXY comproxy:80";
    else if (shExpMatch(host, "*.edu"))
        return "PROXY eduproxy:80";
    else
        return "PROXY proxy:80";
}

url.substring(0,n)

截取协议字符串

function FindProxyForURL(url, host)
{
    if (url.substring(0, 5) == "http:") {
        return "PROXY proxy:80";
    }
    else if (url.substring(0, 4) == "ftp:") {
        return "PROXY fproxy:80";
    }
    else if (url.substring(0, 7) == "gopher:") {
        return "PROXY gproxy";
    }
    else if (url.substring(0, 6) == "https:") {
        return "PROXY secproxy:8080";
    }
    else {
        return "DIRECT";
    }
}

dnsResolve(host)

域名ip解析

unction FindProxyForURL(url, host)
{
if (dnsResolve(host) == "166.111.8.237") {
return "PROXY secproxy:8080";
}
else {
return "PROXY proxy:80";
}
}

myIpAddress

本地IP

function FindProxyForURL(url, host)
{
if (myIpAddress() == "166.111.8.238") {
    return "PROXY proxy:80";
}
else {
    return "DIRECT";
}
}

dnsDomainLevels(host)

几级域名

function FindProxyForURL(url, host)
{
    if (dnsDomainLevels(host) > 0) { // if number of dots in host > 0
        return "PROXY proxy:80";
    }
        return "DIRECT";
}

weekdayRange

星期几

function FindProxyForURL(url, host)
{
    if(weekdayRange("WED", "SAT", "GMT"))
        return "PROXY proxy:80";
    else
        return "DIRECT";
}

Math数学函数

通用js函数

Math.floor(Math.random()*5)

其它

iis设置mime

“扩展名”填 .pac
“MIME类型”填 .txt 或者 .h
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猿来思考

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值