G1V虚拟机透明代理安装教程
0、虚拟机及虚拟系统
1
2
3
4
|
Windows 推荐: VMware
Mac 推荐: Parallels Desktop
虚拟系统 推荐: debian11
|
一、注意事项
** G1V 用户请不要开启 fake-ip.
二、安装 Clash
本文所采用的透明代理方式不依赖于 TUN, 是否是增强版本不重要, 如果可以请尽量使用最新版本.
1
2
3
4
5
6
7
8
9
|
# arm 用户请自行替换
wget https://ghproxy.com/https://github.com/MetaCubeX/Clash.Meta/releases/download/v1.14.4/clash.meta-linux-amd64-v1.14.4.gz
# 解压
gzip -d clash.meta-linux-amd64-v1.14.4.gz
# 安装到系统 PATH
chmod +x clash.meta-linux-amd64-v1.14.4
mv clash.meta-linux-amd64-v1.14.4 /usr/bin/clash
|
创建专用的 clash 用户:
1
|
useradd -M -s /usr/sbin/nologin clash
|
编写 Systemd 配置文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
cat > /lib/systemd/system/clash.service <<EOF
[Unit]
Description=Clash TProxy
After=network.target
[Service]
Type=simple
User=clash
Group=clash
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE CAP_NET_RAW
AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE CAP_NET_RAW
Restart=on-failure
ExecStartPre=+/usr/bin/bash /etc/clash/clean.sh
ExecStart=/usr/bin/clash -d /etc/clash
ExecStartPost=+/usr/bin/bash /etc/clash/iptables.sh
ExecStopPost=+/usr/bin/bash /etc/clash/clean.sh
[Install]
WantedBy=multi-user.target
EOF
|
三、调整配置
本文中 Clash 配置文件、脚本等统一存放到 /etc/clash 目录中, 针对于 Clash.Meta 配置文件, 着重说明重点配置, 完整配置请从官方 Wiki 复制: https://github.com/MetaCubeX/Clash.Meta/blob/Meta/docs/config.yaml
3.0、更新系统
1
|
apt-get update && apt-get upgrade && apt-get dist-upgrade && apt-get clean
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
mkdir /etc/clash
cd /etc/clash
wget https://gitlab.com/colloq168/else/-/raw/main/config.yaml
wget https://raw.iqiq.io/MetaCubeX/meta-rules-dat/release/geoip.dat
wget https://raw.iqiq.io/MetaCubeX/meta-rules-dat/release/geosite.dat
wget https://raw.iqiq.io/MetaCubeX/meta-rules-dat/release/country.mmdb
# http://ip:9090/ui Clash控制面板
wget https://gitlab.com/colloq168/else/-/raw/main/ui.zip
unzip ui.zip
rm ui.zip
|
3.2、防火墙规则
为了保证防火墙规则不被破坏, 本文采用脚本暴力操作, 如果宿主机有其他 iptables 控制程序, 则推荐手动执行并通过 iptables-persistent 等工具进行持久化;
/etc/clash/iptables.sh: 负责启动时添加 iptables 规则
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
|
#!/usr/bin/env bash
set -ex
# ENABLE ipv4 forward
sysctl -w net.ipv4.ip_forward=1
# ROUTE RULES
ip rule add fwmark 666 lookup 666
ip route add local 0.0.0.0/0 dev lo table 666
# clash 链负责处理转发流量
iptables -t mangle -N clash
# 目标地址为局域网或保留地址的流量跳过处理
# 保留地址参考: https://zh.wikipedia.org/wiki/%E5%B7%B2%E5%88%86%E9%85%8D%E7%9A%84/8_IPv4%E5%9C%B0%E5%9D%80%E5%9D%97%E5%88%97%E8%A1%A8
iptables -t mangle -A clash -d 0.0.0.0/8 -j RETURN
iptables -t mangle -A clash -d 127.0.0.0/8 -j RETURN
iptables -t mangle -A clash -d 10.0.0.0/8 -j RETURN
iptables -t mangle -A clash -d 172.16.0.0/12 -j RETURN
iptables -t mangle -A clash -d 192.168.0.0/16 -j RETURN
iptables -t mangle -A clash -d 169.254.0.0/16 -j RETURN
iptables -t mangle -A clash -d 224.0.0.0/4 -j RETURN
iptables -t mangle -A clash -d 240.0.0.0/4 -j RETURN
# 其他所有流量转向到 7893 端口,并打上 mark
iptables -t mangle -A clash -p tcp -j TPROXY --on-port 7893 --tproxy-mark 666
iptables -t mangle -A clash -p udp -j TPROXY --on-port 7893 --tproxy-mark 666
# 转发所有 DNS 查询到 1053 端口
# 此操作会导致所有 DNS 请求全部返回虚假 IP(fake ip 198.18.0.1/16)
iptables -t nat -I PREROUTING -p udp --dport 53 -j REDIRECT --to 1053
# 如果想要 dig 等命令可用, 可以只处理 DNS SERVER 设置为当前内网的 DNS 请求
#iptables -t nat -I PREROUTING -p udp --dport 53 -d 192.168.0.0/16 -j REDIRECT --to 1053
# 最后让所有流量通过 clash 链进行处理
iptables -t mangle -A PREROUTING -j clash
# clash_local 链负责处理网关本身发出的流量
iptables -t mangle -N clash_local
# nerdctl 容器流量重新路由
#iptables -t mangle -A clash_local -i nerdctl2 -p udp -j MARK --set-mark 666
#iptables -t mangle -A clash_local -i nerdctl2 -p tcp -j MARK --set-mark 666
# 跳过内网流量
iptables -t mangle -A clash_local -d 0.0.0.0/8 -j RETURN
iptables -t mangle -A clash_local -d 127.0.0.0/8 -j RETURN
iptables -t mangle -A clash_local -d 10.0.0.0/8 -j RETURN
iptables -t mangle -A clash_local -d 172.16.0.0/12 -j RETURN
iptables -t mangle -A clash_local -d 192.168.0.0/16 -j RETURN
iptables -t mangle -A clash_local -d 169.254.0.0/16 -j RETURN
iptables -t mangle -A clash_local -d 224.0.0.0/4 -j RETURN
iptables -t mangle -A clash_local -d 240.0.0.0/4 -j RETURN
# 为本机发出的流量打 mark
iptables -t mangle -A clash_local -p tcp -j MARK --set-mark 666
iptables -t mangle -A clash_local -p udp -j MARK --set-mark 666
# 跳过 clash 程序本身发出的流量, 防止死循环(clash 程序需要使用 "clash" 用户启动)
iptables -t mangle -A OUTPUT -p tcp -m owner --uid-owner clash -j RETURN
iptables -t mangle -A OUTPUT -p udp -m owner --uid-owner clash -j RETURN
# 让本机发出的流量跳转到 clash_local
# clash_local 链会为本机流量打 mark, 打过 mark 的流量会重新回到 PREROUTING 上
iptables -t mangle -A OUTPUT -j clash_local
# 修复 ICMP(ping)
# 这并不能保证 ping 结果有效(clash 等不支持转发 ICMP), 只是让它有返回结果而已
# --to-destination 设置为一个可达的地址即可
sysctl -w net.ipv4.conf.all.route_localnet=1
iptables -t nat -A PREROUTING -p icmp -d 198.18.0.0/16 -j DNAT --to-destination 127.0.0.1
|
/etc/clash/clean.sh: 负责启动前/停止后清理 iptables 规则(暴力清理)
1
2
3
4
5
6
7
8
9
10
11
12
|
#!/usr/bin/env bash
set -ex
ip rule del fwmark 666 table 666 || true
ip route del local 0.0.0.0/0 dev lo table 666 || true
iptables -t nat -F
iptables -t nat -X
iptables -t mangle -F
iptables -t mangle -X clash || true
iptables -t mangle -X clash_local || true
|
3.3、最终目录结构
所有配置编写完成后, 其目录结构如下:
1
2
3
4
5
6
7
8
9
|
root@openrpi # ❯❯❯ tree -L 1 /etc/clash
/etc/clash
├── clean.sh
├── config.yaml
└── iptables.sh
└── geoip.dat
└── geosite.dat
└── country.mmdb
└── ui
|
最后需要修复 /etc/clash 目录权限, 因为 Clash 启动后会写入其他文件:
1
|
chown -R clash:clash /etc/clash
|
四、启动及测试
如果所有配置和文件安装没问题的话, 可以直接通过 Systemd 启动:
1
2
3
4
5
6
7
8
|
# 重新加载 systemd 配置
sudo systemctl daemon-reload
# 加载开机启动
systemctl enable clash.service
# 启动
systemctl start clash
# 查看日志
jouranlctl -fu clash
|
如果启动成功, 那么此时内网设备将网关设置到当前 Clash 所在机器即可完成透明代理; 如果 Clash 机器足够稳定, 也可以一步到位将内网路由器的 DHCP 设置中下发的网关直接填写为 Clash 机器 IP(Clash 机器需要使用静态 IP).