`
aaronchenwei
  • 浏览: 7861 次
文章分类
社区版块
存档分类
最新评论

Proxy auto-config(PAC)配置

 
阅读更多
Proxy auto-config,简称PAC,是用于定义浏览器如何自动选择适当的代理服务器来访问一个网址。因此,使用PAC文件来定义代理的话,可以方便的实现
  • 实现代理的动态切换(fail-over)
  • 对于不同的url或是ip range,配置不同的server,加速网络访问速度
  • 对于proxy,可以实现负载均衡

一个PAC文件是一个至少定义了一个JavaScript函数的文本文件。这个函数FindProxyForURL(url, host)。有2个参数:url是一个对象的URL,host是一个由这个URL所指向的主机名。返回值是代理的配置。
function FindProxyForURL(url, host) {
  return "PROXY proxy.example.com:8080; DIRECT";
}

这个函数返回值告诉浏览器优先通过代理服务器proxy.example.com的8080端口来获取所有的页面。如果这个代理服务器没有反应,浏览器不使用代理服务器直接访问。多个代理之间用分号(;)分隔。

PAC函数
PAC配置可以使用一系列内置的函数。这些函数最早来源于Netscape公司出的规范。Netscape对于技术来说,是个伟大的公司,可惜的是其的商业化能力。

[list]
  • dnsDomainIs
  • Evaluates hostnames and returns true if hostnames match. Used mainly to match and exception individual hostnames.
    if (dnsDomainIs(host, ".google.com"))
        return "DIRECT";
    

  • shExpMatch
  • Will attempt to match hostname or URL to a specified shell expression, and returns true if matched.
    if (shExpMatch(url, "*.local"))
        return "DIRECT";
    

    if (shExpMatch(host, "vpn.domain.com") ||
        shExpMatch(url, "http://abcdomain.com/folder/*"))
        return "DIRECT";
    

  • isInNet
  • This function evaluates the IP address of a hostname, and if within a specified subnet returns true. If a hostname is passed the function will resolve the hostname to an IP address.
    if (isInNet(dnsResolve(host), "172.16.0.0", "255.240.0.0"))
        return "DIRECT";
    

  • myIpAddress
  • Returns the IP address of the host machine.
    if (isInNet(myIpAddress(), "10.10.1.0", "255.255.255.0"))
        return "PROXY 10.10.5.1:8080";
    

  • dnsResolve
  • Resolves hostnames to an IP address. This function can be used to reduce the number of DNS lookups.
    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";
    

  • isPlainHostName
  • This function will return true if the hostname contains no dots, e.g. http://intranet
    Useful when applying exceptions for internal websites, e.g. may not require resolution of a hostname to IP address to determine if local.
    if (isPlainHostName(host))
        return "DIRECT";
    

  • localHostOrDomainIs
  • Evaluates hostname and only returns true if exact hostname match is found.
    if (localHostOrDomainIs(host, "www.google.com"))
        return "DIRECT";
    

    这可以包括google.com所以子的domain。
    if (localHostOrDomainIs(host, ".google.com"))
        return "DIRECT";
    

  • isResolvable
  • Attempts to resolve a hostname to an IP address and returns true if successful. WARNING – This may cause a browser to temporarily hang if a domain isn’t resolvable.
    if (isResolvable(host))
        return "PROXY proxy1.example.com:8080";
    

  • dnsDomainLevels
  • This function returns the number of DNS domain levels (number of dots) in the hostname. Can be used to exception internal websites which use short DNS names, e.g. http://intranet
    if (dnsDomainLevels(host) > 0)
        return "PROXY proxy1.example.com:8080";
        else return "DIRECT";
    

  • weekdayRange
  • Allows rules to be time based, e.g. only return a proxy during specific days.
    if (weekdayRange("MON", "FRI")) return "PROXY proxy1.example.com:8080";
        else return "DIRECT";
    

  • dateRange
  • Allows rules to be time based, e.g. only return a proxy during specific months.
    if (dateRange("JAN", "MAR")) return "PROXY proxy1.example.com:8080";
        else return "DIRECT";
    

  • timeRange
  • Allows rules to be time based, e.g. only return a proxy during specific hours.
    if (timeRange(8, 18)) return "PROXY proxy1.example.com:8080";
        else return "DIRECT";
    

  • alert
  • The alert() function is not specified in the original PAC specification, although support for this is included in Internet Explorer and Firefox.The function can be used to output the value of a variable or result of a function in a manner that is viewable by the end-user. This can be useful for troubleshooting PAC file rule issues.
    resolved_host = dnsResolve(host);
    alert(resolved_host);
    


    [/list]

    注意
    Firefox或Internet Explorer只支持系统缺省编码的PAC文件,不支持Unicode编码的PAC文件,所以在编写PAC文件的时候要注意文件的编码方式。
    分享到:
    评论

    相关推荐

    Global site tag (gtag.js) - Google Analytics