Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Python, Lua and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Java, C, C++, Ruby, MySQL and Linux / Tomcat systems

Phone System reconfiguration 11.1.2018 to 26.1.2018 - we are on email but incoming landline not available. Temp number 0797 4 925928
Perl script to Auto config Browser

Posted by hugo_owen (hugo_owen), 13 August 2003
Please find below an example of a very simple pac (Proxy Auto Configuration) file.  These tell a browser, IE or Netscape, what to do in terms of URL address.  There is subtle difference between them, in that one says use the UK proxy and the other the US proxy.  If this is to work all browsers would be configured with just one PAC file e.g "company.pac" as the file name in the browser configuration.

This could have a very big impact for our mobile users, they will always be automatically given the nearest proxy server for Internet Browsing wherever they are in the world.

Perl script below delivers a pac file based on an IP address. I found this on the net, but I really don't understand it enough to develop it myself.

What I am looking to do is deliver say the pac for the US if the client IP address is 10.161.x.x and the pac for the UK if the client IP address is 10.0.x.x. again this would get a whole lot more complicated in reality.

Perl script:

my $onCampusPACFName = '/usr/local/web/proxy/cwru-on.pac';

## 'offCampusPACFName' -- The full path to the off-campus PAC file
my $offCampusPACFName = '/usr/local/web/proxy/cwru-off.pac';

## 'logFName' -- The full path to the log file
my $logFName = '/usr/local/web/logs/selectPAC.log';



## MAIN program begins here
$SIG{'__DIE__'} = 'handle_die';

open LOGFILE,">>$logFName" || die "Couldn't open log file for write: $!\n";

my @output;

push @output,"Content-type: application/x-ns-proxy-autoconfig\n\n";

if (isCWRUip($ENV{'REMOTE_ADDR'})) {
 open PACFILE,$onCampusPACFName || die "Couldn't open on campus PAC file name: $!\n";
 push @output, <PACFILE>;
 close PACFILE;
 print LOGFILE scalar(localtime())."\t".time()."\t$ENV{'REMOTE_USER'}\t$ENV{'REMOTE_ADDR'}\ton\n";
} else {
 open PACFILE,$offCampusPACFName || die "Couldn't open off campus PAC file name: $!\n";
 push @output, <PACFILE>;
 close PACFILE;
 print LOGFILE scalar(localtime())."\t".time()."\t$ENV{'REMOTE_USER'}\t$ENV{'REMOTE_ADDR'}\toff\n";

close LOGFILE;

print join '',@output;
exit 0;

Example of PAC File:

function FindProxyForURL(url, host)
if (isPlainHostName(host))
return "DIRECT";
else if (shExpMatch(host, ""))
return "PROXY";
else if (shExpMatch(host, "*"))
return "DIRECT";
else if (shExpMatch(host, "69.*"))
return "DIRECT";
else if (shExpMatch(host, "169.*"))
return "DIRECT";
else if (shExpMatch(host, "10.*"))
return "DIRECT";
else if (shExpMatch(host, "192.168.*"))
return "DIRECT";
else if (shExpMatch(host, "172.30.*"))
return "DIRECT";
else if (shExpMatch(host, ""))
return "DIRECT";
return "PROXY";

Posted by admin (Graham Ellis), 14 August 2003
Hmmm - you say you found this on the net.  Any documentation with it??  It looks to me like the CWRUWWW module checks whether a remote proxy is up via the isCWRUp function, with the code you have copied onto the board sending out one of two alternative auto-config files.

As a whole, the program appears to respond to a request to a CGI url (perhaps it's a script in the cgi-bin directory, or with a .cgi extension), and send our a response which is the configuration details for the browser - the header sent is not the normal "text/html" but something rather different.

The PAC file looks like Javascript rather than Perl, and it's being fed to the browser to allow the browser to extablish the correct proxy; at a first guess, that's where you'll need to make specific changes and it looks reasonably straightforward to follow the current structure.  Perhaps you should start testtingt with something even simpler - just a two-way switch - to see if it works.

If I was experimenting with this and looking for further documentation/help, I would search for details of the x-ns-proxy-autoconfig protocol.  Hope my ramblings help - I'm not really sure what sort of answer you're looking for ....

This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2018: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho