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
For 2023 - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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., 2023: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho