-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathremove-wireguard-server.sh
executable file
·130 lines (107 loc) · 3.24 KB
/
remove-wireguard-server.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/bin/bash
# Remove Wireguard, its config files and peers that are setup by setup-wireguard-server.sh script on a Linux server.
# Copyright (c) 2020 Hamidreza Hosseinkhani (xei) under the terms of the MIT license.
# https://github.com/xei/wireguard-setup-scripts
# Some parts of this script are inspired from https://github.com/angristan/wireguard-install
function check_root_priviledge() {
if [ "${EUID}" -ne 0 ]; then
echo "Permission denied: Please run the script as root!"
exit 1
fi
}
function check_if_wireguard_is_setup() {
if [[ ! -e /etc/wireguard/params ]]; then
echo "WireGuard is not setup on the machine as a VPN server!."
exit 1
fi
}
function retrieve_wireguard_params() {
source /etc/wireguard/params
}
function stop_wireguard_service() {
systemctl stop "wg-quick@${NIC_WG}"
systemctl disable "wg-quick@${NIC_WG}"
# Check if WireGuard is still running
systemctl is-active --quiet "wg-quick@${NIC_WG}"
WG_IS_RUNNING=$?
if [[ ${WG_IS_RUNNING} -eq 0 ]]; then
echo "Stopping WireGuard service failed!"
echo "Run the script again after killing the process!"
exit 1
fi
}
function get_os_name() {
if [[ -e /etc/debian_version ]]; then
source /etc/os-release
OS="${ID}" # debian or ubuntu
elif [[ -e /etc/fedora-release ]]; then
OS=fedora
elif [[ -e /etc/centos-release ]]; then
OS=centos
elif [[ -e /etc/arch-release ]]; then
OS=arch
else
OS=unknown
fi
}
function remove_packages_from_ubuntu() {
apt-get autoremove --purge -y wireguard qrencode
}
function remove_packages_from_debian() {
apt-get autoremove --purge -y wireguard qrencode
}
function remove_packages_from_fedora() {
dnf remove -y wireguard-tools qrencode
dnf autoremove -y
}
function remove_packages_from_centos() {
yum -y remove elrepo-release epel-release kmod-wireguard wireguard-tools qrencode
rm -f "/etc/yum.repos.d/wireguard.repo"
yum -y autoremove
}
function remove_packages_from_arch() {
pacman -Rs --noconfirm wireguard-tools qrencode
}
function close_wireguard_port_in_ufw() {
ufw status | grep -qw active
UFW_IS_ACTIVE=$?
if [[ ${UFW_IS_ACTIVE} -eq 0 ]]; then
ufw delete allow ${SERVER_PORT}/udp
ufw reload
fi
}
function disable_ip_forwarding() {
rm -f /etc/sysctl.d/wg.conf
sysctl --system
}
function remove_config_dir() {
rm -rf /etc/wireguard
}
function main() {
check_root_priviledge
check_if_wireguard_is_setup
retrieve_wireguard_params
stop_wireguard_service
get_os_name
if [[ ${OS} == 'ubuntu' ]]; then
remove_packages_from_ubuntu
close_wireguard_port_in_ufw
elif [[ ${OS} == 'debian' ]]; then
remove_packages_from_debian
elif [[ ${OS} == 'fedora' ]]; then
remove_packages_from_fedora
elif [[ ${OS} == 'centos' ]]; then
remove_packages_from_centos
elif [[ ${OS} == 'arch' ]]; then
remove_packages_from_arch
elif [[ ${OS} == 'unknown' ]]; then
echo "OS not supported: The installer script (so also this script) just supports Debian, Ubuntu, Fedora, CentOS or Arch Linux systems."
exit 1
fi
disable_ip_forwarding
remove_config_dir
echo ""
echo "WireGuard, its config files and peers all removed successfully."
echo "This script did not do anything about Pi-hole ad blocker. If you want to remove it too, please visit its documentation."
}
main