forked from nachaphon-phontree/TrustTunnelClient
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·631 lines (559 loc) · 14.8 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·631 lines (559 loc) · 14.8 KB
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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
#!/bin/sh
# TrustTunnel Client Installation Script
set -e -u
# Function log is an echo wrapper that writes to stderr if the caller
# requested verbosity level greater than 0. Otherwise, it does nothing.
log() {
if [ "$verbose" -gt '0' ]
then
echo "$1" 1>&2
fi
}
# Function error_exit is an echo wrapper that writes to stderr and stops the
# script execution with code 1.
error_exit() {
echo "$1" 1>&2
exit 1
}
# Function usage prints the note about how to use the script.
usage() {
echo 'Usage: install.sh [-o output_dir] [-v] [-h] [-u] [-p archive_path] [-a y|n]' 1>&2
}
# Function parse_opts parses the options list and validates it's combinations.
parse_opts() {
while getopts 'vho:uV:p:a:' opt "$@"
do
case "$opt"
in
'v')
verbose='1'
;;
'h')
usage
exit 0
;;
'o')
output_dir="$OPTARG"
;;
'u')
uninstall='1'
;;
'V')
version="$OPTARG"
;;
'p')
archive_path="$OPTARG"
;;
'a')
case "$OPTARG" in
[yY]|[yY][eE][sS])
auto_answer="y"
;;
[nN]|[nN][oO])
auto_answer="n"
;;
*)
echo "Invalid value for -a. Use 'y' or 'n' (or 'yes'/'no')." 1>&2
exit 1
;;
esac
;;
*)
log "bad option $OPTARG"
usage
;;
esac
done
}
# Function is_little_endian checks if the CPU is little-endian.
#
# See https://serverfault.com/a/163493/267530.
is_little_endian() {
# The ASCII character "I" has the octal code of 111. In the two-byte octal
# display mode (-o), hexdump will print it either as "000111" on a little
# endian system or as a "111000" on a big endian one. Return the sixth
# character to compare it against the number '1'.
#
# Do not use echo -n, because its behavior in the presence of the -n flag is
# explicitly implementation-defined in POSIX. Use hexdump instead of od,
# because OpenWrt and its derivatives have the former but not the latter.
is_little_endian_result="$(
printf 'I'\
| hexdump -o\
| awk '{ print substr($2, 6, 1); exit; }'
)"
readonly is_little_endian_result
[ "$is_little_endian_result" -eq '1' ]
}
# Function set_os sets the os if needed and validates the value.
set_os() {
# Set if needed.
if [ "$os" = '' ]
then
os="$( uname -s )"
case "$os"
in
('Darwin')
os='macos'
;;
('Linux')
os='linux'
;;
(*)
error_exit "Unsupported operating system: '$os'"
;;
esac
fi
# Validate.
case "$os"
in
('macos'|'linux')
# All right, go on.
;;
(*)
error_exit "Unsupported operating system: '$os'"
;;
esac
# Log.
log "Operating system: $os"
}
# Function set_cpu sets the cpu if needed and validates the value.
set_cpu() {
# For macOS there is universal binary, so we don't need to set cpu
if [ "$os" = 'macos' ]
then
return 0
fi
# Set if needed.
if [ "$cpu" = '' ]
then
cpu="$( uname -m )"
case "$cpu"
in
('x86_64'|'x86-64'|'x64'|'amd64')
cpu='x86_64'
;;
('armv7l' | 'armv8l')
cpu='armv7'
;;
('aarch64'|'arm64')
cpu='aarch64'
;;
('mips')
if is_little_endian
then
cpu="mipsel"
fi
;;
(*)
error_exit "unsupported cpu type: $cpu"
;;
esac
fi
# Validate.
case "$cpu"
in
('x86_64'|'armv7'|'aarch64')
# All right, go on.
;;
('mips'|'mipsel')
# That's right too.
;;
(*)
error_exit "Unsupported cpu type: $cpu"
;;
esac
# Log.
log "CPU type: $cpu"
}
# Function for queries with support for auto-answer
ask_user() {
local prompt="$1"
printf "%s" "$prompt"
if [ -n "$auto_answer" ]; then
response="$auto_answer"
echo "$response"
else
read -r response < /dev/tty
fi
}
set_is_root() {
if [ $USER = "root" ]; then
is_root='1'
fi
if [ "$(id -u)" = "0" ]; then
is_root='1'
fi
}
# Function is_dir_owned_by_current_user checks if the output directory is owned by the current user
is_dir_owned_by_current_user() {
dir="$1"
if [ "$os" = "macos" ]; then
# macOS
if ! owner_name=$(stat -f '%Su' "$dir"); then
echo "Cannot stat '$dir'"
return 0
fi
elif [ "$os" = "linux" ]; then
# Linux
if ! owner_name=$(stat -c '%U' "$dir"); then
echo "Cannot stat '$dir'"
return 0
fi
else
echo "Unsupported OS: $os"
return 1
fi
if [ "$owner_name" = "$USER" ]; then
return 0
else
return 1
fi
}
# Function create_dir creates the output directory if it does not exist.
create_dir() {
mkdir -p "$output_dir" 2>/dev/null
if [ $? -eq 1 ];
then
if [ "$is_root" -eq 0 ]; then
echo "Starting sudo to create directory '$output_dir'"
if sudo mkdir -p "$output_dir"; then
sudo chown -R "${SUDO_USER:-$USER}" "$output_dir"
log "'$output_dir' has been created and ownership has been set to '${SUDO_USER:-$USER}'"
else
error_exit "Failed to create '$output_dir' with sudo"
fi
else
error_exit "Failed to create '$output_dir'"
fi
else
log "'$output_dir' has been created"
fi
}
# Check if the directory is owned by the current user and change the ownership if needed
check_owner() {
if [ "${is_root}" -eq 1 ]; then
return 0
fi
if is_dir_owned_by_current_user "$output_dir";
then
log "'$output_dir' exists and is owned by '$USER'"
else
log "'$output_dir' exists but is not owned by '$USER'"
ask_user "Would you like to change the ownership of $output_dir to $USER? [y/N] "
case "$response" in
[yY]|[yY][eE][sS])
target_user="${SUDO_USER:-$USER}"
if sudo chown -R "$target_user" "$output_dir"; then
log "Ownership of $output_dir has been changed to '${target_user}'"
else
error_exit "Failed to change ownership of '$output_dir'"
fi
;;
*)
error_exit "Installation cannot proceed without changing ownership of '$output_dir'"
;;
esac
fi
}
# Function check_out_dir requires the output directory to be set and exist.
check_out_dir() {
if [ "$output_dir" = '' ]
then
# If output_dir is not set, we will install to /opt
output_dir='/opt'
fi
# If output_dir is '.'or '/opt', create inside it `trusttunnel_client` directory
if [ "$output_dir" = '.' ] || [ "$output_dir" = '/opt' ]
then
output_dir="${output_dir}/trusttunnel_client"
fi
if [ "$uninstall" -eq '1' ]
then
echo "TrustTunnel Client will be uninstalled from '$output_dir'"
return 0
else
echo "TrustTunnel Client will be installed to '$output_dir'"
fi
set +e
# Check if directory exists
if [ ! -d "$output_dir" ];
then
log "'$output_dir' directory does not exist, attempting to create it..."
create_dir
else
log "'$output_dir' directory exists"
check_owner
fi
set -e
}
# Function verify_hint prints a hint about how to verify the installation.
verify_hint() {
exe_name=trusttunnel_client
# Check if `.sig` file exists
if [ -f "${output_dir}/${exe_name}.sig" ]
then
echo
echo "To verify the installation, run the following command to import the public key and verify the signature:"
echo " gpg --keyserver 'keys.openpgp.org' --recv-key '28645AC9776EC4C00BCE2AFC0FE641E7235E2EC6'"
echo " gpg --verify ${output_dir}/${exe_name}.sig ${output_dir}/${exe_name}"
fi
}
# Function remove_downloaded_package deletes the archive only if it was downloaded and not transferred.
remove_downloaded_package() {
if [ -z "$archive_path" ]; then
$remove_command "$pkg_name"
fi
}
# Function unpack unpacks the passed archive depending on it's extension.
unpack() {
log "Unpacking package from '$pkg_name' into '$output_dir'"
if ! mkdir -p "$output_dir"
then
error_exit "Cannot create directory '$output_dir'"
fi
if ! tar -C "$output_dir" -f "$pkg_name" -x -z
then
remove_downloaded_package
error_exit "Cannot unpack '$pkg_name'"
fi
remove_downloaded_package
log "Package has been unpacked successfully"
base_name=$(echo "$pkg_name" | sed -E 's!.*/!!')
dir_name=$(echo "${base_name}" | sed -E -e 's/(.*)(\.tar\.gz|\.zip)/\1/')
if [ -z "${dir_name}" ]; then
error_exit "Can not determine directory name inside archive"
fi
if type mv > /dev/null 2>&1; then
mv -f "${output_dir}/${dir_name}/"* "${output_dir}"
elif type ln > /dev/null 2>&1; then
ln -f "${output_dir}/${dir_name}/"* "${output_dir}"
rm -f "${output_dir}/${dir_name}/"*
else
error_exit "You need mv or ln for this script to work. Make sure that \"coreutils-mv\" is installed."
fi
if type rmdir > /dev/null 2>&1; then
rmdir "${output_dir}/${dir_name}"
else
if [ "$(echo "${output_dir}/${dir_name}/"*)" != "${output_dir}/${dir_name}/*" ]; then
error_exit "Directory not empty"
fi
rm -rf "${output_dir:?}/${dir_name:?}"
fi
}
# Function unpack unpacks the passed archive depending on it's extension.
check_package() {
if [ "$uninstall" -eq '1' ]; then
return 0
fi
log "Checking downloaded package '$pkg_name'"
case "$pkg_ext"
in
('zip')
if ! unzip "$pkg_name" -t -q
then
remove_downloaded_package
error_exit "Error checking $pkg_name"
fi
;;
('tar.gz')
if ! tar -f "$pkg_name" -z -t > /dev/null
then
remove_downloaded_package
error_exit "Error checking '$pkg_name'"
fi
;;
(*)
error_exit "Unexpected package extension: '$pkg_ext'"
;;
esac
}
# Function parse_version parses the version from the passed script and it's arguments.
parse_version() {
if [ "$uninstall" -eq '1' ]; then
return 0
fi
if [ -n "$version" ]; then
return 0
fi
# Extract the base name of the script
script_name="${0##*/}"
version=$(echo "${script_name}" | sed -nE 's/.*([0-9]+\.[0-9]+\.[0-9]+).*/\1/p')
if [ -z "$version" ]; then
echo "Version required"
return 1
fi
echo "Version number extracted from script name: $version"
}
# Add version to the package name if it's not empty.
apply_version() {
if [ -z "$version" ]; then
return 0
fi
pkg_name=$(echo "${pkg_name}" | sed -E "s/trusttunnel_client/trusttunnel_client-v${version}/")
}
# Main function.
configure() {
if [ "$uninstall" -eq '1' ]
then
echo 'Uninstalling TrustTunnel Client...'
else
echo 'Installing TrustTunnel Client...'
fi
set_is_root
set_os
set_cpu
check_out_dir
pkg_ext='tar.gz'
if [ -n "$archive_path" ]; then
pkg_name="$archive_path"
need_download='0'
log "Use users archive: $archive_path"
return
fi
parse_version
if [ "$os" = 'macos' ]
then
pkg_name="trusttunnel_client-macos-universal.${pkg_ext}"
else
pkg_name="trusttunnel_client-${os}-${cpu}.${pkg_ext}"
fi
apply_version
url="https://github.com/TrustTunnel/TrustTunnelClient/releases/download/v${version}/${pkg_name}"
readonly output_dir url pkg_name
if [ "$uninstall" -eq '0' ]
then
log "Package name: '$pkg_name'"
log "TrustTunnel Client will be installed to '$output_dir'"
fi
}
# Function handle_uninstall removes the existing package from the output directory.
handle_uninstall() {
# Check if the package is present in the output directory.
if [ ! -f "${output_dir}/trusttunnel_client" ]
then
error_exit "TrustTunnel Client is not installed in '${output_dir}'. Please specify the correct installation directory"
fi
# Check if vpn is running
if pgrep -x "trusttunnel_client" > /dev/null
then
error_exit "TrustTunnel Client is currently running, please stop it before uninstalling"
fi
remove_existing
# Check if the directory is empty
if [ -z "$(ls -A "${output_dir}")" ]
then
set +e
log "Remove empty directory: '${output_dir}'"
rmdir "${output_dir}" 2>/dev/null
if [ $? -eq 1 ];
then
if [ "$is_root" -eq 0 ]; then
echo "Starting sudo to remove '${output_dir}'"
if sudo rmdir "${output_dir}"; then
log "Empty directory '${output_dir}' has been removed"
else
error_exit "Failed to remove empty directory '${output_dir}' with sudo"
fi
else
error_exit "Failed to remove empty directory '${output_dir}'"
fi
else
log "Empty directory '${output_dir}' has been removed"
fi
set -e
fi
}
# Function remove_existing removes the existing package from the output directory before installing a new one.
remove_existing() {
log 'Removing existing package...'
# Remove executable file
rm -f "${output_dir}/trusttunnel_client"
log "'trusttunnel_client' has been removed from '${output_dir}'"
rm -f "${output_dir}/setup_wizard"
log "'setup_wizard' has been removed from '${output_dir}'"
rm -f "${output_dir}/LICENSE"
log "'LICENSE' has been removed from '${output_dir}'"
}
# Function checks if the package is already present in the output directory.
handle_existing() {
if [ "$uninstall" -eq '1' ]
then
handle_uninstall
exit 0
fi
if [ ! -f "${output_dir}/trusttunnel_client" ]
then
return
fi
log "Package 'trusttunnel_client' is already present in '${output_dir}'"
ask_user "Ensure 'TrustTunnel Client' is stopped before proceeding, continue? [y/N]"
remove_existing
}
# Function download downloads the package from the url.
download() {
if [ "$uninstall" -eq '1' ]; then
return 0
fi
if [ "$need_download" -eq '0' ]; then
return
fi
log "Downloading TrustTunnel Client package: $url"
# Temporary file name for testing file creation
tmp_file="tmp_file_check_$$"
# Check if we can write to the current directory by creating a temporary file
if ! touch "$tmp_file" > /dev/null 2>&1; then
if [ "$is_root" -eq 0 ]; then
ask_user "Cannot create file in the current directory. Try downloading as root? [y/N] "
case "$response" in
[yY]|[yY][eE][sS])
remove_command="sudo rm -f"
if ! sudo curl -fsSL "$url" -o "$pkg_name"; then
error_exit "Failed to download $pkg_name: $?"
fi
;;
*)
error_exit "Cannot proceed without file creation rights."
;;
esac
else
log "Cannot create file in the current directory."
error_exit "Cannot proceed without file creation rights."
fi
else
# Cleanup the temporary file
rm "$tmp_file"
if ! curl -fsSL "$url" -o "$pkg_name"; then
error_exit "Failed to download $pkg_name: $?"
fi
fi
log "TrustTunnel Client package has been downloaded successfully"
}
report_success() {
echo
echo "TrustTunnel Client has been installed successfully!"
echo
echo "You may want to configure the client first, see 'cd ${output_dir} && ./setup_wizard -h'"
echo "The client executable is located at '${output_dir}/trusttunnel_client'"
echo
}
# Entrypoint
output_dir=''
verbose='1'
cpu=''
os=''
version='1.0.49'
uninstall='0'
remove_command="rm -f"
is_root='0'
archive_path=''
auto_answer=''
need_download='1'
parse_opts "$@"
configure
download
check_package
handle_existing
unpack
report_success
verify_hint