Proxy auto-config(PAC)配置

Proxy auto-config(PAC)用于定义浏览器如何自动选择合适的代理服务器访问网址。PAC文件包含JavaScript函数FindProxyForURL,该函数根据URL和主机名决定代理配置。PAC支持动态切换代理、按URL或IP范围分配不同服务器、实现负载均衡。内置函数如dnsDomainIs、shExpMatch等用于匹配和判断。请注意,Firefox和IE仅支持非Unicode编码的PAC文件。
部署运行你感兴趣的模型镜像
Proxy auto-config,简称PAC,是用于定义浏览器如何自动选择适当的代理服务器来访问一个网址。因此,使用PAC文件来定义代理的话,可以方便的实现
[list]
[*]实现代理的动态切换(fail-over)
[*]对于不同的url或是ip range,配置不同的server,加速网络访问速度
[*]对于proxy,可以实现负载均衡
[/list]
一个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端口来获取所有的页面。如果这个代理服务器没有反应,浏览器不使用代理服务器直接访问。多个代理之间用分号(;)分隔。

[b]PAC函数[/b]
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]

[b]注意[/b]
Firefox或Internet Explorer只支持系统缺省编码的PAC文件,不支持Unicode编码的PAC文件,所以在编写PAC文件的时候要注意文件的编码方式。

您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值