-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·865 lines (709 loc) · 24.7 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·865 lines (709 loc) · 24.7 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
#!/bin/sh
# shellcheck shell=dash
REPO_OWNER="titovcode"
REPO_NAME="krot"
REQUIRED_SPACE_KB=15360
REQUIRED_SING_BOX_VERSION="1.12.4"
PKG_IS_APK=0
FETCHER=""
TMP_DIR=""
PODKOP_WAS_ENABLED=0
PODKOP_WAS_RUNNING=0
PODKOP_PLUS_I18N_REQUESTED=0
PODKOP_PLUS_RELEASE_JSON=""
PODKOP_PLUS_RELEASE_TAG=""
PODKOP_PLUS_BACKEND_URL=""
PODKOP_PLUS_BACKEND_NAME=""
PODKOP_PLUS_BACKEND_FILE=""
PODKOP_PLUS_APP_URL=""
PODKOP_PLUS_APP_NAME=""
PODKOP_PLUS_APP_FILE=""
PODKOP_PLUS_I18N_URL=""
PODKOP_PLUS_I18N_NAME=""
PODKOP_PLUS_I18N_FILE=""
PODKOP_PLUS_PACKAGE_VERSION=""
command -v apk >/dev/null 2>&1 && PKG_IS_APK=1
msg() {
printf '\033[32;1m%s\033[0m\n' "$1"
}
warn() {
printf '\033[33;1m%s\033[0m\n' "$1"
}
fail() {
printf '\033[31;1m%s\033[0m\n' "$1" >&2
exit 1
}
usage() {
cat <<EOF
Usage: $0
Installs or updates Podkop Plus packages only:
- krot
- luci-app-krot
- luci-i18n-krot-ru when requested or when LuCI language is Russian
External components are managed from the Podkop Plus Updates tab after installation.
EOF
}
parse_args() {
while [ "$#" -gt 0 ]; do
case "$1" in
-h|--help)
usage
exit 0
;;
*)
fail "Unknown installer option: $1"
;;
esac
shift
done
}
cleanup() {
[ -n "$TMP_DIR" ] && rm -rf "$TMP_DIR"
}
read_openwrt_release_value() {
key="$1"
[ -f /etc/openwrt_release ] || return 0
sed -n "s/^${key}='\(.*\)'/\1/p" /etc/openwrt_release 2>/dev/null | head -n 1
}
command_exists() {
command -v "$1" >/dev/null 2>&1
}
init_tmp_dir() {
TMP_DIR="$(mktemp -d /tmp/krot.XXXXXX 2>/dev/null || true)"
if [ -z "$TMP_DIR" ]; then
TMP_DIR="/tmp/krot.$$"
mkdir -p "$TMP_DIR" || fail "Failed to create temporary directory: $TMP_DIR"
fi
}
detect_fetcher() {
if command_exists wget; then
FETCHER="wget"
return 0
fi
if command_exists curl; then
FETCHER="curl"
return 0
fi
fail "wget or curl is required to download Podkop Plus"
}
http_get() {
case "$FETCHER" in
wget)
wget -qO- "$1"
;;
curl)
curl -fsSL "$1"
;;
*)
return 1
;;
esac
}
install_json_helper_path() {
helper_path="$TMP_DIR/install-json.uc"
if [ ! -s "$helper_path" ]; then
cat > "$helper_path" <<'EOF'
#!/usr/bin/env ucode
let fs = require("fs");
function as_string(value) {
return value == null ? "" : "" + value;
}
function read_stdin() {
let input = fs.open("/dev/stdin", "r");
if (!input)
return "";
let data = input.read("all");
input.close();
return data == null ? "" : data;
}
function read_stdin_json() {
try {
return json(read_stdin());
}
catch (e) {
return null;
}
}
function starts_with(value, prefix) {
value = as_string(value);
prefix = as_string(prefix);
return substr(value, 0, length(prefix)) == prefix;
}
function ends_with(value, suffix) {
value = as_string(value);
suffix = as_string(suffix);
return length(value) >= length(suffix) && substr(value, length(value) - length(suffix)) == suffix;
}
function asset_matches(name, kind, ext) {
let suffix = "." + ext;
if (!ends_with(name, suffix))
return false;
if (kind == "backend")
return starts_with(name, "krot_") || starts_with(name, "krot-");
if (kind == "app")
return starts_with(name, "luci-app-krot_") || starts_with(name, "luci-app-krot-");
if (kind == "i18n")
return starts_with(name, "luci-i18n-krot-ru_") || starts_with(name, "luci-i18n-krot-ru-");
return false;
}
function github_message() {
let value = read_stdin_json();
if (value == null)
exit(2);
if (type(value) == "object" && value.message != null)
print(as_string(value.message), "\n");
}
function release_tag() {
let release = read_stdin_json();
if (type(release) == "object" && release.tag_name != null)
print(as_string(release.tag_name), "\n");
}
function release_asset_url(kind, ext) {
let release = read_stdin_json();
if (type(release) != "object" || type(release.assets) != "array")
return;
for (let asset in release.assets) {
if (type(asset) == "object" && asset_matches(asset.name, kind, ext)) {
print(as_string(asset.browser_download_url || ""), "\n");
return;
}
}
}
let mode = ARGV[0] || "";
if (mode == "github-message")
github_message();
else if (mode == "release-tag")
release_tag();
else if (mode == "release-asset-url")
release_asset_url(ARGV[1], ARGV[2]);
else
exit(1);
EOF
fi
printf '%s\n' "$helper_path"
}
install_json_ucode() {
ucode "$(install_json_helper_path)" "$@"
}
download_file_once() {
case "$FETCHER" in
wget)
wget -q -O "$2" "$1"
;;
curl)
curl -fsSL "$1" -o "$2"
;;
*)
return 1
;;
esac
}
download_with_retry() {
url="$1"
output_path="$2"
label="$3"
attempt=1
max_attempts=3
while [ "$attempt" -le "$max_attempts" ]; do
msg "Downloading $label ($attempt/$max_attempts)"
if download_file_once "$url" "$output_path" && [ -s "$output_path" ]; then
return 0
fi
rm -f "$output_path"
warn "Retrying $label"
attempt=$((attempt + 1))
done
return 1
}
pkg_list_installed_names() {
if [ "$PKG_IS_APK" -eq 1 ]; then
apk info 2>/dev/null
else
opkg list-installed 2>/dev/null | awk '{print $1}'
fi
}
pkg_is_installed() {
pkg_name="$1"
if [ "$PKG_IS_APK" -eq 1 ]; then
apk info -e "$pkg_name" >/dev/null 2>&1
else
opkg list-installed 2>/dev/null | grep -Eq "^${pkg_name}([[:space:]-]|$)"
fi
}
pkg_list_update() {
if [ "$PKG_IS_APK" -eq 1 ]; then
apk update </dev/null
else
opkg update </dev/null
fi
}
pkg_install_name() {
pkg_name="$1"
if [ "$PKG_IS_APK" -eq 1 ]; then
apk add "$pkg_name" </dev/null
else
opkg install "$pkg_name" </dev/null
fi
}
pkg_remove_if_installed() {
pkg_name="$1"
if ! pkg_is_installed "$pkg_name"; then
return 0
fi
if [ "$PKG_IS_APK" -eq 1 ]; then
apk del "$pkg_name" >/dev/null 2>&1 </dev/null || true
else
opkg remove --force-depends "$pkg_name" >/dev/null 2>&1 </dev/null || true
fi
}
pkg_remove_matching_prefix() {
prefix="$1"
for pkg_name in $(pkg_list_installed_names | grep "^$prefix" 2>/dev/null); do
pkg_remove_if_installed "$pkg_name"
done
}
pkg_install_files() {
if [ "$PKG_IS_APK" -eq 1 ]; then
apk add --allow-untrusted "$@" </dev/null
else
opkg install --force-overwrite --force-downgrade "$@" </dev/null
fi
}
ensure_bootstrap_tool() {
tool_name="$1"
package_name="$2"
if command_exists "$tool_name"; then
return 0
fi
msg "Installing bootstrap dependency: $package_name"
pkg_install_name "$package_name" || fail "Failed to install $package_name"
}
sync_time() {
current_year=""
if ! command_exists ntpd; then
return 0
fi
current_year="$(date +%Y 2>/dev/null || true)"
case "$current_year" in
''|*[!0-9]*) current_year=0 ;;
esac
if [ "$current_year" -ge 2024 ]; then
return 0
fi
ntpd -q \
-p 194.190.168.1 \
-p 216.239.35.0 \
-p 216.239.35.4 \
-p 162.159.200.1 \
-p 162.159.200.123 >/dev/null 2>&1 || true
}
check_root() {
if command_exists id && [ "$(id -u)" != "0" ]; then
fail "Please run this installer as root"
fi
}
check_system() {
release=""
major=""
model=""
available_space=""
[ -f /etc/openwrt_release ] || fail "This installer supports OpenWrt only"
model="$(cat /tmp/sysinfo/model 2>/dev/null || true)"
[ -n "$model" ] && msg "Router model: $model"
release="$(read_openwrt_release_value "DISTRIB_RELEASE")"
major="$(printf '%s' "$release" | sed 's/[^0-9].*$//' | cut -d. -f1)"
if [ -n "$major" ] && [ "$major" -lt 24 ]; then
fail "Podkop Plus requires OpenWrt 24.10 or newer"
fi
available_space="$(df /overlay 2>/dev/null | awk 'NR==2 {print $4}')"
[ -n "$available_space" ] || available_space="$(df / 2>/dev/null | awk 'NR==2 {print $4}')"
if [ -n "$available_space" ] && [ "$available_space" -lt "$REQUIRED_SPACE_KB" ]; then
fail "Not enough free flash space. Available: $((available_space / 1024)) MB, required: $((REQUIRED_SPACE_KB / 1024)) MB"
fi
}
confirm_prompt() {
prompt_text="$1"
answer=""
if [ ! -t 0 ]; then
msg "$prompt_text [y/N]: y (non-interactive)"
return 0
fi
while :; do
printf '%s [y/N]: ' "$prompt_text"
read -r answer || return 1
case "$answer" in
y|Y)
return 0
;;
n|N|"")
return 1
;;
*)
warn "Please answer y or n."
;;
esac
done
}
get_luci_main_lang() {
command_exists uci || return 0
uci -q get luci.main.lang 2>/dev/null || true
}
sanitize_semver() {
printf '%s\n' "$1" | sed 's/^v//;s/-.*$//;s/[^0-9.].*$//'
}
version_ge() {
lhs_major=0
lhs_minor=0
lhs_patch=0
rhs_major=0
rhs_minor=0
rhs_patch=0
lhs_version="$(sanitize_semver "$1")"
rhs_version="$(sanitize_semver "$2")"
old_ifs="$IFS"
IFS='.'
set -- $lhs_version
IFS="$old_ifs"
[ -n "$1" ] && lhs_major="$1"
[ -n "$2" ] && lhs_minor="$2"
[ -n "$3" ] && lhs_patch="$3"
IFS='.'
set -- $rhs_version
IFS="$old_ifs"
[ -n "$1" ] && rhs_major="$1"
[ -n "$2" ] && rhs_minor="$2"
[ -n "$3" ] && rhs_patch="$3"
if [ "$lhs_major" -gt "$rhs_major" ]; then
return 0
fi
if [ "$lhs_major" -lt "$rhs_major" ]; then
return 1
fi
if [ "$lhs_minor" -gt "$rhs_minor" ]; then
return 0
fi
if [ "$lhs_minor" -lt "$rhs_minor" ]; then
return 1
fi
[ "$lhs_patch" -ge "$rhs_patch" ]
}
extract_package_version() {
package_name="$1"
case "$package_name" in
krot_*.ipk)
printf '%s\n' "$package_name" | sed 's/^krot_//;s/_[^_]*\.ipk$//'
;;
krot_*.apk)
printf '%s\n' "$package_name" | sed 's/^krot_//;s/\.apk$//'
;;
krot-*.ipk)
printf '%s\n' "$package_name" | sed 's/^krot-//;s/-[^-]*\.ipk$//'
;;
krot-*.apk)
printf '%s\n' "$package_name" | sed 's/^krot-//;s/\.apk$//'
;;
luci-app-krot_*.ipk)
printf '%s\n' "$package_name" | sed 's/^luci-app-krot_//;s/_[^_]*\.ipk$//'
;;
luci-app-krot_*.apk)
printf '%s\n' "$package_name" | sed 's/^luci-app-krot_//;s/\.apk$//'
;;
luci-app-krot-*.ipk)
printf '%s\n' "$package_name" | sed 's/^luci-app-krot-//;s/-[^-]*\.ipk$//'
;;
luci-app-krot-*.apk)
printf '%s\n' "$package_name" | sed 's/^luci-app-krot-//;s/\.apk$//'
;;
luci-i18n-krot-ru_*.ipk)
printf '%s\n' "$package_name" | sed 's/^luci-i18n-krot-ru_//;s/_[^_]*\.ipk$//'
;;
luci-i18n-krot-ru_*.apk)
printf '%s\n' "$package_name" | sed 's/^luci-i18n-krot-ru_//;s/\.apk$//'
;;
luci-i18n-krot-ru-*.ipk)
printf '%s\n' "$package_name" | sed 's/^luci-i18n-krot-ru-//;s/-[^-]*\.ipk$//'
;;
luci-i18n-krot-ru-*.apk)
printf '%s\n' "$package_name" | sed 's/^luci-i18n-krot-ru-//;s/\.apk$//'
;;
*)
printf '%s\n' "$package_name"
;;
esac
}
fetch_github_latest_release_json() {
owner="$1"
repo="$2"
response=""
message=""
url="https://api.github.com/repos/${owner}/${repo}/releases/latest"
response="$(http_get "$url" 2>/dev/null || true)"
[ -n "$response" ] || fail "Failed to query GitHub latest release metadata for ${owner}/${repo}"
message="$(printf '%s' "$response" | install_json_ucode github-message 2>/dev/null)" ||
fail "GitHub returned an invalid latest release response for ${owner}/${repo}"
case "$message" in
*"API rate limit"*|*"rate limit exceeded"*)
fail "GitHub API rate limit reached. Try again later."
;;
"Not Found")
fail "No published latest release found for ${owner}/${repo}"
;;
esac
printf '%s' "$response"
}
resolve_krot_release() {
asset_ext="ipk"
[ "$PKG_IS_APK" -eq 1 ] && asset_ext="apk"
PODKOP_PLUS_RELEASE_JSON="$(fetch_github_latest_release_json "$REPO_OWNER" "$REPO_NAME")"
PODKOP_PLUS_RELEASE_TAG="$(printf '%s' "$PODKOP_PLUS_RELEASE_JSON" | install_json_ucode release-tag 2>/dev/null)"
[ -n "$PODKOP_PLUS_RELEASE_TAG" ] || fail "Failed to detect the Podkop Plus release tag"
PODKOP_PLUS_BACKEND_URL="$(printf '%s' "$PODKOP_PLUS_RELEASE_JSON" | install_json_ucode release-asset-url backend "$asset_ext" 2>/dev/null | sed -n '1p')"
[ -n "$PODKOP_PLUS_BACKEND_URL" ] || fail "The Podkop Plus release does not contain a krot .$asset_ext package"
PODKOP_PLUS_APP_URL="$(printf '%s' "$PODKOP_PLUS_RELEASE_JSON" | install_json_ucode release-asset-url app "$asset_ext" 2>/dev/null | sed -n '1p')"
[ -n "$PODKOP_PLUS_APP_URL" ] || fail "The Podkop Plus release does not contain a luci-app-krot .$asset_ext package"
PODKOP_PLUS_BACKEND_NAME="$(basename "$PODKOP_PLUS_BACKEND_URL")"
PODKOP_PLUS_APP_NAME="$(basename "$PODKOP_PLUS_APP_URL")"
PODKOP_PLUS_PACKAGE_VERSION="$(extract_package_version "$PODKOP_PLUS_BACKEND_NAME")"
PODKOP_PLUS_I18N_URL=""
PODKOP_PLUS_I18N_NAME=""
if [ "$PODKOP_PLUS_I18N_REQUESTED" -eq 1 ]; then
PODKOP_PLUS_I18N_URL="$(printf '%s' "$PODKOP_PLUS_RELEASE_JSON" | install_json_ucode release-asset-url i18n "$asset_ext" 2>/dev/null | sed -n '1p')"
[ -n "$PODKOP_PLUS_I18N_URL" ] || fail "The Podkop Plus release does not contain a luci-i18n-krot-ru .$asset_ext package"
PODKOP_PLUS_I18N_NAME="$(basename "$PODKOP_PLUS_I18N_URL")"
fi
}
remove_conflicting_dns_proxy() {
if ! pkg_is_installed "https-dns-proxy"; then
return 0
fi
warn "Detected conflicting package: https-dns-proxy"
confirm_prompt "Remove the conflicting https-dns-proxy package and continue?" || fail "Please remove https-dns-proxy manually and run the installer again"
pkg_remove_if_installed "luci-app-https-dns-proxy"
pkg_remove_if_installed "https-dns-proxy"
pkg_remove_matching_prefix "luci-i18n-https-dns-proxy"
}
remove_old_sing_box_if_needed() {
installed_version=""
pkg_is_installed "sing-box" || return 0
command_exists sing-box || return 0
installed_version="$(sing-box version 2>/dev/null | head -n 1 | awk '{print $3}')"
[ -n "$installed_version" ] || return 0
if version_ge "$installed_version" "$REQUIRED_SING_BOX_VERSION"; then
return 0
fi
warn "sing-box $installed_version is older than the required version $REQUIRED_SING_BOX_VERSION. Removing the old package first."
[ -x /etc/init.d/krot ] && /etc/init.d/krot stop >/dev/null 2>&1 || true
[ -x /etc/init.d/podkop ] && /etc/init.d/podkop stop >/dev/null 2>&1 || true
restore_podkop_dnsmasq_failsafe
pkg_remove_if_installed "sing-box"
}
restore_podkop_dnsmasq_failsafe_raw() {
podkop_dnsmasq_section="krot"
podkop_dns_address="127.0.0.42"
podkop_config_name="krot"
podkop_dnsmasq_changed=0
podkop_default_has_dns=0
podkop_split_instance_present=0
command_exists uci || return 0
podkop_interfaces="$(uci -q get "dhcp.$podkop_dnsmasq_section.interface" 2>/dev/null)"
[ -n "$podkop_interfaces" ] ||
podkop_interfaces="$(uci -q get "$podkop_config_name.settings.source_network_interfaces" 2>/dev/null)"
[ -n "$podkop_interfaces" ] || podkop_interfaces="br-lan"
podkop_default_servers="$(uci -q get 'dhcp.@dnsmasq[0].server' 2>/dev/null)"
for podkop_value in $podkop_default_servers; do
[ "$podkop_value" = "$podkop_dns_address" ] && podkop_default_has_dns=1
done
if uci -q show "dhcp.$podkop_dnsmasq_section" >/dev/null 2>&1; then
podkop_split_instance_present=1
podkop_dnsmasq_changed=1
fi
uci -q delete "dhcp.$podkop_dnsmasq_section" >/dev/null 2>&1 || true
podkop_backup_notinterfaces="$(uci -q get 'dhcp.@dnsmasq[0].podkop_notinterface' 2>/dev/null)"
if [ -n "$podkop_backup_notinterfaces" ]; then
uci -q delete 'dhcp.@dnsmasq[0].notinterface' >/dev/null 2>&1 || true
for podkop_value in $podkop_backup_notinterfaces; do
uci -q add_list "dhcp.@dnsmasq[0].notinterface=$podkop_value" >/dev/null 2>&1 || true
done
uci -q delete 'dhcp.@dnsmasq[0].podkop_notinterface' >/dev/null 2>&1 || true
podkop_dnsmasq_changed=1
else
if [ "$podkop_split_instance_present" -eq 1 ] || [ "$podkop_default_has_dns" -eq 1 ]; then
for podkop_value in $podkop_interfaces; do
uci -q del_list "dhcp.@dnsmasq[0].notinterface=$podkop_value" >/dev/null 2>&1 && podkop_dnsmasq_changed=1
done
fi
uci -q delete 'dhcp.@dnsmasq[0].podkop_notinterface' >/dev/null 2>&1 || true
fi
podkop_backup_servers="$(uci -q get 'dhcp.@dnsmasq[0].podkop_server' 2>/dev/null)"
if [ -n "$podkop_backup_servers" ]; then
uci -q delete 'dhcp.@dnsmasq[0].server' >/dev/null 2>&1 || true
for podkop_value in $podkop_backup_servers; do
uci -q add_list "dhcp.@dnsmasq[0].server=$podkop_value" >/dev/null 2>&1 || true
done
uci -q delete 'dhcp.@dnsmasq[0].podkop_server' >/dev/null 2>&1 || true
podkop_dnsmasq_changed=1
else
uci -q del_list "dhcp.@dnsmasq[0].server=$podkop_dns_address" >/dev/null 2>&1 && podkop_dnsmasq_changed=1
uci -q delete 'dhcp.@dnsmasq[0].podkop_server' >/dev/null 2>&1 || true
fi
podkop_noresolv="$(uci -q get 'dhcp.@dnsmasq[0].podkop_noresolv' 2>/dev/null)"
if [ -n "$podkop_noresolv" ]; then
uci -q set "dhcp.@dnsmasq[0].noresolv=$podkop_noresolv" >/dev/null 2>&1 || true
uci -q delete 'dhcp.@dnsmasq[0].podkop_noresolv' >/dev/null 2>&1 || true
podkop_dnsmasq_changed=1
elif [ "$podkop_default_has_dns" -eq 1 ]; then
uci -q set 'dhcp.@dnsmasq[0].noresolv=0' >/dev/null 2>&1 || true
podkop_dnsmasq_changed=1
fi
podkop_cachesize="$(uci -q get 'dhcp.@dnsmasq[0].podkop_cachesize' 2>/dev/null)"
if [ -n "$podkop_cachesize" ]; then
uci -q set "dhcp.@dnsmasq[0].cachesize=$podkop_cachesize" >/dev/null 2>&1 || true
uci -q delete 'dhcp.@dnsmasq[0].podkop_cachesize' >/dev/null 2>&1 || true
podkop_dnsmasq_changed=1
elif [ "$podkop_default_has_dns" -eq 1 ]; then
uci -q set 'dhcp.@dnsmasq[0].cachesize=150' >/dev/null 2>&1 || true
podkop_dnsmasq_changed=1
fi
[ "$podkop_dnsmasq_changed" -eq 1 ] || return 0
uci -q commit dhcp >/dev/null 2>&1 || true
[ -x /etc/init.d/dnsmasq ] && /etc/init.d/dnsmasq restart >/dev/null 2>&1 || true
}
restore_podkop_dnsmasq_failsafe() {
[ -x /usr/bin/krot ] && /usr/bin/krot restore_dnsmasq >/dev/null 2>&1 || true
if [ -r /usr/lib/krot/dnsmasq_failsafe_restore.sh ]; then
sh /usr/lib/krot/dnsmasq_failsafe_restore.sh >/dev/null 2>&1 || true
else
restore_podkop_dnsmasq_failsafe_raw
fi
}
remember_service_state() {
service_status=""
if [ -x /etc/init.d/krot ] && /etc/init.d/krot enabled >/dev/null 2>&1; then
PODKOP_WAS_ENABLED=1
fi
if [ -x /etc/init.d/krot ]; then
service_status="$(/etc/init.d/krot status 2>/dev/null || true)"
if [ "$service_status" = "running" ]; then
PODKOP_WAS_RUNNING=1
return 0
fi
fi
if [ -x /etc/init.d/krot ] && /etc/init.d/krot running >/dev/null 2>&1; then
PODKOP_WAS_RUNNING=1
return 0
fi
if [ -x /usr/bin/krot ] && /usr/bin/krot get_status 2>/dev/null | grep -q '"running":1'; then
PODKOP_WAS_RUNNING=1
fi
}
stop_conflicting_services() {
[ -x /etc/init.d/krot ] && /etc/init.d/krot stop >/dev/null 2>&1 || true
restore_podkop_dnsmasq_failsafe
[ -x /etc/init.d/krot ] && /etc/init.d/krot disable >/dev/null 2>&1 || true
}
deactivate_original_podkop_if_present() {
[ -x /etc/init.d/podkop ] || return 0
if /etc/init.d/podkop running >/dev/null 2>&1; then
warn "Detected a running original Podkop service. Stopping it before installing Podkop Plus."
/etc/init.d/podkop stop >/dev/null 2>&1 || warn "Failed to stop the original Podkop service."
fi
if /etc/init.d/podkop enabled >/dev/null 2>&1; then
warn "Detected an enabled original Podkop autostart. Disabling it before installing Podkop Plus."
/etc/init.d/podkop disable >/dev/null 2>&1 || warn "Failed to disable original Podkop autostart."
fi
}
cleanup_legacy_installation() {
backend_package_installed=0
pkg_is_installed "krot" && backend_package_installed=1
remember_service_state
stop_conflicting_services
pkg_remove_matching_prefix "luci-i18n-krot"
pkg_remove_if_installed "luci-app-krot"
if [ "$backend_package_installed" -eq 0 ]; then
rm -rf /usr/lib/krot
rm -f /etc/init.d/krot
rm -f /usr/bin/krot
fi
rm -rf /www/luci-static/resources/view/krot
rm -f /usr/share/luci/menu.d/luci-app-krot.json
rm -f /usr/share/rpcd/acl.d/luci-app-krot.json
rm -f /etc/uci-defaults/50_luci-krot
rm -f /usr/lib/lua/luci/i18n/krot.ru.lmo
rm -f /usr/lib/lua/luci/i18n/krot.en.lmo
rm -f /usr/lib/lua/luci/i18n/krot.ru.lua
rm -f /usr/lib/lua/luci/i18n/krot.en.lua
}
decide_i18n_installation() {
luci_lang="$(get_luci_main_lang)"
if pkg_is_installed "luci-i18n-krot-ru"; then
PODKOP_PLUS_I18N_REQUESTED=1
msg "The Russian interface package is already installed and will be updated."
return 0
fi
case "$luci_lang" in
ru|ru_*|ru-*)
msg "LuCI language is Russian."
;;
esac
if confirm_prompt "Install the Russian interface language package?"; then
PODKOP_PLUS_I18N_REQUESTED=1
return 0
fi
warn "Continuing without the Russian interface language package."
}
download_krot_packages() {
PODKOP_PLUS_BACKEND_FILE="$TMP_DIR/$PODKOP_PLUS_BACKEND_NAME"
PODKOP_PLUS_APP_FILE="$TMP_DIR/$PODKOP_PLUS_APP_NAME"
PODKOP_PLUS_I18N_FILE=""
download_with_retry "$PODKOP_PLUS_BACKEND_URL" "$PODKOP_PLUS_BACKEND_FILE" "$PODKOP_PLUS_BACKEND_NAME" || fail "Failed to download $PODKOP_PLUS_BACKEND_NAME"
download_with_retry "$PODKOP_PLUS_APP_URL" "$PODKOP_PLUS_APP_FILE" "$PODKOP_PLUS_APP_NAME" || fail "Failed to download $PODKOP_PLUS_APP_NAME"
if [ -n "$PODKOP_PLUS_I18N_URL" ]; then
PODKOP_PLUS_I18N_FILE="$TMP_DIR/$PODKOP_PLUS_I18N_NAME"
download_with_retry "$PODKOP_PLUS_I18N_URL" "$PODKOP_PLUS_I18N_FILE" "$PODKOP_PLUS_I18N_NAME" || fail "Failed to download $PODKOP_PLUS_I18N_NAME"
fi
}
install_packages() {
pkg_install_files "$PODKOP_PLUS_BACKEND_FILE" || fail "krot installation failed"
pkg_install_files "$PODKOP_PLUS_APP_FILE" || fail "luci-app-krot installation failed"
if [ -n "$PODKOP_PLUS_I18N_FILE" ]; then
pkg_install_files "$PODKOP_PLUS_I18N_FILE" || fail "luci-i18n-krot-ru installation failed"
fi
}
post_install() {
rm -f /var/luci-indexcache* /tmp/luci-indexcache*
rm -f /tmp/krot.latest-version.cache
rm -f /var/run/krot/system-info.json
rm -f /var/run/krot/server-country-cache.json
rm -f /tmp/krot/system-info.json
[ -x /etc/init.d/rpcd ] && /etc/init.d/rpcd reload >/dev/null 2>&1 || true
if [ "$PODKOP_WAS_ENABLED" -eq 1 ] && [ -x /etc/init.d/krot ]; then
/etc/init.d/krot enable >/dev/null 2>&1 || true
fi
if [ "$PODKOP_WAS_RUNNING" -eq 1 ] && [ -x /etc/init.d/krot ]; then
/etc/init.d/krot start >/dev/null 2>&1 || /etc/init.d/krot restart >/dev/null 2>&1 || warn "Failed to start Podkop Plus after upgrade."
fi
}
main() {
trap cleanup EXIT HUP INT TERM
parse_args "$@"
check_root
init_tmp_dir
detect_fetcher
sync_time
check_system
decide_i18n_installation
deactivate_original_podkop_if_present
pkg_list_update || fail "Failed to update package lists"
ensure_bootstrap_tool "ucode" "ucode"
resolve_krot_release
remove_conflicting_dns_proxy
remove_old_sing_box_if_needed
cleanup_legacy_installation
download_krot_packages
install_packages
post_install
msg "Podkop Plus $PODKOP_PLUS_PACKAGE_VERSION has been installed successfully"
msg "Source release: ${REPO_OWNER}/${REPO_NAME}@${PODKOP_PLUS_RELEASE_TAG}"
warn "Open LuCI and review your rules before enabling Podkop Plus"
}
main "$@"