osnosn

  博客园 :: 首页 :: 博问 :: 闪存 :: :: 联系 :: 订阅 订阅 :: 管理 ::

yum_apt_golang_curl_py3_pip3_php_git_wget_svn_opkg_haskell_cargo.rust_docker_设置_proxy_socks5_tsocks_proxychains_国内开源镜像mirrors

转载注明来源: 本文链接 来自osnosn的博客,写于 2020-03-11.

各种客户端应用的代理设置,支持的代理类型

yum, dnf

  • 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 这几种代理类型。
  • 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

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";
    • 如果s5是通过 ssh -R 提供的,想要允许子网使用,服务端的 sshd_config 要设置 GatewayPorts yes
      服务端是 op-21.02 的话,/etc/config/dropbear 要加上 option GatewayPorts 'on',并且要用 ssh -R '*:1080'连。
      如果ssh是通过tsocks-1.8连接。tsocks.conf 的缺省部分,不要设置 server =,要设置 fallback = yes
      建议通过ncat或netcat连接,不通过tsocks或proxychains。

golang

  • go get 指令支持 环境变量 http_proxy 和 https_proxy,指定普通代理。其中go get又使用 git 获取源码。
    所以, 配置 git 的代理 + export http_proxy=socks5://127.0.0.1:1080
  • 或者只用 GOPROXY=https://goproxy.io,direct 环境变量。见 【goproxy.io】首页的介绍。
    或者 GOPROXY=https://goproxy.cn,direct 环境变量。见 【goproxy.cn】首页的介绍。
    还有几个可用, (https://)gonexus.dev/ , https://mirrors.aliyun.com/goproxy/ , (https://)athens.azurefd.net , (https://)gocenter.io ,, https://repo.huaweicloud.com/repository/goproxy/ ,
    这个用不了, https://proxy.golang.org ,
    • GOPROXY= 只接受 https 。如要用 http,则要设置 GO111MODULE=on 。
    • goproxy.io 不是用常规的代理协议。go 访问 github.com , 会以 https://goproxy.io/github.com/xxx 的形式访问。
    • Go 1.13 及以上(推荐)
      go env -w GO111MODULE=on
      go env -w GOPROXY=https://goproxy.cn,direct
      其实写在 "~/.config/go/env" 文件中。
    • Go 1.12 及以下
      export GO111MODULE=on
      export GOPROXY=https://goproxy.cn,direct
  • "net/http" 包, 缺省支持 http 和 socks5 代理.
    proxystr="http://192.168.2.2:80"      //http proxy
    proxystr="socks5://192.168.2.2:1080"  // socks5 proxy(remote DNS解析)
    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)
       ...
       ...
    
  • 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()
  • socks5的另一个支持包是,import "golang.org/x/net/proxy"中的func SOCKS5(),最终的支持来自import "golang.org/x/net/internal/socks"。(golang.org 难以访问,比较累。)

wget

  • 支持环境变量 http_proxy= , https_proxy=
  • 或者设置 ~/.wgetrc
    https_proxy = http://user:pwd@127.0.0.1:8087/
    http_proxy = http://user:pwd@127.0.0.1:8087/
    ftp_proxy = http://user:pwd@127.0.0.1:8087/
    #proxy_user=user
    #proxy_password=pwd
    ### If you do not want to use proxy at all, set this to off.
    use_proxy = on
    ### 以下也支持
    #httpsproxy = http://user:pwd@127.0.0.1:8087/
    #httpproxy = http://user:pwd@127.0.0.1:8087/
    #ftpproxy = http://user:pwd@127.0.0.1:8087/
    #proxyuser=user
    #proxypassword=pwd
    #useproxy = on
    
  • wget 无论是环境变量,还是 .wgetrc 只支持 http 代理,不支持 socks5
    如果非要使用 socks5 ,那就套上 tsocks 。

