Featured image of post Develop tools on Windows

Develop tools on Windows

Windows 开发工具

Windows 开发工具

WSL 代理脚本

  • proxy.sh
 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
#!/bin/sh
hostip=$(cat /etc/resolv.conf | grep nameserver | awk '{ print $2 }')
# Ubuntu
wslip=$(hostname -I | awk '{print $1}')
# Arch
# wslip=$(ip addr show | grep -oP 'inet \K[\d.]+' | sed -n '2p')
# 这里填写主机代理的端口
port=7890

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

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

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

    git config --global http.proxy "${PROXY_HTTP}"
    git config --global https.proxy "${PROXY_HTTP}"
}

unset_proxy(){
    unset http_proxy
    unset HTTP_PROXY
    unset https_proxy
    unset HTTPS_PROXY
    git config --global --unset http.proxy
    git config --global --unset https.proxy
}

test_setting(){
    echo "Host ip:" ${hostip}
    # echo "WSL ip:" ${wslip}
    echo "Current proxy:" $https_proxy
}

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

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

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

使用方法

  • 测试:proxy test
  • 开启:proxy set
  • 关闭:proxy unset

实测 Win11 WSL 下 Arch 和 Ubuntu 均可使用。

更新(Since WSL 2.0.0)

参考官方文档可以配置镜像模式网络,从而让 WSL 直接使用 Windows 主机的代理,上述方案作废。

WSL 输入卡顿解决方案

本人电脑配置足够,但在 WSL2 中输入仍然遇到比较严重的卡顿问题。原因是使用 zsh + suggestion 插件的时候,由于 WSL2 默认会添加 Windows 下的环境变量(参见互操作设置),输入命令时会不断查找环境变量中的可执行文件,而 WSL2 的跨文件系统 IO 性能有十分低下,最终造成了体验问题。

讨论列表中给出的解决方案是编辑 WSL 下的 /etc/wsl.conf 文件,设置

1
2
[interop]
appendWindowsPath = false

这一方案的确能够解决问题(需要重启 WSL 后生效),但也失去了一些便捷,例如在 WSL 终端中键入 code . 打开 Windows 下的 VS Code。一种解决方案是参考启动设置,通过启动命令手动添加 VS Code 的路径。但这一方案在本人系统中并未生效,最终采用的是编辑 .zshrc 文件手动修改 $PATH 环境变量。

1
2
# VSCode PATH
export PATH="$PATH:/mnt/d/Programs/Microsoft VS Code/bin"

之后就可以正常使用 code 命令了。

Licensed under CC BY-NC-SA 4.0
Built with Hugo
主题 StackJimmy 设计