解决 NAT 模式下的 WSL 不支持 localhost 代理

原始提示

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

写脚本

nano ~/.wsl_proxy.sh

脚本内容

#!/bin/bash

# 每次执行时动态获取 Windows IP
WIN_IP=$(grep nameserver /etc/resolv.conf | awk '{print $2}')

# 改成你自己的端口
HTTP_PORT=7890
SOCKS_PORT=7891

export http_proxy="http://$WIN_IP:$HTTP_PORT"
export https_proxy="$http_proxy"
export all_proxy="socks5h://$WIN_IP:$SOCKS_PORT"

export HTTP_PROXY="$http_proxy"
export HTTPS_PROXY="$https_proxy"
export ALL_PROXY="$all_proxy"

权限

chmod +x ~/.wsl_proxy.sh

nano ~/.bashrc

验证

source ~/.wsl_proxy.sh
echo $WIN_IP
curl -v https://www.google.com