OS X PAC Global Automatic Proxy Settings

Go to System Preferences → Network → Advanced → agent, then in the "Agent Configuration File" column fill PAC file URL.

OS X PAC Global Automatic Proxy Settings

Wiki

PAC file is a plain text format, in fact, JavaScript files. Chrome/Chromium extension SwitchSharp the "Auto Switch Mode" function is actually simple to create and maintain a PAC file, but the function is relatively weak.

PAC file FindProxyForURL function

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

URL parameter is the user input URL, URL parameter host is the host name.

For example, the URL http://www.twitter.com/, then the host is www.twitter.com

A simple PAC file like this:

function FindProxyForURL(url, host) {
    return "DIRECT";
}

The PAC file actually didn't do anything to any URL, will be DIRECT (Direct Internet).

PAC file the return value type

In addition to return DIRECT outside, there are two common ways:

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

Then, specify a http proxy using PAC should be written

function FindProxyForURL(url, host) {
    return "PROXY 127.0.0.1:8087";
}

You can even specify multiple agents

function FindProxyForURL(url, host) {
    return "DIRECT; PROXY 127.0.0.1:8087; SOCKS5 proxysample.com:8080";
}

The meaning of the phrase statement is

  • All URL, are directly connected;
  • If you can not connect directly, then use the http proxy 127.0.0.1:8087 connection;
  • If you still can not connect, then use proxysample.com:8080 This socks5 proxy connection.

Consensus sequence using different connections and statements in the order, you can change according to their actual situation.

Maybe you know exactly which sites can not directly connected, you must connect with PROXY or SOCKS5, you can specify the proxy configuration on the site

function FindProxyForURL(url, host) {
    if (shExpMatch(url,"*.google.com/*")) {
        return "PROXY 127.0.0.1:8087";
    }
    if (shExpMatch(url, "*.wikipedia.com:*/*")) {
        return "SOCKS5 proxysample.com:8080";
    }
    if (isInNet(host, "10.0.0.0",  "255.0.0.0")){
        return "DIRECT";
    }
    return "DIRECT; PROXY 127.0.0.1:8087; SOCKS5 proxysample.com:8080";
}

Introduces two new functions this PAC file, but the literal meaning, we can probably guess the meaning of the code:

  • When the URL is *google.com/*, automatically using PROXY proxy;
  • When the URL is *wikipedia.cm/*, automatically using SOCKS5 proxy;
  • When the host is within the 10.0.0.0/255.0.0.0 subnet automatically when directly connected;

If you do not match, then followed by DIRECT, PROXY, order SOCKS5 attempt.

shExpMatch function is used to match the URL or host, matching a similar with DOS wildcard. For example used earlier *.Google.com/* can match any included .Google.com/ string.

PAC file Chrome/Chromium extension SwitchSharp also created a custom function that can be used to match a regular expression, but personally think that usually not need to use complex regular expression matching the URL.

isInNet function is used to return the requested host is in a specified region. Notably, the second parameter must be isInNet IP, can not be a host name. Hence the need to convert host names into IP. For example, isInNet (host, dnsResolve (www.google.com), 255.255.255.0).

0.00 avg. rating (0% score) - 0 votes