2

I remember a while back reading about editing the proxy pac file which would switch proxies every set time interval, such as on an hourly basis.

But I cannot find the function or remember how to do this,

am I mistaken or is this possible with proxy.pac?

I am using mozilla.

UPDATE: Is FindProxyForURL() called every time a HTTP request is sent out?

KJW
  • 15,035
  • 47
  • 137
  • 243

2 Answers2

3

The PAC file is simply a Javascript function function FindProxyForURL(url, host) {} that gets the URL of the resource being fetched and returns a string specifying which proxy to use (or DIRECT for no proxy at all) for that resource.
All browser requests go through the function, regardless of the protocol.

In that function block, you should be able to query the current time and make a decision on which proxy to return.

For instance:

function FindProxyForURL(url, host) {
    // If URL has no dots in host name, send traffic direct.
    if (isPlainHostName(host)) return "DIRECT";

    // Known local Top Level Domains are direct
    if(/\.(local|lcl|domain|grp|localdomain)(\:\d+)?($|\/)/i.test(url))
        return "DIRECT";

    // Split traffic depending on the time
    var dTime = new Date();
    var hours = dTime.getHours();
    if (hours < 12) {
        // From midnight to lunchtime, use Proxy A
        // which is a standard HTTP proxy on port 8080
        return "PROXY proxyA.example.com:8080"
    } else {
        // From lunchtime to midnight, use Proxy B
        // which is a Socks5 proxy on port 777
        return "SOCKS5 proxyB.example.com:777"
    }
}
Renaud Bompuis
  • 16,596
  • 4
  • 56
  • 86
  • a question about this is that, how often is FindProxyForURL() run? what about when I need to make the proxy switch every 15 minutes? I'm not sure how often the `dTime` variable is updated. – KJW Mar 22 '12 at 21:01
  • The function is called for every single request made by the browser. The function receives the full URL, so you can switch proxy depending on what site the browser is trying to reach. – Renaud Bompuis Mar 22 '12 at 22:49
  • @Renaud Bompuis Hi, does browser really call this function before each request? Do you have any resources that written this? Thanks. – jdiver Jun 02 '14 at 11:15
1

Or you could rely on existing PAC functions:

timeRange() can be used to specify different proxies for a specific time range. Note example would utilize 'proxy1.example.com' 8am through to 6pm. Example:

if (timeRange(8, 18)) return "PROXY proxy1.example.com:8080";
    else return "DIRECT";
Oleg
  • 24,465
  • 8
  • 61
  • 91