#wait for TUN device while ! ip address show "$TUN_DEV" > /dev/null; do sleep 0.2 done
ip route flush table "$CLASH_TABLE" ip route add default via "$TUN_ADDR" dev "$TUN_DEV" table "$CLASH_TABLE" ip route add "$TUN_NET" dev "$TUN_DEV" table "$CLASH_TABLE"
ip rule add from all table "$CLASH_TABLE" ip rule add uidrange "${CLASH_UID}-${CLASH_UID}" table "$MAIN_TABLE" ip rule add to 192.168.0.0/16 table "$MAIN_TABLE" ip rule add to 172.16.0.0/12 table "$MAIN_TABLE" ip rule add to 10.0.0.0/8 table "$MAIN_TABLE"
ip rule delete from all table "$CLASH_TABLE" ip rule delete uidrange "${CLASH_UID}-${CLASH_UID}" table "$MAIN_TABLE" ip rule delete to 192.168.0.0/16 table "$MAIN_TABLE" ip rule delete to 172.16.0.0/12 table "$MAIN_TABLE" ip rule delete to 10.0.0.0/8 table "$MAIN_TABLE"
ip route flush table "$CLASH_TABLE"
基本思路是利用 rule-based routing 来将所有流量重定向至 clash 的 TUN 设备,并让由 clash 自己的流量遵循原始路由表。
Setup systemd
将下述文件保存为 /etc/systemd/system/clash.service :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# /etc/systemd/system/clash.service [Unit] Description=A rule based proxy tunnel After=network-online.target