curl

  • 支持 http:// https:// socks4:// socks4a:// socks5:// socks5h://
    • socks5h://(remote DNS解析)
  • 支持 环境变量, 比如:
    • http_proxy=socks5://1.1.1.1:1080
    • https_proxy=socks5://1.1.1.1:1080
    • ALL_PROXY=socks5://1.1.1.1:1080
  • 设置 ~/.curlrc , 支持两种格式
    ### 代理设置
    --proxy http://user:pw@127.0.0.1:888
    #proxy=http://user:pw@127.0.0.1:888
    #proxy=socks5h://user:pw@127.0.0.1:1080
    ### 跟随重定向,follow location
    location
    #--location
    

python3 urllib.request ; requests ; pycurl

  • 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 PySocksapt install python3-socksyum install python36-pysocksdnf 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()无效
    #mys5=sockshandler.SocksiPyHandler(socks.SOCKS5,'192.168.2.2',1080,username='user',password='pwd',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-2.24 包支持http和socks5代理, (需要装PySocks)
    import requests
    ## verify=False 访问https时不检查证书
    # 去除不检查证书的警告
    requests.packages.urllib3.disable_warnings(requests.packages.urllib3.exceptions.InsecureRequestWarning)
    #proxy={'http':'http://user:pwd@192.168.1.100:88','https':'http://user:pwd@192.168.1.100:88'}
    proxy={'http':'socks5://user:pwd@192.168.1.100:1080','https':'socks5://user:pwd@192.168.1.100:1080'}
    rr = requests.get(url, headers = header, allow_redirects=False,verify=False,proxies=proxy,timeout=5)
    
  • 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 缺省 15
    timeout = 60
    index-url = https://repo.huaweicloud.com/repository/pypi/simple
    #index-url = https://mirrors.aliyun.com/pypi/simple/
    proxy = socks5://usr:pwd@192.168.1.22:1080
    #proxy = http://usr:pwd@192.168.1.22:8080
    
    socks5:// 需要 PySocks 包支持。pip3 install PySocksapt install python3-socksyum install python36-pysocksdnf install python3-pysocks

PHP

  • 使用 php 的 curl 函数,支持http和socks5代理。 $ch=curl_init();
    • curl_setopt($ch, CURLOPT_PROXY, 'socks5h://user:pwd@192.168.1.1:1080');
    • curl_setopt($ch, CURLOPT_PROXY, 'http://user:pwd@192.168.1.1:8080');

tsocks 1.8

proxychians4

  • proxychains-ng, 即 proxychains4 。是一个类似 tsocks 的命令。
    • tsocks 只支持socks5,可以根据不同的destination ip,走不同的 socks5。没有socks5嵌套。
    • proxychains 支持 http, socks5,没有destination ip的设置,proxy可以嵌套。

git

  • GIt设置代理】,【git如何设置使用代理
    Configure Git to use a proxy
  • 不能用 tsocks 来套,会出错。
  • 设置
    git config --global https.proxy http://user:psw@127.0.0.1:1080
    git config --global https.proxy https://user:psw@127.0.0.1:1080
    git config --global http.proxy 'socks5://user:psw@127.0.0.1:1080'
    git config --global https.proxy 'socks5://user:psw@127.0.0.1:1080'
    密码中有特殊字符的要用 % 编码,比如 @ --> %40
  • 取消
    git config --global --unset http.proxy
    git config --global --unset https.proxy
  • git "--global" 的配置在 ~/.gitconfig, "--local" 的配置在当前项目的 .git/config
  • 有一个公共代理站,见【ghproxy.com
  • 为 git 和 ssh 设置 socks5 协议的代理
    如何为 Git 设置代理
    • ssh://, 使用 ProxyCommand
      .ssh/config 中配置连接 github的 ssh账号,
      使用ProxyCommand /bin/nc -x 192.168.x.xx:1080 %h %p
      或,使用ProxyCommand /usr/bin/ncat --proxy-type socks5 -x 192.168.x.xx:1080 --proxy-auth usr:pwd %h %p
    • git://, 在 man git-config 有提到。
      使用 git config --global core.gitProxy '/opt/mypxy.sh'
      或者 export GIT_PROXY_COMMAND=/opt/mypxy.sh
      # mypxy.sh
      ncat --proxy ... "$@"
      

