PAC Auto Proxy file format, teach you how to write Pac file

Source: Internet
Author: User

PAC file format

The PAC file is in plain text format and is actually a JavaScript file. The "Auto Switch Mode" feature of Chrome/chromium's extended switchy! is actually creating and maintaining a simple PAC file, but the functionality is weak.

For general applications, even if you know little about JavaScript and programming, you can implement basic functionality through the introduction of this article.

PAC file FindProxyForURL function

The PAC file must contain a function: FindProxyForURL (URL, host).

The parameter URL is the URL that the user enters, and the parameter host is the host name in the URL.

For example, the URL is http://www.truevue.org/javascript/pac-proxy-setting, then host is www.truevue.org

One of the simplest PAC file contents is as follows:

This PAC file actually does nothing and will "direct" (directly connected) to any URL.

PAC file return value type

In addition to the return "DIRECT", there are two common ways of doing this:

PROXY proxysample.com:8080

HTTP proxy host and port, the host can also be represented by IP

SOCKS5 socks5sample.com:1080

SOCKS5 proxy host and port, the host can also be represented by IP

Well, we can guess that using a PAC to specify an HTTP proxy should write this

function FindProxyForURL (URL, host) {  return "PROXY 192.168.1.1:3128";}

You can even specify multiple proxies  

function FindProxyForURL (URL, host) {  

The meaning of this sentence is:

    1. For all URLs, connect directly;
    2. If you can not connect directly, then use 192.168.1.1:3128 this HTTP proxy connection;
    3. If you are still unable to connect, use lilinux.net:1080 this SOCKS5 proxy connection.

The order of the different connections is consistent with the order in the statement, and you can change it according to your own situation.

Perhaps you clearly know which sites are not directly connected, you must use proxy or SOCKS5 connection, you can specify the proxy configuration for the site

function FindProxyForURL (URL, host) {   if (shExpMatch (URL, "*.google.com/*")) {     return ' PROXY 192.168.1.1:3128 ' ;   }   if (shExpMatch (URL, "*.wikipedia.com:*/*")) {     return "SOCKS5 lilinux.net:1080";   }   if (isInNet (host, "10.0.0.0",  "255.0.0.0")) {     return "DIRECT";   }   Return "DIRECT; PROXY 192.168.1.1:3128; SOCKS5 lilinux.net:1080 "; }

This PAC file introduces two new functions, but literally we can guess what the code means:

    1. When the URL is *.google.com/*, the proxy agent is used automatically;
    2. When the URL is *.wikipedia.cm/*, the SOCKS5 proxy is used automatically;
    3. When the host is within the subnet of the 10.0.0.0/255.0.0.0, it is automatically connected directly;
    4. If they do not match, then try the order of direct, PROXY, SOCKS5 in sequence.

The shExpMatch function is used to match the URL or host, matching the same way as a DOS wildcard. For example, the previously used "*.google.com/*" can match any string containing ". google.com/".

The Chrome/chromium extension switchy! creates a PAC file that also customizes a function that can be used to match a regular expression, although the individual believes that a strong regular expression is not usually required on a URL match.

The isInNet function is used to return the requested host within the specified domain. It is important to note that the second parameter of the isinnet must be IP and cannot be a host name. Therefore, the host name needs to be converted to IP. For example "isInNet (host, Dnsresolve (www.google.com)," 255.255.255.0 ")" Here, should be able to solve your problem. "

A PAC file can be used with JavaScript functions

Of course, PAC is more than that simple, it also provides a lot of other functions, in this article is not described in detail. The JavaScript functions that can be used in the PAC proxy file are listed in Http://www.truevue.org/javascript/pac-functions.

You may want to publish the PAC file to the Internet so that other users just need to specify the URL of the PAC file in the browser. You have to configure your server map. pac file suffix to MIME type: Application/x-ns-proxy-autoconfig If you are using a Netscape server, edit the mime.types file under the Config directory. If it is Apache, CERN or NCSA server, use the AddType directive.

PAC Auto Proxy file format, teach you how to write Pac file

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.