top of page

Proxy PAC file (with sample)

  • robin05156
  • 2023年7月6日
  • 読了時間: 2分

What is a PAC file?

Proxy Auto-ConfigurationProxy Auto-Configuration) file is a text file that can be written to direct the browser to directly access the specified website or to go through the proxy server only under specific conditions.

For example, if you specify the conditions without a PAC file, you can specify the days of the week and time period to go through the proxy, or only when accessing the specified domain or URL. You can specify not to go through a proxy, etc.


How to set up a PAC file

On Windows, go to Internet Options in Control Panel

Connections tab - Click the LAN Settings button to display the Local Area Network (LAN) Settings window.

Check "Use automatic configuration script" and enter the path where the pac file is located in the "Address" field.

Example if you are on the internet: https://domain/sample.pac

Example when placed as a local file: file://c:/tmp/sample.pac

ree


How to write a PAC file

PAC files are written in JavaScript format and can specify proxy rules and conditions. Below is a sample of how to write a basic PAC file.


function FindProxyForURL(url, host) {
    // Proxy definition
    // State the proxy and port to use
    var proxy = "PROXY proxy.example.com:3128";

    // Application conditions of proxy
    // The condition that it goes through the proxy only when accessing "www.amazon.com"
    if (shExpMatch(url, "https://www.amazon.com/")) {
        return proxy;
    }
    // otherwise connect directly without proxy
    return "DIRECT";
}

You can use multiple proxies

If you define multiple proxies, specify to return different proxies for different conditions.

function FindProxyForURL(url, host) {
    // Proxy definition
    var proxy1 = "PROXY proxy1.example.com:8080";
    var proxy2 = "PROXY proxy2.example.com:8080";

    // Application conditions of proxy
    if (condition1) {
        return proxy1;
    }
    if (condition2) {
        return proxy2;
    }
    return "DIRECT";
}

Supported Functions

In a PAC file, you can specify conditions using various functions. Typical functions are:

  • shExpMatch(str, pattern): Checks if the string str matches the wildcard pattern pattern.

  • dnsDomainIs(host, domain): Checks if the host name host belongs to the domain domain.

  • isInNet(ip, subnet, mask): Checks if the IP address ip belongs to the specified subnet subnet.

  • isResolvable(host): Checks if the host name host is resolvable.

You can combine these functions to specify proxy conditions in your PAC file.




 
 
 

© Copyright ROBIN planning LLC.

bottom of page