svn

  • svn 配置代理,支持 http 代理,不支持 socks5
    修改 ~/.subversion/servers[global]
    http-proxy-host
    http-proxy-port
    http-proxy-username
    http-proxy-password

haskell stack

  • 使用环境变量
    export http_proxy=http://user:pwd@192.168.1.1:80
    export https_proxy=http://user:pwd@192.168.1.1:80
    stack setup
    

rust

  • "~/.cargo/config" 更换源,代理,(2022-06)
    [source.crates-io]
    #registry = "https://github.com/rust-lang/crates.io-index"
    registry = "sparse+https://index.crates.io/"        
    replace-with = 'ustc'
    [source.ustc]
    #registry = "git://mirrors.ustc.edu.cn/crates.io-index"
    registry = "sparse+https://mirrors.ustc.edu.cn/crates.io-index/"
    [http]
    proxy = "http://user:pass@192.168.0.1:3333"
    check-revoke = false
    [https]
    proxy = "http://192.168.0.1:3333"
    #proxy = "socks5://user:pass@192.168.0.1:23456"
    
    缺点,cargo search 无法使用镜像。
  • 指定搜索还是用 crates-io,加入 ~/.cargo/config
    [registry]
    default = "crates-io"
    
  • Rust Crates 源使用帮助
  • cargo 支持环境变量,
    rustup 也使用环境变量,
    http_proxy="http://user:pass@127.0.0.1:1080"
    https_proxy="http://user:pass@127.0.0.1:1080"
    
  • 使用字节跳动的镜像。【rsproxy.cn】(2023-10)
    [source.crates-io]
    replace-with = 'rsproxy-sparse'
    [source.rsproxy]
    registry = "https://rsproxy.cn/crates.io-index"
    [source.rsproxy-sparse]
    registry = "sparse+https://rsproxy.cn/index/"
    [registries.rsproxy]
    index = "https://rsproxy.cn/crates.io-index"
    [net]
    git-fetch-with-cli = true
    [registry]
    default = "rsproxy"
    

openWRT opkg

  • /etc/opkg.conf 例子
    optinon no_check_certificate 1
    optinon http_proxy 127.0.0.1:7890
    # optinon http_proxy http://user:pass@127.0.0.1:7890/
    #不支持# optinon https_proxy 127.0.0.1:7890
    #不支持# optinon https_proxy http://user:pass@127.0.0.1:7890/
    optinon ftp_proxy 127.0.0.1:7890
    option http_timeout 5
    optinon proxy_user abc
    optinon proxy_passwd def
    
  • op-19,op-21,op-22, 默认安装的wget(其实是uclient-fetch),仅支持通过无认证的代理访问http,不支持通过代理访问https,不支持代理的认证。(可以建本地的无认证代理,再转发)
    除非安装完整版wget,op-19用opkg install wget,op-21,op-22用opkg install wget-ssl
    • op21,op22中/etc/opkg.conf并不支持https_proxy的配置项。https访问只能在 ~/.wgetrc配置,支持代理的认证。
      http访问,配置 /etc/opkg.conf 或者 ~/.wgetrc (二选一),支持代理认证。
    • 应该也支持,环境变量 http_proxy=https_proxy=,(未测试)。
  • 换源,清华大学:sed -i s_downloads.openwrt.org_mirrors.tuna.tsinghua.edu.cn/openwrt_ /etc/opkg/distfeeds.conf

docker pull 设置通过 http 代理

ios 设置自动代理

  • ios 设置自动代理
  • 就是使用 .pac 文件,URL 就是指向这个.pac文件的连接。
    function FindProxyForURL(url, host){
      return "SOCKS 192.168.1.120:1090";
    }
    

国内开源镜像站, mirrors


转载注明来源: 本文链接 https://www.cnblogs.com/osnosn/p/12461119.html 来自osnosn的博客.

posted on 2020-03-11 17:34  osnosn  阅读(548)  评论(0编辑  收藏  举报