Last active
November 17, 2024 18:48
-
-
Save SaeedDev94/e4368fab27ee027c5147f8aa70435867 to your computer and use it in GitHub Desktop.
Linux tun2socks routing helper
This file contains hidden or 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
#!/bin/bash | |
if [ "$EUID" -ne 0 ]; then echo "Please run as root"; exit; fi | |
GATEWAY=$(ip route | awk '/default/ {print $3}') | |
INTERFACE=$(ip route | awk '/default/ {print $5}') | |
RESOLVE_CONF=$(cat /etc/resolv.conf) | |
SOCKS_SCHEME="socks5" | |
SOCKS_ADDRESS="127.0.0.1" | |
SOCKS_PORT="10808" | |
TUN_NAME="tun0" | |
TUN_ADDRESS="10.10.10.10" | |
TUN_ROUTE="default dev $TUN_NAME" | |
TUN_SETUP="$(realpath $0) $TUN_NAME" | |
PROXY_SERVER=(x.x.x.x) | |
EXCLUDE=(PROXY_SERVER) | |
addRoute() { | |
local ROUTE=$(ip route | grep "$1") | |
if [ -z "$ROUTE" ]; then ip route add $1; fi | |
} | |
delRoute() { | |
local ROUTE=$(ip route | grep "$1") | |
if [ -n "$ROUTE" ]; then ip route del $1; fi | |
} | |
excludeRoutes() { | |
for name in "${EXCLUDE[@]}"; do | |
declare -n list="$name" | |
for ip in "${list[@]}"; do | |
local ROUTE="$ip via $GATEWAY dev $INTERFACE" | |
case $1 in | |
"delete") delRoute "$ROUTE";; | |
*) addRoute "$ROUTE";; | |
esac | |
done | |
done | |
} | |
start() { | |
# Handle Routes | |
excludeRoutes | |
# Handle DNS | |
/opt/dns-tcp-socks-proxy/dns_proxy /opt/dns-tcp-socks-proxy/dns_proxy.conf | |
# Start tun2socks | |
tun2socks -device "tun://$TUN_NAME" -proxy "$SOCKS_SCHEME://$SOCKS_ADDRESS:$SOCKS_PORT" -tun-post-up "$TUN_SETUP" -interface "$INTERFACE" -tcp-auto-tuning | |
} | |
stop() { | |
# Cleanup Routes | |
excludeRoutes "delete" | |
delRoute "$TUN_ROUTE" | |
# Restore DNS | |
echo "$RESOLVE_CONF" > /etc/resolv.conf | |
} | |
case $1 in | |
"$TUN_NAME") | |
ip address add "$TUN_ADDRESS/24" dev "$TUN_NAME" | |
ip link set dev "$TUN_NAME" up | |
addRoute "$TUN_ROUTE metric 1";; | |
*) | |
# Run your proxy in background | |
xray run -c config.json > /dev/null & | |
trap stop EXIT | |
start;; | |
esac |
Can we define PROXY_SERVER as a domain?
Can we define PROXY_SERVER as a domain?
I'm not sure but 99% the answer is no !!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
DNS resolution with socks proxy in ubuntu