Repository navigation
Expand file tree
/
Copy pathsb.sh
More file actions
2278 lines (2124 loc) · 75.8 KB
/
Copy pathsb.sh
File metadata and controls
2278 lines (2124 loc) · 75.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
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
#!/system/bin/sh
# StevenBlock v1.0 backend.
# Every WebUI request, Action press, install step and boot task runs through this script.
# Public commands print exactly one line of JSON: {"ok":..,"code":"..","message":"..","data":{..}}
# shellcheck disable=SC2016
# (SC2016: the awk programs below are single-quoted on purpose.)
if [ -z "${SB_BB:-}" ]; then
for _bb in /data/adb/magisk/busybox /data/adb/ksu/bin/busybox /data/adb/ap/bin/busybox; do
if [ -x "$_bb" ]; then
SB_BB=$_bb
ASH_STANDALONE=1
export SB_BB ASH_STANDALONE
exec "$_bb" sh "$0" "$@"
fi
done
SB_BB=none
export SB_BB
fi
umask 022
PATH=/system/bin:/system/xbin:/vendor/bin:${PATH:-/bin}
export PATH
SB_VER=v1.0
SB_VCODE=1000
SELF=$0
case $SELF in /*) ;; *) SELF=$(pwd)/$SELF ;; esac
MODDIR=${SB_MODDIR:-${SELF%/*}}
MODROOT=/data/adb/modules
DATA=/data/adb/stevenblock
CFG=$DATA/config
SEQF=$DATA/seq
PENDF=$DATA/reboot.pending
LOGF=$DATA/stevenblock.log
RULESD=$DATA/rules
ALLOWF=$RULESD/allow.list
BLOCKF=$RULESD/block.list
CACHED=$DATA/cache
LISTCACHE=$CACHED/list.domains
METAD=$CACHED/meta
BACKUPD=$DATA/backup
ORIGHOSTS=$BACKUPD/hosts.original
RUND=$DATA/run
TMPD=$DATA/tmp
LOCKD=$RUND/lock
METALOCK=$RUND/meta.lock
OPF=$RUND/op
DLPART=$TMPD/download.part
DLHDR=$TMPD/download.hdr
HOSTSD=$MODDIR/system/etc
HOSTS=$HOSTSD/hosts
PROP=$MODDIR/module.prop
SYS_HOSTS=/system/etc/hosts
INIT_HOSTS=/proc/1/root/system/etc/hosts
INIT_NS=/proc/1/ns/mnt
MOUNT_TARGET=/system/etc/hosts
MOUNTF=$RUND/mount
PORT=9090
IDLE_LIMIT=600
UA="StevenBlock/1.0 (+https://github.com/mikropsoft/StevenBlock)"
LIST_IDS="stevenblack hblock hagezi-light hagezi-normal hagezi-pro hagezi-proplus hagezi-ultimate"
MODE=cli
DL_TOOL=
DL_PIDF=
NL='
'
CR=$(printf '\r')
TAB=$(printf '\t')
# ---------------------------------------------------------------- lists
list_info() {
case $1 in
stevenblack)
L_NAME='StevenBlack Main'; L_GROUP=unified; L_LEVEL=green; L_LEVELTXT=Balanced
L_URL='https://raw.githubusercontent.com/StevenBlack/hosts/refs/heads/master/hosts'
L_DESC='Classic unified list for ads and malware, with very few false positives.' ;;
hblock)
L_NAME='hBlock'; L_GROUP=unified; L_LEVEL=blue; L_LEVELTXT=Mixed
L_URL='https://hblock.molinero.dev/hosts'
L_DESC='Merges dozens of public blocklists into one broad, all-purpose list.' ;;
hagezi-light)
L_NAME='HaGeZi Light'; L_GROUP=hagezi; L_LEVEL=green; L_LEVELTXT=Basic
L_URL='https://raw.githubusercontent.com/hagezi/dns-blocklists-legacy/refs/heads/main/hosts/light.txt'
L_DESC='Basic protection from ads, tracking and telemetry. Built to break nothing.' ;;
hagezi-normal)
L_NAME='HaGeZi Normal'; L_GROUP=hagezi; L_LEVEL=blue; L_LEVELTXT=All-round
L_URL='https://raw.githubusercontent.com/hagezi/dns-blocklists-legacy/refs/heads/main/hosts/multi.txt'
L_DESC='All-round protection from ads, tracking, phishing, malware and scams.' ;;
hagezi-pro)
L_NAME='HaGeZi Pro'; L_GROUP=hagezi; L_LEVEL=yellow; L_LEVELTXT=Extended
L_URL='https://raw.githubusercontent.com/hagezi/dns-blocklists-legacy/refs/heads/main/hosts/pro.txt'
L_DESC='Extended protection with many more trackers. May need an allow rule.' ;;
hagezi-proplus)
L_NAME='HaGeZi Pro++'; L_GROUP=hagezi; L_LEVEL=orange; L_LEVELTXT=Aggressive
L_URL='https://raw.githubusercontent.com/hagezi/dns-blocklists-legacy/refs/heads/main/hosts/pro.plus.txt'
L_DESC='Aggressive blocking. Some legitimate sites or app features may break.' ;;
hagezi-ultimate)
L_NAME='HaGeZi Ultimate'; L_GROUP=hagezi; L_LEVEL=red; L_LEVELTXT=Maximum
L_URL='https://raw.githubusercontent.com/hagezi/dns-blocklists-legacy/refs/heads/main/hosts/ultimate.txt'
L_DESC='The most aggressive list. Can restrict app and website features.' ;;
*) return 1 ;;
esac
return 0
}
# ---------------------------------------------------------------- awk programs
AWK_JESC='
BEGIN { ORS = ""; printf "\"" }
{
if (NR > 1) printf "\\n"
s = $0; o = ""; n = length(s)
for (i = 1; i <= n; i++) {
c = substr(s, i, 1)
if (c == "\\") o = o "\\\\"
else if (c == "\"") o = o "\\\""
else if (c == "\t") o = o " "
else if (c != "\r") o = o c
}
printf "%s", o
}
END { printf "\"" }'
AWK_NORM='
function okdom(d, n, i, L) {
if (length(d) > 253) return 0
if (d !~ /^([a-z0-9_]([a-z0-9_-]*[a-z0-9_])?\.)+[a-z]([a-z0-9-]*[a-z0-9])?$/) return 0
if (length(d) > 63) {
n = split(d, L, ".")
for (i = 1; i <= n; i++) if (length(L[i]) > 63) return 0
}
return 1
}
BEGIN {
SYS["localhost"] = 1; SYS["localhost.localdomain"] = 1; SYS["local"] = 1
SYS["localdomain"] = 1; SYS["broadcasthost"] = 1; SYS["0.0.0.0"] = 1
}
{
sub(/\r$/, "")
c = index($0, "#")
if (c) $0 = substr($0, 1, c - 1)
if (NF == 0) next
if (NF == 1) s = 1
else if ($1 ~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/ || index($1, ":")) s = 2
else next
for (f = s; f <= NF; f++) {
d = tolower($f)
sub(/\.$/, "", d)
if (d in SYS) continue
if (d ~ /^ip6-/) continue
if (!okdom(d)) continue
if (d in seen) continue
seen[d] = 1
print d
n++
}
}
END { print n + 0 > cntf }'
AWK_COMPILE='
BEGIN {
while ((getline l < allowf) > 0) if (l != "") A[l] = 1
close(allowf)
while ((getline l < blockf) > 0) if (l != "" && !(l in A) && !(l in B)) { B[l] = 1; BO[++nb] = l }
close(blockf)
print "# StevenBlock " ver " - systemless hosts file"
print "# List: " name
print "# Source: " url
print "# Managed by StevenBlock. Manual edits are overwritten."
print "# https://github.com/mikropsoft/StevenBlock"
print ""
print "127.0.0.1 localhost"
print "::1 localhost ip6-localhost"
print ""
}
{
if ($0 in A) next
if ($0 in B) inl[$0] = 1
print "0.0.0.0 " $0
n++
}
END {
for (i = 1; i <= nb; i++) if (!(BO[i] in inl)) { print "0.0.0.0 " BO[i]; n++ }
print n + 0 > cntf
}'
AWK_HEAD='
{ l = $0; sub(/\r$/, "", l) }
l !~ /^[ \t]*#/ { next }
{ x = tolower(l) }
cnt == "" && x ~ /(number of (unique )?(domains|entries)|blocked domains|total (domains|entries)|entries)[ \t]*:/ {
v = l; sub(/^[^:]*:[ \t]*/, "", v); gsub(/[,. _]/, "", v)
if (v ~ /^[0-9]+$/) cnt = v
}
dt == "" && x ~ /^[ \t]*#[ \t]*(date|last[ -]modified|last updated|updated)[ \t]*:/ {
v = l; sub(/^[^:]*:[ \t]*/, "", v); dt = v
}
END { print cnt; print dt }'
AWK_DATE='
function civ(y, m, d, era, yoe, doy, doe) {
y -= (m <= 2)
era = int((y >= 0 ? y : y - 399) / 400)
yoe = y - era * 400
doy = int((153 * (m + (m > 2 ? -3 : 9)) + 2) / 5) + d - 1
doe = yoe * 365 + int(yoe / 4) - int(yoe / 100) + doy
return era * 146097 + doe - 719468
}
BEGIN {
split("jan feb mar apr may jun jul aug sep oct nov dec", MN, " ")
for (i = 1; i <= 12; i++) MI[MN[i]] = i
}
NR == 1 {
s = tolower($0)
gsub(/[(),]/, " ", s)
y = 0; m = 0; d = 0; hh = 0; mm = 0; ss = 0; off = 0
if (match(s, /[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]/)) {
y = substr(s, RSTART, 4) + 0; m = substr(s, RSTART + 5, 2) + 0; d = substr(s, RSTART + 8, 2) + 0
s = substr(s, 1, RSTART - 1) " " substr(s, RSTART + 10)
}
n = split(s, T, /[ \t]+/)
for (i = 1; i <= n; i++) {
t = T[i]
sub(/^t/, "", t)
if (t == "") continue
if (t ~ /^[0-9]+:[0-9]+/) {
split(t, P, ":"); hh = P[1] + 0; mm = P[2] + 0; ss = (P[3] ~ /^[0-9]/) ? int(P[3]) : 0
if (match(t, /[+-][0-9][0-9]:?[0-9][0-9]$/)) {
o = substr(t, RSTART); gsub(/:/, "", o)
off = (substr(o, 2, 2) * 3600 + substr(o, 4, 2) * 60) * (substr(o, 1, 1) == "-" ? -1 : 1)
}
}
else if (t ~ /^[0-9][0-9][0-9][0-9]$/ && !y) y = t + 0
else if (t ~ /^[0-9][0-9]?$/ && !d) d = t + 0
else if ((substr(t, 1, 3) in MI) && !m) m = MI[substr(t, 1, 3)]
else if (t ~ /^[+-][0-9][0-9][0-9][0-9]$/) off = (substr(t, 2, 2) * 3600 + substr(t, 4, 2) * 60) * (substr(t, 1, 1) == "-" ? -1 : 1)
}
if (y < 1990 || m < 1 || m > 12 || d < 1 || d > 31 || hh > 23 || mm > 59) exit
printf "%.0f\n", civ(y, m, d) * 86400 + hh * 3600 + mm * 60 + ss - off
exit
}'
AWK_OPJSON='
function esc(s, o, i, c, n) {
o = ""; n = length(s)
for (i = 1; i <= n; i++) {
c = substr(s, i, 1)
if (c == "\\") o = o "\\\\"
else if (c == "\"") o = o "\\\""
else if (c >= " ") o = o c
}
return o
}
{ i = index($0, "="); if (i) O[substr($0, 1, i - 1)] = substr($0, i + 1) }
END {
while ((getline l < hdr) > 0) {
sub(/\r$/, "", l); sub(/^[ \t]+/, "", l); x = tolower(l)
if (x ~ /^http\/[0-9.]+[ \t]+[0-9]/) cl = ""
else if (x ~ /^content-length:/) { v = l; sub(/^[^:]*:[ \t]*/, "", v); if (v ~ /^[0-9]+$/) cl = v }
}
st = O["STATE"]; run = 0
if (st == "running") { if (alive == 1) run = 1; else st = "interrupted" }
ph = O["PHASE"]
tot = O["TOTAL"] + 0
if (!tot && cl != "") tot = cl + 0
can = "false"
if (run && (ph == "prepare" || ph == "check" || ph == "download" || ph == "verify" || ph == "optimize")) can = "true"
printf "{\"running\":%s,\"state\":\"%s\",\"kind\":\"%s\",\"list\":\"%s\",\"phase\":\"%s\",\"code\":\"%s\",\"message\":\"%s\",\"seq\":%.0f,\"bytes\":%.0f,\"total\":%.0f,\"domains\":%.0f,\"reboot\":%s,\"started\":%.0f,\"ended\":%.0f,\"cancellable\":%s,\"cancelling\":%s}", (run ? "true" : "false"), esc(st), esc(O["KIND"]), esc(O["LIST"]), esc(ph), esc(O["CODE"]), esc(O["MSG"]), O["SEQ"] + 0, (run ? bytes + 0 : 0), tot, O["DOMAINS"] + 0, (O["REBOOT"] == "1" ? "true" : "false"), O["START"] + 0, O["END"] + 0, can, (cancelling == 1 && run ? "true" : "false")
}'
AWK_LOGS='
function esc(s, o, i, c, n) {
o = ""; n = length(s)
for (i = 1; i <= n; i++) {
c = substr(s, i, 1)
if (c == "\\") o = o "\\\\"
else if (c == "\"") o = o "\\\""
else if (c >= " ") o = o c
}
return o
}
BEGIN { ORS = ""; printf "[" }
{
i = index($0, "|"); if (!i) next
t = substr($0, 1, i - 1); r = substr($0, i + 1)
j = index(r, "|"); if (!j) next
lv = substr(r, 1, j - 1); m = substr(r, j + 1)
if (t !~ /^[0-9]+$/) next
if (lv != "INFO" && lv != "WARN" && lv != "ERROR") lv = "INFO"
printf "%s{\"t\":%s,\"l\":\"%s\",\"m\":\"%s\"}", (n++ ? "," : ""), t, lv, esc(m)
}
END { printf "]" }'
AWK_QUERY='
BEGIN { for (i = 33; i < 127; i++) H[sprintf("%02x", i)] = sprintf("%c", i) }
function dec(s, o, i, c, h) {
o = ""
for (i = 1; i <= length(s); i++) {
c = substr(s, i, 1)
if (c == "%") {
h = tolower(substr(s, i + 1, 2))
if (!(h in H)) return "\001"
c = H[h]; i += 2
} else if (c == "+") return "\001"
o = o c
}
return o
}
{
n = split($0, P, "&")
for (i = 1; i <= n; i++) {
k = P[i]; v = ""
j = index(k, "=")
if (j) { v = substr(k, j + 1); k = substr(k, 1, j - 1) }
if (k == "t" || k == "c" || k == "a" || k == "b") V[k] = dec(v)
}
}
END { print V["t"]; print V["c"]; print V["a"]; print V["b"] }'
AWK_VALID='
{
d = $0
if (length(d) < 1 || length(d) > 253) exit 1
n = split(d, L, ".")
if (n < 2) exit 1
for (i = 1; i <= n; i++) {
if (length(L[i]) < 1 || length(L[i]) > 63) exit 1
if (i < n) { if (L[i] !~ /^[a-z0-9_]([a-z0-9_-]*[a-z0-9_])?$/) exit 1 }
else if (L[i] !~ /^[a-z][a-z]+$/ && L[i] !~ /^xn--[a-z0-9]([a-z0-9-]*[a-z0-9])?$/) exit 1
}
exit 0
}'
# ---------------------------------------------------------------- helpers
now() { date +%s; }
nap() { sleep "$1" 2>/dev/null || sleep 1; }
is_num() { case $1 in '' | *[!0-9]*) return 1 ;; esac; return 0; }
say() { [ "$MODE" = install ] && printf '%s\n' "$1"; return 0; }
fsize() { _fs=$(stat -c %s "$1" 2>/dev/null); is_num "$_fs" || _fs=0; printf '%s' "$_fs"; }
fmt_num() { printf '%s\n' "${1:-0}" | awk '{ n = $1 ""; o = ""; while (length(n) > 3) { o = "," substr(n, length(n) - 2) o; n = substr(n, 1, length(n) - 3) } print n o }'; }
fmt_mb() { awk -v b="${1:-0}" 'BEGIN { printf "%.1f MB", b / 1048576 }'; }
clean_val() { printf '%s' "$1" | tr -d '\000-\037\134' | cut -c1-200; }
count_lines() { _cn=0; [ -s "$1" ] && _cn=$(grep -c . "$1" 2>/dev/null); is_num "$_cn" || _cn=0; printf '%s' "$_cn"; }
boot_id() { cat /proc/sys/kernel/random/boot_id 2>/dev/null; }
ensure_dirs() {
mkdir -p "$RULESD" "$METAD" "$BACKUPD" "$RUND" "$TMPD" 2>/dev/null
chmod 0700 "$DATA" 2>/dev/null
[ -f "$ALLOWF" ] || : > "$ALLOWF"
[ -f "$BLOCKF" ] || : > "$BLOCKF"
}
log() {
_lg_m=$2
case $_lg_m in *"$NL"* | *"$CR"* | *"$TAB"*) _lg_m=$(printf '%s' "$_lg_m" | tr '\r\n\t' ' ') ;; esac
[ -d "$DATA" ] || return 0
printf '%s|%s|%s\n' "$(date +%s)" "$1" "$_lg_m" >> "$LOGF" 2>/dev/null || return 0
_lg_n=$(wc -l < "$LOGF" 2>/dev/null)
if is_num "$_lg_n" && [ "$_lg_n" -gt 500 ]; then
tail -n 500 "$LOGF" > "$LOGF.trim" 2>/dev/null && mv -f "$LOGF.trim" "$LOGF"
fi
return 0
}
jstr() {
case $1 in
*\\* | *\"* | *"$NL"* | *"$CR"* | *"$TAB"*) printf '%s' "$1" | awk "$AWK_JESC" ;;
*) printf '"%s"' "$1" ;;
esac
}
jnum() { if is_num "$1"; then printf '%s' "$1"; else printf 'null'; fi; }
reply() {
_rp_d=${4:-}
[ -n "$_rp_d" ] || _rp_d='{}'
printf '{"ok":%s,"code":"%s","message":%s,"data":%s}\n' "$1" "$2" "$(jstr "$3")" "$_rp_d"
}
# ---------------------------------------------------------------- config
cfg_load() {
C_LIST=; C_ENABLED=1; C_ETAG=; C_SRCDATE=; C_UPDATED=0; C_COUNT=0; C_DOMAINS=0; C_SETUP=
if [ -f "$CFG" ]; then
while IFS='=' read -r _ck _cv; do
case $_ck in
LIST) C_LIST=$_cv ;;
ENABLED) C_ENABLED=$_cv ;;
ETAG) C_ETAG=$_cv ;;
SRCDATE) C_SRCDATE=$_cv ;;
UPDATED) C_UPDATED=$_cv ;;
COUNT) C_COUNT=$_cv ;;
DOMAINS) C_DOMAINS=$_cv ;;
SETUP) C_SETUP=$_cv ;;
esac
done < "$CFG"
fi
list_info "$C_LIST" >/dev/null 2>&1 || C_LIST=stevenblack
[ "$C_ENABLED" = 0 ] || C_ENABLED=1
is_num "$C_UPDATED" || C_UPDATED=0
is_num "$C_COUNT" || C_COUNT=0
is_num "$C_DOMAINS" || C_DOMAINS=0
case $C_SETUP in ready | pending | failed) ;; *) C_SETUP=ready ;; esac
}
cfg_save() {
{
printf 'LIST=%s\n' "$C_LIST"
printf 'ENABLED=%s\n' "$C_ENABLED"
printf 'ETAG=%s\n' "$C_ETAG"
printf 'SRCDATE=%s\n' "$C_SRCDATE"
printf 'UPDATED=%s\n' "$C_UPDATED"
printf 'COUNT=%s\n' "$C_COUNT"
printf 'DOMAINS=%s\n' "$C_DOMAINS"
printf 'SETUP=%s\n' "$C_SETUP"
} > "$CFG.tmp" && mv -f "$CFG.tmp" "$CFG"
}
# ---------------------------------------------------------------- locks
lock_pid() { _lp=; [ -f "$1/pid" ] && read -r _lp < "$1/pid"; printf '%s' "$_lp"; }
lock_alive() {
[ -d "$1" ] || return 1
_la=$(lock_pid "$1")
if [ -z "$_la" ]; then
_la_m=$(stat -c %Y "$1" 2>/dev/null)
is_num "$_la_m" || return 1
[ $(($(date +%s) - _la_m)) -lt 20 ]
return
fi
[ -d "/proc/$_la" ] || return 1
grep -q 'sb\.sh' "/proc/$_la/cmdline" 2>/dev/null || return 1
return 0
}
lock_take() {
if mkdir "$1" 2>/dev/null; then echo "$$" > "$1/pid"; return 0; fi
lock_alive "$1" && return 1
rm -rf "$1"
if mkdir "$1" 2>/dev/null; then echo "$$" > "$1/pid"; return 0; fi
return 1
}
lock_drop() { rm -rf "$1" 2>/dev/null; return 0; }
# shellcheck disable=SC2329
lock_drop_if_mine() { [ "$(lock_pid "$1")" = "$$" ] && rm -rf "$1" 2>/dev/null; return 0; }
busy_reply() { reply false busy '⏳ Another task is still running. Please wait until it finishes.'; }
# ---------------------------------------------------------------- operation state
op_load() {
OP_KIND=; OP_LIST=; OP_PHASE=; OP_STATE=; OP_CODE=; OP_MSG=; OP_SEQ=0; OP_START=0; OP_END=0; OP_TOTAL=0; OP_DOMAINS=0; OP_REBOOT=0
[ -f "$OPF" ] || return 0
while IFS='=' read -r _ok _ov; do
case $_ok in
KIND) OP_KIND=$_ov ;;
LIST) OP_LIST=$_ov ;;
PHASE) OP_PHASE=$_ov ;;
STATE) OP_STATE=$_ov ;;
CODE) OP_CODE=$_ov ;;
MSG) OP_MSG=$_ov ;;
SEQ) OP_SEQ=$_ov ;;
START) OP_START=$_ov ;;
END) OP_END=$_ov ;;
TOTAL) OP_TOTAL=$_ov ;;
DOMAINS) OP_DOMAINS=$_ov ;;
REBOOT) OP_REBOOT=$_ov ;;
esac
done < "$OPF"
}
op_save() {
[ "$MODE" = install ] && return 0
{
printf 'KIND=%s\nLIST=%s\nPHASE=%s\nSTATE=%s\nCODE=%s\n' "$OP_KIND" "$OP_LIST" "$OP_PHASE" "$OP_STATE" "$OP_CODE"
printf 'MSG=%s\nSEQ=%s\nSTART=%s\nEND=%s\nTOTAL=%s\n' "$OP_MSG" "$OP_SEQ" "$OP_START" "$OP_END" "$OP_TOTAL"
printf 'DOMAINS=%s\nREBOOT=%s\n' "$OP_DOMAINS" "$OP_REBOOT"
} > "$OPF.tmp" 2>/dev/null && mv -f "$OPF.tmp" "$OPF"
}
op_begin() {
_ob=0
[ -f "$SEQF" ] && read -r _ob < "$SEQF"
is_num "$_ob" || _ob=0
_ob=$((_ob + 1))
echo "$_ob" > "$SEQF"
OP_KIND=$1; OP_LIST=$2; OP_PHASE=prepare; OP_STATE=running; OP_CODE=; OP_MSG=
OP_SEQ=$_ob; OP_START=$(now); OP_END=0; OP_TOTAL=0; OP_DOMAINS=0; OP_REBOOT=0
rm -f "$DLPART" "$DLHDR" "$RUND/cancel"
op_save
}
op_phase() { OP_PHASE=$1; op_save; }
op_finish() {
OP_STATE=$1; OP_CODE=$2; OP_MSG=$3; OP_END=$(now)
op_save
}
op_reconcile() {
[ -f "$OPF" ] || return 0
grep -q '^STATE=running$' "$OPF" 2>/dev/null || return 0
lock_alive "$LOCKD" && return 0
op_load
OP_PHASE=; OP_STATE=error; OP_CODE=interrupted; OP_END=$(now)
OP_MSG='⚠️ The last task was interrupted. Your hosts file is intact — run it again to finish.'
op_save
lock_drop "$LOCKD"
log WARN "A background task stopped unexpectedly; its state was reset"
}
op_json() {
op_reconcile
if [ ! -f "$OPF" ]; then
printf '{"running":false,"state":"idle","seq":0}'
return 0
fi
_oj_b=0
[ -f "$DLPART" ] && _oj_b=$(fsize "$DLPART")
_oj_a=0
lock_alive "$LOCKD" && _oj_a=1
_oj_c=0
[ -f "$RUND/cancel" ] && _oj_c=1
_oj=$(awk -v bytes="$_oj_b" -v alive="$_oj_a" -v cancelling="$_oj_c" -v hdr="$DLHDR" "$AWK_OPJSON" "$OPF" 2> /dev/null)
case $_oj in
'{'*'}') printf '%s' "$_oj" ;;
*) printf '{"running":%s,"state":"%s","seq":0}' "$([ "$_oj_a" = 1 ] && echo true || echo false)" "$([ "$_oj_a" = 1 ] && echo running || echo idle)" ;;
esac
}
cancel_requested() { [ -f "$RUND/cancel" ]; }
spawn() {
_sp_out=$1
shift
_sp_mark=$RUND/spawn.$$.$(date +%s)
rm -f "$_sp_mark"
if command -v setsid >/dev/null 2>&1; then
SB_SPAWN_MARK=$_sp_mark setsid sh "$SELF" "$@" < /dev/null > "$_sp_out" 2>&1 &
else
SB_SPAWN_MARK=$_sp_mark nohup sh "$SELF" "$@" < /dev/null > "$_sp_out" 2>&1 &
fi
_sp_i=0
while [ "$_sp_i" -lt 50 ]; do
if [ -f "$_sp_mark" ]; then
rm -f "$_sp_mark"
return 0
fi
nap 0.1
_sp_i=$((_sp_i + 1))
done
return 1
}
spawned() { [ -n "${SB_SPAWN_MARK:-}" ] && : > "$SB_SPAWN_MARK"; return 0; }
# ---------------------------------------------------------------- downloads
dl_detect() {
[ -n "$DL_TOOL" ] && [ "$DL_TOOL" != none ] && return 0
if command -v curl >/dev/null 2>&1 && curl --version >/dev/null 2>&1; then
DL_TOOL=curl
elif wget --help 2>&1 | grep -qi -e '--header'; then
DL_TOOL=wget
else
DL_TOOL=none
return 1
fi
return 0
}
hdr_parse() {
_hp=$(awk '
{ l = $0; sub(/\r$/, "", l); sub(/^[ \t]+/, "", l); x = tolower(l) }
x ~ /^http\/[0-9.]+[ \t]+[0-9][0-9][0-9]/ { split(l, p, /[ \t]+/); code = p[2]; cl = ""; tot = ""; et = ""; lm = ""; next }
x ~ /^content-length:/ { v = l; sub(/^[^:]*:[ \t]*/, "", v); if (v ~ /^[0-9]+$/) cl = v; next }
x ~ /^content-range:/ { v = l; sub(/^.*\//, "", v); if (v ~ /^[0-9]+$/) tot = v; next }
x ~ /^etag:/ { v = l; sub(/^[^:]*:[ \t]*/, "", v); et = v; next }
x ~ /^last-modified:/ { v = l; sub(/^[^:]*:[ \t]*/, "", v); lm = v; next }
END { if (tot == "" && code == "200") tot = cl; print code; print cl; print tot; print et; print lm }' "$1" 2>/dev/null)
{ read -r DL_CODE; read -r DL_CL; read -r DL_TOTAL; read -r DL_ETAG; read -r DL_LASTMOD; } << EOF
$_hp
EOF
is_num "$DL_CODE" || DL_CODE=
is_num "$DL_CL" || DL_CL=
is_num "$DL_TOTAL" || DL_TOTAL=
DL_ETAG=$(clean_val "$DL_ETAG")
DL_LASTMOD=$(clean_val "$DL_LASTMOD")
}
# dl_get URL OUT HDR MODE MODE=full downloads everything, MODE=peek only the first 8 KB.
# curl peeks with a real HTTP Range request. BusyBox wget rejects 206 responses it did not
# ask for itself, so it streams the file and closes the connection after the first 8 KB.
dl_get() {
_dg_u=$1; _dg_o=$2; _dg_h=$3; _dg_m=$4
rm -f "$_dg_o" "$_dg_h.err"
: > "$_dg_h"
DL_FAIL=; DL_ERR=; DL_CODE=; DL_CL=; DL_TOTAL=; DL_ETAG=; DL_LASTMOD=
case $DL_TOOL in
curl)
if [ "$_dg_m" = peek ]; then
curl -sS -L --connect-timeout 15 --speed-limit 1024 --speed-time 30 -A "$UA" -r 0-8191 -D "$_dg_h" -o "$_dg_o" "$_dg_u" 2> "$_dg_h.err" &
else
curl -sS -L --connect-timeout 15 --speed-limit 1024 --speed-time 30 -A "$UA" -D "$_dg_h" -o "$_dg_o" "$_dg_u" 2> "$_dg_h.err" &
fi
;;
wget)
if [ "$_dg_m" = peek ]; then
sh -c 'wget -q -S -T 15 -U "$1" -O - "$2" 2> "$3" | head -c 8192 > "$4"' sb-peek "$UA" "$_dg_u" "$_dg_h" "$_dg_o" &
else
wget -q -S -T 15 -U "$UA" -O "$_dg_o" "$_dg_u" 2> "$_dg_h" &
fi
;;
*)
DL_FAIL=network
DL_ERR='no downloader available'
return 1
;;
esac
_dg_p=$!
[ -n "$DL_PIDF" ] && echo "$_dg_p" > "$DL_PIDF"
wait "$_dg_p"
_dg_rc=$?
[ -n "$DL_PIDF" ] && rm -f "$DL_PIDF"
hdr_parse "$_dg_h"
if [ "$DL_TOOL" = curl ]; then
DL_ERR=$(head -n 1 "$_dg_h.err" 2>/dev/null)
else
DL_ERR=$(grep -m 1 '^wget:' "$_dg_h" 2>/dev/null)
fi
DL_ERR=$(clean_val "$DL_ERR")
rm -f "$_dg_h.err"
if [ -z "$DL_CODE" ]; then
DL_FAIL=network
elif [ "$DL_CODE" != 200 ] && [ "$DL_CODE" != 206 ]; then
DL_FAIL=http
elif [ "$_dg_rc" -ne 0 ] && [ "$_dg_m" = full ]; then
DL_FAIL=incomplete
fi
[ -z "$DL_FAIL" ]
}
dl_reason() {
case $DL_FAIL in
http) printf 'HTTP %s' "$DL_CODE" ;;
incomplete) printf 'transfer interrupted%s' "${DL_ERR:+ ($DL_ERR)}" ;;
*) printf 'no connection%s' "${DL_ERR:+ ($DL_ERR)}" ;;
esac
}
dl_fetch() {
_df_t=1
while :; do
dl_get "$1" "$2" "$3" "$4" && return 0
if [ "$DL_FAIL" = http ]; then
case $DL_CODE in 408 | 425 | 429 | 5??) ;; *) return 1 ;; esac
fi
[ "$_df_t" -ge 3 ] && return 1
cancel_requested && return 1
[ "$MODE" = install ] || log WARN "Download attempt $_df_t failed: $(dl_reason). Retrying"
nap $((_df_t * 2))
cancel_requested && return 1
_df_t=$((_df_t + 1))
done
}
looks_html() {
head -c 4096 "$1" 2>/dev/null | tr '[:upper:]' '[:lower:]' | grep -q -e '<!doctype' -e '<html' -e '<head' -e '<body' -e '<title'
}
head_info() {
_hi=$(head -c 8192 "$1" 2>/dev/null | awk "$AWK_HEAD")
{ read -r HI_COUNT; read -r HI_DATE; } << EOF
$_hi
EOF
is_num "$HI_COUNT" || HI_COUNT=
HI_DATE=$(clean_val "$HI_DATE")
}
date_epoch() { [ -n "$1" ] && printf '%s\n' "$1" | awk "$AWK_DATE"; return 0; }
free_kb() { df -Pk "$1" 2>/dev/null | awk 'NR == 2 { print $4 }'; }
# ---------------------------------------------------------------- hosts file
set_hosts_perm() {
chmod 0644 "$1" 2>/dev/null
chown 0:0 "$1" 2>/dev/null
chcon u:object_r:system_file:s0 "$1" 2>/dev/null
return 0
}
compile_into() {
_ci_o=$1; _ci_e=$2; _ci_l=$3
list_info "$C_LIST" || return 1
if [ "$_ci_e" = 1 ]; then
[ -s "$_ci_l" ] || return 1
rm -f "$TMPD/compile.count"
awk -v allowf="$ALLOWF" -v blockf="$BLOCKF" -v cntf="$TMPD/compile.count" -v ver="$SB_VER" -v name="$L_NAME" -v url="$L_URL" "$AWK_COMPILE" "$_ci_l" > "$_ci_o" 2>/dev/null || return 1
CH_COUNT=
[ -f "$TMPD/compile.count" ] && read -r CH_COUNT < "$TMPD/compile.count"
rm -f "$TMPD/compile.count"
is_num "$CH_COUNT" || return 1
else
{
printf '# StevenBlock %s - systemless hosts file\n' "$SB_VER"
printf '# Blocking is disabled. Turn it back on from the StevenBlock WebUI.\n'
printf '# https://github.com/mikropsoft/StevenBlock\n\n'
printf '127.0.0.1 localhost\n::1 localhost ip6-localhost\n'
} > "$_ci_o" 2>/dev/null || return 1
CH_COUNT=0
fi
return 0
}
write_clean_hosts() {
mkdir -p "$HOSTSD"
{
printf '# StevenBlock %s - systemless hosts file\n' "$SB_VER"
printf '# No blocklist is installed yet. StevenBlock downloads one as soon as it can.\n'
printf '# https://github.com/mikropsoft/StevenBlock\n\n'
printf '127.0.0.1 localhost\n::1 localhost ip6-localhost\n'
} > "$HOSTSD/.hosts.sbclean" && set_hosts_perm "$HOSTSD/.hosts.sbclean" && mv -f "$HOSTSD/.hosts.sbclean" "$HOSTS"
}
backup_hosts() {
[ -f "$HOSTS" ] || return 0
rm -f "$BACKUPD/hosts.3"
[ -f "$BACKUPD/hosts.2" ] && mv -f "$BACKUPD/hosts.2" "$BACKUPD/hosts.3"
[ -f "$BACKUPD/hosts.1" ] && mv -f "$BACKUPD/hosts.1" "$BACKUPD/hosts.2"
cp -f "$HOSTS" "$BACKUPD/hosts.1"
}
restore_backup() {
if [ -f "$BACKUPD/hosts.1" ] && cp -f "$BACKUPD/hosts.1" "$HOSTSD/.hosts.sbrestore" 2>/dev/null; then
set_hosts_perm "$HOSTSD/.hosts.sbrestore"
if mv -f "$HOSTSD/.hosts.sbrestore" "$HOSTS"; then
log WARN "Restored the previous hosts file from backup"
return 0
fi
fi
rm -f "$HOSTSD/.hosts.sbrestore"
write_clean_hosts
log ERROR "No usable backup was found; wrote a clean localhost-only hosts file"
return 1
}
verify_hosts() {
[ -s "$HOSTS" ] || return 1
grep -qx '127\.0\.0\.1 localhost' "$HOSTS" || return 1
grep -qx '::1 localhost ip6-localhost' "$HOSTS" || return 1
_vh=$(grep -c '^0\.0\.0\.0 ' "$HOSTS" 2>/dev/null)
[ "${_vh:-0}" = "$1" ]
}
# install_hosts NEWFILE EXPECTED -> 0 ok, 1 write failed, 2 verification failed. Sets IH_CHANGED.
install_hosts() {
IH_CHANGED=1
mkdir -p "$HOSTSD"
if [ -f "$HOSTS" ] && cmp -s "$1" "$HOSTS"; then
rm -f "$1"
IH_CHANGED=0
return 0
fi
backup_hosts
set_hosts_perm "$1"
if ! mv -f "$1" "$HOSTS"; then
rm -f "$1"
restore_backup
return 1
fi
set_hosts_perm "$HOSTS"
if ! verify_hosts "$2"; then
log ERROR "The new hosts file failed verification"
restore_backup
return 2
fi
return 0
}
live_hosts() {
if head -c 1 "$INIT_HOSTS" > /dev/null 2>&1; then printf '%s' "$INIT_HOSTS"; else printf '%s' "$SYS_HOSTS"; fi
}
mark_pending() { boot_id > "$PENDF.tmp" 2>/dev/null && mv -f "$PENDF.tmp" "$PENDF"; return 0; }
pending_flag() {
[ -f "$PENDF" ] || return 1
[ "$(cat "$PENDF" 2>/dev/null)" = "$(boot_id)" ]
}
hosts_sane() {
[ -s "$1" ] && grep -qx '127\.0\.0\.1 localhost' "$1" && grep -qx '::1 localhost ip6-localhost' "$1"
}
# Makes sure a sane module hosts file exists before it is mounted: rebuilds it from the cache when it is missing or damaged.
repair_hosts() {
hosts_sane "$HOSTS" && return 0
mkdir -p "$HOSTSD"
rm -f "$HOSTSD/.hosts.sbnew"
cfg_load
_rh=0
if [ "$C_ENABLED" != 1 ]; then
compile_into "$HOSTSD/.hosts.sbnew" 0 && _rh=1
elif [ "$C_SETUP" = ready ] && [ -s "$LISTCACHE" ]; then
compile_into "$HOSTSD/.hosts.sbnew" 1 "$LISTCACHE" && _rh=1
fi
if [ "$_rh" = 1 ] && hosts_sane "$HOSTSD/.hosts.sbnew"; then
set_hosts_perm "$HOSTSD/.hosts.sbnew"
if mv -f "$HOSTSD/.hosts.sbnew" "$HOSTS"; then
C_DOMAINS=$CH_COUNT
cfg_save
log WARN "The hosts file was missing or damaged; rebuilt it from the cached list"
return 0
fi
fi
rm -f "$HOSTSD/.hosts.sbnew"
write_clean_hosts || return 1
log WARN "The hosts file was missing or damaged; wrote a clean localhost-only file"
return 0
}
in_init_ns() {
_ns_s=$(readlink /proc/self/ns/mnt 2>/dev/null)
[ -n "$_ns_s" ] && [ "$_ns_s" = "$(readlink "$INIT_NS" 2>/dev/null)" ]
}
# Bind-mounts the module hosts file over /system/etc/hosts in init's mount namespace.
bind_hosts() {
if in_init_ns; then
mount -o bind "$HOSTS" "$MOUNT_TARGET" 2>/dev/null && return 0
fi
if [ "$SB_BB" != none ]; then
"$SB_BB" nsenter -t 1 -m -- "$SB_BB" mount -o bind "$HOSTS" "$MOUNT_TARGET" 2>/dev/null && return 0
fi
if [ -x /system/bin/nsenter ]; then
/system/bin/nsenter -t 1 -m -- /system/bin/mount -o bind "$HOSTS" "$MOUNT_TARGET" 2>/dev/null && return 0
fi
in_init_ns || mount -o bind "$HOSTS" "$MOUNT_TARGET" 2>/dev/null
}
mount_record() {
printf '%s\n%s\n' "$(boot_id)" "$1" > "$MOUNTF.tmp" 2>/dev/null && mv -f "$MOUNTF.tmp" "$MOUNTF"
return 0
}
mount_mode() {
_mm_b=; _mm=
[ -f "$MOUNTF" ] && { read -r _mm_b; read -r _mm; } < "$MOUNTF"
[ "$_mm_b" = "$(boot_id)" ] || _mm=
printf '%s' "$_mm"
}
# Runs at boot after the root manager (or its metamodule) has mounted modules.
# Recent KernelSU, SukiSU and APatch builds only mount modules through a metamodule, and some
# metamodules mount a copy taken at install time. If the system isn't using StevenBlock's current
# hosts file and no other module provides one, StevenBlock bind-mounts its own file directly.
ensure_mounted() {
[ -f "$HOSTS" ] || return 0
if cmp -s "$(live_hosts)" "$HOSTS"; then
[ -n "$(mount_mode)" ] || mount_record manager
return 0
fi
_em_cf=$(conflicts)
if [ -n "$_em_cf" ]; then
mount_record conflict
log WARN "Another module also replaces /system/etc/hosts ($_em_cf), so StevenBlock left it alone"
return 0
fi
set_hosts_perm "$HOSTS"
if bind_hosts && cmp -s "$(live_hosts)" "$HOSTS"; then
mount_record self
log INFO "The root manager didn't apply StevenBlock's hosts file, so StevenBlock mounted it directly"
return 0
fi
mount_record failed
log ERROR "Couldn't mount the hosts file onto /system/etc/hosts"
return 1
}
applied_state() {
if [ ! -f "$HOSTS" ]; then printf 'unknown'; return 0; fi
if cmp -s "$(live_hosts)" "$HOSTS"; then
printf 'applied'
elif pending_flag; then
printf 'pending'
elif [ -n "$(conflicts)" ]; then
printf 'overridden'
else
printf 'inactive'
fi
}
describe() {
cfg_load
list_info "$C_LIST"
case $C_SETUP in
pending) printf '🛡️ ⏳ Setup pending • %s downloads automatically once you are online' "$L_NAME"; return 0 ;;
failed) printf '🛡️ ❌ Setup incomplete • Open the WebUI and pick a list to finish setup'; return 0 ;;
esac
_ds=$(applied_state)
if [ "$_ds" = pending ]; then
if [ "$C_ENABLED" = 1 ]; then
printf '🛡️ 🟡 Reboot required • %s • %s domains ready' "$L_NAME" "$(fmt_num "$C_DOMAINS")"
else
printf '🛡️ 🟡 Reboot required • Blocking will be turned off'
fi
elif [ "$C_ENABLED" != 1 ]; then
printf '🛡️ 🔴 Disabled • %s is paused' "$L_NAME"
elif [ "$_ds" = overridden ]; then
printf '🛡️ ⚠️ Overridden • Another module replaces /system/etc/hosts'
elif [ "$_ds" = inactive ]; then
printf '🛡️ 🔴 Not active • Reboot to activate %s' "$L_NAME"
else
printf '🛡️ 🟢 Active • %s • %s domains blocked' "$L_NAME" "$(fmt_num "$C_DOMAINS")"
fi
}
set_description() {
[ -f "$PROP" ] || return 0
_sd=$(describe)
awk -v d="$_sd" 'BEGIN { f = 0 } /^description=/ { if (!f) print "description=" d; f = 1; next } { print } END { if (!f) print "description=" d }' "$PROP" > "$PROP.sbtmp" 2>/dev/null && mv -f "$PROP.sbtmp" "$PROP"
chmod 0644 "$PROP" 2>/dev/null
return 0
}
flush_dns() {
_fd=
if [ -x /system/bin/ndc ]; then
case $(/system/bin/ndc resolver flushdefaultif 2>&1) in 200*) _fd='flushdefaultif' ;; esac
_fd_ip=/system/bin/ip
[ -x "$_fd_ip" ] || _fd_ip=ip
for _fd_n in $("$_fd_ip" rule 2>/dev/null | awk '{ for (i = 1; i < NF; i++) if ($i == "fwmark" && $(i + 1) ~ /^0x1[0-9a-f][0-9a-f][0-9a-f][0-9a-f]\/0x1ffff$/) print substr($(i + 1), 4, 4) }' | sort -u); do
_fd_id=$((0x$_fd_n))
case $(/system/bin/ndc resolver flushnet "$_fd_id" 2>&1) in 200*) _fd="${_fd:+$_fd, }flushnet $_fd_id" ;; esac
done
fi
if [ -n "$_fd" ]; then
log INFO "DNS cache flushed ($_fd)"
else
log INFO "DNS cache flush is not available on this Android version; the reboot clears it"
fi
return 0
}
conflicts() {
_cf=
for _cf_d in "$MODROOT"/*; do
[ -d "$_cf_d" ] || continue
_cf_i=${_cf_d##*/}
[ "$_cf_i" = stevenblock ] && continue
[ -f "$_cf_d/system/etc/hosts" ] || continue
[ -f "$_cf_d/disable" ] && continue
[ -f "$_cf_d/remove" ] && continue
_cf="$_cf $_cf_i"
done
printf '%s' "${_cf# }"
}
detect_manager() {
MGR_NAME=; MGR_VER=
_dm_b=$(boot_id)
if [ -f "$RUND/manager" ]; then
{ read -r _dm_c; read -r MGR_NAME; read -r MGR_VER; } < "$RUND/manager"
[ "$_dm_c" = "$_dm_b" ] && [ -n "$MGR_NAME" ] && return 0
fi
if [ "${APATCH:-}" = true ] || [ -x /data/adb/apd ]; then
MGR_NAME=APatch
MGR_VER=${APATCH_VER:-$(timeout 3 /data/adb/apd -V 2>/dev/null | head -n 1)}
elif [ "${KSU:-}" = true ] || [ -x /data/adb/ksud ]; then
if [ -d /data/data/com.sukisu.ultra ]; then
MGR_NAME='SukiSU Ultra'
elif [ -d /data/data/com.rifsxd.ksunext ]; then
MGR_NAME='KernelSU Next'
else
MGR_NAME=KernelSU
fi
MGR_VER=${KSU_VER:-$(timeout 3 /data/adb/ksud -V 2>/dev/null | head -n 1)}
elif [ -n "${MAGISK_VER:-}" ] || [ -d /data/adb/magisk ]; then
MGR_NAME=Magisk
MGR_VER=${MAGISK_VER:-$(timeout 3 magisk -v 2>/dev/null | head -n 1)}
MGR_VER=${MGR_VER%%:*}
else
MGR_NAME=Unknown
fi
case $MGR_VER in *' '*) MGR_VER=${MGR_VER##* } ;; esac
MGR_VER=$(clean_val "$MGR_VER")
[ -d "$RUND" ] && printf '%s\n%s\n%s\n' "$_dm_b" "$MGR_NAME" "$MGR_VER" > "$RUND/manager" 2>/dev/null
return 0
}
httpd_available() { [ "$SB_BB" != none ] && "$SB_BB" --list 2>/dev/null | grep -qx httpd; }
open_url() {
[ -x /system/bin/am ] || return 1