PAC代理文件示例

PAC file example
PAC files should start with a clear and concise coding methodology. You can achieve the same result using several different methods using the PAC file functions that are available and the flexibility of the 

JavaScript

 language. The following example shows how to:

  • Normalize the requested URL for pattern matching
  • Bypass the proxy when the destination is a plain hostname (a hostname that doesn’t include a domain)
  • Bypass the proxy for a defined set of local domains
  • Bypass non-routable addresses (RFC 3330, better known as Special-Use IPv4 Addresses)
  • Send remaining HTTP, HTTPS, and FTP traffic to a specific proxy
function FindProxyForURL(url, host) /* Normalize the URL for pattern matching */ { url = url.toLowerCase(); host = host.toLowerCase(); /* Don’t proxy local hostnames */ if (isPlainHostName(host)) { return ‘DIRECT’; } /* Don’t proxy local domains */ if (dnsDomainIs(host, “.example1.com”) || (host == “example1.com”) || dnsDomainIs(host, “.example2.com”) || (host == “example2.com”) || dnsDomainIs(host, “.example3.com”) || (host == “example3.com”)) { return ‘DIRECT’; } /* Don’t proxy non-routable addresses (RFC 3330) */ if (isInNet(hostIP, ‘0.0.0.0’, ‘255.0.0.0’) || isInNet(hostIP, ‘10.0.0.0’, ‘255.0.0.0’) || isInNet(hostIP, ‘127.0.0.0’, ‘255.0.0.0’) || isInNet(hostIP, ‘169.254.0.0’, ‘255.255.0.0’) || isInNet(hostIP, ‘172.16.0.0’, ‘255.240.0.0’) || isInNet(hostIP, ‘192.0.2.0’, ‘255.255.255.0’) || isInNet(hostIP, ‘192.88.99.0’, ‘255.255.255.0’) || isInNet(hostIP, ‘192.168.0.0’, ‘255.255.0.0’) || isInNet(hostIP, ‘198.18.0.0’, ‘255.254.0.0’) || isInNet(hostIP, ‘224.0.0.0’, ‘240.0.0.0’) || isInNet(hostIP, ‘240.0.0.0’, ‘240.0.0.0’)) { return ‘DIRECT’; } /* Don’t proxy local addresses.*/ if (false) { return ‘DIRECT’; } } if (url.substring(0, 5) == ‘http:’ || url.substring(0, 6) == ‘https:’ || url.substring(0, 4) == ‘ftp:’) { return ‘PROXY xyz1.example.com:8080’; } return ‘DIRECT’; }
The following example shows a simple load distribution and failover using DNS:
{ if (isInNet(myIpAddress(), “10.1.0.0”, “255.255.0.0”)) { return “PROXY xyz1.example.com:8080; ” + “PROXY xyz2.example.com:8080”; } if (isInNet(myIpAddress(), “10.2.0.0”, “255.255.0.0”)) { return “PROXY xyz1.example.com:8080; ” + “PROXY xyz2.example.com:8080”; } if (isInNet(myIpAddress(), “10.3.0.0”, “255.255.0.0”)) { return “PROXY xyz2.example.com:8080; ” + “PROXY xyz1.example.com:8080”; } if (isInNet(myIpAddress(), “10.4.0.0”, “255.255.0.0”)) { return “PROXY xyz2.example.com:8080; ” + “PROXY xyz1.example.com:8080”; } else return “DIRECT”; }
The following example (new in version 2.9) shows how to specify URLs to open in the native browser and URLs to block:
function FindProxyForURL(url, host) { if (shExpMatch (url, “*example.org*”)){ return “PROXY example.net:8080; PROXY :3128”; } if (dnsDomainIs (host, “blackberry.com”)){ return “NATIVE”; } if (dnsDomainIs (host, “.example.com”)){ return “BLOCK”; } //redirect on http page if (shExpMatch (url, “*domain123.example.net*”)){ return “BLOCK http://domain1.example.org/”; } return “DIRECT”; }
 

Proxy Auto Config文件格式说明

PAC文件实际上是一个Script, 通过PAC我们可以让系统根据情况判断使用哪一个Proxy来访问目标网址, 这样做的好处:

  1. 分散Proxy的流量,避免Proxy Server负载过高
  2. 针对个别条件设定, 加快浏览速度
  3. 设定请求顺序, 自动依次尝试多个Proxy途径

PAC文件是纯文本, 格式和JavaScript一样, 不能包含任何HTML标签, 一定要定义的是Function FindProxyForURL

function FindProxyForURL(url, host) {
  ...
}

一个完整的例子

复制代码
var domains = {
    "google.com": 1,
    "facebook.com": 1,
    "bing.com":1
};
 
var proxy = "SOCKS5 127.0.0.1:1080; SOCKS 127.0.0.1:1080; DIRECT;";
 
var direct = 'DIRECT;';
 
function FindProxyForURL(url, host) {
    var lastPos;
    do {
        if (domains.hasOwnProperty(host)) {
            return proxy;
        }
        lastPos = host.indexOf('.') + 1;
        host = host.slice(lastPos);
    } while (lastPos >= 1);
    return direct;
}
复制代码

另一个例子

复制代码
function FindProxyForURL(url, host)
{
    url  = url.toLowerCase();
    host = host.toLowerCase();

    if (isInNet(dnsResolve(host), "10.0.0.0", "255.0.0.0")
      || isInNet(dnsResolve(host), "172.16.0.0",  "255.240.0.0")
      || isInNet(dnsResolve(host), "192.168.0.0", "255.255.0.0")
      || isInNet(dnsResolve(host), "127.0.0.0", "255.255.255.0")
    ) {
      return "DIRECT";
    }

    if (shExpMatch(url,"*twitter*")
      || shExpMatch(url,"*google*")
      || shExpMatch(url,"*facebook*")
      || shExpMatch(url,"*blogspot*")
      || shExpMatch(url,"*youtube*")
      || shExpMatch(url,"*gstatic*")
      || shExpMatch(url,"*ytimg*")
      || shExpMatch(url,"*ggpht*")
      || shExpMatch(url,"*github*")
    ) {
       return "SOCKS 192.168.1.1:3128";
    }
    return 'DIRECT';
}
复制代码

 

如果使用了PAC, 则浏览器在接受我们要求的网址后会去执行ret = FindProxyForURL(url, host). 其中,
url是所要求网址的完整路径, 
host是对方的计算机名称(就是在://和/之中的部份). 
return值ret是Proxy的组态. 它的格式有下列三种:

  • DIRECT: 直接联机而不透过Proxy
  • PROXY host:port 使用指定的Proxy
  • SOCKS host:port 使用指定的Socks代理

比如说当浏览器得到的是Proxy aa.bb.cc:3128; Proxy bb.cc.dd:3128; DIRECT 的话,那浏览器会先尝试aa.bb.cc:3128, 如果无法使用, 再尝试bb.cc.dd:3128, 还是不行的话, 就直接联机.

PAC中的预设函数

在PAC中,除了可以使用一般JavaScript的Function外, 它还定义了一些预设的函数

isPlainHostName(host)
host 由网址取得的主机名称. 
此 Function 会判断 host 是否为不包含网域 (Domain). 如果不包含则 return true, 如果包含则 return false. 

dnsDomainIs(host,  domain)
host 由网址取得的主机名称. 
domain 指定的网域. 
此 Function 会判断 host 是否属于网域 domain. 如果是则 return true, 否则return false. 

localHostOrDomainIs(host,  hostdom)
host 由网址取得的主机名称. 
hostdom 完整的网域名称. 
此 Function 会判断 host 是否为 hostdom,或 host 是否为 hostdom 的主机名称. 如果是则 return true, 否则 return false.

isResolvable(host)
host 由网址取得的主机名称. 
此 Function 会尝试透过 DNS 去解析 host,如果解析成功则 return true, 否则return false.

isInNet(host,  pattern,  mask)
host主机名称,可以是 Domain Name 或 IP. 如果是 Domain Name,则会透过 DNS 查出 IP. 
pattern IP. 
mask对应于pattern的遮盖. 
此 Function 会 host 是否在指定的 IP 范围内,如果是则 return true, 否则 return false.

dnsResolve(host)
host 要透过 DNS 解晰的主机名称.
此 Function 会透过 DNS 去解析 host,return 值即为解析之结果.

myIpAddress()
此 Function 会 return 浏览器所在计算机之 IP 地址.

dnsDomainLevels(host) 
host 由网址取得的主机名称.
此 Function 会 return host 的 Domain 层数(点的数目).

shExpMatch(str,  shexp)
str 要进行比对的字符串.
shexp 比对的条件.
此 Function 会比对 str 是否符合 shexp 的表示式(此表示式为 shell expression 而非 regular expressions). 如果是则 return true, 否则 return false.

weekdayRange(), dateRange(), timeRange()
这三个函数的功用都是检查线在时间是否在指定范围内,可以设定分时段使用代理服务

 

 

https://docs.blackberry.com/en/blackberry-dynamics-apps/blackberry-access/3_1/blackberry-access-administration-guide/lnb1484165962504/wun1475010470352/jcu1484166495397

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

发表评论

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

滚动至顶部