MacOS Sierra 使用 Shadowsocks + Privoxy 中轉代理為終端命令列 Terminal 提供出牆服務

Fish發表於2017-03-27

安裝Privoxy

brew install privoxy

如果提示 brew 命令不存在,那就先安裝它,正常情況下你會看到安裝成功的提示:

==> Downloading https://homebrew.bintray.com/bottles/privoxy-3.0.26.sierra.bottl
######################################################################## 100.0%
==> Pouring privoxy-3.0.26.sierra.bottle.1.tar.gz
==> Caveats
To have launchd start privoxy now and restart at login:
  brew services start privoxy
Or, if you don`t want/need a background service you can just run:
  privoxy /usr/local/etc/privoxy/config
==> Summary
?  /usr/local/Cellar/privoxy/3.0.26: 52 files, 1.8MB

配置Privoxy

vim /usr/local/etc/privoxy/config

開啟配置檔案,在最下面新增:

listen-address 0.0.0.0:8118
forward-socks5 / 127.0.0.1:1080 .

1080Shadowsocks代理的埠,8118是開啟http代理的埠。使用0.0.0.0即可在區域網內使用此代理,如只想本機使用,使用127.0.0.1

啟動Privoxy

Terminal中直接指定配置檔案路徑啟動服務:

如果不指定配置檔案路徑,它預設會讀取 ~/config 配置檔案。

/usr/local/sbin/privoxy /usr/local/etc/privoxy/config

檢查服務是否啟動

~ netstat -an | grep 8118
tcp4       0      0  *.8118                 *.*                    LISTEN
tcp4       0      0  127.0.0.1.8118         *.*                    LISTEN

測試效果

注意這個測試需要先安裝 httpie ,一個 Python 的網路命令列工具

沒有代理的情況:

~  http www.youtube.com

http: error: ConnectionError: HTTPConnectionPool(host=`www.youtube.com`, port=80): Max retries exceeded with url: / (Caused by NewConnectionError(`<requests.packages.urllib3.connection.HTTPConnection object at 0x11116f748>: Failed to establish a new connection: [Errno 65] No route to host`,)) while doing GET request to URL: http://www.youtube.com/

有代理的情況:
先執行

export http_proxy=`http://127.0.0.1:8118`
export https_proxy=`http://127.0.0.1:8118`

看到已經生效:

~  http www.youtube.com
HTTP/1.1 301 Moved Permanently
Cache-Control: no-cache
Content-Length: 0
Content-Type: text/html; charset=utf-8
Date: Mon, 27 Mar 2017 05:58:58 GMT
Expires: Tue, 27 Apr 1971 19:44:06 EST
Location: https://www.youtube.com/
P3P: CP="This is not a P3P policy! See http://support.google.com/accounts/answer/151657?hl=en for more info."
Proxy-Connection: keep-alive
Server: YouTubeFrontEnd
Set-Cookie: VISITOR_INFO1_LIVE=NN4ZuKpIZ6I; path=/; domain=.youtube.com; expires=Sat, 25-Nov-2017 17:51:58 GMT; httponly
Set-Cookie: YSC=7YRju-cRVO0; path=/; domain=.youtube.com; httponly
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block; report=https://www.google.com/appserve/security-bugs/log/youtube

Privoxy開機啟動

開啟終端,切換目錄到 ~/Library/LaunchAgents,建立 plist 檔案

cd  ~/Library/LaunchAgents
sudo touch local.privoxy.plist

sudo vim local.privoxy.plist 編輯檔案,輸入一下內容:

<?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>local.arcueid.privoxy</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/sbin/privoxy</string>
        <string>--no-daemon</string>
        <string>/usr/local/etc/privoxy/config</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <true/>
    <key>StandardErrorPath</key>
    <string>/usr/local/Cellar/privoxy/privoxy.log</string>
    <key>StandardOutPath</key>
    <string>/usr/local/Cellar/privoxy/privoxy.log</string>
</dict>
</plist>

執行
sudo launchctl load ~/Library/LaunchAgents/local.privoxy.plist

其他說明

要確保Shadowsocks是正常啟動的,而且要是開機自動啟動。不然Privoxy也代理轉發不出去

只給給git配置代理

使用代理,終端中輸入:git config --global http.proxy 127.0.0.1:8118

不使用代理,終端中輸入:git config --global --unset-all http.proxy

這裡的也可以直接使用Shadowsockssocks5代理 git,不用Privoxy

git config --global http.proxy `socks5://127.0.0.1:1080` 
git config --global https.proxy `socks5://127.0.0.1:1080`
給終端全部使用代理

把代理伺服器地址寫入shell配置檔案.bashrc或者.zshrc
直接在.bashrc或者.zshrc新增下面內容

export https_proxy=`http://127.0.0.1:8118`
export http_proxy=`http://127.0.0.1:8118`

相關文章