[总结]利用 Privoxy 让命令行下的 wget 和 curl 等命令实现自动代理

curl 和 wget 都可以通过附带参数来使用代理

curl -x http://127.0.0.1:8087 github.com
wget -e "http_proxy=127.0.0.1:8087" github.com

临时用用还好,但是经常使用也就麻烦了,可以通过配置文件简化

shell> cat ~/.wgetrc
https_proxy = http://127.0.0.1:8087
http_proxy = http://127.0.0.1:8087
ftp_proxy = http://127.0.0.1:8087

use_proxy = on
continue = on
check_certificate = off

shell> cat ~/.curlrc
-L
proxy = 127.0.0.1:8087

参数解释:

https_proxy、http_proxy、ftp_proxy 是指定三个协议时的代理

use_proxy 表示开启代理

continue 表示启用断点续传(等同于wget -c)

check_certificate 是关闭 ssl 证书检查,不然一些 https 的可能无法下载,安全问题暂时忽略咯

-L 是告诉 curl 如果遇到重定向,跟过去,如果不设置就会终止请求

proxy 设置代理,也可以指定比如 socks5

其中 127.0.0.1:8087 是代理地址,但是这样等于全是流量都走代理了,不是很好。
下面通过配置 Privoxy 来解决这个问题

Privoxy 配置

安装自行解决,Privoxy 需要把请求转发到 Shadowsocks 或者 GoAgent,以 Shadowsocks 为例,打开 privoxy/config,写入:


actionsfile pac.action
listen-address  127.0.0.1:8087
forward-socks5  /   127.0.0.1:1080  .

配置解释

actionsfile 是引入一个配置

listen-address 是 Privoxy 监听的地址和端口

forward-socks5 是设置把请求转发到哪里

其中 pac.action 大致格式如下:

{{alias}}
direct      = +forward-override{forward .}
shadowsocks    = +forward-override{forward-socks5 127.0.0.1:1080 .}

# default
{direct}
/

# shadowsocks
{shadowsocks}
.imgur.com
.sf.net
.gmail.com
.who.is

{shadowsocks} 段里的域名是需要走代理,自行添加就好,修改后重启 Privoxy。

现在如果使用 wget 或者curl 下载就可以自动选择使用代理加速啦。

还一个好处是 Mac 下的 brew 从 sourceforget 下载软件不会出问题啦,同时有个小问题是如果运行brew doctor会有个警告,大致意思是说如果你的 brew 下载软件有问题可能是 curlrc引起的,请移除他。。。不用理会。

PS: 其实 ~/.wgetrc 的代理配置也可以设置为全局变量,比如写到 bashrc 或者 profile 又或者 environment,这样一些其他软件也可以走代理,比如 ruby bundle。

标签: wget, curl, proxy, brew

已有 2 条评论

  1. [...]把SOCKS Proxy转成HTTP Proxy会方便命令行工具如curl和wget的使用。有几种办法,ShadowSocks的Wiki推荐使用ProxyChains;很多文章推荐使用polipo,可惜polipo的Github页面上说作者已经停止更新了;还有提到使用Privoxy来达到目的的。总之选一款适合你用的就行。[...]

  2. Zhu Zhe Zhu Zhe

    privoxy 好像不支持ftp

添加新评论