-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.sh
229 lines (203 loc) · 5.94 KB
/
setup.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
#!/bin/bash
Has_sudo()
{
if [ `whoami` != root ]; then
echo Please run this script as root or using sudo
return 1
exit
else
return 0
fi
}
Log()
{
# Example : 'Log "ERROR | ERROR MESSAGE"'
_errorMessage=$1
_date="[ $(date) ] |"
_logFile=/var/log/ovpn.log
_logFileLines=
if [ -f $_logFile ]; then
_logFileLines=$(wc -l $_logFile | cut -d " " -f 1)
fi
echo "$_date $_errorMessage" >> $_logFile
echo "$_errorMessage"
chown -f root:root $_logFile
chmod -f 770 $_logFile
if [ $_logFileLines -ge 1000 ]; then
sed -i '1d' $_logFile
fi
}
Change_variable()
{
# Change_variable varToChange newVarContent VarType sourceFile
varToChange=$1
newVarContent=$2
varType=$3
sourceFile=$4
if [[ ! -n $varToChange ]] || [[ ! -n $newVarContent ]]; then
echo "Function Change_variable requires 2 parameters: varToChange newVarContent"
exit
elif [[ $varType == "array" ]]; then
sed -i -e "s|$varToChange=.*|$varToChange=\($newVarContent\)|g" $sourceFile
else
sed -i -e "s|$varToChange=.*|$varToChange=$newVarContent|g" $sourceFile
fi
}
Fix_Permissions()
{
Has_sudo
if id "openvpn" &>/dev/null; then
chown -Rfv openvpn:openvpn /etc/openvpn
chmod -Rfv 750 /etc/openvpn
elif id "nm-openvpn" &>/dev/null; then # OpenSuse
chown -Rfv nm-openvpn:nm-openvpn /etc/openvpn
chmod -Rfv 750 /etc/openvpn
else
echo "ERROR - NO openvpn USER FOUND!!!"
echo "PLEASE SET UP YOUR OWN PERMISSIONS FOR"
echo "/etc/openvpn"
fi
}
Install_dependancies()
{
Has_sudo
packagesNeeded="openvpn"
echo "Preparing to install needed dependancies for OpenVPN..."
if [ -f /etc/os-release ]; then
source /etc/os-release
crbOrPowertools=
os_detected=true
echo "ID=$ID"
case "$ID" in
fedora) dnf install $packagesNeeded -y ;;
rhel) dnf install $packagesNeeded -y ;;
debian) apt install $packagesNeeded -y ;;
ubuntu) apt install $packagesNeeded -y ;;
linuxmint) apt install $packagesNeeded -y ;;
elementary) apt install $packagesNeeded -y ;;
arch) pacman -Syu $packagesNeeded ;;
endeavouros) pacman -Syu $packagesNeeded ;;
manjaro) pacman -Syu $packagesNeeded ;;
opensuse*) zypper install $packagesNeeded ;;
esac
else
os_detected=false
echo "|-------------------------------------------------------------------|"
echo "| ******WARNING****** |"
echo "| ******ERROR****** |"
echo "| FAILED TO FIND /etc/os-release FILE. |"
echo "| PLEASE MANUALLY INSTALL THESE PACKAGES: |"
echo "| openvpn |"
echo "|-------------------------------------------------------------------|"
Log "ERROR | FAILED TO FIND /etc/os-release FILE!"
Log "ERROR | COULD NOT IDENTIFY PACKAGE MANAGER!"
read -p "Press ENTER to continue" ENTER
fi
}
Disable_IPv6()
{
if [ -x "$(command -v ufw)" ]; then
read -p "Would you like to disable IPv6 in UFW? : [y/N] " disableIPv6
if [[ $disableIPv6 == [yY] ]] || [[ $disableIPv6 == [yY][eE][sS] ]]; then
Change_variable IPV6 no null /etc/default/ufw
ufw disable
ufw enable
fi
ufw reload
fi
}
Setup()
{
Has_sudo
_serviceStorageDir=
_serviceActiveDir=
if [ ! -x "$(command -v ovpn)" ]; then
Install_dependancies
echo "Press ENTER to skip"
read -p "Input your VPN Provider : " accountFileName
read -p "Input your VPN Account Name : " vpnAccountName
read -p "Input your VPN Password : " vpnPassword
clear
elif [ -f /etc/openvpn/ovpn.conf ]; then # Remove on v1.2.5
mkdir -p /etc/openvpn/.ovpn/configs /etc/openvpn/.ovpn/backups
mv -f /etc/openvpn/ovpn.conf /etc/openvpn/.ovpn/configs/ovpn.conf
fi
if [ -x "$(command -v sv)" ]; then
_isRunit=true
if [[ $_service == *"@"* ]]; then
_service=$(echo $_service | cut -d "@" -f 1)
fi
fi
if [ -d /etc/sv ]; then # Void Linux - Runit
_serviceStorageDir=/etc/sv
cp -rfv .services/runit/* $_serviceStorageDir/
elif [ -d /etc/runit/sv ]; then # Artix Linux - Runit
_serviceStorageDir=/etc/runit/sv
cp -rfv .services/runit/* $_serviceStorageDir/
elif [ -x "$(command -v rc-update)" ]; then
_serviceStorageDir=/etc/init.d
cp -rfv .services/openrc/* $_serviceStorageDir/
elif [ -x "$(command -v dinitctl)" ]; then
_serviceStorageDir=/etc/dinit.d
cp -rfv .services/dinit/* $_serviceStorageDir/
elif [ -x "$(command -v systemctl)" ]; then
_serviceStorageDir=/usr/lib/systemd/system/
cp -rfv .services/systemd/* $_serviceStorageDir/
else
Log "ERROR | NO INIT SYSTEM FOUND, EXITING!"
exit
fi
if [[ -n $vpnAccountName ]]; then
mkdir -p /etc/openvpn/accounts/ /etc/openvpn/client/ /etc/openvpn/.ovpn/configs /etc/openvpn/.ovpn/backups
echo "$accountUserName" >> /etc/openvpn/accounts/$accountFileName
echo "$accountPassWord" >> /etc/openvpn/accounts/$accountFileName
ls -w 1 /etc/openvpn/accounts/
fi
Fix_Permissions
Disable_IPv6
cp ovpn.sh /bin/ovpn
ln -s /bin/ovpn /usr/local/bin/ovpn
chown -Rf root:root $_serviceStorageDir
chmod -Rf +x $_serviceStorageDir
# install -C -m 770 -o root $SOURCEFILE $DESTFILE
if [ -x "$(command -v sestatus)" ]; then
/sbin/restorecon -v /usr/lib/systemd/system/openvpn-client@.service
/sbin/restorecon -v /usr/lib/systemd/system/killswitch.service
/sbin/restorecon -v /usr/bin/ovpn
fi
if [ -x "$(command -v apt)" ] || [ -x "$(command -v pacman)" ] || [ -x "$(command -v zypper)" ]; then
cp -f .man-page/ovpn.1 /usr/share/man/man1/
elif [ -x "$(command -v dnf)" ]; then
cp -f .man-page/ovpn.1 /usr/local/share/man/man1/
fi
chmod +x /bin/ovpn
ovpn -h
}
Restore()
{
Has_sudo
if [ ! -f $1 ]; then
echo "ERROR: $1 IS NOT A FILE. TRY AGAIN!"
exit
fi
importTar=$1
tar xf $importTar -C /
Fix_Permissions
Setup
ovpn -e -s
echo "Complete!"
}
# MAIN
if [[ -n "$1" ]]; then
while [[ -n "$1" ]]; do
case "$1" in
-rb) Restore $2
shift ;;
*) echo "Option $1 not recognized" ;;
esac
shift
done
else
Setup
fi
# END MAIN