在上一篇文章中“给多数Shell命令启用代理”已经总结过部分shell命令启用代理一事,这里主要总结下wget代理需要注意的地方!
今日在使用wget下载一个国外镜像站点的资源时,发现速度非常的慢,下载完,需要20h,几kb/s。所以需要挂代理,按照上篇文章的方式,配置了ALL_PROXY
和all_proxy
均不起效果。于是看了看wget的man文档,其中-e
参数支持引用.wgetrc配置变量,如下
-e command --execute command Execute command as if it were a part of .wgetrc. A command thus invoked will be executed after the commands in .wgetrc, thus taking precedence over them. If you need to specify more than one wgetrc command, use multiple instances of -e.
其中.wgetrc支持配置代理有这三个内容
http_proxy https_proxy If set, the http_proxy and https_proxy variables should contain the URLs of the proxies for HTTP and HTTPS connections respectively. ftp_proxy This variable should contain the URL of the proxy for FTP connections. It is quite common that http_proxy and ftp_proxy are set to the same URL.
也就是说,它仅支持环境变量中设置http_proxy
和https_proxy
,于是我将本地socks5的代理,配置至其中,如下过程
humh@MacBook-Pro ~ export https_proxy=socks5://127.0.0.1:6666 humh@MacBook-Pro ~ wget https://archive.apache.org/dist/hadoop/core/hadoop-2.7.6/hadoop-2.7.6.tar.gz -O tmp/hadoop-2.7.6.tar.gz 解析代理服务器 URL socks5://127.0.0.1:6666 时发生错误:不支持的协议类型 “socks5”。
惊讶的发现,原来wget并不支持socks5协议的代理。(同时需要注意,如果配置http_proxy
,则http资源才能走代理 ,https资源需要配置https_proxy
,必须一一对应)。
补充:wget除了环境变量这种方式,本身可以直接采用上面-e
参数启用代理,格式为:wget ... -e use_proxy=on -e http_proxy=127.0.0.1:8080
或wget ... -e http_proxy=127.0.0.1:8080
如何解决wget使用socks5协议的代理
可以使用tsocks解决!!它可以让应用程序在不经任何修改的情况下,使用socks代理!点击官网,支持wget、git等。
1、安装
这里因为本人使用Mac,所以阐述如何通过homebrew安装,如果你是Ubuntu的话,可以尝试apt-get install tsocks
通过brew search tsocks
发现没有这个工具包,搜了一下,发现这个工具包,官方库没有,需要通过第三方库。
首先brew tap
查看本地库
✘ humh@MacBook-Pro ~ brew tap homebrew/cask homebrew/core homebrew/services
说明只有官方核心库,这里添加tsocks的库。运行brew tap Anakros/homebrew-tsocks
。再次tap验证
✘ humh@MacBook-Pro ~ brew tap anakros/tsocks homebrew/cask homebrew/core homebrew/services
tsocks的第三方库添加成功,此时运行brew install --HEAD tsocks
安装最新版tsocks。
说明:homebrew第三方库软件安装的原理,大致是,homebrew支持三方创作,支持github等,只要你的仓库名符合homebrew-something的格式即可。
2、配置tsocks
Mac默认配置位于/usr/local/etc/tsocks.conf
,其他系统类似,只要找到配置文件即可。配置如下
humh@MacBook-Pro ~ vim /usr/local/etc/tsocks.conf #配置如下: server = 127.0.0.1 server_port = 6666 server_type = 5
- server:需要使用的代理,其地址
- server_port:需要使用的代理,其端口号
- server_type:需要使用的代理,其协议类型,这里5代理socks5协议,具体可参考最后的官方文档中说明。
3、验证使用
只要在你需要代理的命令前,加上tsocks
即可,如wget,举例:tsocks wget https://archive.apache.org/dist/hadoop/core/hadoop-2.7.6/hadoop-2.7.6.tar.gz -O ./hadoop-2.7.6.tar.gz
本文参考
- “How to set proxy for wget?”:https://stackoverflow.com/questions/11211705/how-to-set-proxy-for-wget
- “TSOCKS”:http://tsocks.sourceforge.net/faq.php
- “Mac 下安装 tsocks 前置代理工具”:https://www.coderxing.com/install-tsocks-on-macos.html
- “Anakros/homebrew-tsocks”:https://github.com/Anakros/homebrew-tsocks
- “homebrew的tap功能详解”:https://segmentfault.com/a/1190000012826983
发表评论