-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwireguard_vpn.sh
executable file
·100 lines (80 loc) · 1.81 KB
/
wireguard_vpn.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
#!/usr/bin/env bash
#
# Author: Stanislav Zhuk <stanislav.zhuk.work@gmail.com>
#
#{{{ bash settings
set -o errexit
set -o nounset
set -o pipefail
#}}}
# shellcheck disable=SC1090
source "$(dirname "$(realpath "${BASH_SOURCE[0]}")")/output_helpers.sh"
# shellcheck disable=SC2034
script_name="Wireguard VPN"
# shellcheck disable=SC2034
script_version="1.0.0"
script_intro
opt_help=false
opt_down=false
opt_up=false
while getopts dhu OPT; do
case "${OPT}" in
d) opt_down=true ;;
h) opt_help=true ;;
u) opt_up=true ;;
*) opt_help=true ;;
esac
done
if [[ ${opt_up} == false ]] && [[ ${opt_down} == false ]]; then
opt_help=true
fi
if "${opt_help}"; then
info "Usage: wireguard-vpn [options]
Options:
-u INTERFACE_NAME (up)
-d INTERFACE_NAME (down)
-h (help)"
exit 0
fi
function current_port() {
sudo wg show | grep -oP "listening port: \K.*"
}
function invalid_interface() {
failure "Please provide a valid interface name"
}
function vpnup() {
if [[ -z "${1}" ]]; then
invalid_interface
fi
if nmcli con show --active | grep -qw "^${1}"; then
success "Already activated"
exit 0
fi
if nmcli con show | grep -qw "^${1}"; then
nmcli con up id "${1}"
#sudo ufw allow "$(current_port)"/udp
else
invalid_interface
fi
}
function vpndown() {
if [[ -z "${1}" ]]; then
invalid_interface
fi
if nmcli con show --active | grep -qw "^${1}"; then
#sudo ufw delete allow "$(current_port)"/udp
nmcli con down id "${1}"
exit 0
fi
if nmcli con show | grep -qw "^${1}"; then
success "Already deactivated"
exit 0
else
invalid_interface
fi
}
if "${opt_up}"; then
vpnup "${2}"
elif "${opt_down}"; then
vpndown "${2}"
fi