Skip to content

Instantly share code, notes, and snippets.

@yangon99
Last active September 9, 2020 09:54
Show Gist options
  • Save yangon99/60a33b0dbb3be8b5fd7c174b27d48a84 to your computer and use it in GitHub Desktop.
Save yangon99/60a33b0dbb3be8b5fd7c174b27d48a84 to your computer and use it in GitHub Desktop.
#!/bin/sh
# You have to configure "tun", "interface-name" and "dns" for clash to use this script
# ref: https://github.com/Dreamacro/clash/wiki/configuration
# https://github.com/Dreamacro/clash/wiki/premium-core-features#tun-device
if [ `id -u` -ne 0 ]; then
echo 'Must run as root!'
exit 1
fi
if [ ! -f "/etc/clash-tun.conf" ]; then
if [ $SUDO_USER ]; then
echo /home/$SUDO_USER > /etc/clash-tun.conf
else
SUDO_USER=(`ls /home`)
echo /home/$SUDO_USER > /etc/clash-tun.conf
fi
fi
USER_HOME=`cat /etc/clash-tun.conf`
LOCAL_SOCKET=127.0.0.1:7891
TUN_GW=198.18.0.1
TUN_ADDR=198.18.0.1
TUN_MASK=255.255.0.0
ORIG_GW=`ip route get 1 | awk '{print $3;exit}'`
ORIG_ST_SCOPE=`ip route get 1 | awk '{print $5;exit}'`
ORIG_ST=`ip route get 1 | awk '{print $7;exit}'`
echo "Original send through address $ORIG_ST"
echo "Original send through scope $ORIG_ST_SCOPE"
echo "Original gateway $ORIG_GW"
config_route() {
# Give some time for Mellow to open the TUN device.
sleep 3
# tun_ver="v0.17.1\n"`clash -v|awk '{print $2;exit}'|awk -F- '{print $1;exit}'`"\n"
# ret=`echo $tun_ver|sort -V`
# if [ "$ret"x = "$tun_ver"x ]; then
# echo 'Wake up tun...'
# ip link set clash0 up
# ip addr add $TUN_GW dev clash0
# fi
echo 'Setting up routing table...'
ip route del default table main
ip route add default via $TUN_GW table main
ip route add default via $ORIG_GW dev $ORIG_ST_SCOPE table default
ip rule add from $ORIG_ST table default
mv /etc/resolv.conf /etc/resolv.conf.bak
echo "nameserver 127.0.0.1" > /etc/resolv.conf
}
recover_route() {
echo 'Recovering routing table...'
ip rule del from $ORIG_ST table default
ip route del default table default
ip route del default table main
ip route add default via $ORIG_GW table main
mv /etc/resolv.conf.bak /etc/resolv.conf
}
# Configure the routing table in the background.
config_route&
clash-tun -d $USER_HOME/.config/clash
# Recover the routing table after clash exits.
recover_route
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment