-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbash_funcs
2106 lines (1878 loc) · 62 KB
/
bash_funcs
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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# shellcheck disable=SC2034
#
# This file must be sourced.
#
# Collection of Bash functions
# Made by Jiab77
#
# These Bash functions can be included in any scripts
# or in your current Bash session that way:
#
# [[ -r /path/to/bash_funcs ]] && source /path/to/bash_funcs
#
# You can also the above line in your '~/.bashrc' file.
#
# Some methods have been renamed starting from v0.3.0:
#
# log_err -> err_log
# get_version -> get_self_version
# is_root_or_die -> be_root_or_die
#
# error messages color have been changed too.
#
# Version 0.6.0
# Colors
RED="\033[1;31m"
GREEN="\033[1;32m"
YELLOW="\033[1;33m"
BLUE="\033[1;34m"
PURPLE="\033[1;35m"
CYAN="\033[1;36m"
WHITE="\033[1;37m"
NC="\033[0m"
NL="\n"
TAB="\t"
# Functions
function bye() {
# Usage
if [[ $1 == "-h" || $1 == "--help" ]]; then
echo -e "${NL}Usage: $FUNCNAME [flags] -- Shorthand method for stopping computers"
echo -e "${NL}Flags:"
echo -e " -h | --help${TAB}${TAB}Print this message and exit"
echo
return
fi
# Main
echo -e "${NL}Bye.${NL}"
sudo poweroff
}
function die() {
echo -e "${NL}${RED}[ERROR] ${YELLOW}$*${NC}${NL}" >&2
return 255
}
function err() {
echo -e "${NL}${RED}[ERROR] ${YELLOW}$*${NC}${NL}" >&2
}
function err_log() {
echo -e "${NL}$*${NC}${NL}" >&2
}
function log() {
echo -e "${NL}$*${NC}${NL}"
}
function get_url() {
# Config
local QUIET_MODE=false
local WRAPPER
# Usage
if [[ $1 == "-h" || $1 == "--help" ]]; then
echo -e "${NL}Usage: $FUNCNAME [flags] <url> -- Open URLs with 'curl' or 'wget'."
echo -e "${NL}Flags:"
echo -e " -h | --help${TAB}Print this message and exit"
echo -e " -q | --quiet${TAB}Enable quiet mode"
echo
return
fi
# Flags
if [[ $1 == "-q" || $1 == "--quiet" ]]; then
QUIET_MODE=true
shift
fi
# Init
[[ -n $(command -v curl 2>/dev/null) ]] && WRAPPER="curl"
[[ -n $(command -v wget 2>/dev/null) && -z $WRAPPER ]] && WRAPPER="wget"
[[ -z $WRAPPER ]] && die "Unable to find proper backend. Please install 'curl' or 'wget' and try again."
# Main
if [[ $QUIET_MODE == true && $WRAPPER == "curl" ]]; then
$WRAPPER -s "$@"
elif [[ $QUIET_MODE == true && $WRAPPER == "wget" ]]; then
$WRAPPER -q "$@"
else
$WRAPPER "$@"
fi
}
function get_self_path() {
local FILE_PATH
[[ -n "${BASH_SOURCE[0]}" ]] && FILE_PATH="${BASH_SOURCE[0]}" || FILE_PATH="$0"
if [[ -n "$FILE_PATH" ]]; then
echo -n "$FILE_PATH"
else
die "Could not get self path."
fi
}
function get_self_version() {
local FILE_PATH ; FILE_PATH="$(get_self_path)"
if [[ -n $(command -v awk 2>/dev/null) ]]; then
grep -m1 "# Version" "$FILE_PATH" | awk '{ print $3 }'
else
grep -m1 "# Version" "$FILE_PATH" | cut -d" " -f3
fi
}
function show_self_header() {
echo -e "${NL}Collection of Bash functions"
}
function show_self_usage() {
local FILE_PATH ; FILE_PATH="$(get_self_path)"
echo -e "${NL}Usage: $FUNCNAME [flags] -- Collection of Bash functions"
echo -e "${NL}Flags:"
echo -e " -h | --help${TAB}${TAB}Print this message and exit"
echo -e " -c | --changelog${TAB}Print latest changes and exit"
echo -e " -l | --list${TAB}${TAB}Print list of included functions and exit"
echo -e " -v | --version${TAB}Print file version and exit"
echo -e " -i | --install${TAB}Self install in '.bashrc' and exit"
echo
return
}
function show_self_functions() {
local FILE_PATH ; FILE_PATH="$(get_self_path)"
local INDEX=0
show_self_header
echo -e "${NL}Gathering functions from '$(realpath "$FILE_PATH")'...${NL}"
for F in $(grep -E 'function|\(\)' "$FILE_PATH" | grep -v "\$1" | tail -n+3 | sort | cut -d" " -f2); do
echo "$((++INDEX)). $F"
done
echo -e "${NL}Done.${NL}"
return
}
function show_self_changes() {
local FILE_PATH ; FILE_PATH="$(get_self_path)"
show_self_header
echo -e "${NL}Loading 'git' journal...${NL}"
if [[ -d "$FILE_PATH/.git" && -n $(command -v git 2>/dev/null) ]]; then
git log
else
die "You must be in the project folder and must have 'git' installed to run this function.${NL}"
fi
}
function show_self_version() {
show_self_header
echo -e "${NL}Version: $(get_self_version)${NL}"
return
}
function show_self_loading() {
local FILE_PATH ; FILE_PATH="$(get_self_path)"
echo -e "${NL}Loaded '$(basename "$FILE_PATH")' file"
echo -e " - From: $(dirname "$(realpath "$FILE_PATH")")"
echo -e " - Version: $(get_self_version)${NL}"
}
function self_install() {
local BASHRC_PATH="$HOME/.bashrc"
local FILE_PATH ; FILE_PATH="$(get_self_path)"
echo -ne "${NL}${WHITE}Initializing '${PURPLE}self_install${WHITE}'..."
if [[ $(grep -c "$FILE_PATH" "$BASHRC_PATH") -eq 0 ]]; then
{
echo -e "\n# Load 'bash_funcs' function file"
echo "[[ -r $(realpath "$FILE_PATH") ]] && source $(realpath "$FILE_PATH")"
echo "[[ -n show_self_loading ]] && show_self_loading 2>/dev/null"
} >> "$BASHRC_PATH"
if [[ $(grep -c "$(realpath "$FILE_PATH")" "$BASHRC_PATH") -eq 1 ]]; then
echo -e " ${GREEN}done${NC}${NL}"
else
echo -e " ${RED}failed${NC}${NL}"
return 1
fi
else
echo ; die "Already installed."
fi
}
function show_diff() {
local BIN_DIFF ; BIN_DIFF=$(command -v diff 2>/dev/null)
local COLS ; COLS=$(tput cols)
local SCREEN_SIZE=$((COLS-10))
# Usage
if [[ $# -eq 0 || $1 == "-h" || $1 == "--help" ]]; then
echo -e "${NL}Usage: $FUNCNAME [flags] <old file> <new file> - Show differences between two files${NL}"
return
fi
# Checks
[[ -z $BIN_DIFF ]] && die "You must have 'diff' installed to run this script."
# Args
local OLD_FILE="$1"
local NEW_FILE="$2"
# Main
diff -s -y -W $SCREEN_SIZE --suppress-common-lines --color "$OLD_FILE" "$NEW_FILE"
}
function show_errors() {
# Config
local MAX_OUTPUT_LINES=2000
# Internals
local BIN_JCTL ; BIN_JCTL=$(command -v journalctl 2>/dev/null)
# Usage
if [[ $1 == "-h" || $1 == "--help" ]]; then
echo -e "${NL}Usage: $FUNCNAME [flags] -- Show system logs using 'journalctl' from systemd."
echo -e "${NL}Flags:"
echo -e " -h | --help${TAB}Print this message and exit"
echo
return
fi
# Checks
[[ -z $BIN_JCTL ]] && die "You must have 'journalctl' installed to run this script."
# Main
journalctl -fn "$MAX_OUTPUT_LINES" -p4 -b
}
function show_logs() {
# Config
local MAX_OUTPUT_LINES=2000
# Internals
local BIN_JCTL ; BIN_JCTL=$(command -v journalctl 2>/dev/null)
# Usage
if [[ $1 == "-h" || $1 == "--help" ]]; then
echo -e "${NL}Usage: $FUNCNAME [flags] -- Show system logs using 'journalctl' from systemd."
echo -e "${NL}Flags:"
echo -e " -h | --help${TAB}Print this message and exit"
echo
return
fi
# Checks
[[ -z $BIN_JCTL ]] && die "You must have 'journalctl' installed to run this script."
# Main
journalctl -fn "$MAX_OUTPUT_LINES" -b
}
function show_kernel_logs() {
# Config
local DEFAULT_ARGS="-wH"
# Internals
local BIN_DMESG ; BIN_DMESG=$(command -v dmesg 2>/dev/null)
# Usage
if [[ $1 == "-h" || $1 == "--help" ]]; then
echo -e "${NL}Usage: $FUNCNAME [flags] -- Show kernel logs using 'dmesg' command."
echo -e "${NL}Flags:"
echo -e " -h | --help${TAB}Print this message and exit"
echo
return
fi
# Checks
[[ -z $BIN_DMESG ]] && die "You must have 'dmesg' installed to run this script."
# Main
sudo dmesg "$DEFAULT_ARGS"
}
function show_version() {
echo -e "${NL}Version: $(get_self_version)${NL}"
}
function compare_version() {
printf '%s\n' "$@" | sort -C -V
}
function set_console_title() {
local TITLE ; TITLE="$1"
echo -ne "\033]0;$TITLE\007"
}
# shellcheck disable=SC2120
function get_env() {
if [[ $# -eq 1 ]]; then
sort <(printenv) | grep -i "$1"
else
sort <(printenv)
fi
}
function set_env() {
if [[ $# -eq 2 ]]; then
export "$1"="$2"
else
return 1
fi
}
# Thanks 'fonciarz" from THC group for suggesting
# to check for 'noexec' mount option before trying
# to run download scripts.
function check_shm_noexec() {
mount | grep /dev/shm | grep -c noexec
}
function check_device_temp() {
if [[ $1 -le 30 ]]; then
echo -e "${CYAN}cooling down${NC}"
elif [[ $1 -le 60 ]]; then
echo -e "${GREEN}all good${NC}"
elif [[ $1 -lt 80 && $1 -gt 60 ]]; then
echo -e "${YELLOW}getting hot${NC}"
elif [[ $1 -ge 80 ]]; then
echo -e "${RED}critical${NC}"
else
echo -e "${PURPLE}undefined${NC}"
fi
}
# shellcheck disable=SC2012
function get_thermal_zones_count() {
local TZ_COUNT
TZ_COUNT=$(ls -1d /sys/class/thermal/thermal_zone* | wc -l)
echo -n $((TZ_COUNT-1))
}
# shellcheck disable=SC2013
function get_device_temp() {
if [[ $(printenv | grep -ci android) -ne 0 && $(printenv | grep -ci termux) -ne 0 ]]; then
if [[ -n $(command -v termux-battery-status 2>/dev/null) && -n $(command -v jq 2>/dev/null) ]]; then
termux-battery-status 2>/dev/null | jq -rc .temperature | cut -d"." -f1
fi
else
for T in $(cat /sys/class/thermal/thermal_zone*/temp); do
echo $((T/1000))
done | sort -nr | head -n1
fi
}
function show_device_temp() {
for N in $(seq 0 1 "$(get_thermal_zones_count)") ; do
if [[ -r "/sys/class/thermal/thermal_zone${N}/type" ]]; then
echo "$(cat /sys/class/thermal/thermal_zone"${N}"/type) - $(($(cat /sys/class/thermal/thermal_zone"${N}"/temp)/1000))°C - $(check_device_temp $(($(cat /sys/class/thermal/thermal_zone"${N}"/temp)/1000)))"
fi
done
}
function watch_device_temp() {
watch -c -n1 'bash -c "source '"${BASH_SOURCE[0]}"' ; show_device_temp"'
}
function watch_tcp_congestion() {
watch -c -n1 'ss -tin | grep -iE "bbr|cubic" --color=always'
}
function get_hostname_from_session() {
if [[ -n $SESSION_MANAGER ]]; then
cut -d"/" -s -f2 <<< "$SESSION_MANAGER" | cut -d":" -f1
fi
return 1
}
function get_shell_id() {
[[ -n $PANTHEON_TERMINAL_ID ]] && echo -n "$PANTHEON_TERMINAL_ID"
return 1
}
function get_shell_level() {
[[ -n $SHLVL ]] && echo -n $SHLVL
return 1
}
function get_user_desktop() {
if [[ -n $XDG_SESSION_DESKTOP ]]; then
echo -n "$XDG_SESSION_DESKTOP"
elif [[ -n $XDG_CURRENT_DESKTOP ]]; then
echo -n "${XDG_CURRENT_DESKTOP,,}"
elif [[ -n $DESKTOP_SESSION ]]; then
echo -n "$DESKTOP_SESSION"
elif [[ -n $GDMSESSION ]]; then
echo -n "$GDMSESSION"
else
return 1
fi
}
function is_wayland() {
[[ -n $XDG_SESSION_TYPE && $XDG_SESSION_TYPE == "wayland" ]] && return 0
[[ -n $WAYLAND_DISPLAY ]] && return 0
return 1
}
function is_xorg() {
[[ -n $XDG_SESSION_TYPE && $XDG_SESSION_TYPE == "x11" ]] && return 0
[[ -n $DISPLAY ]] && return 0
return 1
}
function is_termux() {
[[ $(printenv | grep -ci "termux") -ne 0 ]] && return 0
return 1
}
function is_root() {
[[ $(id -u) -eq 0 ]] && return 0
return 1
}
function be_root_or_die() {
[[ $(id -u) -ne 0 ]] && die "You must run this script as root or with '${YELLOW}sudo${RED}'."
}
function clean_str() {
[[ $# -eq 0 ]] && die "[$FUNCNAME] Too few arguments."
local RAW_STR ; RAW_STR="$1"
local NEW_STR ; NEW_STR="${RAW_STR//\'/}"
echo -n "$NEW_STR"
}
function str_replace() {
[[ $# -gt 3 ]] && die "[$FUNCNAME] Too many arguments."
[[ $# -lt 3 ]] && die "[$FUNCNAME] Too few arguments."
local SEARCH ; SEARCH="$1"
local REPLACE ; REPLACE="$2"
local SUBJECT ; SUBJECT="$3"
echo -n "${SUBJECT//$SEARCH/$REPLACE}"
}
function str_to_lower() {
[[ $# -eq 0 ]] && die "[$FUNCNAME] Too few arguments."
local RAW_STR ; RAW_STR="$1"
local NEW_STR ; NEW_STR="${RAW_STR,,}"
echo -n "$NEW_STR"
}
function str_to_upper() {
[[ $# -eq 0 ]] && die "[$FUNCNAME] Too few arguments."
local RAW_STR ; RAW_STR="$1"
local NEW_STR ; NEW_STR="${RAW_STR^^}"
echo -n "$NEW_STR"
}
function str_lower_to_cap() {
[[ $# -eq 0 ]] && die "[$FUNCNAME] Too few arguments."
local RAW_STR ; RAW_STR="$1"
local NEW_STR ; NEW_STR="${RAW_STR,}"
echo -n "$NEW_STR"
}
function str_upper_to_cap() {
[[ $# -eq 0 ]] && die "[$FUNCNAME] Too few arguments."
local RAW_STR ; RAW_STR="$1"
local NEW_STR ; NEW_STR="${RAW_STR^}"
echo -n "$NEW_STR"
}
function set_script_dir() {
[[ -z $SCRIPT_DIR ]] && SCRIPT_DIR="$(dirname "$0")"
}
function set_script_file() {
[[ -z $SCRIPT_FILE ]] && SCRIPT_FILE="$(basename "$0")"
}
function set_script_name() {
[[ -z $SCRIPT_FILE ]] && set_script_file
[[ -n $SCRIPT_FILE && -z $SCRIPT_NAME ]] && SCRIPT_NAME="${SCRIPT_FILE/.sh/}"
}
function get_dir_name() {
[[ -z $SCRIPT_DIR ]] && SCRIPT_DIR="$(dirname "$0")"
echo -n "$(basename "$SCRIPT_DIR")"
}
function get_script_name() {
if [[ -n $SCRIPT_FILE ]]; then
echo -n "${SCRIPT_FILE/.sh/}"
else
SCRIPT_FILE="$(basename "$0")"
echo -n "${SCRIPT_FILE/.sh/}"
fi
}
function set_config_file() {
[[ -z $SCRIPT_NAME ]] && set_script_name
[[ -z $CONFIG_FILE ]] && CONFIG_FILE="$(get_script_name).conf"
}
function load_xdg_defs() {
if [[ -r ~/.config/user-dirs.dirs ]]; then
# shellcheck source=/dev/null
source ~/.config/user-dirs.dirs
fi
[[ -z $XDG_CONFIG_HOME ]] && XDG_CONFIG_HOME="$HOME/.config"
}
function load_config_file() {
[[ -z $SCRIPT_NAME ]] && set_script_name
[[ -z $CONFIG_FILE ]] && set_config_file
if [[ -r "$XDG_CONFIG_HOME/$CONFIG_FILE" ]]; then
# shellcheck source=/dev/null
source "$XDG_CONFIG_HOME/$CONFIG_FILE"
elif [[ -r "$XDG_CONFIG_HOME/$(get_dir_name)/$CONFIG_FILE" ]]; then
# shellcheck source=/dev/null
source "$XDG_CONFIG_HOME/$(get_dir_name)/$CONFIG_FILE"
elif [[ -r "$SCRIPT_DIR/$CONFIG_FILE" ]]; then
# shellcheck source=/dev/null
source "$SCRIPT_DIR/$CONFIG_FILE"
fi
}
function get_mpv_version() {
if [[ -n $(command -v awk 2>/dev/null) ]]; then
mpv --version | head -n1 | awk '{ print $2 }'
else
mpv --version | head -n1 | cut -d" " -f2
fi
}
function get_mpv_mpris_lib() {
local MPV_MPRIS_PATH
[[ -z $XDG_CONFIG_HOME ]] && load_xdg_defs
if [[ -r "$XDG_CONFIG_HOME/mpv/scripts/mpris.so" ]]; then
MPV_MPRIS_PATH="$XDG_CONFIG_HOME/mpv/scripts/mpris.so"
elif [[ -r /etc/mpv/scripts/mpris.so ]]; then
MPV_MPRIS_PATH=/etc/mpv/scripts/mpris.so
else
MPV_MPRIS_PATH=
fi
echo -n "$MPV_MPRIS_PATH"
}
# shellcheck disable=SC2015
function screen_lock() {
# Big thanks to Skyper from THC group for suggesting to fallback on 'ssh -M'
# to solve the issue where it was necessary to request user password twice
#
# Also thanks to Matthew from THC group for suggesting to reduce SSH backend checks
# Usage
if [[ $1 == "-h" || $1 == "--help" ]]; then
echo -e "${NL}Usage: $FUNCNAME [flags] -- Lock computer screen remotely."
echo -e "${NL}Flags:"
echo -e " -h | --help${TAB}Print this message and exit"
echo
return
fi
# Main
if [[ $# -eq 0 ]]; then
if [[ -z $(command -v loginctl 2>/dev/null) ]]; then
die "You must have 'loginctl' installed to run this function."
fi
local CURRENT_SESS_ID ; CURRENT_SESS_ID=$(loginctl user-status | grep -m1 Session | cut -d"*" -f2 | cut -d" " -f1)
err_log "Found active session id: $CURRENT_SESS_ID"
[[ -n $CURRENT_SESS_ID ]] && loginctl lock-session "$CURRENT_SESS_ID" && err_log "Locked." || err_log "Failed."
return $?
else
if [[ -z $(command -v ssh 2>/dev/null) && -z $(command -v hpnssh 2>/dev/null) ]]; then
die "You must have 'ssh' or 'hpnssh' installed to run this function."
fi
err_log "Initializing connection multiplexing to $1..."
local REMOTE_SESS_QUEUE
local REMOTE_SESS_ID
local SSH_BACKEND
if [[ -n $(command -v hpnssh 2>/dev/null) ]]; then
SSH_BACKEND=hpnssh
elif [[ -z $SSH_BACKEND && -n $(command -v ssh 2>/dev/null) ]]; then
SSH_BACKEND=ssh
else
die "Could not find proper ssh backend."
fi
if [[ $SSH_BACKEND == "hpnssh" ]]; then
REMOTE_SESS_QUEUE=$(hpnssh -p22 -M -S "/tmp/hpnssh-%r@%h:%p" -fnNT "$1" ; echo $$)
REMOTE_SESS_ID=$(hpnssh -p22 -S "/tmp/hpnssh-%r@%h:%p" "$1" loginctl user-status | grep -m1 Session | cut -d"*" -f2 | cut -d" " -f1)
else
REMOTE_SESS_QUEUE=$(ssh -M -S "/tmp/ssh-%r@%h:%p" -fnNT "$1" ; echo $$)
REMOTE_SESS_ID=$(ssh -S "/tmp/ssh-%r@%h:%p" "$1" loginctl user-status | grep -m1 Session | cut -d"*" -f2 | cut -d" " -f1)
fi
err_log "Found remote session id: $REMOTE_SESS_ID"
if [[ -n $REMOTE_SESS_QUEUE && -n $REMOTE_SESS_ID ]]; then
if [[ $SSH_BACKEND == "hpnssh" ]]; then
hpnssh -p22 -S "/tmp/hpnssh-%r@%h:%p" "$1" loginctl lock-session "$REMOTE_SESS_ID" && err_log "Locked." || err_log "Failed."
hpnssh -p22 -O "exit" -S "/tmp/hpnssh-%r@%h:%p" "$1" && err_log "Connection queue removed." || err_log "Unable to remove connection queue."
return $?
else
ssh -S "/tmp/ssh-%r@%h:%p" "$1" loginctl lock-session "$REMOTE_SESS_ID" && err_log "Locked" || err_log "Failed"
ssh -O "exit" -S "/tmp/ssh-%r@%h:%p" "$1" && err_log "Connection queue removed." || err_log "Unable to remove connection queue."
return $?
fi
fi
fi
return 1
}
# shellcheck disable=SC2015
function screen_unlock() {
# Big thanks to Skyper from THC group for suggesting to fallback on 'ssh -M'
# to solve the issue where it was necessary to request user password twice
#
# Also thanks to Matthew from THC group for suggesting to reduce SSH backend checks
# Usage
if [[ $1 == "-h" || $1 == "--help" ]]; then
echo -e "${NL}Usage: $FUNCNAME [flags] -- Unlock computer screen remotely."
echo -e "${NL}Flags:"
echo -e " -h | --help${TAB}Print this message and exit"
echo
return
fi
# Main
if [[ $# -eq 0 ]]; then
if [[ -z $(command -v loginctl 2>/dev/null) ]]; then
die "You must have 'loginctl' installed to run this function."
fi
local CURRENT_SESS_ID ; CURRENT_SESS_ID=$(loginctl user-status | grep -m1 Session | cut -d"*" -f2 | cut -d" " -f1)
err_log "Found locked session id: $CURRENT_SESS_ID"
[[ -n $CURRENT_SESS_ID ]] && loginctl unlock-session "$CURRENT_SESS_ID" && err_log "Unlocked." || err_log "Failed."
return $?
else
if [[ -z $(command -v ssh 2>/dev/null) && -z $(command -v hpnssh 2>/dev/null) ]]; then
die "You must have 'ssh' or 'hpnssh' installed to run this function."
fi
err_log "Initializing connection multiplexing to $1..."
local REMOTE_SESS_QUEUE
local REMOTE_SESS_ID
local SSH_BACKEND
if [[ -n $(command -v hpnssh 2>/dev/null) ]]; then
SSH_BACKEND=hpnssh
elif [[ -z $SSH_BACKEND && -n $(command -v ssh 2>/dev/null) ]]; then
SSH_BACKEND=ssh
else
die "Could not find proper ssh backend."
fi
if [[ $SSH_BACKEND == "hpnssh" ]]; then
REMOTE_SESS_QUEUE=$(hpnssh -p22 -M -S "/tmp/hpnssh-%r@%h:%p" -fnNT "$1" ; echo $$)
REMOTE_SESS_ID=$(hpnssh -p22 -S "/tmp/hpnssh-%r@%h:%p" "$1" loginctl user-status | grep -m1 Session | cut -d"*" -f2 | cut -d" " -f1)
else
REMOTE_SESS_QUEUE=$(ssh -M -S "/tmp/ssh-%r@%h:%p" -fnNT "$1" ; echo $$)
REMOTE_SESS_ID=$(ssh -S "/tmp/ssh-%r@%h:%p" "$1" loginctl user-status | grep -m1 Session | cut -d"*" -f2 | cut -d" " -f1)
fi
err_log "Found remote session id: $REMOTE_SESS_ID"
if [[ -n $REMOTE_SESS_QUEUE && -n $REMOTE_SESS_ID ]]; then
if [[ $SSH_BACKEND == "hpnssh" ]]; then
hpnssh -p22 -S "/tmp/hpnssh-%r@%h:%p" "$1" loginctl unlock-session "$REMOTE_SESS_ID" && err_log "Unlocked." || err_log "Failed."
hpnssh -p22 -O "exit" -S "/tmp/hpnssh-%r@%h:%p" "$1" && err_log "Connection queue removed." || err_log "Unable to remove connection queue."
return $?
else
ssh -S "/tmp/ssh-%r@%h:%p" "$1" loginctl unlock-session "$REMOTE_SESS_ID" && err_log "Unlocked." || err_log "Failed."
ssh -O "exit" -S "/tmp/ssh-%r@%h:%p" "$1" && err_log "Connection queue removed." || err_log "Unable to remove connection queue."
return $?
fi
fi
fi
return 1
}
function find_leaks() {
local SEARCH_BACKEND
local DEFAULT_SEARCH
DEFAULT_SEARCH="api|api_id|api_hash|api_key|user|username|pass|password|creds|credential|token|oauth_token|key|client_api_key|server_api_key|site_key|secret_key|server_key|user_key|secret"
# Usage
if [[ $1 == "-h" || $1 == "--help" ]]; then
echo -e "${NL}Usage: $FUNCNAME [flags] <folder> -- Find leaks in given folder."
echo -e "${NL}Flags:"
echo -e " -h | --help${TAB}Print this message and exit"
echo
return
fi
# Main
if [[ -n $(command -v rg 2>/dev/null) ]]; then
SEARCH_BACKEND="rg"
elif [[ -z $SEARCH_BACKEND && -n $(command -v grep 2>/dev/null) ]]; then
SEARCH_BACKEND="grep"
else
err_log "Could not find proper search backend."
return 1
fi
if [[ $# -eq 1 ]]; then
if [[ $SEARCH_BACKEND == "rg" ]]; then
rg --color always -j"$(nproc)" -nwie "$DEFAULT_SEARCH" --iglob "$1"
else
grep --color=always -nR -wiE "$DEFAULT_SEARCH" --include "$1"
fi
return $?
elif [[ $# -eq 2 ]]; then
if [[ $SEARCH_BACKEND == "rg" ]]; then
rg --color always -j"$(nproc)" -nwie "$DEFAULT_SEARCH" --iglob "$1" "$2"
else
grep --color=always -nR -wiE "$DEFAULT_SEARCH" --include "$1" "$2"
fi
return $?
else
err_log "Too many arguments."
return 1
fi
}
function hex_decode() {
# Config
local INPUT_STRING
local OUTPUT_STRING
# Usage
if [[ $1 == "-h" || $1 == "--help" ]]; then
echo -e "${NL}Usage: $FUNCNAME [flags] <input> -- Decode hex encoded string / file."
echo -e "${NL}Flags:"
echo -e " -h | --help${TAB}Print this message and exit"
echo
return
fi
# Init
INPUT_STRING="$1"
# Checks
[[ $# -eq 0 ]] && die "Missing input string or file."
[[ -z $INPUT_STRING ]] && die "Empty input string or file."
# Main
if [[ -r "$INPUT_STRING" ]]; then
OUTPUT_STRING="$(echo -ne "$(cat "$INPUT_STRING")")"
else
OUTPUT_STRING="$(echo -ne "$INPUT_STRING")"
fi
if [[ -z $OUTPUT_STRING ]]; then
die "Could not decode input string or file."
else
echo -e "$OUTPUT_STRING"
fi
}
function base64_decode() {
# Config
local INPUT_STRING
local OUTPUT_STRING
# Usage
if [[ $1 == "-h" || $1 == "--help" ]]; then
echo -e "${NL}Usage: $FUNCNAME [flags] <input> -- Decode base64 encoded string / file."
echo -e "${NL}Flags:"
echo -e " -h | --help${TAB}Print this message and exit"
echo
return
fi
# Init
INPUT_STRING="$1"
# Checks
[[ $# -eq 0 ]] && die "Missing input string or file."
[[ -z $INPUT_STRING ]] && die "Empty input string or file."
# Main
if [[ -r "$INPUT_STRING" ]]; then
OUTPUT_STRING="$(cat "$INPUT_STRING" | base64 -d -)"
else
OUTPUT_STRING="$(echo -ne "$INPUT_STRING" | base64 -d -)"
fi
if [[ -z $OUTPUT_STRING ]]; then
die "Could not decode input string or file."
else
echo -e "$OUTPUT_STRING"
fi
}
function deobfuscate() {
# Config
local INPUT_STRING
local OUTPUT_STRING
# Usage
if [[ $1 == "-h" || $1 == "--help" ]]; then
echo -e "${NL}Usage: $FUNCNAME [flags] <input> -- Decode obfuscated string / file."
echo -e "${NL}Flags:"
echo -e " -h | --help${TAB}Print this message and exit"
echo
return
fi
# Init
INPUT_STRING="$1"
# Checks
[[ $# -eq 0 ]] && die "Missing input string or file."
[[ -z $INPUT_STRING ]] && die "Empty input string or file."
# Main
OUTPUT_STRING_B64="$(base64_decode "$INPUT_STRING")"
[[ -z $OUTPUT_STRING_B64 ]] && die "Could not decode input string or file."
OUTPUT_STRING_HEX="$(hex_decode "$OUTPUT_STRING_B64")"
[[ -z $OUTPUT_STRING_HEX ]] && die "Could not process input string or file."
echo -e "$OUTPUT_STRING_HEX"
}
function sf_proxy() {
# Thanks to the Segfault project created by the THC group
# Config
local USER_SECRET
local PROXY_ADDR="127.0.0.1:1080"
local SERVER_ADDR="root@segfault.net"
# Usage
if [[ $1 == "-h" || $1 == "--help" ]]; then
echo -e "${NL}Usage: $FUNCNAME [flags] [secret] -- Create web proxy using the SegFault project."
echo -e "${NL}Flags:"
echo -e " -h | --help${TAB}Print this message and exit"
echo
return
fi
# Check for user secret value
[[ -n $1 ]] && USER_SECRET="$1"
# Check for possibly stored secret file
[[ -z $USER_SECRET && -r $HOME/.sf-secret ]] && USER_SECRET=$(cat "$HOME/.sf-secret")
# Main
if [[ -n $USER_SECRET ]]; then
echo -e "${NL}Connecting... [Using given secret value]${NL}"
ssh -o StrictHostKeyChecking=no -o PubkeyAuthentication=no -o PreferredAuthentications=password -o "SetEnv SECRET=$USER_SECRET" -D "$PROXY_ADDR" "$SERVER_ADDR"
else
echo -e "${NL}Connecting... [Password is: segfault]${NL}"
ssh -o StrictHostKeyChecking=no -o PubkeyAuthentication=no -o PreferredAuthentications=password -D "$PROXY_ADDR" "$SERVER_ADDR"
fi
}
function sf_download() {
# Thanks to the Segfault project created by the THC group
# Internals
local BIN_CURL ; BIN_CURL=$(command -v curl 2>/dev/null)
# Usage
if [[ $1 == "-h" || $1 == "--help" ]]; then
echo -e "${NL}Usage: $FUNCNAME [flags] <url> -- Download given URL withing SegFault web proxy."
echo -e "${NL}Flags:"
echo -e " -h | --help${TAB}Print this message and exit"
echo
return
fi
# Checks
[[ $# -eq 0 ]] && die "Missing input URL."
[[ -z $BIN_CURL ]] && die "Please, install 'curl' and try again."
# Main
if [[ $(ss -tunl | grep -c 1080) -ne 0 ]]; then
curl -fsSL -x socks5h://0 "$@"
else
die "Please run the 'sf_proxy' command in another terminal and try again."
fi
}
function sf_ip() {
# Thanks to the Segfault project created by the THC group
# Internals
local BIN_CURL ; BIN_CURL=$(command -v curl 2>/dev/null)
# Usage
if [[ $1 == "-h" || $1 == "--help" ]]; then
echo -e "${NL}Usage: $FUNCNAME [flags] -- Get IP address from SegFault web proxy."
echo -e "${NL}Flags:"
echo -e " -h | --help${TAB}Print this message and exit"
echo
return
fi
# Checks
[[ -z $BIN_CURL ]] && die "Please, install 'curl' and try again."
# Main
if [[ $(ss -tunl | grep -c 1080) -ne 0 ]]; then
if [[ -n $(command -v jq 2>/dev/null) ]]; then
curl -sSL -x socks5h://0 ipinfo.io | jq .
else
curl -sSL -x socks5h://0 ipinfo.io ; echo
fi
else
die "Please run the 'sf_proxy' command in another terminal and try again."
fi
}
function get_ip() {
# Internals
# local BIN_CURL ; BIN_CURL=$(command -v curl 2>/dev/null)
# Usage
if [[ $1 == "-h" || $1 == "--help" ]]; then
echo -e "${NL}Usage: $FUNCNAME [flags] -- Get IP address with the IPInfo service."
echo -e "${NL}Flags:"
echo -e " -h | --help${TAB}Print this message and exit"
echo
return
fi
# Checks
# [[ -z $BIN_CURL ]] && die "Please, install 'curl' and try again."
# Main
if [[ -n $(command -v jq 2>/dev/null) ]]; then
# curl -sSL ipinfo.io | jq .
get_url -q ipinfo.io | jq .
else
# curl -sSL ipinfo.io ; echo
get_url ipinfo.io ; echo
fi
}
function my_ip() {
# Show device ip address using several tricks
# from the THC hacking group
# Config
local local_ip
local external_ip
local json_data
# Internals
local BIN_JQ ; BIN_JQ=$(command -v jq 2>/dev/null)
# Usage
if [[ $1 == "-h" || $1 == "--help" ]]; then
echo -e "${NL}Usage: $FUNCNAME [flags] -- Show device IP addresses."
echo -e "${NL}Flags:"
echo -e " -h | --help${TAB}Print this message and exit"
echo -e " -j | --json${TAB}Return data in JSON format"
echo
return
fi
# Main
local_ip="$(ip -br a | grep -i "up" | awk '{ print $3 }')"
if [[ -n $(command -v dig 2>/dev/null) ]]; then
external_ip="$(dig +short myip.opendns.com @resolver1.opendns.com)"
elif [[ -n $(command -v host 2>/dev/null) ]]; then
external_ip="$(host myip.opendns.com resolver1.opendns.com)"
elif [[ -n $(command -v curl 2>/dev/null) ]]; then
#external_ip="$(curl -sSL ifconfig.io/ip)"
external_ip="$(curl -sSL ifconfig.me)"
else
external_ip="$(hostname -i | cut -d' ' -f3)"
fi
if [[ $1 == "-j" || $1 == "--json" ]]; then
json_data='{ "local":"'$local_ip'", "external":"'$external_ip'" }'
[[ -n $BIN_JQ ]] && echo "$json_data" | jq . || echo "$json_data"
else
echo "My Local IP address: ${local_ip}"
echo "My WAN/Public IP address: ${external_ip}"
fi
}
function bench() {
# Config
local BENCH_URL="bench.sh"
# Internals
local BIN_CURL ; BIN_CURL=$(command -v curl 2>/dev/null)
local BIN_WGET ; BIN_WGET=$(command -v wget 2>/dev/null)
# Usage
if [[ $1 == "-h" || $1 == "--help" ]]; then
echo -e "${NL}Usage: $FUNCNAME [flags] -- Benchmark device using the 'bench.sh' service."
echo -e "${NL}Flags:"
echo -e " -h | --help${TAB}Print this message and exit"
echo
return
fi
# Main
if [[ -n $BIN_CURL ]]; then
curl -Lso- "$BENCH_URL" | bash
elif [[ -n $BIN_WGET ]]; then
wget -qO- "$BENCH_URL" | bash
else
die "Please install 'curl' or 'wget' and try again."
fi
}
function updater() {
# Config
local DEBUG_MODE=true
local UPDATER_URL="https://raw.githubusercontent.com/Jiab77/updater/main/updater.sh"
local UPDATER_PATH="/dev/shm/updater.sh"
# Internals
local BIN_CURL ; BIN_CURL=$(command -v curl 2>/dev/null)
local BIN_WGET ; BIN_WGET=$(command -v wget 2>/dev/null)
# Checks
if [[ $(is_termux ; echo $?) -eq 0 ]]; then
UPDATER_PATH="/tmp/updater.sh"
else
[[ $(check_shm_noexec) -eq 1 ]] && die "Mountpoint '/dev/shm' has been mounted with 'noexec' option."
fi
# Get script from repo
if [[ -n $BIN_CURL ]]; then
curl -sSL "$UPDATER_URL" > "$UPDATER_PATH"
elif [[ -n $BIN_WGET ]]; then
wget -qO- "$UPDATER_URL" > "$UPDATER_PATH"