-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.sh
executable file
·273 lines (263 loc) · 7.47 KB
/
manage.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#!/bin/bash
#
# ===========================================
# PROJECT: MultiVPN Server
# ===========================================
# Author: Gabriel Hinz
# Date: 12-30-2021
# Version: 1.0
# -------------------------------------------
# Desc: Project to create and manipulate
# multiple simultaneous vpn servers.
#
# To know more about this project, see in
# the github project page.
#
# License: MIT License
# ------------------------------------------
source config
# ----------------------------------
# Functions
# ----------------------------------
usage() {
echo -e "Usage: $0 [OPTIONS]" \
"\nInfo:" \
"\n -h,\t--help\t\tShow help message" \
"\n -l,\t--list\t\tLists all created vpns" \
"\nIntegrations:" \
"\n --init\t\t\tStarts creating a new vpn" \
"\n --start [vpn|all]\tStart the vpn, can be passed the name of the vpn or 'all'" \
"\n --stop [vpn|all]\tStop the vpn, can be passed the name of the vpn or 'all'" \
"\n --enable [vpn]\t\tEnable to turn on vpn on reboot" \
"\n --forward [vpn]\tCreate or remove a forward rule for a vpn"
exit 1;
}
check_dependencies() {
package_install() {
os=$(awk -F= '/^NAME/{print $2}' /etc/os-release | tr -d '"')
if grep -qs "ubuntu" /etc/os-release; then
apt-get update
apt-get install $1 -y 2>/dev/null
elif grep -qs "centos" /etc/os-release; then
yum update
yum install epel-release -y
yum install $1 -y 2>/dev/null
else
cecho red "Can't install $1! Try install manually."
exit 1
fi
}
echo -e "\nChecking dependencies.."
if ! which openvpn &>/dev/null; then
read -rep $'\nOpenVPN not installed, continue to install? [y/N] ' install
[[ "$install" =~ ^[yY]$ ]] || exit 1
cecho yellow "Installing OpenVPN.."
package_install openvpn
if ! which openvpn &>/dev/null; then
cecho red "Try to install in another way!"
exit 1
fi
else
mkdir -p /etc/openvpn/multivpn
fi
if ! which wget &>/dev/null; then
read -rep $'\nwget not installed, continue to install? [y/N] ' install
[[ "$install" =~ ^[yY]$ ]] || exit 1
cecho yellow "Installing wget.."
package_install wget
if ! which wget &>/dev/null; then
cecho red "Try to install in another way!"
exit 1
fi
fi
if [ ! -f $SERVICE_ROOT/multivpn.service ]; then
cp $SERVICE_NAME $SERVICE_ROOT/
fi
iptables_file="$ROOT_VPN/multivpn/rc.iptables"
if [ ! -f $iptables_file ]; then
mkdir -p $ROOT_VPN/multivpn
cat $RULES_MAIN > $iptables_file
fi
echo "Checking configuration.."
if [ -z "$VPN_DNS" ] || [ -z "$VPN_ROUTE" ] || [ -z "$VPN_MASK" ]; then
cecho red "Configure network settings in config file!"
exit 1
fi
}
start_vpn() {
vpn_targ=$1
vpn_home=$ROOT_VPN/$vpn_targ
if ps -ef | grep "[o]penvpn --cd $vpn_home" &>/dev/null; then
if [ -e $vpn_home/vpn-server.lock ]; then
cecho green "[Running]"
return 1
else
touch $vpn_home/vpn-server.lock
cecho green "[Locked]"
return 1
fi
else
if [ -e $vpn_home/vpn-server.lock ]; then
rm -f $vpn_home/vpn-server.lock
fi
fi
source $vpn_home/.vars
/usr/sbin/openvpn --cd $vpn_home --script-security $security --daemon \
--config $vpn_home/openvpn.conf &>/dev/null \
&& echo_success || echo_failed
[[ $? -eq 0 ]] && touch $vpn_home/vpn-server.lock
return $?
}
stop_vpn() {
vpn_targ=$1
vpn_home=$ROOT_VPN/$vpn_targ
vpn_pid=$(ps -ef | grep "[o]penvpn --cd $vpn_home" | awk '{ print $2 }')
if [ ! -z $vpn_pid ]; then
kill $vpn_pid &>/dev/null && echo_success || echo_failed
else
cecho red "[Not Running]"
fi
[[ $? -eq 0 ]] && rm -f $vpn_home/vpn-server\.lock
return $?
}
list_vpn() {
for vpn in $_ALL; do
if ps -ef | grep "[o]penvpn --cd $ROOT_VPN/$vpn" &>/dev/null; then
log_file=$(awk '/status/ {print $2}' $ROOT_VPN/$vpn/openvpn.conf)
connections=$(cat $log_file | egrep -o $VIP_REGEX | wc -l)
echo -e "VPN: $vpn"
echo -e "STATUS: $(cecho green 'Running')"
echo -e "CONNECTIONS: $connections"
echo -e "---------------------"
else
echo -e "VPN: $vpn"
echo -e "STATUS: $(cecho red 'Stopped')"
echo -e "CONNECTIONS: 0"
echo -e "---------------------"
fi
done
return $?
}
# ----------------------------------
# Start
# ----------------------------------
if [ $EUID -ne 0 ]; then
cecho red "Error: run with root user"
exit
fi
$ROOT/resources/header && check_dependencies
_ALL=$(find $ROOT_VPN/ -maxdepth 1 -type d | egrep -o $VPN_REGEX)
_OPTIONS=$(getopt -o lh --long help,enable:,list,start:,stop:,init,forward: -- "$@")
eval set -- "$_OPTIONS"
while true; do
case "$1" in
--start )
[[ "${2}" =~ $VPN_REGEX || "${2}" = 'all' ]] && arg=${2} || usage
cecho yellow "\nSTARTING:"
if [ $arg = 'all' ]; then
for vpn in $_ALL; do
printf '%-25s' "Starting $vpn"
start_vpn $vpn
done
else
printf '%-25s' "Starting $arg"
if [ -f "$ROOT_VPN/$arg/openvpn.conf" ]; then
start_vpn $arg
else
cecho red "[Not found]"
exit 1
fi
fi
break ;;
--stop )
[[ "${2}" =~ $VPN_REGEX || "${2}" = 'all' ]] && arg=${2} || usage
cecho yellow "\nSTOPPING:"
if [ $arg = 'all' ]; then
for vpn in $_ALL; do
printf '%-25s' "Stopping $vpn"
stop_vpn $vpn
done
else
if [ -f "$ROOT_VPN/$arg/openvpn.conf" ]; then
printf '%-25s' "Stopping $arg"
stop_vpn $arg
else
printf '%-25s' "Stopping $arg"
cecho red "[Not found]"
exit 1
fi
fi
break ;;
--enable )
[[ "${2}" =~ $VPN_REGEX || "${2}" = 'all' ]] && arg=${2} || usage
cecho yellow "\nENABLING:"
if [ ! -f $ROOT_VPN/multivpn/autostart ]; then
cp $RULES_AUTOSTART $ROOT_VPN/multivpn/
fi
if [ $arg = 'all' ]; then
for vpn in $_ALL; do
printf '%-22s' "Enabling $vpn"
if ! grep -q $vpn $ROOT_VPN/multivpn/.enabled 2>/dev/null; then
echo $vpn >> $ROOT_VPN/multivpn/.enabled
fi
cecho green "[Enabled]"
done
else
printf '%-22s' "Enabling $arg"
if ! grep -q $arg $ROOT_VPN/multivpn/.enabled 2>/dev/null; then
echo $arg >> $ROOT_VPN/multivpn/.enabled
fi
cecho green "[Enabled]"
fi
break ;;
--forward )
[[ "${2}" =~ $VPN_REGEX ]] && arg=${2} || usage
cecho yellow "\nVPN FORWARD CONFIG:"
read -p "Add or Remove [a/R]: " config_type
if [[ $config_type =~ ^[aA] ]]; then
[[ -f $ROOT_VPN/$arg/.vars ]] && source $ROOT_VPN/$arg/.vars || {
cecho red "Can't find $arg"
exit 0
}
read -p "Allow $arg FORWARD (ex: 192.168.1.0/24): " forward_range
rule="iptables -A FORWARD -i $interface -s $network/24 -d $forward_range -j ACCEPT"
echo $rule >> $ROOT_VPN/$arg/rules/iptables && {
iptables -D FORWARD -j DROP 2>/dev/null; $rule
iptables -A FORWARD -j DROP
}
elif [[ $config_type =~ ^[rR] ]]; then
[[ -f $ROOT_VPN/$arg/.vars ]] && source $ROOT_VPN/$arg/.vars || {
cecho red "Can't find $arg"
exit 0
}
echo -e "\nRules:"
[[ $(cat "$ROOT_VPN/$arg/rules/iptables" | wc -l) == 0 ]] && {
echo -e "There are no rules to remove."
exit 1
}
IFS=$'\n'
for line in $(cat "$ROOT_VPN/$arg/rules/iptables"); do
counter=$((counter+1))
echo "[$counter]" "$line"
done
read -rep $'\nSelect rule number to remove: ' rule_line
rule=$(sed -n ${rule_line}p $ROOT_VPN/$arg/rules/iptables)
if test $rule; then
exclude_rule=$(echo $rule | sed 's/\-A/\-D/')
bash -c $exclude_rule
fi
sed -i "${rule_line}d" $ROOT_VPN/$arg/rules/iptables
fi
break ;;
--init )
bash create-vpn.sh
break ;;
--list | -l )
cecho yellow "\nVPN DETAILS:"
echo -e "---------------------"
list_vpn
break ;;
--help | -h ) usage ;;
-- ) echo; usage ;;
esac
done