VPN 代理与 TUN 模式配置指南

VPN 代理与 TUN 模式配置指南

本文介绍代理工具的核心概念和 sing-box 在 WSL2 环境下的配置方法。

核心概念

Fake-IP 模式

fake-ip 用于内部存储域名和 IP 的映射,便于根据域名分流。当应用请求 DNS 解析时,代理工具返回一个虚假的 IP 地址,并在内部记录该 IP 对应的真实域名,从而实现基于域名的精确分流。

Nameserver-Policy

nameserver-policy 用于指定特定域名的 DNS 服务器,避免内网域名无法解析。例如,公司内网域名需要使用内网 DNS 服务器才能正确解析。

注意:使用 nameserver-policy 时需同时配置相应的分流规则。

系统代理 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": ["gitlab.fzcsxl.com"],
"server": "company-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

参考资料