我的新项目:为中国用户提供的 PAC 代理自动配置脚本

哎,我懒得翻译了,这是 README.md 里的内容:

china-pac

This is a fast PAC specifically designed for Chinese users.

It’ll automatically sync all Chinese domains from dnsmasq-china-list and rebuild the package (if there’s any updates) every one hour.

The PAC will return:

  • “DIRECT” if the host is an ipv4/ipv6 address
  • “DIRECT” if the host is a Chinese domain
  • a proxy (default SOCKS 127.0.0.1:1080)

Note that the chromium browser doesn’t load the PAC if its file size is larger than 1M, while ungoogled-chromium has removed that limit.

Installation

Debian & Ubuntu

Install via the OBS repo (see obs-repo for setup).

Other Operating Systems

./generate.sh

Configuration

You can change the default proxy in proxy file, or add extra domains in extra.list file.

For Debian & Ubuntu users, all the configuration files are in /etc/china-pac/.
Be sure to run sudo dpkg-reconfigure china-pac after modifying any configuration files.
The generated PAC file is located at /var/lib/china-pac/proxy.pac.

为了防止新手不会用,我还是说一下用法吧,典型的用法如下:

  1. 安装这个软件包
  2. 安装一个 web 服务器(比如 share
  3. 编写一个 systemd 服务用来启动 web 服务器,并设为开机自启

如果你安装了 share,只需要拷贝已存在的 service 文件:

mkdir -p ~/.config/systemd/user/
cp /usr/lib/systemd/system/share.service ~/.config/systemd/user/

再稍加修改即可:

[Unit]
Description=Share Service

[Service]
Type=simple
Environment=SHARE_LOG_TIME=
ExecStart=share -b 127.0.0.1 -p 1234 %h/Public/
Restart=on-failure

[Install]
WantedBy=default.target

然后启动这个服务并设为开机自启:

systemctl --user enable --now share.service

最后,我们再设置一个软链接即可:

ln -sf /var/lib/china-pac/proxy.pac ~/Public/

咦是用Set的,新思路啊。

这其实是我在几年前从 GitHub - zhiyi7/gfw-pac: 科学上网 PAC 成品文件以及生成器。可自定义代理域名和直连域名。不在自定义名单里的域名再按IP匹配,CNIP走直连,其他走代理,支持IPv6 学来的,当时这个项目把域名当作属性存在一个对象里,和用 Set 基本上一样高效。

刚刚去搜索并查看了下上面那个项目,发现已经换成用列表了,不知道为什么。。。

这个项目使用的radix tree,很高效,但也复杂。

我使用的方案是把域名反转然后丢进一个正则里。这一步是在预处理阶段完成的。我有一个YAML配置文件来对不同的域名进行分流,会被预处理成几个不同的正则,然后把要分流的域名反转后挨个匹配,看看应该走哪个代理。

我不知道JavaScript编译这种巨长的正则的性能如何,但Rust反正挺费内存的……所以我在Rust里放弃了这种方案,改用Aho-Corasick了。

1 个赞