1

安装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'

你可能感兴趣的

载入中...