#!/bin/sh
#a script to manage wireguard easier
#author:Wos
#if you already connected wireguard,but can't surf the network,you can try blow firewall rule
#sudo iptables -A INPUT -i wg0 -m state --state ESTABLISHED,RELATED -j ACCEPT
function usage(){
echo -e "need root priority"
echo -e "usage:`basename $0` -u #up wg0"
echo -e " :`basename $0` -d #down wg0"
echo -e " :`basename $0` -u [config_file] #up from configuration file"
echo -e ""
exit -1
}
[ `id -u` != 0 ] && usage
NUM=$(ip link|grep wg0|wc -l)
if [[ $# -eq 1 ]]; then
#down wg0
if [[ "${1}" == "-d" ]] && [[ $NUM -eq 0 ]]; then
echo -e "\033[96;40mno wireguard instance running!\033[0m"
exit -1
elif [[ "${1}" == "-d" ]] && [[ $NUM -ne 0 ]]; then
wg-quick down wg0
echo -e "\033[96;40mwireguard instance already shutdown!\033[0m"
echo -e ""
exit -1
#up wg0
elif [[ "${1}" == "-u" ]] && [[ $NUM -ne 0 ]]; then
wg show
echo -e "\033[96;40mwireguard instance already running!\033[0m"
echo -e ""
exit -1
elif [[ "${1}" == "-u" ]] && [[ $NUM -eq 0 ]]; then
wg-quick up wg0
wg show
echo -e "\033[96;40mwireguard instance already running!\033[0m"
echo -e ""
exit -1
else
usage
fi
elif [[ $# -eq 2 ]]; then
#up from a configuration file
if [[ "${1}" == "-u" ]] && [[ -f "${2}" ]] && [[ $NUM -ne 0 ]]; then
wg show
echo -e "\033[96;40mwireguard instance already running!\033[0m"
echo -e ""
exit -1
elif [[ "${1}" == "-u" ]] && [[ -f "${2}" ]] && [[ $NUM -eq 0 ]]; then
sed -i -e 's/,fd.*//g' -e 's/,::.*//g' ${2}
cp ${2} /etc/wireguard/wg0.conf
wg-quick up wg0
wg show
echo -e "\033[96;40mwireguard instance already running!\033[0m"
echo -e ""
exit -1
else
usage
fi
else
usage
fi