Skip to content

Instantly share code, notes, and snippets.

@maboloshi
Last active April 28, 2024 09:05
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save maboloshi/a4546e04c260177a50e6b7704f1c9b73 to your computer and use it in GitHub Desktop.
Save maboloshi/a4546e04c260177a50e6b7704f1c9b73 to your computer and use it in GitHub Desktop.
[软件 Proxy 设置] #proxy #git #ssh #chrome #Sublime-Text-4

软件 Proxy 设置

Proxy 一般有2种协议,一种是 HTTP/HTTPS 协议,一种是 SOCKS 协议。优先使用 SOCKS 协议的 SOCKS5 版本。[2种协议的区别 待补充外部引用]

一般各平台的代理客户端除了提供一个本地的 socks5 Proxy ,还会提供一个本地的 http Proxy 。 在客户端相应设置中查看 socks5 协议监听的端口,HTTP Proxy 设置中查看 http 协议监听的端口。

注意:不少客户端提供混合模式,即socks5http端口号是同一个,所以不用费尽心思在软件设定中找单独的 http 端口设置功能了。

Chrome 浏览器 Proxy 设置

使用 Proxy SwitchyOmega 这个插件来管理 Proxy,并且注意为每个情景填写正确的协议名和端口。

Sublime Text 4 中 PackageControl Proxy 设置

  • 打开 “Preferences -> Package Settings -> Package Control-> Settings – User”
  • 在 JSON 文件中插入
"http_proxy": "socks5://127.0.0.1:1087",
"https_proxy": "socks5://127.0.0.1:1087",

Sublime Text 4 的 PackageControl 已经支持 SOCKS 协议。
PS: Sublime Text 的汉化可使用我的这个插件:sublime-text-chinese

SSH Proxy 设置

使用 SSH 的 ProxyCommand 选项,配合 nc 或者 connect 等命令,实现 Proxy 。

  • 命令行模式
# HTTP Proxy:
ssh -o "ProxyCommand connect -H 127.0.0.1:7070 %h %p" user@server.net
# SOCKS5 Proxy:
ssh -o "ProxyCommand connect -S 127.0.0.1:1080 %h %p" user@server.net
  • 设置 SSH 配置文件

一般 SSH 配置文件位置:~/.ssh/config。 更多 SSH 设置详见:SSH_CONFIG(5)

示例: 仅对 User 为: git 访问 HostName 为: github.com 域名有效

Host github.com
   HostName github.com
   ############
   # 如果使用git@ssh.github.com进行传输,HostName和Port设置如下,
   # 详见 https://help.github.com/articles/using-ssh-over-the-https-port/
   # HostName ssh.github.com
   # Port 443
   ############
   User git
   # HTTP Proxy:
   # ProxyCommand connect -H 127.0.0.1:7070 %h %p
   # SOCKS5 Proxy:
   # ProxyCommand connect -S 127.0.0.1:1080 %h %p

示例: 全局,即对任意域名均有效

Host *
    # HTTP Proxy:
    # ProxyCommand connect -H 127.0.0.1:7070 %h %p
    # SOCKS5 Proxy:
    # ProxyCommand connect -S 127.0.0.1:1080 %h %p

附: SSH Proxy Command -- connect

connect is the simple relaying command to make network connection via SOCKS and https proxy. It is mainly intended to be used as proxy command of OpenSSH. You can make SSH session beyond the firewall with this command.

Project page

  • Mac 下安装
brew install connect

Git Proxy 设置

git 目前支持的三种协议http://https://ssh://git://

