Skip to content

Instantly share code, notes, and snippets.

@OrcaXS
Forked from merrickluo/wireguard.sh
Created August 11, 2018 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OrcaXS/4a6396fbadcf3ae27ec42f3df91849a9 to your computer and use it in GitHub Desktop.
Save OrcaXS/4a6396fbadcf3ae27ec42f3df91849a9 to your computer and use it in GitHub Desktop.
wireguard.sh
#!/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