如何使用语法检查器测试代理PAC文件语法

时间:2020-01-09 10:42:04  来源:igfitidea点击:

如何使用命令行选项在Apple OS X/Linux/* BSD或者Unix等操作系统下测试Proxy PAC文件语法?
您可以使用Perl编写的称为pactester的工具。
此工具测试代理自动配置(PAC)文件。

浏览器使用PAC文件来确定URL的正确代理。
由于PAC文件评估机制是在浏览器内部生成的,因此无法从外部访问,因此判断浏览器将用于特定URL的代理的唯一方法是手动检查PAC文件。
但是,手动检查并不能很好地扩展规模。

Pactester解决了这个问题。
它利用JavaScript解释器和Netscape/Mozilla API评估PAC文件并自动化整个过程。

Pactester读取PAC文件,在JavaScript上下文中对其进行评估,并使用此PAC文件逻辑来确定特定URL的代理。

安装

Debian和Ubuntu Linux用户执行以下命令来安装此工具:

$ sudo apt-get install libpacparser1

输出示例:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following NEW packages will be installed:
  libpacparser1
0 upgraded, 1 newly installed, 0 to remove and 11 not upgraded.
Need to get 395 kB of archives.
After this operation, 1,339 kB of additional disk space will be used.
Get:1 http://debian.osuosl.org/debian/ squeeze/main libpacparser1 amd64 1.2.6-2 [395 kB]
Fetched 395 kB in 2s (153 kB/s)                       
Selecting previously deselected package libpacparser1.
(Reading database ... 281091 files and directories currently installed.)
Unpacking libpacparser1 (from .../libpacparser1_1.2.6-2_amd64.deb) ...
Processing triggers for man-db ...
Setting up libpacparser1 (1.2.6-2) ...

有关源代码安装的说明

执行以下wget命令以下载源代码:

$ cd /tmp/
$ wget http://pactester.googlecode.com/files/pactester-1.0.8.tar.gz

输出示例:

--2012-11-20 21:34:17--  http://pactester.googlecode.com/files/pactester-1.0.8.tar.gz
Resolving pactester.googlecode.com... 74.125.128.82, 2404:6800:4005:c00::52
Connecting to pactester.googlecode.com|74.125.128.82|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1214308 (1.2M) [application/x-gzip]
Saving to: `pactester-1.0.8.tar.gz'
 
100%[============================- ] 12,14,308    352K/s   in 3.4s    
 
2012-11-20 21:34:21 (352 KB/s) - `pactester-1.0.8.tar.gz' saved [1214308/1214308]

使用tar命令解压缩tar包:

$ tar xvf pactester-1.0.8.tar.gz

输出示例:

pactester-1.0.8/
pactester-1.0.8/README
pactester-1.0.8/build/
pactester-1.0.8/build/Log-Log4perl-1.08.tar.gz
pactester-1.0.8/build/install.sh
pactester-1.0.8/build/js-1.5.tar.gz
pactester-1.0.8/build/JavaScript-SpiderMonkey-0.17.tar.gz
pactester-1.0.8/pactester
pactester-1.0.8/INSTALL
pactester-1.0.8/COPYING
pactester-1.0.8/pac_utils.js

在~/pactester目录中安装pactester,执行:

$ mkdir $HOME/pactester
$ cd pactester-1.0.8/build/
$ ./install.sh $HOME/pactester

输出示例:

....
.
<snip>
...
Installing /home/Hyman/pactester/lib/perl/5.10.1/auto/JavaScript/SpiderMonkey/SpiderMonkey.so
Installing /home/Hyman/pactester/lib/perl/5.10.1/JavaScript/SpiderMonkey.pm
Installing /home/Hyman/pactester/man/man3/JavaScript::SpiderMonkey.3pm
Appending installation info to /home/Hyman/pactester/lib/perl/5.10.1/perllocal.pod
-e 
 
 
-e ###########################################
>>PERLLIB=-e /home/Hyman/pactester/share/perl/5.10.1:/home/Hyman/pactester/lib/perl/5.10.1
-e I have set up PERLLIB path in pactester script, so you won't have to
-e set it as an environment variable :)

如何使用pactester命令测试代理自动配置(pac)文件?

语法为:

pactester -p /path/to/proxy.pac.file -u url
pactester -p /path/to/proxy.pac.file -u url -c client.ip.address.here

示例proxy.pac文件:

function FindProxyForURL(url, host) {
    // Your proxy server name and port
    var proxyserver = 'squidproxy1.theitroad.local.home.com:3128';
    //
    //  Here's a list of hosts to connect via the PROXY server
    //
    var proxylist = new Array(
        "reddit.com",
        "www.theitroad.local",
        "mail.google.com",
        "www.pandora.com",
        "www.google.com",
	"www.hulu.com",
	"mail.theitroad.local",
	"mail.theitroad.com",
	"slashdot.org"
    );
    // Return our proxy name for matched domains/hosts
    for(var i=0; i<proxylist.length; i++) {
        var value = proxylist[i];
        if ( localHostOrDomainIs(host, value) ) {
            return "PROXY "+proxyserver;
        }
    }
    return "DIRECT";
}

要找出pac文件proxy.pac为URL http://www.theitroad.local返回的代理字符串:

$ pactester -p proxy.pac -u http://www.theitroad.local

输出示例:

PROXY squidproxy1.theitroad.local.home.com:3128

要找出pac文件proxy.pac为URL http://www.google.com返回的代理字符串:

$ pactester -p proxy.pac -u http://www.google.com

输出示例:

DIRECT

测试IP地址为192.168.1.5的客户端的设置:

$ pactester -p wpad.dat -c 192.168.1.5 -u http://www.google.com

/tmp/proxy.url.test文件中创建网址/主机列表:

http://www.theitroad.local
http://theitroad.com
https://mail.google.com
https://www.google.com
http://nas03
http://router1
http://www.pandora.com

现在,找出/tmp/proxy.url.test文件中指定的URL列表的代理设置。

$ pactester -p /var/www/html/proxy.pac -f /tmp/proxy.url.test

输出示例:

http://www.theitroad.local : PROXY squidproxy1.theitroad.local.home.com:3128
http://theitroad.com : DIRECT
https://mail.google.com : PROXY squidproxy1.theitroad.local.home.com:3128
https://www.google.com : PROXY squidproxy1.theitroad.local.home.com:3128
http://nas03 : DIRECT
http://router1 : DIRECT
http://www.pandora.com : PROXY squidproxy1.theitroad.local.home.com:3128