wls2 设置代理

获取 wls2 网关

cat /etc/resolv.conf

输出

# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:
# [network]
# generateResolvConf = false
nameserver 172.31.144.1

cat /etc/resolv.conf |grep nameserver |cut -d " " -f 2

输出

172.31.144.1

bash 设置代理

export ALL_PROXY="http://172.31.144.1:10809"

vscode 设置

使用 bash 脚本管理

创建 proxy-manager.sh 文件

#!/bin/sh
#设置使用 windows 代理
###############
hostip=$(cat /etc/resolv.conf |grep nameserver |cut -d " " -f 2)
wslip=$(hostname -I | awk '{print $1}')
# windows 代理服务端口号
port=10809

PROXY_HTTP="http://${hostip}:${port}"

set_proxy(){
    export ALL_PROXY="${PROXY_HTTP}"
}

get_help(){
     echo -e "These are common Proxy commands used in various situations:\n"
     echo "help    show this help info"
     echo "set     set proxy"
     echo "unset   unset current proxy"
     echo "status  show current proxy information"
}

unset_proxy(){
    unset ALL_PROXY
}

test_setting(){
    echo "Host IP:" ${hostip}
    echo "WSL2 IP:" ${wslip}
    echo "当前代理:" $ALL_PROXY
}

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

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

elif [ "$1" = "status" ]
then
    test_setting
else
    get_help
fi

设置脚本可执行权限

chmod u+x proxy-manager.sh

创建 alias

vim ~/.bashrc

添加

alias proxy="source /path/to/proxy-manager.sh"

保存后记得source ~/.bashrc。至此即可使用 proxy 命令管理 wls2 中的代理

$ proxy
These are common Proxy commands used in various situations:

help    show this help info
set     set proxy
unset   unset current proxy
status  show current proxy information

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!