WSL2或linux中设置脚本以便捷开启命令行代理

WSL2或linux中设置脚本以便捷开启命令行代理

前提

支持本地网络共享的代理服务器

正文

wsl2的命令行无法直接使用系统代理,或者在一些linux服务器中,我们希望能在当前shell使用代理,这时候可以通过命令来实现代理:

1
2
3
4
5
6
7
8
export http_proxy="${地址}"
export HTTP_PROXY="${地址}"

export https_proxy="${地址}"
export HTTPS_proxy="${地址}"

export ALL_PROXY="${地址}"
export all_proxy="${地址}"

同时,git也可以设置代理地址

1
2
git config --global http.https://github.com.proxy ${地址}
git config --global https.https://github.com.proxy ${地址}

手动设置比较麻烦,这里集成到脚本

1
2
# 新建文件
vim yourPath/proxy.sh

针对wsl2版本2.0以前

写入如下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/bin/sh
hostip=$(cat /etc/resolv.conf | grep nameserver | awk '{ print $2 }')
wslip=$(hostname -I | awk '{print $1}')
port=yourPort
testurl=yourUrl

PROXY_HTTP="http://${hostip}:${port}"
#PROXY_SOCKS5="socks5://${hostip}:${port}"
#PROXY_SOCKS5="socks5h://${hostip}:${port}"

set_proxy(){
export http_proxy="${PROXY_HTTP}"
export HTTP_PROXY="${PROXY_HTTP}"

export https_proxy="${PROXY_HTTP}"
export HTTPS_proxy="${PROXY_HTTP}"

export ALL_PROXY="${PROXY_SOCKS5}"
export all_proxy="${PROXY_SOCKS5}"

git config --global http.https://github.com.proxy ${PROXY_HTTP}
git config --global https.https://github.com.proxy ${PROXY_HTTP}

echo "Proxy has been opened."
}

unset_proxy(){
unset http_proxy
unset HTTP_PROXY
unset https_proxy
unset HTTPS_PROXY
unset ALL_PROXY
unset all_proxy
git config --global --unset http.https://github.com.proxy
git config --global --unset https.https://github.com.proxy

echo "Proxy has been closed."
}

test_setting(){
echo "Host IP:" ${hostip}
echo "WSL IP:" ${wslip}
echo "Try to connect to testUrl..."
resp=$(curl -I -s --connect-timeout 5 -m 5 -w "%{http_code}" -o /dev/null ${testurl})
if [ ${resp} = 200 ]; then
echo "Proxy setup succeeded!"
else
echo "Proxy setup failed!"
fi
}

if [ "$1" = "set" ]
then
set_proxy

elif [ "$1" = "unset" ]
then
unset_proxy

elif [ "$1" = "test" ]
then
test_setting
else
echo "Unsupported arguments."
fi

解释一下,前面第一句主要是获取主机ip,因为某种原因,wsl每次启动时,主机的wlan共享代理ip总会变,所以通过该命令获取。

1
hostip=$(cat /etc/resolv.conf | grep nameserver | awk '{ print $2 }')

注意!!!这里需要设置代理共享的端口号,即脚本中的port变量,以及设置测试连接是否成功的网站,即脚本中的testurl变量。

关于socks注释

这里解释下注释的socks5代理,对于socks代理需要了解你的代理服务器所支持的socks版本,是socks5还是socks4还是socks5h,有以下对应关系:

1
2
3
4
5
6
7
8
# socks4
socks://{ip}:{port}

# socks5
socks5://{ip}:{port}

# socks5h
socks5h://{ip}:{port}

在bash中,如果注释,则意味着未设置socks代理变量,不必专门注释后面的all_proxy,会设为空字符串。这里默认注释,因为大部分情况下http代理已经够用,如果需要socks代理,请了解socks版本后,解除对应socks版本的注释即可。

针对wsl2版本2.0以后以及一般linux

对于wsl

