常用 http/https 以及 socks5代理总结
格式
# 设置http代理
export http_proxy=
# 设置https代理
export HTTPS_PROXY=
# 设置ftp代理
export FTP_PROXY=
# 同时设置http、https以及ftp代理
export ALL_PROXY=
在终端临时生效
# 设置代理,只在当前终端有效
$ export http_proxy=http://<IP>:<PORT>
或是
$ export http_proxy=socks5://127.0.0.1:1080
$ export HTTPS_PROXY=socks5://127.0.0.1:1080
# 取消代理
$ unset http_proxy
$ unset https_proxy
写入配置文件 (如: .bashrc) 永久有效
$ vi ~/.bashrc
# 添加如下内容:
# set proxy
function setproxy() {
export http_proxy=socks5://127.0.0.1:1080
export HTTPS_PROXY=socks5://127.0.0.1:1080
export FTP_PROXY=socks5://127.0.0.1:1080
}
# unset proxy
function unsetproxy() {
unset http_proxy HTTPS_PROXY FTP_PROXY
}
保存退出,执行source ~/.bashrc使得配置立即生效。或是关闭当前终端,重新打开,在终端中输入:
# 设置代理
$ setproxy
# 取消代理
$ unsetproxy
socks5 和 socks5h 的区别
socks5(本地解析 hostname)
$export HTTPS_PROXY=socks5://127.0.0.1:1080
$curl https://www.google.com
curl: (51) SSL: certificate subject name (www.beforeprom.com) does not match target host name 'www.google.com'
socks5h(由 socks server 解析 hostname)
$ export HTTPS_PROXY=socks5h://127.0.0.1:1080
$ curl https://www.google.com
<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="en"><head><meta content="Search the world's information, including webpages, images, videos and more. Google has many special features to help you find exactly what you're looking for." name="description">
....
也就是说 socks5 适合本地能够解析目标主机域名 (比如 github.com) 但是访问速度慢, 来提高下载速度
socks5h 用与本地不能解析目标主机域名 (比如 google), 由代理服务器解析目标主机域名
curl 通过-x设置代理
curl -x socks5h://127.0.0.1:1080 www.google.com
curl、wget 与 pac 的关系
pac 是 js 文件, curl 和 wget 不能解析 js 文件。
也就是说,默认情况下,系统代理模式设为 pac 模式
此时浏览器已经可以访问被墙的网站,但是 wget 和 curl 却不行, 必须通过http_proxy和HTTPS_PROXY指定代理,才能访问