-ie Browser automatically configures proxy script-proxy.pac file and Pac-related syntax

Source: Internet
Author: User
Tags gopher

When using the laptop to go to the home and the unit, because the unit is the agent with the Internet, the home is directly connected. So every time you have to modify the agent settings of IE, although it is a trivial matter, but every time to modify is always a bit annoying, so refer to Google, wrote a script to automatically configure the agent Just connect to the LAN and you can surf the Internet.

function FindProxyForURL (url,host) {
if (isPlainHostName (host)) | | shExpMatch (URL, "http://127.0.0.1/*") | | isInNet (Host, "192.168.0.0", "255.255.255.0")) {
return "direct";
} else {
if (myipaddress () = = "192.168.0.49")
Return "PROXY 192.168.0.6:808";
Else
return "direct";
}
}


Save the above code as a Proxy.pac file, such as: C:\proxy.pac, and then in the IE Menu "tools", "Intel Options", "Connect" LAN Settings

Use the automatic configuration script to select, and then fill in the Address bar File://c:/proxy.pac, note that the file is two slashes, to make the settings take effect, you need to turn off IE and reopen.

Now we introduce this PROXY.PAC script file, the syntax of the script is JS syntax, JS built-in functions can be used, to implement automatic configuration agent, need to implement FindProxyForURL this function, its parameter URL represents the connection to be accessed, the host name to access the connection , the function has three return parameters
Direct: Connect directly
Proxy Ip:port
Socket Ip:port
return result case insensitive

Other built-in syntax for PAC see http://wp.netscape.com/eng/mozilla/2.0/relnotes/demo/proxy-live.html

Another important application of PAC scripting is the control of the PAC script in the case where multiple proxy servers coexist:

· Users randomly choose to use any one of the multiple proxy servers to achieve the purpose of traffic load balancing;

· The administrator uses the PAC script to control the user and not use a proxy server, so that the proxy server can be maintained in time;

· Let the server work in the main standby mode, when the primary server is down, will automatically switch to the other backup server without interruption of service;

· Automatically select the best proxy server depending on the destination you are visiting.


Here are a few examples of PAC, by the way, learn its syntax

A, isPlainHostName (host), this example demonstrates whether a local host, such as http://myservername/
The way access, if it is directly connected, otherwise use the proxy

function FindProxyForURL (URL, host)
{
if (isPlainHostName (host))
return "DIRECT";
Else
Return "PROXY proxy:80";
}



B, dnsDomainIs (Host, ""), localHostOrDomainIs (Host, ""), this example demonstrates the access to the host
Whether it belongs to a domain and a domain name, if it belongs to the host name of the. company.com domain, and the domain name is not
Www.company.com and home.company.com are directly connected, otherwise use proxy access.

function FindProxyForURL (URL, host)
{
if (isPlainHostName (host) | |
dnsDomainIs (Host, ". company.com")) &&
!localhostordomainis (Host, "www.company.com") &&
!localhostordomainis (Host, "home.company.com"))

return "DIRECT";
Else
Return "PROXY proxy:80";
}


C, isresolvable (host), this example shows whether the host name can be resolved by the DNS server, if directly accessible, no
It is accessed through the proxy.

function FindProxyForURL (URL, host)
{
if (isresolvable (host))
return "DIRECT";
Else
Return "PROXY proxy:80";
}


D, isInNet (host, "", ""), this example shows whether the access IP is within a subnet, if it is directly accessible,
Otherwise, through the proxy, the example shows access to the Tsinghua IP section of the homepage without agents.

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



E, shExpMatch (host, ""), this example demonstrates changing the connection type based on the host domain name, local host, *.edu,
*.com are used in different connection modes respectively.

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



F, url.substring (), this example demonstrates the choice of different proxies according to different protocols, HTTP, HTTPS, FTP,
The gopher use different proxies respectively.

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



G, Dnsresolve (host), this example demonstrates whether the access host is an IP, if it is to use the proxy, otherwise straight
Connection.

function FindProxyForURL (URL, host)
{
if (dnsresolve (host) = = "166.111.8.237") {
Return "PROXY secproxy:8080";
}
else {
Return "PROXY proxy:80";
}
}



H, myIpAddress (), this example demonstrates whether a local IP is an IP, if you are using a proxy, or directly make
With the connection.

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



I, dnsDomainLevels (host), this example shows the number of levels of the domain name of the access host, that is, the domain name has several points
If the domain name is a bit, access by proxy, otherwise directly connected.

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



J, Weekdayrange (), this example shows the scope of the current date to change the use of the proxy, if it is GMT time Wednesday
By Saturday, use a proxy connection, or connect directly.

function FindProxyForURL (URL, host)
{
if (Weekdayrange ("WED", "SAT", "GMT"))
Return "PROXY proxy:80";
Else
return "DIRECT";
}



K, the last example is to demonstrate the use of proxies randomly, so that you can take advantage of the proxy server.

function FindProxyForURL (url,host)
{
return Randomproxy ();
}

function Randomproxy ()
{
Switch (Math.floor (Math.random () * 5))
{
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
}
}

-ie Browser automatically configures proxy script-proxy.pac file and Pac-related syntax

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.