Controlling Internet Access using a Pac File - Setting the Proxy server based on the Client subnet
(Page 2 of 4 )
One of the most useful things you can do is send different subnets through different proxies. This is very good for multiple sites that each have their own Internet connection.
To do this we use a simple if statement to test for the client's IP address, and then set the proxy server based on what is returned, like this:
// Test for entire subnets
// if (isInNet(myIpAddress(), "192.168.0.0", "255.255.255.0"))
// proxy = "PROXY 192.168.0.1:8080";
This statement uses the function IsInNet to determine whether or not the client's IP address matches the one you specify. In the above code if you are any client on the 192.168.0.0 subnet, the function will return true and set the proxy variable to be 192.168.0.1:8080.
You can also use a similar block of code to send individual clients to a certain proxy instead of the entire subnet. This could be useful if certain people are heavy Internet users and have a dedicated proxy.
// Test for individual clients
if (isInNet(myIpAddress(), "192.168.0.25", "255.255.255.255"))
proxy = "PROXY 192.168.0.242:8080";
Of course if you want to use this on certain clients, they will have to have a static IP address.
Directing Traffic based on URL
Next we will test for a certain site URL, and if we find the user wants to access that URL we will send them down yet another proxy.
if (url.substring(0, 24) == "http://www.microsoft.com")
proxy = "PROXY 192.168.0.242:8080";
This function tests the URL string passed into the PAC file for the website address. The 0, 24 parameters tell the function to start at position 0 from the left hand side of the string, and count 24 characters. If the string matches the function returns true and the proxy variable is set.
As we are using the Substring function, we can not only test for a specific web site, but specific protocols as well. For instance you might want all of your FTP traffic to go out over a different link from your normal HTTP and HTTPS traffic.
if (url.substring(0, 4) == "ftp:")
proxy = "PROXY 192.168.0.241:8080";
Next: Making sure Internal Sites don't use the Proxy >>
More BrainDump Articles
More By Luke Niland