yanyue404 / blog

Just blog and not just blog.
https://yanyue404.github.io/blog/
Other
88 stars 13 forks source link

终端代理配置指南 #270

Open yanyue404 opened 8 months ago

yanyue404 commented 8 months ago

macOS & Linux

通过设置 http_proxy、https_proxy,可以让终端走指定的代理。 配置脚本如下,在终端直接执行,只会临时生效:

# 设置代理
export http_proxy=http://127.0.0.1:8899
export https_proxy=$http_proxy

# 或是一行命令
export https_proxy=http://127.0.0.1:8899 http_proxy=http://127.0.0.1:8899

# 还原代理
unset http_proxy https_proxy

8899 是 http 代理 Whistle 对应的端口,请根据你的实际情况修改。

Windows

以下代理生效情况均使用 curl -I http://www.google.com 进行验证。

Git Bash

设置方法同 "macOS & Linux"


#  设置代理
export http_proxy=http://127.0.0.1:8899
export https_proxy=$http_proxy

# -I/–head  只显示请求头信息
$ curl -I http://www.google.com

HTTP/1.1 200 OK
Connection: close
Transfer-Encoding: chunked
Cache-Control: private
Content-Security-Policy-Report-Only: object-src 'none';base-uri 'self';script-src 'nonce-93sLvK4rJwYE9FtSfUG6Zw' 'strict-dynamic' 'report-sample' 'unsafe-eval' 'unsafe-inline' https: http:;report-uri https://csp.withgoogle.com/csp/gws/other-hp
Content-Type: text/html; charset=ISO-8859-1
Date: Fri, 02 Feb 2024 02:07:31 GMT
Expires: Fri, 02 Feb 2024 02:07:31 GMT
P3p: CP="This is not a P3P policy! See g.co/p3phelp for more info."
Server: gws
Set-Cookie: 1P_JAR=2024-02-02-02; expires=Sun, 03-Mar-2024 02:07:31 GMT; path=/; domain=.google.com; Secure
Set-Cookie: AEC=Ae3NU9MS6jMZwtay0TsP0xRR63IAWlYgmabJzkTifIJB9fgipii_59R6s9M; expires=Wed, 31-Jul-2024 02:07:31 GMT; path=/; domain=.google.com; Secure; HttpOnly; SameSite=lax
Set-Cookie: NID=511=O8E9SUcRhRPHyBCTZef1vKSdwgVmWjAPzfytd_0Vg1TzfKQgNYwNqzViLd5mI7DX9hbGZfEH6JVUWXfA3cxQciyRx9RJnUIKJTWbOgJvz9LH0UP4uajkmLOUtcPq_sehwvLQ9si30UsZlIWAIiazfSRQWzsfCnd4U28xjPust8M; expires=Sat, 03-Aug-2024 02:07:31 GMT; path=/; domain=.google.com; HttpOnly
X-Frame-Options: SAMEORIGIN
X-Xss-Protection: 0

# 还原代理
unset http_proxy https_proxy

$ curl -I http://www.google.com
curl: (28) Failed to connect to www.google.com port 80: Timed out

PowerShell

#  PowerShell 中的 curl 语法:
curl.exe [options] <url>

# 你可以运行以下命令以获取有关 curl 命令及其选项(例如 -a、-C 等)的更多信息…
curl.exe --help

# 设置代理
$env:http_proxy="http://127.0.0.1:8899"
$env:https_proxy="http://127.0.0.1:8899"

curl.exe -I http://www.google.com

HTTP/1.1 200 OK
Connection: close
Transfer-Encoding: chunked
Cache-Control: private
Content-Security-Policy-Report-Only: object-src 'none';base-uri 'self';script-src 'nonce-0sRctw1-FGtkEYrRPuAQ_g' 'strict-dynamic' 'report-sample' 'unsafe-eval' 'unsafe-inline' https: http:;report-uri https://csp.withgoogle.com/csp/gws/other-hp
Content-Type: text/html; charset=ISO-8859-1
Date: Fri, 02 Feb 2024 02:47:05 GMT
Expires: Fri, 02 Feb 2024 02:47:05 GMT
P3p: CP="This is not a P3P policy! See g.co/p3phelp for more info."
Server: gws
Set-Cookie: 1P_JAR=2024-02-02-02; expires=Sun, 03-Mar-2024 02:47:05 GMT; path=/; domain=.google.com; Secure
Set-Cookie: AEC=Ae3NU9PVYx8ZSnnCXgPFapze7dAPXGMkc2Jcm1lgzi3F6v-AlaQjbqtfL_U; expires=Wed, 31-Jul-2024 02:47:05 GMT; path=/; domain=.google.com; Secure; HttpOnly; SameSite=lax
Set-Cookie: NID=511=G2nL2I9u1NaiFioj97Xl8P5t7AwjJFXKwlQYzvvT_WzXvICMrp_52cNdKnEFZiyTtVEKLFfs_Cd0eqaUl6VKDP_mWvx3CiDDLiInIjRTcvolpcPqTv-fK7Ek_CoQKkXR8rObAJ4qcbUqVYFuFkzsc5DNQ3fEqawO_Olfy4-GRPU; expires=Sat, 03-Aug-2024 02:47:05 GMT; path=/; domain=.google.com; HttpOnly
X-Frame-Options: SAMEORIGIN
X-Xss-Protection: 0

# 还原代理
$env:http_proxy=""
$env:https_proxy=""
curl.exe -I http://www.google.com
curl: (7) Failed to connect to www.google.com port 80: Timed out

cmd

注意:在 Windows 下使用全局代理方式不会对 cmd 生效

# 设置代理
set http_proxy=http://127.0.0.1:8899
set https_proxy=http://127.0.0.1:8899

# 还原代理
set http_proxy=
set https_proxy=

其他代理设置

git 代理

# 查看全局配置
git config --list

# 设置
git config --global http.proxy 'http://127.0.0.1:8899'
git config --global https.proxy 'https://127.0.0.1:8899'

# 恢复
git config --global --unset http.proxy
git config --global --unset https.proxy

npm

# 查看全局配置
npm config list -g

# 设置
npm config set proxy http://127.0.0.1:8899 -g
npm config set https-proxy http://127.0.0.1:8899 -g

# 恢复
npm config delete proxy
npm config delete https-proxy

参考