为 Mac 终端设置 Shadowsocks 代理

《Ubuntu 搭建 shadowsocks》 一文中,我们讨论了如何在墙外 Ubuntu 服务器上搭建 Shadowsocks。

但是大部分终端并不支持 SS 使用的 socks5 协议,此时,我们就需要使用 polipo 来进行协议转换,让终端也支持 SS 代理。

安装

1
brew install polipo

修改配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>homebrew.mxcl.polipo</string>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>ProgramArguments</key>
<array>
<string>/usr/local/opt/polipo/bin/polipo</string>
<string>socksParentProxy=localhost:1080</string>
</array>
<!-- Set `ulimit -n 20480`. The default OS X limit is 256, that's
not enough for Polipo (displays 'too many files open' errors).
It seems like you have no reason to lower this limit
(and unlikely will want to raise it). -->
<key>SoftResourceLimits</key>
<dict>
<key>NumberOfFiles</key>
<integer>20480</integer>
</dict>
</dict>
</plist>

修改的地方在于增加:

1
<string>socksParentProxy=localhost:1080</string>

开机启动

1
2
3
ln -sfv /usr/local/opt/polipo/*.plist ~/Library/LaunchAgents
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.polipo.plist
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.polipo.plist

验证及使用

打开终端,输入

1
2
3
4
➜  ~ curl ip.gs
当前 IP:219.134.121.82 来自:中国广东深圳 电信
➜ ~ http_proxy=http://localhost:8123 curl ip.gs
当前 IP:45.33.58.130 来自:美国加利福尼亚州费利蒙 Linode, LLC.

如上所示,为某个命令设置代理,前面加上http_proxy=http://localhost:8123 后接命令即可。

注:8123是polipo的默认端口,如有需要,可以修改成其他有效端口。