微软更新了wsl,可以直接方便的共享系统网络了。

这时如果直接打开wsl,在未配置的情况下,应该会显示:

1
wsl: 检测到 localhost 代理配置,但未镜像到 WSL。NAT 模式下的 WSL 不支持 localhost 代理。

打开或创建wsl配置文件(位于C:/User/%你的用户名/.wslconfig),并添加以下内容:

1
2
3
4
5
6
[experimental]
autoMemoryReclaim=gradual
networkingMode=mirrored
dnsTunneling=true
firewall=true
autoProxy=true

这样就解决了这个问题,同时因为镜像到wsl,主机的ip将会默认为127.0.0.1

所以前面脚本里的第一句可以更改为:

1
2
3
hostip=$(cat /etc/resolv.conf | grep nameserver | awk '{ print $2 }')
更改为
hostip=127.0.0.1

对于一般linux

对于linux就更简单了,hostip设置为代理服务器地址即可。

添加到bash或zsh

在bash中添加命令即可:

1
2
3
4
5
vim ~/.bashrc
# 添加命令
alias proxy="source yourPath/proxy.sh"
# 更新
source ~/.bashrc

便可以通过命令开启代理

1
2
3
proxy set 开启
proxy unset 关闭
proxy test 测试

添加认证信息

有时候需要有username和password的认证信息,可以如下更改PROXY变量:

1
2
3
4
5
6
7
username=yourUsername
password=yourPassword


PROXY_HTTP="http://${username}:${password}@${hostip}:${port}"
#PROXY_SOCKS5="socks5://${username}:${password}@${hostip}:${port}"
#PROXY_SOCKS5="socks5h://${username}:${password}@${hostip}:${port}"

完整版本如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/bin/sh
hostip=$(cat /etc/resolv.conf | grep nameserver | awk '{ print $2 }')
wslip=$(hostname -I | awk '{print $1}')
port=yourPort
testurl=yourUrl

username=yourUsername
password=yourPassword


PROXY_HTTP="http://${username}:${password}@${hostip}:${port}"
#PROXY_SOCKS5="socks5://${username}:${password}@${hostip}:${port}"
#PROXY_SOCKS5="socks5h://${username}:${password}@${hostip}:${port}"

set_proxy(){
export http_proxy="${PROXY_HTTP}"
export HTTP_PROXY="${PROXY_HTTP}"

export https_proxy="${PROXY_HTTP}"
export HTTPS_proxy="${PROXY_HTTP}"

export ALL_PROXY="${PROXY_SOCKS5}"
export all_proxy="${PROXY_SOCKS5}"

git config --global http.https://github.com.proxy ${PROXY_HTTP}
git config --global https.https://github.com.proxy ${PROXY_HTTP}

echo "Proxy has been opened."
}

unset_proxy(){
unset http_proxy
unset HTTP_PROXY
unset https_proxy
unset HTTPS_PROXY
unset ALL_PROXY
unset all_proxy
git config --global --unset http.https://github.com.proxy
git config --global --unset https.https://github.com.proxy

echo "Proxy has been closed."
}

test_setting(){
echo "Host IP:" ${hostip}
echo "WSL IP:" ${wslip}
echo "Try to connect to testUrl..."
resp=$(curl -I -s --connect-timeout 5 -m 5 -w "%{http_code}" -o /dev/null ${testurl})
if [ ${resp} = 200 ]; then
echo "Proxy setup succeeded!"
else
echo "Proxy setup failed!"
fi
}

if [ "$1" = "set" ]
then
set_proxy

elif [ "$1" = "unset" ]
then
unset_proxy

elif [ "$1" = "test" ]
then
test_setting
else
echo "Unsupported arguments."
fi

参考:

使用WSL2时控制台输出“wsl: 检测到 localhost 代理配置,但未镜像到 WSL。NAT 模式下的 WSL 不支持 localhost 代理-CSDN博客

wsl2配置代理 - 博客园 (cnblogs.com)