forked from JRGTH/xigmanas-archivers-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwireguard-init
580 lines (527 loc) · 23.5 KB
/
wireguard-init
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
#!/bin/sh
# wireguard-init
# A simple wireguard add-on for XigmaNAS x64 11.x and later.
# License: BSD2CLAUSE (BSD 2-clause Simplified License).
# Debug script
set -x
# Copyright (c) 2023 FCTR
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that following conditions are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS 'AS IS' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# Set environment.
PATH=${PATH}:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
# Determine full working directory.
CWDIR=$(dirname "$(realpath "$0")") # /mnt/nvme/extensions/wireguard
# Global variables.
PLATFORM=$(uname -m) # amd64
PRODUCT=$(uname -i) # XIGMANAS-x64
PRDVERSION=$(uname -r | cut -d '-' -f1 | tr -d '.') # 131
PRDPLATFORM=$(cat /etc/platform) # x64-embedded
SCRIPTNAME=$(basename "$0") # wireguard-init
CONFIG="/cf/conf/config.xml" # /cf/conf/config.xml
APPNAME="wireguard"
EXTLOGFILE="${CWDIR}/log/${APPNAME}_ext.log" # /mnt/nvme/extensions/wireguard/log/wireguard_ext.log
FULLAPPNAME="${APPNAME}-addon" # wireguard-addon
PKGCONF="/etc/pkg/FreeBSD.conf" # /etc/pkg/FreeBSD.conf
USRLOCAL="/usr/local"
APPPATH="${USRLOCAL}/bin" # /usr/local/bin
APPBIN="wg"
# PKG_BUNDLE="bash gettext-runtime indexinfo readline wireguard wireguard-kmod wireguard-tools"
# MAIN_PKG="wireguard"
PKG_BUNDLE="bash gettext-runtime indexinfo readline wireguard-tools"
MAIN_PKG="wireguard-tools"
WWWPATH="/usr/local/www"
WIREGUARDCONF="/conf/${APPNAME}_config"
WIREGUARDCONFLINK="/var/etc/${APPNAME}conf"
# INSTALLPATH="${CWDIR}/${FULLAPPNAME}" # /mnt/nvme/extensions/wireguard/wireguard-addon
BRANCH="master"
GITURL="https://github.com/fctr/xigmanas-${APPNAME}-extension/archive/${BRANCH}.zip"
VERFILE="https://raw.githubusercontent.com/fctr/xigmanas-${APPNAME}-extension/${BRANCH}/version"
error_notify()
{
# Log/notify message on error and exit.
MSG="$*"
logger -t "${SCRIPTNAME}" "${MSG}"
echo "$*" >&2; exit 1
}
runtime_config()
{
# Create required directories if missing.
if [ ! -d "${CWDIR}/conf" ]; then
mkdir -p "${CWDIR}/conf"
fi
if [ ! -d "${CWDIR}/log" ]; then
mkdir -p "${CWDIR}/log"
fi
if [ ! -d "${CWDIR}/locale-wireguard" ]; then
mkdir -p "${CWDIR}/locale-wireguard"
fi
}
update_freebsdconf()
{
# Update FreeBSD.conf to fetch latest packages.
if [ -f "${PKGCONF}" ]; then
if grep -q "quarterly" ${PKGCONF}; then
sed -i '' -E "s/quarterly/latest/g" ${PKGCONF} || \
error_notify "Error: A problem has occurred while updating FreeBSD.conf file."
fi
fi
}
wireguard_native_download()
{
# Check if wireguard already exist.
if [ ! -f "${APPPATH}/${APPBIN}" ]; then
# Install latest wireguard packages.
update_freebsdconf
pkg install -y ${MAIN_PKG} || error_notify "Error: A problem has occurred while fetching ${MAIN_PKG}."
fi
}
wireguard_initial_download()
{
# Check if wireguard already exist.
if [ ! "$(ls -A '${CWDIR}/download/All')" ]; then
update_freebsdconf
pkg fetch -d -y -o "${CWDIR}/download" ${MAIN_PKG} || \
error_notify "Error: A problem has occurred while fetching ${MAIN_PKG}."
fi
}
ext_initial_download()
{
# Always ensure the version file is present, otherwise update the extension files on startup.
if [ ! -f "${CWDIR}/version" ]; then
echo "Fetching and extracting extension files..."
mkdir -p "${CWDIR}/update"
fetch -ao "${CWDIR}/update" --no-verify-peer --timeout=30 ${GITURL} || \
error_notify "Error: A problem has occurred while fetching extension package."
tar -xf "${CWDIR}/update/${BRANCH}.zip" --exclude='.git*' --strip-components 1 -C "${CWDIR}/update"
chmod +x "${CWDIR}/update/${SCRIPTNAME}"
cp -Rf "${CWDIR}/update/" "${CWDIR}/"
rm -r "${CWDIR}/update"
rm -f "${CWDIR}/${BRANCH}.zip"
echo "Done"
fi
}
extension_upgrade()
{
# Perform an online extension upgrade.
DATE=$(date +"%a %b %d %T %Y")
echo "Looking for new extension package..."
mkdir -p "${CWDIR}/update"
fetch -ao "${CWDIR}/update" --no-verify-peer --timeout=30 "${VERFILE}" || \
error_notify "Error: A problem has occurred while fetching version file."
# Compare version files and fetch latest package if available.
if [ -f "${CWDIR}/update/version" ]; then
UPDATEVER=$(tr -cd '[:digit:]' < "${CWDIR}/update/version")
CURRENTVER=$(tr -cd '[:digit:]' < "${CWDIR}/version")
if [ "${UPDATEVER}" -gt "${CURRENTVER}" ]; then
echo "New ${FULLAPPNAME} package found, performing upgrade..."
fetch -ao "${CWDIR}/update" --no-verify-peer --timeout=30 "${GITURL}" || \
error_notify "Error: A problem has occurred while fetching extension package."
tar -xf "${CWDIR}/update/${BRANCH}.zip" --exclude='.git*' --strip-components 1 -C "${CWDIR}/update"
chmod +x "${CWDIR}/update/${SCRIPTNAME}"
rm -f "${CWDIR}/update/${BRANCH}.zip"
cp -rf "${CWDIR}/update"/* "${CWDIR}/"
rm -r "${CWDIR}/update"
rm -f "${CWDIR}/${BRANCH}.zip"
# Logging the update event.
UPDATEVERSION=$(cat "${CWDIR}/version")
echo "${DATE}: extension upgraded to ${UPDATEVERSION}" >> "${EXTLOGFILE}"
echo "Extension package upgrade completed"
else
echo "Extension package is on the latest version"
rm -r "${CWDIR}/update"
fi
fi
}
product_check()
{
# Check for the working product.
if [ "${PRODUCT}" = "NAS4FREE-x64" ] || [ "${PRODUCT}" = "XIGMANAS-x64" ]; then
postinit_cmd
gui_start
fi
}
create_addon_env()
{
# Create required directories.
if [ ! -d "${CWDIR}/conf" ]; then
mkdir -p "${CWDIR}/conf"
fi
if [ ! -d "${CWDIR}/locale-wireguard" ]; then
mkdir -p "${CWDIR}/locale-wireguard"
fi
if [ ! -d "${CWDIR}/download/All" ]; then
mkdir -p "${CWDIR}/download/All"
fi
if [ ! -d "${CWDIR}/log" ]; then
mkdir -p "${CWDIR}/log"
fi
if [ ! -d "${CWDIR}/${FULLAPPNAME}" ]; then
mkdir -p "${CWDIR}/${FULLAPPNAME}"
fi
# Link wireguard-init to /usr/local/sbin.
if [ ! -f "${USRLOCAL}/sbin/${SCRIPTNAME}" ]; then
ln -fs "${CWDIR}/${SCRIPTNAME}" "${USRLOCAL}/sbin/${SCRIPTNAME}"
fi
}
platform_check()
{
# Check for working platform.
if [ "${PRDPLATFORM}" = "x64-embedded" ]; then
create_addon_env
ext_initial_download
wireguard_initial_download
sys_installpkgs
elif [ "${PRDPLATFORM}" = "x64-full" ]; then
create_addon_env
ext_initial_download
wireguard_native_download
sys_installpkgs
fi
}
sys_installpkgs()
{
if [ "${PRDPLATFORM}" = "x64-embedded" ]; then
local MAIN_FILE=$(pkg search -q ${MAIN_PKG} | awk "NR==1")
pkg add "${CWDIR}/download/All/${MAIN_FILE}.pkg" || error_notify "Error: A problem has occurred while installing ${MAIN_PKG}."
fi
if [ ! -f "${CWDIR}/conf/wg0.conf" ]; then
touch "${CWDIR}/conf/wg0.conf" || error_notify "Error: A problem has occurred while creating the wg0.conf file."
fi
chmod 660 "${CWDIR}/conf/wg0.conf"
if [ ! -f "${USRLOCAL}/etc/wireguard/wg0.conf" ]; then
ln -s "${CWDIR}/conf/wg0.conf" "${USRLOCAL}/etc/wireguard/wg0.conf"
fi
if [ "${PRDPLATFORM}" = "x64-embedded" ]; then
local ACTIVATE_ON_BOOT=$(sysrc -f "${CWDIR}${WIREGUARDCONF}" -i -n ACTIVATE_ON_BOOT)
if [ "${ACTIVATE_ON_BOOT}" = "YES" ]; then
sysrc wireguard_interfaces="wg0"
service wireguard enable
service wireguard start
fi
fi
}
postinit_cmd()
{
# Check and generate temporary php script for postinit command.
if ! grep -qw "${CWDIR}/${SCRIPTNAME}" "${CONFIG}"; then
touch "${CWDIR}/postinit" || error_notify "Error: A problem has occurred while creating the postinit file."
chmod +x "${CWDIR}/postinit"
if [ ! "${PRDVERSION}" -ge "110" ]; then
# Generate php script for NAS4Free 10.3 versions.
cat << EOF > "${CWDIR}/postinit"
<?php
require_once("config.inc");
require_once("functions.inc");
\$cmd = dirname(__FILE__)."/${SCRIPTNAME}";
\$i =0;
if ( is_array(\$config['rc']['postinit'] ) && is_array( \$config['rc']['postinit']['cmd'] ) ) {
for (\$i; \$i < count(\$config['rc']['postinit']['cmd']);) {
if (preg_match('/${SCRIPTNAME}/', \$config['rc']['postinit']['cmd'][\$i])) break; ++\$i; }
}
\$config['rc']['postinit']['cmd'][\$i] = \$config['cmd']."\$cmd";
write_config();
?>
EOF
else
# Generate php script for NAS4Free/XigmaNAS 11.x versions.
cat << EOF > "${CWDIR}/postinit"
<?php
require_once("config.inc");
require_once("functions.inc");
\$cmd = dirname(__FILE__)."/${SCRIPTNAME}";
\$name = "WireGuard Extension";
\$comment = "Start ${APPNAME}";
\$rc = &array_make_branch(\$config,'rc','param');
if(false === array_search_ex(\$name,\$rc,'name')):
\$rc_param = [];
\$rc_param['uuid'] = uuid();
\$rc_param['name'] = \$name;
\$rc_param['value'] = \$cmd;
\$rc_param['comment'] = \$comment;
\$rc_param['typeid'] = '2';
\$rc_param['enable'] = true;
\$rc[] = \$rc_param;
write_config();
endif;
unset(\$rc);
?>
EOF
fi
# Execute temporary php script.
if [ "${OBI_INSTALL}" != "ON" ]; then
echo "Creating postinit command..."
php-cgi -f "${CWDIR}/postinit" && (rm "${CWDIR}/postinit" || \
error_notify "Error: A problem has occurred while executing postinit file.")
echo "Done"
fi
sysrc -f "${CWDIR}${WIREGUARDCONF}" GUI_ENABLE=YES INSTALL_DIR="${CWDIR}" ACTIVATE_ON_BOOT=NO >/dev/null 2>&1
fi
}
gui_start()
{
# Initialize the extension gui.
if [ -d "${CWDIR}/gui" ]; then
# Always ensure the config directory/file exist.
if [ ! -f "${CWDIR}${WIREGUARDCONF}" ]; then
# Try to restore default configuration.
runtime_config
# Set default config.
sysrc -f "${CWDIR}${WIREGUARDCONF}" GUI_ENABLE=YES INSTALL_DIR="${CWDIR}" ACTIVATE_ON_BOOT=NO >/dev/null 2>&1
fi
GUI_STATUS=$(sysrc -f "${CWDIR}${WIREGUARDCONF}" -qn GUI_ENABLE)
if [ "${GUI_STATUS}" = "YES" ]; then
# Store the installation path and link conf.
if ! sysrc -f "${CWDIR}${WIREGUARDCONF}" -n INSTALL_DIR | grep -q "${CWDIR}"; then
sysrc -f "${CWDIR}${WIREGUARDCONF}" INSTALL_DIR="${CWDIR}" >/dev/null 2>&1
fi
mkdir -p "${WIREGUARDCONFLINK}"
ln -fhs "${CWDIR}/conf" "${WIREGUARDCONFLINK}/conf"
# Link the gui files.
if [ ! -d "${WWWPATH}/ext" ]; then
mkdir -p "${WWWPATH}/ext"
fi
ln -fhs "${CWDIR}/gui/ext/wireguard-gui" "${WWWPATH}/ext/" || error_notify "Error: A problem has occurred while copying extension gui files."
ln -fhs "${CWDIR}/gui/wireguard-gui.php" "${WWWPATH}/" || error_notify "Error: A problem has occurred while linking extension gui files."
ln -fhs "${CWDIR}/gui/wireguard-gui-lastshake.php" "${WWWPATH}/" || error_notify "Error: A problem has occurred while linking extension gui files."
ln -fhs "${CWDIR}/gui/wireguard-gui-transferred.php" "${WWWPATH}/" || error_notify "Error: A problem has occurred while linking extension gui files."
fi
fi
}
gui_enable()
{
# Relink conf and copy the gui files.
if [ -d "${CWDIR}/gui" ]; then
mkdir -p "${WIREGUARDCONFLINK}"
ln -fhs "${CWDIR}/conf" "${WIREGUARDCONFLINK}/conf"
sysrc -f "${CWDIR}${WIREGUARDCONF}" GUI_ENABLE=YES >/dev/null 2>&1
if [ ! -d "${WWWPATH}/ext" ]; then
mkdir -p "${WWWPATH}/ext"
fi
ln -fhs "${CWDIR}/gui/ext/wireguard-gui" "${WWWPATH}/ext/" || error_notify "Error: A problem has occurred while copying extension gui files."
ln -fhs "${CWDIR}/gui/wireguard-gui.php" "${WWWPATH}/" || error_notify "Error: A problem has occurred while linking extension gui files."
ln -fhs "${CWDIR}/gui/wireguard-gui-lastshake.php" "${WWWPATH}/" || error_notify "Error: A problem has occurred while linking extension gui files."
ln -fhs "${CWDIR}/gui/wireguard-gui-transferred.php" "${WWWPATH}/" || error_notify "Error: A problem has occurred while linking extension gui files."
else
error_notify "Error: Extension gui files not found."
fi
}
gui_disable()
{
# Disable gui if -t option specified.
if [ -d "${CWDIR}/gui" ]; then
rm -f "${WWWPATH}/wireguard-gui.php"
rm -f "${WWWPATH}/wireguard-gui-lastshake.php"
rm -f "${WWWPATH}/wireguard-gui-transferred.php"
rm -rf "${WWWPATH}/ext/wireguard-gui"
rm -f "${LOCALSHAREPATH}/locale-wireguard"
rm -rf "${WIREGUARDCONFLINK}"
sysrc -f "${CWDIR}${WIREGUARDCONF}" GUI_ENABLE=NO >/dev/null 2>&1 || error_notify "Error: A problem while removing extension gui files."
else
error_notify "Error: Extension gui files not found."
fi
# Remove empty ext folder to prevent empty "Extensions" tab.
if [ -d "${WWWPATH}/ext" ]; then
if [ ! "$(ls -A ${WWWPATH}/ext)" ]; then
rm -r "${WWWPATH}/ext"
fi
fi
}
pkg_upgrade()
{
# Update FreeBSD.conf file.
update_freebsdconf
# Check for wireguard updates.
if [ "${PRDPLATFORM}" = "x64-embedded" ]; then
# To fix pkg: Repository FreeBSD missing. 'pkg update' required on Embedded platforms.
pkg update
for PKG in ${PKG_BUNDLE}; do
echo "Looking for new ${PKG} package..."
# Set product version variables.
# We have to use the manifest file for version comparison for Embedded platform.
NOWPKG=$(awk -F'\"version\":' '{print $2}' "${CWDIR}/${FULLAPPNAME}/${PKG}/+MANIFEST" | awk -F\" '{print $2}' | tr -cd '[:digit:]')
NEWPKG=$(pkg search -Q version "${PKG}" | awk 'NR==2' | cut -d: -f 2 | tr -cd '[:digit:]')
# Extract new fetched packages for Embedded platform on new version.
if [ "${NEWPKG}" -gt "${NOWPKG}" ]; then
echo "New ${PKG} package found, performing upgrade..."
NEWPKG_VER=$(pkg search "${PKG}" | awk 'NR==1' | cut -d'-' -f2 | cut -d' ' -f1)
pkg fetch -y -o "${CWDIR}/download" "${PKG}-${NEWPKG_VER}" || \
error_notify "Error: A problem has occurred while fetching/locating ${PKG}."
echo "Extracting ${PKG}-${NEWPKG_VER}..."
tar -Jxf "${CWDIR}/download/All/${APPNAME}-${NEWPKG_VER}".[tp][xk][zg] -C "${CWDIR}/${FULLAPPNAME}/" || \
error_notify "Error: A problem has occurred while extracting files."
mv "${CWDIR}/${FULLAPPNAME}"/+* "${CWDIR}/${FULLAPPNAME}/${PKG}/"
else
echo "No new ${PKG} package available"
fi
done
elif [ "${PRDPLATFORM}" = "x64-full" ]; then
for PKG in ${PKG_BUNDLE}; do
echo "Looking for new ${PKG} package..."
# Set current version.
PKG=$(pkg info | grep ${APPNAME} | awk 'NR==1' | grep -o -E "([0-9]{1,}\.)+[0-9]{1,}" | tr -d '.')
# Issue wireguard upgrade command in Full platforms.
pkg upgrade -y "${PKG}" || \
error_notify "Error: A problem has occurred while upgrading ${APPNAME}."
# Set new version.
NEWPKG=$(pkg info | grep "${PKG}" | awk 'NR==1' | grep -o -E "([0-9]{1,}\.)+[0-9]{1,}" | tr -d '.')
if [ "${NEWPKG}" -gt "${PKG}" ]; then
echo "New ${PKG} package found and upgraded"
else
echo "No new ${PKG} package available"
fi
done
fi
# Check for extension updates.
extension_upgrade
}
reset_install()
{
# Reset the extension environment.
echo "Removing extension files..."
if [ -d "${CWDIR}/conf" ]; then
rm -rf "${CWDIR}/conf"
fi
if [ -d "${CWDIR}/log" ]; then
rm -rf "${CWDIR}/log"
fi
if [ -d "${CWDIR}/locale-wireguard" ]; then
rm -rf "${CWDIR}/locale-wireguard"
fi
if [ -d "${CWDIR}/${FULLAPPNAME}" ]; then
rm -rf "${CWDIR:?}/${FULLAPPNAME:?}"
fi
if [ -d "${CWDIR}/download" ]; then
rm -rf "${CWDIR}/download"
fi
if [ -f "${CWDIR}/version" ]; then
rm -f "${CWDIR}/version"
fi
runtime_config
}
remove_addon()
{
# Confirm for addon removal.
while :
do
read -r "Do you wish to proceed with the ${FULLAPPNAME} removal? [y/N]:" yn
case ${yn} in
[Yy]) break;;
[Nn]) exit 0;;
esac
done
echo "Proceeding..."
if [ -d "${WWWPATH}/wireguard-gui.php" ]; then
rm -f "${WWWPATH}/wireguard-gui.php"
rm -rf "${WWWPATH}/ext/wireguard-gui"
rm -f "${LOCALSHAREPATH}/locale-wireguard"
rm -rf "${WIREGUARDCONFLINK}"
fi
# Check for working platform.
if [ "${PRDPLATFORM}" = "x64-embedded" ]; then
if [ -f "${USRLOCAL}/bin/${APPBIN}" ]; then
pkg delete -y ${PKG_BUNDLE}
fi
elif [ "${PRDPLATFORM}" = "x64-full" ]; then
if [ -f "${USRLOCAL}/bin/${APPBIN}" ]; then
pkg delete -y ${PKG_BUNDLE}
fi
fi
# Remove addon related files and folders only-
# to protect any user-created custom files.
FILES="conf download gui locale-wireguard log wireguard-addon README.md postinit CHANGELOG version wireguard-init update"
for file in ${FILES}; do
if [ -f "${CWDIR}/${file}" ] || [ -d "${CWDIR}/${file}" ]; then
rm -rf "${CWDIR:?}/${file}"
fi
done
if [ ! -f "${USRLOCAL}/sbin/${SCRIPTNAME}" ]; then
rm "${USRLOCAL:?}/sbin/${SCRIPTNAME}"
fi
echo "Done"
echo "Please manually remove the wireguard Extension Command Script from the WebGUI."
exit 0
}
get_versions()
{
# Get wireguard-addon extension version.
if [ -f "${CWDIR}/version" ]; then
APPVERSION=$(cat "${CWDIR}/version")
else
APPVERSION="version file not found"
fi
# Display product versions.
for PKG in ${PKG_BUNDLE}; do
printf "\n%s version:" "$PKG" && pkg version -n "$PKG" -q | rev | cut -f 1 -d'-' | rev | cut -f 1 -w
done
printf "\nExtension version:" && echo "${FULLAPPNAME} ${APPVERSION}"
exit 0
}
exec_status()
{
# Log on startup success, else logging with faults.
if [ $? -eq 0 ]; then
MSG="script has been started successfully"
logger -t "${SCRIPTNAME}" "${MSG}"
else
MSG="script started with faults"
logger -t "${SCRIPTNAME}" "${MSG}"
fi
}
wireguard_init()
{
# Check for system compatibility.
if [ ! "${PLATFORM}" = "amd64" ]; then
echo "Unsupported platform"; exit 1
fi
# Check for product compatibility.
if [ ! "${PRDVERSION}" -ge "110" ]; then
echo "Unsupported version"; exit 1
fi
echo "Initializing ${APPNAME}..."
# Function calls.
platform_check
product_check
exec_status
}
# Run-time configuration.
runtime_config
while getopts ":ouxrvgth" option; do
case ${option} in
[h]) echo "Usage: ${SCRIPTNAME} -[option]";
echo "Options:"
echo " -u Upgrade WireGuard/Add-On packages."
echo " -v Display product version."
echo " -g Enables the addon GUI."
echo " -t Disable the addon GUI."
echo " -x Reset ${FULLAPPNAME}."
echo " -r Remove ${FULLAPPNAME}."
echo " -h Display this help message."; exit 0;;
[o]) OBI_INSTALL="ON";; # Prevent nested PHP-CGI call for installation with OBI.
[u]) pkg_upgrade;;
[x]) reset_install;;
[r]) remove_addon;;
[v]) get_versions;;
[g]) gui_enable; exit 0 ;; # Enable the addon gui.
[t]) gui_disable; exit 0 ;; # Disable the addon gui.
[?]) echo "Invalid option, -h for usage."; exit 1;;
esac
done
wireguard_init