“WSL2 还是可以滴”


最近升级了win11,WSL 也顺带着升级到了 WSL2,但是 WSL2 有一点非常蛋疼,IP地址不是固定的,就很难受,每次用 git 的时候设置 proxy 走 Windows 的代理都很麻烦,所以今天查了下资料,花了点时间写了个 Python 脚本来自动化的完成这个功能 注意:该脚本依赖 IPy 这个模块,需要使用下面的命令手动安装一下

1
sudo pip3 install IPy

下面是脚本的内容:

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
# coding=utf-8

import socket
import fcntl
import struct
import os
from IPy import IP

# 本地代理端口,你的和我不一定一样,按照自己的设置改动一下
PORT = 7890


def get_ip_address(ifname):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    return socket.inet_ntoa(fcntl.ioctl(
        s.fileno(),
        0x8915,  # SIOCGIFADDR
        struct.pack(('256s').encode("utf-8"), (ifname[:15]).encode("utf-8"))
    )[20:24])


def get_windows_ip(wsl_ip):
    ip = IP(wsl_ip).make_net("20").strNormal()
    ip = ip.split('/')[0][:-1] + '1'
    return ip


def config_git_proxy(ip):
    config_str = (
        "git config --global https.proxy 'socks5://%s:%d'" % (ip, PORT))
    # print(config_str)
    ret = os.system(config_str)
    if ret:
        print("set git porxy fail")
        return
    else:
        config_str = (
            "git config --global http.proxy 'socks5://%s:%d'" % (ip, PORT))
        # print(config_str)
        ret = os.system(config_str)
    if ret:
        print("set git porxy fail")
        return
    else:
        print("set git porxy success")
        return


if __name__ == "__main__":
    wsl_ip = get_ip_address('eth0')
    windows_ip = get_windows_ip(wsl_ip)
    # print(windows_ip)
    config_git_proxy(windows_ip)

你可以保存为 xxx.py 文件,然后

1
 python3 xxx.py

执行完成后使用:

1
git config --list

可以查看 git 的代理是否设置成功。 要想取消代理也很简单,可以使用如下命令:

1
2
git config --global --unset http.proxy
git config --global --unset https.proxy

完。