-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathvpnservice.sh
333 lines (293 loc) · 12 KB
/
vpnservice.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#!/bin/bash
refreshVPN () {
# Clean up
rm -r $vpn_path/ovpn_files/
mkdir $vpn_path/ovpn_files
# Create local ovpn directory if it does not exist
if [ ! -d $vpn_path/local_ovpn/ ];then
mkdir $vpn_path/local_ovpn
fi
for profile in $(ls $vpn_path/vpn_profiles/*);do
# Grab variable names
vpn_name=$(grep 'vpn_name' $profile | awk -F '=' '{print $2}')
echo "Configuring profile for $vpn_name..." >> $vpn_path/refresh.log
vpn_configs_url=$(grep 'vpn_configs_url' $profile | awk -F '=' '{print $2}')
vpn_configs_password=$(grep 'vpn_configs_password' $profile | awk -F '=' '{print $2}')
vpn_username=$(grep 'vpn_username' $profile | awk -F '=' '{print $2}')
vpn_password=$(grep 'vpn_password' $profile | awk -F '=' '{print $2}')
# Create ovpn_files directory if it does not exist
if [ ! -d $vpn_path/ovpn_files/$vpn_name ];then
echo "Creating directory for $vpn_name..." >> $vpn_path/refresh.log
mkdir $vpn_path/ovpn_files/$vpn_name
else
# Clean up
echo "Cleaning up old ovpn files for $vpn_name..." >> $vpn_path/refresh.log
rm $vpn_path/ovpn_files/$vpn_name/*.ovpn
fi
# Create local ovpn sub directory if it does not exist
if [ ! -d $vpn_path/local_ovpn/$vpn_name ];then
mkdir $vpn_path/local_ovpn/$vpn_name
fi
# Cleanup and setup temp directory
echo "Delete temp folder..." >> $vpn_path/refresh.log
rm -r $vpn_path/ovpn_tmp/
echo "Create temp folder..." >> $vpn_path/refresh.log
mkdir $vpn_path/ovpn_tmp
# We have VPN credentials
if [ ! -z "$vpn_username" ] && [ ! -z "$vpn_password" ];then
if [ $vpn_configs_url = "local" ];then
# Local ovpn files
cp $vpn_path/local_ovpn/$vpn_name/*.ovpn $vpn_path/ovpn_files/$vpn_name/
else
# Download ovpn config files
echo "Downloading $vpn_name configs..." >> $vpn_path/refresh.log
wget --no-check-certificate -O $vpn_path/openvpn.zip $vpn_configs_url
# Unzip files
# Check if the zip is password-protected
if [ ! -z "$vpn_configs_password" ];then
echo "Unzipping password-protected $vpn_name..." >> $vpn_path/refresh.log
unzip -P $vpn_configs_password -q $vpn_path/openvpn.zip -d $vpn_path/ovpn_tmp
else
echo "Unzipping $vpn_name..." >> $vpn_path/refresh.log
unzip -q $vpn_path/openvpn.zip -d $vpn_path/ovpn_tmp
fi
# Clean up and move ovpn files
echo "Cleaning up and moving $vpn_name ovpn files..." >> $vpn_path/refresh.log
rm $vpn_path/openvpn.zip
# Check if ovpn files are in current folder
success=0
if ls $vpn_path/ovpn_tmp/*.ovpn >/dev/null 2>&1;then
echo "ovpn files in main folder"
success=1
else
# Check for TCP folder
echo "Checking for TCP folder to find ovpn files..."
tcp_folder_exists=$(ls -d $vpn_path/ovpn_tmp/*/ | grep -c -i tcp)
if [ $tcp_folder_exists -eq 1 ];then
cp $(ls -d $vpn_path/ovpn_tmp/*/ | grep -i tcp)/*.ovpn $vpn_path/ovpn_tmp/
success=1
fi
fi
if [ $success -eq 1 ];then
echo "Renaming files with spaces..."
for f in $vpn_path/ovpn_tmp/*\ *; do mv "$f" "${f// /_}" >/dev/null 2>&1; done
echo "Moving ovpn files to $vpn_name folder..."
mv $vpn_path/ovpn_tmp/*.ovpn $vpn_path/ovpn_files/$vpn_name/
rm -r $vpn_path/ovpn_tmp/
else
echo "Failed importing $vpn_name profile!" >> $vpn_path/refresh.log
echo "" >> $vpn_path/refresh.log
fi
fi
# Store user name and password
echo "Creating user.txt files with creds for $vpn_name..." >> $vpn_path/refresh.log
echo $vpn_username > $vpn_path/ovpn_files/$vpn_name/user.txt
echo $vpn_password >> $vpn_path/ovpn_files/$vpn_name/user.txt
# Edit ovpn files with creds
echo "Editing $vpn_name files..." >> $vpn_path/refresh.log
for i in $(ls $vpn_path/ovpn_files/$vpn_name/*.ovpn);do sed -i "s@auth-user-pass@auth-user-pass $vpn_path\/ovpn_files\/$vpn_name\/user.txt@g" $i;done
for i in $(ls $vpn_path/ovpn_files/$vpn_name/*.ovpn);do echo "" >> $i;done
for i in $(ls $vpn_path/ovpn_files/$vpn_name/*.ovpn);do echo "log $vpn_path/vpn.log" >> $i;done
echo "Successfully loaded $vpn_name profile!" >> $vpn_path/refresh.log
# We don't have VPN credentials
else
echo "Downloading CSV file..." >> $vpn_path/refresh.log
curl $vpn_configs_url | dos2unix | tail -n +3 > $vpn_path/ovpn_tmp/configs.csv
uniqueid=$(date +%s)
echo "Parsing CSV file..." >> $vpn_path/refresh.log
while IFS='' read -r line || [[ -n "$line" ]];do
country=$(echo "$line" | awk -F ',' '{print $7}')
echo "$line" | awk -F ',' '{print $NF}' | base64 -d > $vpn_path/ovpn_tmp/$country-$uniqueid.ovpn
echo "log /home/vpn/vpn.log" >> $vpn_path/ovpn_tmp/$country-$uniqueid.ovpn
uniqueid=$((uniqueid+1))
done < $vpn_path/ovpn_tmp/configs.csv
mv $vpn_path/ovpn_tmp/*.ovpn $vpn_path/ovpn_files/$vpn_name/
rm -r $vpn_path/ovpn_tmp/
fi
done
# Adding countries
echo "Updating country list..." >> $vpn_path/refresh.log
for country in $(cat $vpn_path/countries.txt | awk -F ',' '{print $1}');do
# Create folder if it does not exist
if [ ! -d $vpn_path/ovpn_files/Country_$country ];then mkdir $vpn_path/ovpn_files/Country_$country;fi
# Cleanup folder if it already exists
if ls $vpn_path/ovpn_files/Country_$country/*.ovpn >/dev/null 2>&1; then rm $vpn_path/ovpn_files/Country_$country/*.ovpn; fi
# Copy ovpn files
line=$(cat $vpn_path/countries.txt | grep $country)
for i in ${line//,/ };do
# Loop through VPN provider folders
for folder in $( ls -I Country_* $vpn_path/ovpn_files/);do
find $vpn_path/ovpn_files/$folder -iname "$i.*" -exec cp {} $vpn_path/ovpn_files/Country_$country/ \;
done
done
done
# Clean up country mismatch
find $vpn_path/ovpn_files/Country_UK -type f -iname ukraine* -exec rm -f {} \;
echo "Done!" >> $vpn_path/refresh.log
rm $vpn_path/refresh
if [ -f $vpn_path/stop ];then rm $vpn_path/stop;fi
}
killOVPN () {
echo "Killing OVPN..."
for i in {1..4}; do killall openvpn;done
if [ -f currentvpn.txt ];then rm currentvpn.txt;fi
}
stopVPN () {
echo "Disconnecting VPN..."
for i in {1..4}; do killall openvpn;done
if [ -f currentvpn.txt ];then rm currentvpn.txt;fi
rm currentvpn.txt
rm stop
increment
}
currentprovider () {
# get current VPN provider
provider=${providers[$providersindex]}
# count number of ovpn files for VPN provider
providertotal=$provider\total
providertotal=$(ls $vpn_path/ovpn_files/$provider/*.ovpn | sed 's/^.*\///g' | wc -l)
# get current .conf file
providerindex=$provider\index
eval $provider\index=$((1 + RANDOM % $providertotal))
location=$(ls $vpn_path/ovpn_files/$provider/*.ovpn | sed 's/^.*\///g' | sed -n ${!providerindex}\p)
}
startVPN () {
echo "Starting $provider with access point $location"
if [ -f vpn.log ];then echo "" > vpn.log;fi
if [ -f $vpn_path/refresh ];then rm $vpn_path/refresh;fi
openvpn --config "$vpn_path/ovpn_files/$provider/$location" --script-security 2 --float --route-up $vpn_path/up.sh --down $vpn_path/dn.sh --daemon 2>&1
echo "$location"
echo $(date) > date.log
}
increment () {
# increment providers array
providersindex=$((providersindex + 1))
echo "$providersindex total provider: $totalproviders providerindex ${!providerindex} out of $providertotal"
if [ $providersindex -gt $((totalproviders - 1)) ];then providersindex=0;fi
# increment index within specific provider
let eval $provider\index++
if [ ${!providerindex} -gt $providertotal ];then eval $provider\index=1;fi
}
checkVPN () {
if [ -f $vpn_path/refresh ] || [ -f $vpn_path/start ];then
echo "VPN being refreshed or restarted..."
else
echo "Checking VPN status..."
success=$(tail -5 vpn.log | egrep -c '(Sequence Completed)')
waittime=0
while [ $success -eq 0 ];do
if [ -f $vpn_path/stop ];then break;fi
clear
echo "Waiting for connection ($waittime/15)..."
tail -5 vpn.log
sleep 1
success=$(tail -5 vpn.log | egrep -c '(Sequence Completed)')
waittime=$((waittime +1))
if [ $waittime -eq 15 ];then
echo "Failed to connect, trying again!"
echo "Error with $location on $(date) errors=$errors pingcheck=$pingcheck" >> error.log
killOVPN
rm vpn.log
currentprovider
startVPN
checkVPN
fi
done
echo "VPN connected OK"
echo "$provider $location" > currentvpn.txt
echo "$provider $location" > $share_path/currentvpn.txt
fi
}
readproviders () {
# Unset array
unset providers
# array of VPN providers
index=0
while read line; do providers[$index]="$line";index=$(($index+1));done < $vpn_path/providers.txt
# variables initialization
providersindex=0
totalproviders=${#providers[@]}
for i in $(cat $vpn_path/providers.txt);do let eval $i\index=1;done
}
###################
###################
# Assign current VPN directory based on where script runs from
vpn_path=$(pwd)
# Share path
if [ -f $vpn_path/vpn.cfg ];then
share_path=$(sed -n 's/^share_path = //p' $vpn_path/vpn.cfg )
else
share_path=""
fi
# Set PATH variable
export PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin"
# Disable traffic from victim to VPN
/sbin/iptables -A INPUT -s $(cat $vpn_path/drop.txt) -j DROP
# Downcheck
downcheck=0
if [ -f custom ];then rm custom;fi
killOVPN
if [ -vpn.log ];then rm vpn.log;fi
# infinite loop
while :
do
echo "-----"
echo "$provider $location"
echo "-----"
if [ -f vpn.log ];then tail -5 vpn.log;fi
if [ -f refresh ];then
echo "Refreshing ovpn files..."
killOVPN
refreshVPN
fi
if [ -f $share_path/rotate ] || [ -f rotate ];then
echo "Rotating IP address..."
killOVPN
rm off
rm vpn.log
if [ -f currentvpn.txt ];then rm currentvpn.txt;fi
currentprovider
startVPN
checkVPN
rm $share_path/rotate
rm rotate
fi
if [ -f custom ];then
echo "Starting new VPN connection..."
killOVPN
rm off
rm vpn.log
if [ -f currentvpn.txt ];then rm currentvpn.txt;fi
if [ -f start ];then rm start;fi
provider=$(cat $vpn_path/providers.txt | awk -F ',' '{print $1}')
location=$(cat $vpn_path/providers.txt | awk -F ',' '{print $2}')
if [ -z $location ];then
readproviders
currentprovider
startVPN
checkVPN
else
startVPN
checkVPN
fi
rm custom
if [ -f start ];then rm start;fi
fi
if [ -f stop ];then
stopVPN
touch off
fi
# Check if VPN is down after 1 minute
if [ -f off ];then
echo "VPN has not started or is off"
else
if [ $downcheck -eq 60 ];then
echo "Checking VPN at $(date)" >> error.log
downcheck=0
checkVPN
else
downcheck=$((downcheck+1))
fi
fi
sleep 1
done