#! /bin/sh -e # -------------------------------------------------- # wgctl - toggle Wireguard VPN up/dn or get status # -------------------------------------------------- # written by woog@rawtext.club ; copyleft 2026 # # Vars: WGint='wg0' # wg(4) pseudo-NIC Ngate='192.168.1.1' # non-WG gateway # proton-CN-ST_PN Wserv='212.102.44.166' # VPN Endpoint (minus port) Waddr='0.0.0.0' # VPN AllowedIPs Wgate='10.2.0.2' # VPN Address (minus subnet) WGns4='10.2.0.1' # VPN DNS WGns6='2a07:b944::2:1' # VPN DNS (IPv6; if avail.) # Functions: usage() { cat </dev/null 2>&1 sleep 10 done } wg_up() { if (! wgconfig "$WGint" >/dev/null ) then echo "$WGint not configured." exit 1 elif [ -z "$(ifconfig -d "$WGint")" ] then printf '\n => Wireguard interface %s already UP.\n\n' "$WGint" exit 1 else sudo ifconfig "$WGint" up && \ sudo route add "$Wserv" "$Ngate" && \ sudo route change default "$Wgate" && \ echo "adding nameserver $Wname" ; sleep 1 && \ echo "nameserver $WGns4 $WGns6" |sudo resolvconf -x -a "$WGint" - && \ wping & printf '\n => Wireguard interface %s is UP.\n\n' "$WGint" fi } wg_dn() { if (! wgconfig "$WGint" >/dev/null ) then echo "$WGint not configured." exit 1 elif [ -z "$(ifconfig -u "$WGint")" ] then printf '\n => Wireguard interface %s already DOWN.\n\n' "$WGint" exit 1 else sudo ifconfig "$WGint" down && \ sudo route delete "$Wserv" && \ sudo route change default "$Ngate" && \ sudo resolvconf -f -d "$WGint" && \ echo "removing nameserver(s) $WGns4 $WGns6" && \ printf '\n => Wireguard interface %s is DOWN.\n\n' "$WGint" fi } wstat() { if (! wgconfig "$WGint" >/dev/null ) then echo "$WGint not configured." elif [ -z "$(ifconfig -u "$WGint")" ] then printf '\n => %s is DOWN.\n\n' "$WGint" elif [ -z "$(ifconfig -d "$WGint")" ] then printf '\n => %s is UP.\n\n' "$WGint" fi } # Main: case "$1" in -[hH?]) usage ;; -[uU]) wg_up ;; -[dD]) wg_dn ;; -[tT]) printf "\nnslookup: %s \n" 'ProtonVPN' nslookup -query=A -timeout=10 protonvpn.com ping -c3 protonvpn.com ; echo ;; -[pP]) echo ;wgconfig "$WGint" ; echo ;; -[iI]) echo ;ifconfig -v "$WGint" ; echo ;; *) wstat ;; esac