VPN 代理与 TUN 模式配置

Fake-IP 模式

fake-ip 用于在代理内部维护 域名与 IP 的映射关系,以便在连接建立时能够根据域名进行精确分流。 当应用发起 DNS 查询时,代理工具会拦截该请求并返回一个 虚假的 IP 地址。这样应用可以像正常情况一样使用该 IP 建立连接,而代理在内部记录该 IP 对应的真实域名。 当后续连接请求到达代理时,代理可以通过该映射恢复出原始域名,从而像传统代理那样基于域名进行分流或远程解析,同时避免本地 DNS 解析带来的泄露问题(泄露看DNS的查询策略)。

sing-box还是用realip,sniff记录ip和域名关系 代理客户端决定发到哪里,远程服务端负责解析和转发 nameserver-policy 用于指定特定域名的 DNS 服务器,避免内网域名无法解析。例如,公司内网域名需要使用内网 DNS 服务器才能正确解析。

系统代理 vs TUN 模式

两种模式的流量最终都会经过代理端口(如 7897),主要区别在于流量的捕获方式:

模式 工作原理 优势 劣势
系统代理 应用程序把域名发给代理 配置简单 需应用支持代理设置
TUN 模式 系统强制将所有流量交给虚拟网卡 全局代理,无需应用支持 配置复杂,可能影响局域网访问

提示:如果存在内网服务器,需要配置 DNS 服务器以便获取正确的内网地址并进行分流。

WSL2 环境配置

前置条件

.wslconfig 文件中配置:

1
2
3
4
5
6
[wsl2]
networkingMode=mirrored
autoProxy=false
dnsTunneling=false
dnsProxy=false
firewall=false

重要:需要在 Windows 防火墙中放行相关端口。

安装 sing-box

1
2
# 安装 sing-box
curl -fsSL https://sing-box.app/install.sh | sh

配置文件

配置文件路径:/etc/sing-box/config.json

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
70
71
72
73
74
75
76
77
78
79
80
81
82
{
"log": {
"level": "info",
"timestamp": true
},
"dns": {
"servers": [
{
"tag": "google-doh",
"type": "https",
"server": "8.8.8.8",
"detour": "proxy"
},
{
"tag": "company-dns",
"type": "udp",
"server": "192.168.13.1"
},
{
"tag": "local-dns",
"type": "local"
}
],
"rules": [
{
"domain_suffix": ["xxx.com"],
"server": "xxx-dns"
}
],
"final": "google-doh",
"strategy": "ipv4_only"
},
"inbounds": [
{
"type": "tun",
"tag": "tun-in",
"interface_name": "tun0",
"address": ["172.19.0.1/30"],
"auto_route": true,
"strict_route": true,
"stack": "gvisor",
"mtu": 9000,
"sniff": true
}
],
"outbounds": [
{
"type": "socks",
"tag": "proxy",
"server": "127.0.0.1",
"server_port": 7897,
"version": "5"
},
{
"type": "direct",
"tag": "direct"
}
],
"route": {
"rules": [
{
"port": [123],
"outbound": "direct"
},
{
"protocol": "dns",
"action": "hijack-dns"
},
{
"ip_is_private": true,
"outbound": "direct"
},
{
"protocol": "quic",
"outbound": "direct"
}
],
"auto_detect_interface": true,
"default_domain_resolver": "google-doh",
"final": "proxy"
}
}

配置说明

配置项 说明
dns.servers 配置多个 DNS 服务器,包括代理 DNS 和内网 DNS
dns.rules 内网域名使用指定的 DNS 服务器解析
inbounds.tun TUN 虚拟网卡配置,创建 tun0 接口
route.rules 路由规则,私有 IP 直连,其他走代理

已知问题

  • 局域网代理目前存在问题,无法通过 Git 协议访问,只能通过 HTTP

参考资料