Proxy PAC

From Ever changing code
Revision as of 11:14, 15 November 2022 by Pio2pio (talk | contribs)
Jump to navigation Jump to search

Proxy auto-config | Proxy PAC

A proxy auto-config (PAC) file defines how web browsers and other user agents can automatically choose the appropriate proxy server (access method) for fetching a given URL.

A PAC file contains a JavaScript function FindProxyForURL(url, host). This function returns a string with one or more access method specifications. These specifications cause the user agent to use a particular proxy server or to connect directly.

// Option 1
function FindProxyForURL(url, host) {
  // our local URLs from the domains below example.com don't need a proxy:
  if (shExpMatch(host, "*.foo.acme.com")) {
    return "PROXY proxy.acme.com:8123";
  }

  if (shExpMatch(host, "*.foo-dev.acme.com")) {
    return "PROXY proxy.acme.com:8123";
  }

  if (shExpMatch(host, "*.foo-prod.acme.com")) {
    return "PROXY proxy.acme.com:8123";
  }

  if (shExpMatch(host, "*.example.com")) {
    return "PROXY proxy.acme.com:8123";
  }

  // All other requests go through port 8080 of proxy.example.com.
  // should that fail to respond, go directly to the WWW:
  return "DIRECT";
}


// Option 2
function FindProxyForURL(url, host) {
  // our local URLs from the domains below example.com don't need a proxy:
  if (shExpMatch(host, "*.foo.acme.com")     || 
      shExpMatch(host, "*.foo-dev.acme.com") ||
      shExpMatch(host, "*.foo-prod.acme.com") ||
      shExpMatch(host, "*.example.com")) {
    return "PROXY proxy.acme.com:8123";
  }

  // All other requests go through port 8080 of proxy.example.com.
  // should that fail to respond, go directly to the WWW:
  return "DIRECT";
}