-
Notifications
You must be signed in to change notification settings - Fork 12
/
setup-clash-tun.sh
104 lines (85 loc) · 3.87 KB
/
setup-clash-tun.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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/bin/bash
/opt/script/clean-clash-tun.sh
/opt/script/setup-clash-cgroup.sh
ipset create localnetwork hash:net
ipset add localnetwork 127.0.0.0/8
ipset add localnetwork 10.0.0.0/8
ipset add localnetwork 192.168.0.0/16
ipset add localnetwork 224.0.0.0/4
ipset add localnetwork 172.16.0.0/12
ip tuntap add "$PROXY_TUN_DEVICE_NAME" mode tun user $PROXY_TUN_USER
ip link set "$PROXY_TUN_DEVICE_NAME" up
ip address replace "$PROXY_TUN_ADDRESS" dev "$PROXY_TUN_DEVICE_NAME"
ip route replace default dev "$PROXY_TUN_DEVICE_NAME" table "$PROXY_ROUTE_TABLE"
ip rule add fwmark "$PROXY_FWMARK" lookup "$PROXY_ROUTE_TABLE"
iptables -t mangle -N CLASH
iptables -t mangle -F CLASH
if [ $PROXY_BYPASS_USER ];then
iptables -t mangle -A CLASH -m owner --uid-owner "$PROXY_BYPASS_USER" -j RETURN
fi
iptables -t mangle -A CLASH -d "$PROXY_FORCE_NETADDR" -j MARK --set-mark "$PROXY_FWMARK"
if [ -d /sys/fs/cgroup/system.slice/ ]; then
iptables -t mangle -A CLASH -m cgroup --path bypass_proxy -j RETURN
else
iptables -t mangle -A CLASH -m cgroup --cgroup "$PROXY_BYPASS_CGROUP" -j RETURN
fi
iptables -t mangle -A CLASH -m addrtype --dst-type BROADCAST -j RETURN
iptables -t mangle -A CLASH -m set --match-set localnetwork dst -j RETURN
iptables -t mangle -A CLASH -p udp --sport 6771 -j RETURN
iptables -t mangle -A CLASH -j MARK --set-mark "$PROXY_FWMARK"
iptables -t nat -N CLASH_DNS
iptables -t nat -F CLASH_DNS
if [ $PROXY_BYPASS_USER ];then
iptables -t nat -A CLASH_DNS -m owner --uid-owner "$PROXY_BYPASS_USER" -j RETURN
fi
if [ -d /sys/fs/cgroup/system.slice/ ]; then
iptables -t nat -A CLASH_DNS -m cgroup --path bypass_proxy -j RETURN
else
iptables -t nat -A CLASH_DNS -m cgroup --cgroup "$PROXY_BYPASS_CGROUP" -j RETURN
fi
iptables -t nat -A CLASH_DNS -p udp -j REDIRECT --to-ports "$PROXY_DNS_PORT"
iptables -t mangle -I OUTPUT -j CLASH
iptables -t nat -I OUTPUT -p udp --dport 53 -j CLASH_DNS
if [ $ENABLE_PREROUTING = 1 ];then
iptables -t mangle -I PREROUTING -m set ! --match-set localnetwork dst -j MARK --set-mark "$PROXY_FWMARK"
iptables -t nat -I PREROUTING -p udp --dport 53 -j REDIRECT --to "$PROXY_DNS_PORT"
fi
if [ $ENABLE_IPv6 = 1 ];then
ipset create localnetwork6 hash:net -6
ipset add localnetwork6 fe80::/10
ipset add localnetwork6 ::1/128
ipset add localnetwork6 ff00::/8
ipset add localnetwork6 ::/128
ip -6 route replace default dev "$PROXY_TUN_DEVICE_NAME" table "$PROXY_ROUTE_TABLE"
ip -6 rule add fwmark "$PROXY_FWMARK" lookup "$PROXY_ROUTE_TABLE"
ip6tables -t mangle -N CLASH6
ip6tables -t mangle -F CLASH6
if [ $PROXY_BYPASS_USER ];then
ip6tables -t mangle -A CLASH6 -m owner --uid-owner "$PROXY_BYPASS_USER" -j RETURN
fi
if [ -d /sys/fs/cgroup/system.slice/ ]; then
ip6tables -t mangle -A CLASH6 -m cgroup --path bypass_proxy -j RETURN
else
ip6tables -t mangle -A CLASH6 -m cgroup --cgroup "$PROXY_BYPASS_CGROUP" -j RETURN
fi
ip6tables -t mangle -A CLASH6 -m set --match-set localnetwork6 dst -j RETURN
ip6tables -t mangle -A CLASH6 -p udp --sport 6771 -j RETURN
ip6tables -t mangle -A CLASH6 -j MARK --set-mark "$PROXY_FWMARK"
ip6tables -t nat -N CLASH_DNS6
ip6tables -t nat -F CLASH_DNS6
if [ $PROXY_BYPASS_USER ];then
ip6tables -t nat -A CLASH_DNS6 -m owner --uid-owner "$PROXY_BYPASS_USER" -j RETURN
fi
if [ -d /sys/fs/cgroup/system.slice/ ]; then
ip6tables -t nat -A CLASH_DNS6 -m cgroup --path bypass_proxy -j RETURN
else
ip6tables -t nat -A CLASH_DNS6 -m cgroup --cgroup "$PROXY_BYPASS_CGROUP" -j RETURN
fi
ip6tables -t nat -A CLASH_DNS6 -p udp -j REDIRECT --to-ports "$PROXY_DNS_PORT"
ip6tables -t mangle -I OUTPUT -j CLASH6
ip6tables -t nat -I OUTPUT -p udp --dport 53 -j CLASH_DNS6
if [ $ENABLE_PREROUTING = 1 ];then
ip6tables -t mangle -I PREROUTING -m set ! --match-set localnetwork6 dst -j MARK --set-mark "$PROXY_FWMARK"
ip6tables -t nat -I PREROUTING -p udp --dport 53 -j REDIRECT --to "$PROXY_DNS_PORT"
fi
fi