
本文中的软件使用 MacPorts(2.3.4) 安装。
安装 Privoxy(3.0.21)、py-pip(8.1.2)、gfwlist2privoxy(1.0.3)
$ sudo port install privoxy py-pip
$ sudo pip install gfwlist2privoxy
查找 gfwlist2privoxy 的安装路径
$ find /opt -iname 'gfwlist2privoxy'
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/gfwlist2privoxy
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/gfwlist2privoxy
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/gfwlist2privoxy-1.0.3-py2.7.egg-info
配置 Privoxy
生成空文件:
$ sudo touch /opt/local/etc/privoxy/gfwlist.action
修改文件所有者:
$ sudo chown privoxy:privoxy /opt/local/etc/privoxy/gfwlist.action
修改文件权限:
$ sudo chmod 660 /opt/local/etc/privoxy/gfwlist.action
允许当前用户修改该文件,便于后续使用当前用户的 crontab
定时生成该文件:
$ sudo chmod +a "${USER} allow read,write" /opt/local/etc/privoxy/gfwlist.action
查看文件权限:
$ ls -le /opt/local/etc/privoxy/gfwlist.action
-rw-rw----+ 1 privoxy privoxy 62526 Sep 6 00:00 /opt/local/etc/privoxy/gfwlist.action
0: user:YOURNAME allow read,write
在文件 /opt/local/etc/privoxy/config
的行 actionsfile user.action
下一行追加 actionsfile gfwlist.action
,原文件会备份为 /opt/local/etc/privoxy/config.bak
。注意这个命令就是写作三行的:
$ sudo sed -i '.bak' '/actionsfile user.action/a\ \
actionsfile gfwlist.action\
' /opt/local/etc/privoxy/config
对比原文件和修改后的文件,确保上述命令被正确执行:
$ sudo diff -ruN /opt/local/etc/privoxy/config.bak /opt/local/etc/privoxy/config
--- /opt/local/etc/privoxy/config.bak
+++ /opt/local/etc/privoxy/config
@@ -370,6 +370,7 @@
actionsfile match-all.action # Actions that are applied to all sites and maybe overruled later on.
actionsfile default.action # Main actions file
actionsfile user.action # User customizations
+actionsfile gfwlist.action
#
#
# 2.5. filterfile
删除备份文件
$ sudo rm /opt/local/etc/privoxy/config.bak
启动 Privoxy
$ sudo launchctl load -w /Library/LaunchDaemons/org.macports.Privoxy.plist
生成 gfwlist.action 文件
下载 gfwlist
并生成 gfwlist.action
文件。其中 127.0.0.1:1080
是监听在本地 1080 端口的 socks5 代理服务器,至于如何建立一个 socks5 代理服务器并不在本文讨论的范畴。
/opt/local/etc/privoxy/gfwlist.action
修改后不需要重启 Privoxy
,Privoxy
会自动应用修改。
$ mkdir -p ~/bin/
$ echo "\!this is a user defined rule" >> ~/bin/user_rule.txt
$ echo "ip.cn" >> ~/bin/user_rule.txt
$ /opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/gfwlist2privoxy -f /opt/local/etc/privoxy/gfwlist.action -p '127.0.0.1:1080' -t socks5 --user-rule ~/bin/user_rule.txt
可以将这个命令加入 crontab
中定时执行,使用命令crontab -e
编辑当前用户的 crontab
,
添加一条记录,这里的例子是每天0点执行一次:
#minute hour mday month wday command
0 0 * * * /opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/gfwlist2privoxy -f /opt/local/etc/privoxy/gfwlist.action -p '127.0.0.1:1080' -t socks5 --user-rule ~/bin/user_rule.txt > /dev/null 2>&1
测试
测试 socks5 代理服务器正在监听 1080 端口:
$ telnet 127.0.0.1 1080
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
测试通过 socks5 代理服务器访问 ip.cn
:
curl -x 'socks5://127.0.0.1:1080' ip.cn
当前 IP:x.x.x.x 来自:xxxxxx xxx
测试通过 Privoxy 访问 ip.cn
分流到了 socks5 代理服务器。我们已经把 ip.cn
添加到了 user_rule.txt
,因此下述命令输出结果应该和上述命令一致:
$ curl -x 'http://127.0.0.1:8118' ip.cn
当前 IP:x.x.x.x 来自:xxxxxx xxx
使用
在操作系统的系统偏好设置
(系统偏好设置->网络->高级…->代理)里设置 Web 代理(HTTP)
和安全 Web 代理(HTTPS)
为 Privoxy 监听的地址 127.0.0.1:8118
。
#gfwlist, #gfwlist2privoxy, #privoxy
#mac-os-x, #macports
#curl
#minute