对于 HTTP/HTTPS 协议(http://https://

远程仓库链接格式类似:

http://github.com/cms-sw/cmssw.git
https://github.com/cms-sw/cmssw.git

实际调用 libcurl 与服务器通信,详见curl(1)--proxy选项以及git-config(1)中的http.proxy条目。

仅限终端环境中有效.

设置http.proxyhttps.proxy

终端命令:

# 设置全域有效
git config --global http.proxy 'socks5://127.0.0.1:1080'
git config --global https.proxy 'socks5://127.0.0.1:1080'
# 取消代理
git config --global --unset http.proxy
git config --global --unset https.proxy
####
# 仅对github.com有效
git config --global http.https://github.com.proxy socks5://127.0.0.1:1080
# 取消代理
git config --global --unset http.https://github.com.proxy

--global 全局

对应的设置文件,~/.gitconfig, %USERPROFILE%\.gitconfig(win)(全局, 用户级)或项目文件夹中的.gitconfig.

# 全域有效
[http]
      proxy = socks5://127.0.0.1:1080
[https]
      proxy = socks5://127.0.0.1:1080

# 仅对github.com有效
[http "https://github.com"]
      proxy = socks5://127.0.0.1:1080
[https "https://github.com"]
      proxy = socks5://127.0.0.1:1080

对于 SSH 协议(ssh://

远程仓库链接格式类似:

  • git@github.com:cms-sw/cmssw.git
  • ssh://git@github.com/cms-sw/cmssw.git

实际调用 ssh 命令与服务器通信,需要配置 ssh 的 ProxyCommand 参数。

详见SSH Proxy 设置

对于 GIT 协议(git://

远程仓库链接格式类似:

git://github.com/cms-sw/cmssw.git

git 协议比较特殊,需要由 core.gitproxy 设置一个额外的 proxy 脚本,才能使用上 socks 代理。

  • 编写 git-proxy 脚本,示例:
#!/bin/bash
## Proxy wrapper for git protocol (9418).
## 任意域名均有效
## git config --global core.gitproxy "proxy-command"
##
## 仅对example.com域名有效
## git config --global core.gitproxy '"proxy-command" for example.com'
exec connect -S 127.0.0.1:1080 "$@"
  • 设置core.gitproxy

终端命令:

# 设置
# 任意域名均有效
git config --global core.gitproxy "git-proxy"
# 仅对example.com域名有效
git config --global core.gitproxy '"git-proxy" for example.com'
# 取消
git config --global --unset core.gitproxy

对应的设置文件:

# 全域有效
[core]
    gitproxy = /path/to/git-proxy
# 仅对example.com域名有效
[core]
    gitproxy = "/path/to/git-proxy" for "example.com"

或者

export GIT_PROXY_COMMAND="/path/to/git-proxy"

git 三种协议的 Proxy 设置互不影响,互不冲突。 具体看 man git-config

参考

  1. 如何为 Git 设置代理?
  2. 设置 Git Proxy
  3. Tutorial: how to use git through a proxy

Node Proxy 设置

终端命令:

npm -g config set proxy http://127.0.0.1:7890
npm -g config set https-proxy http://127.0.0.1:7890

-g--global 全局

对应的设置文件,全局:%appdata%\npm\etc(Win), $PREFIX/etc/npmrc(unix) 或项目文件夹中的.npmrc,用户设置~/.npmrc.

仅支持 HTTP/HTTPS 协议。

Conda Proxy 设置

终端命令:

conda config --set proxy_servers.http http://127.0.0.1:7890
conda config --set proxy_servers.https http://127.0.0.1:7890

对应的设置文件,~/.condarc, %USERPROFILE%\.condarc(win)(全局, 用户级)或项目文件夹中的.condarc.

仅支持 HTTP/HTTPS 协议。

终端 Proxy 设置

原理:export http_proxy、https_proxy 或 ALL_PROXY,那么 wget、curl、git http/https协议等这类网络命令依据该设置进行 Proxy。

  • 设置http_proxyhttps_proxyALL_PROXY

终端命令:

# 开启 Proxy
export http_proxy=socks5://127.0.0.1:1080
export https_proxy=socks5://127.0.0.1:1080
# 或者直接设置ALL_PROXY
export ALL_PROXY=socks5://127.0.0.1:1080
# 关闭 Proxy
unset http_proxy
unset https_proxy
unset ALL_PROXY

可以在终端运行,也可以写入shell配置文件.bashrc或者.zshrc。也可以设置函数或者别名,便于开/关 Proxy。

注意: 修改完.bashrc或者.zshrc等 后重启终端或 source 生效。

可以通过curl -i http://ip.cn查看 IP 改变来测试是否生效

  • 示例: 通过设置 alias 简写来简化操作
alias setproxy="export ALL_PROXY=socks5://127.0.0.1:1080"
alias unsetproxy="unset ALL_PROXY"
alias ip="curl -i http://ip.cn"

Windows - PowerShell Proxy 设置

  • 设置http_proxyhttps_proxyALL_PROXY

命令:

# 开启 Proxy
$env:http_proxy=socks5://127.0.0.1:1080
$env:https_proxy=socks5://127.0.0.1:1080
# 或者直接设置ALL_PROXY
$env:ALL_PROXY=socks5://127.0.0.1:1080

Windows - CMD Proxy 设置

  • 设置http_proxyhttps_proxyALL_PROXY

命令:

# 开启 Proxy
set http_proxy=socks5://127.0.0.1:1080
set https_proxy=socks5://127.0.0.1:1080
# 或者直接设置ALL_PROXY
set ALL_PROXY=socks5://127.0.0.1:1080

参考:

  1. 设置socks5代理
  2. 设置 Git 代理
  3. macOS 终端走Proxy
@maboloshi
Copy link
Author

pip 镜像设置

pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
pip config set global.trusted-host mirrors.aliyun.com

@maboloshi
Copy link
Author

Anaconda 镜像设置

清华大学

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge 
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
conda config --set show_channel_urls yes

北京外国语大学

conda config --add channels https://mirrors.bfsu.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.bfsu.edu.cn/anaconda/cloud/conda-forge 
conda config --add channels https://mirrors.bfsu.edu.cn/anaconda/cloud/msys2/
conda config --set show_channel_urls yes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment