Last active
April 21, 2023 08:33
-
-
Save aMir733/ab7269b052dd6053cc7b2f321b6a05c4 to your computer and use it in GitHub Desktop.
tun2socks tunnel init script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
[[ $(id -u) != 0 ]] && exit 1 | |
TUN2SOCKS=tun2socks | |
INIT=xray.client | |
DEFAULTS=() | |
INTERFACE=usb0 | |
trap "cleanup" EXIT | |
cleanup() { | |
ip link del dev tun0 | |
for default in ${DEFAULTS[@]} ; do | |
ip route del $default via $GATEWAY dev $INTERFACE metric 1 | |
done | |
/etc/init.d/$INIT stop | |
/etc/init.d/NetworkManager restart | |
pkill '^tun2socks$' | |
} | |
IP=$(jq -r '.outbound.settings.vnext[].address' "/etc/${INIT/./\/config.}.json") | |
#DNS=$(jq -r '.dns.servers[]' "/etc/${INIT/./\/config.}.json") || exit 1 | |
#DEFAULTS+=($DNS) | |
if [[ "$IP" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]] ; then | |
DEFAULTS+=("$IP") | |
else | |
DEFAULTS+=($(dig +short $IP)) | |
fi | |
GATEWAY="$(ip route get 1.1.1.1 | sed -n 's/.* via \([^\ ]*\) .*/\1/p')" | |
if [[ ${#GATEWAY} == 0 ]] || ip a | grep -w tun0 ; then | |
cleanup | |
fi | |
if ! ip link set dev tun0 up &>/dev/null ; then | |
ip tuntap add mode tun dev tun0 | |
ip addr add 198.18.0.1/15 dev tun0 | |
ip link set dev tun0 up | |
fi | |
ip route del default | |
for default in ${DEFAULTS[@]} ; do | |
ip route add $default via $GATEWAY dev $INTERFACE metric 1 | |
done | |
ip route add default via 198.18.0.1 dev tun0 metric 2 | |
echo -e 'nameserver 1.1.1.1' > /etc/resolv.conf | |
/etc/init.d/$INIT restart | |
$TUN2SOCKS -device tun0 -proxy socks5://127.0.0.1:1080 | |
cleanup |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment