用本本上网时,往返家里和单位,因为单位是用的代理上网,家里是直接连接.因此每次都要修改IE的代理设置,虽然是个小事,但是每次都要修改总是有点烦
![](https://www.cnblogs.com/Emoticons/emsad.gif)
,于是参考GOOGLE,写了一个自动配置代理的脚本.这样无论在家和在公司,只要连上局域网就可以上网了
![](https://www.cnblogs.com/Emoticons/emteeth.gif)
.
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
function FindProxyForURL(url,host)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if((isPlainHostName(host))||shExpMatch(url,"http://127.0.0.1/*")||isInNet(host,"192.168.0.0", "255.255.255.0"))
{
return "direct";
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} else
{
if(myIpAddress()=="192.168.0.49")
return "PROXY 192.168.0.6:808";
else
return "direct";
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
把以上代码另存为proxy.pac的文件,如:c:\proxy.pac,然后在IE的菜单里"工具"->"INTEL选项"->"连接"->局域网设置
把使用自动配置脚本选上,然后在地址栏填入
file://c:/proxy.pac,注意file后是两斜杠,要使设置生效,需要关闭IE再重新打开.
现在我们介绍下这个proxy.pac脚本文件,脚本的语法是js语法,js的内置函数可以使用,要实现自动配置代理,需要实现FindProxyForURL这个函数,其参数url代表要访问的连接,host代表要访问连接的主机名,该函数有三个返回参数
direct:直接连接
proxy IP:PORT
socket IP:PORT
返回结果大小写不敏感
PAC的其他内置语法见
http://wp.netscape.com/eng/mozilla/2.0/relnotes/demo/proxy-live.html
PAC脚本另外一个重要的应用是多台代理服务器并存的情况下,通过pac脚本的控制:
- 用户随机选择使用多台代理服务器中的任意一台来达到流量负载均衡的目的;
- 管理员通过PAC脚本控制用户使用和不使用某台代理服务器,这样可以空出时间对代理服务器进行维护;
- 让服务器工作在主备模式,当主服务器宕机时,会自动切换到其它备用服务器而不会中断服务;
- 根据访问目的地的不同,自动选择最佳代理服务器。
下面列举几个PAC的实例,顺便学习下它的语法
a、isPlainHostName(host),本例演示判断是否为本地主机,如http://myservername/
的方式访问,如果是直接连接,否则使用代理
function FindProxyForURL(url, host)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
if (isPlainHostName(host))
return "DIRECT";
else
return "PROXY proxy:80";
} b、dnsDomainIs(host, "")、localHostOrDomainIs(host, ""),本例演示判断访问主机
是否属于某个域和某个域名,如果属于.company.com域的主机名,而域名不是
www.company.com和home.company.com的直接连接,否则使用代理访问。
function FindProxyForURL(url, host)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
if ((isPlainHostName(host) ||
dnsDomainIs(host, ".company.com")) &&
!localHostOrDomainIs(host, "www.company.com") &&
!localHostOrDomainIs(host, "home.company.com"))
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return "DIRECT";
else
return "PROXY proxy:80";
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
c、isResolvable(host),本例演示主机名能否被dns服务器解析,如果能直接访问,否
则就通过代理访问。
function FindProxyForURL(url, host)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
if (isResolvable(host))
return "DIRECT";
else
return "PROXY proxy:80";
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
d、isInNet(host, "", ""),本例演示访问IP是否在某个子网内,如果是就直接访问,
否则就通过代理,例子演示访问清华IP段的主页不用代理。
function FindProxyForURL(url, host)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
if (isInNet(host, "166.111.0.0", "255.255.0.0"))
return "DIRECT";
else
return "PROXY proxy:80";
} e、shExpMatch(host, ""),本例演示根据主机域名来改变连接类型,本地主机、*.edu、
*.com分别用不同的连接方式。
function FindProxyForURL(url, host)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
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";
} f、url.substring(),本例演示根据不同的协议来选择不同的代理,http、https、ftp、
gopher分别使用不同的代理。
function FindProxyForURL(url, host)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (url.substring(0, 5) == "http:")
{
return "PROXY proxy:80";
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else if (url.substring(0, 4) == "ftp:")
{
return "PROXY fproxy:80";
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else if (url.substring(0, 7) == "gopher:")
{
return "PROXY gproxy";
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else if (url.substring(0, 6) == "https:")
{
return "PROXY secproxy:8080";
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else
{
return "DIRECT";
}
} g、dnsResolve(host),本例演示判断访问主机是否某个IP,如果是就使用代理,否则直
接连接。
function FindProxyForURL(url, host)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (dnsResolve(host) == "166.111.8.237")
{
return "PROXY secproxy:8080";
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else
{
return "PROXY proxy:80";
}
} h、myIpAddress(),本例演示判断本地IP是否某个IP,如果是就使用代理,否则直接使
用连接。
function FindProxyForURL(url, host)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (myIpAddress() == "166.111.8.238")
{
return "PROXY proxy:80";
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else
{
return "DIRECT";
}
} i、dnsDomainLevels(host),本例演示访问主机的域名级数是几级,就是域名有几个点
如果域名中有点,就通过代理访问,否则直接连接。
function FindProxyForURL(url, host)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (dnsDomainLevels(host) > 0)
{ // if number of dots in host > 0
return "PROXY proxy:80";
}
return "DIRECT";
} j、weekdayRange(),本例演示当前日期的范围来改变使用代理,如果是GMT时间周三
到周六,使用代理连接,否则直接连接。
function FindProxyForURL(url, host)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
if(weekdayRange("WED", "SAT", "GMT"))
return "PROXY proxy:80";
else
return "DIRECT";
} k、最后一个例子是演示随机使用代理,这样可以好好利用代理服务器。
function FindProxyForURL(url,host)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
return randomProxy();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
function randomProxy()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
switch( Math.floor( Math.random() * 5 ) )
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case 0:
return "PROXY proxy1:80";
break;
case 1:
return "PROXY proxy2:80";
break;
case 2:
return "PROXY proxy3:80";
break;
case 3:
return "PROXY proxy4:80";
break;
case 4:
return "PROXY proxy5:80";
break;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)