-
-
Save OrcaXS/4a6396fbadcf3ae27ec42f3df91849a9 to your computer and use it in GitHub Desktop.
wireguard.sh
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
#!/bin/bash | |
set -e | |
[[ $UID != 0 ]] && exec sudo -E "$(readlink -f "$0")" "$@" | |
up() { | |
# setup wg0 | |
ip link add dev wg0 type wireguard | |
ip addr add dev wg0 10.0.200.13/32 | |
wg setconf wg0 /etc/wireguard/wg0.conf | |
ip link set up wg0 | |
# route wg0 traffic to main table | |
wg set wg0 fwmark 1234 | |
ip rule add not fwmark 1234 table 2468 | |
ip rule add table main suppress_prefixlength 0 | |
ip route add default dev wg0 table 2468 | |
# route all china ip to main table | |
while IFS='' read -r line || [[ -n "$line" ]]; do | |
ip rule add to $line lookup main | |
done < /etc/wireguard/china_ip_list.txt | |
} | |
down() { | |
# setup wg0 | |
ip link del dev wg0 | |
# route all china ip to main table | |
while IFS='' read -r line || [[ -n "$line" ]]; do | |
ip rule del to $line lookup main | |
done < /etc/wireguard/china_ip_list.txt | |
# route wg0 traffic to main table | |
ip rule del not fwmark 1234 table 2468 | |
ip rule del table main suppress_prefixlength 0 | |
} | |
check() { | |
curl ipinfo.io/ip | |
curl -L tool.lu/ip | |
} | |
command="$1" | |
shift | |
case "$command" in | |
up) up "$@" ;; | |
down) down "$@" ;; | |
check) check "$@" ;; | |
*) echo "Usage: $0 up|down|check" >&2; exit 1 ;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment