yum_apt_golang_curl_py3_设置_proxy_socks5
转载注明来源: 本文链接 来自osnosn的博客,写于 2020-03-11.
各种客户端应用的代理设置,支持的代理类型
yum
- centos7 修改
/etc/yum.conf- 如 http 代理, 添加一行
proxy=http://192.168.2.2:80 - 如 socks5 代理, 添加一行
proxy=socks5://192.168.2.2:1080 - 支持 http, ftp, https, socks4, socks4a, socks5, socks5h 这几种代理类型。
- 如 http 代理, 添加一行
- centos8 修改
/etc/dnf/dnf.conf- 如 http 代理, 添加一行
proxy=http://192.168.2.2:80 - 如 socks5 代理, 添加一行
proxy=socks5://192.168.2.2:1080 - 同centos7, 支持 http, ftp, https, socks4, socks4a, socks5, socks5h 这几种代理类型。
- 另, 还支持 socks, 大概是会自动判断socks4 or 5 的版本吧。(我猜测)
proxy=socks://192.168.2.2:1080
- 如 http 代理, 添加一行
apt
- 在
/etc/apt/apt.conf文件中加入一行: - 支持http代理:
Acquire::http::Proxy "http://192.168.2.2:80"; - 支持socks5h代理(remote DNS解析):
Acquire::http::Proxy "socks5h://192.168.2.2:1080";
golang
- "net/http" 包, 缺省支持 http 和 socks5 代理.
proxystr="http://192.168.2.2:80" //http proxy
proxystr="socks5://192.168.2.2:1080" // socks5 proxy
proxyURL, err = url.Parse(proxystr)
tr = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
Proxy: http.ProxyURL(proxyURL),
}
client = &http.Client{Timeout: time.Duration(20) * time.Second, Transport: tr}
req, _ = http.NewRequest("GET", myurl, nil)
resp, err := client1.Do(req)
...
...
- go get 指令支持 环境变量 http_proxy 和 https_proxy
- golang net.DialTCP( ) 不支持http的CONNECT代理,也不支持socks5代理。
- 自己实现吧。这两种代理都比较简单,协议并不复杂。
- 实现参考: 一个简单的Golang实现的Socks5 Proxy
- 注意:如果不打算提供账号,发送 05 01 00 就好。 如果你发 05 02 00 02, 有的socks5服务即使不需要账号认证也会回复 05 02, 导致认证失败。
- "net/http" 包中的socks5支持,来自
net/http/socks_bundle.go中的func socksNewDialer()- 文档: "net/http"
- socks5的另一个支持包是,
import "golang.org/x/net/proxy"中的func SOCKS5(),最终的支持来自import "golang.org/x/net/internal/socks"。(golang.org 难以访问,比较累。)
curl
- 支持
http://https://socks4://socks4a://socks5://socks5h:// - 支持 环境变量, 比如:
http_proxy=socks5://1.1.1.1:1080https_proxy=socks5://1.1.1.1:1080ALL_PROXY=socks5://1.1.1.1:1080
python3 urllib.request
- urllib.request
import urllib.request,ssl
context = ssl._create_unverified_context()
#myhh=urllib.request.ProxyHandler({
# 'http' : 'http://192.168.2.2:80',
# 'https' : 'http://192.168.2.2:80',
# })
#opener = urllib.request.build_opener(myhh,urllib.request.HTTPSHandler(context=context))#不验证证书
opener = urllib.request.build_opener(urllib.request.HTTPSHandler(context=context))#不验证证书
opener.add_handler(urllib.request.ProxyHandler({
'http' : 'http://192.168.2.2:80', #py3检查第一行的代理协议,支持 http:// https://
'https' : 'http://192.168.2.2:80' #py3不检查,无论写什么,代理协议与第一行相同。
}))
req=urllib.request.Request('http://baidu.com')
con=opener.open(req)
print(con.read())
- py3 的 urllib.request 不直接支持 socks5 代理。
需要 PySocks 包支持。pip3 install PySocks或apt install python3-socks或yum install python36-pysocks或dnf install python3-pysocks
import urllib,urllib.parse,urllib.request
import ssl
import socks,sockshandler
context = ssl._create_unverified_context()
mys5=sockshandler.SocksiPyHandler(socks.SOCKS5,'192.168.2.2',1080,rdns=True) #add_handler()无效
opener = urllib.request.build_opener(mys5,urllib.request.HTTPSHandler(context=context))#不验证证书
req=urllib.request.Request('http://baidu.com')
con=opener.open(req)
- py3 的 requests 包支持http和socks5代理
- py3 的 pycurl 也支持http和socks5代理
pip3
- 命令行参数,
--proxy=socks5://user:pwd@192.168.1.22:1080,--proxy=http://user:pwd@192.168.1.22:8080 ~/.pip/pip.conf用户配置文件中: (顺便改镜像为"华为云")
[global]
timeout = 600 #缺省是15
index-url = https://mirrors.huaweicloud.com/repository/pypi/simple
proxy = socks5://usr:pwd@192.168.1.22:1080
#proxy = http://usr:pwd@192.168.1.22:8080
PHP
- 使用 php 的 curl 函数,支持http和socks5代理