-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·384 lines (328 loc) · 9.04 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·384 lines (328 loc) · 9.04 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
#!/bin/sh
# SPDX-License-Identifier: MIT
set -eu
REPO=${IPREGION_REPO:-romanilyin/ipregion-openwrt}
RELEASE=${IPREGION_RELEASE:-latest}
INSTALL_LUCI=${IPREGION_INSTALL_LUCI:-1}
APK_UPDATE=${IPREGION_APK_UPDATE:-1}
APK_FLAGS=${IPREGION_APK_FLAGS:---allow-untrusted}
GITHUB_API=${IPREGION_GITHUB_API:-https://api.github.com}
GITHUB_DOWNLOAD_BASE=${IPREGION_GITHUB_DOWNLOAD_BASE:-https://github.com}
DOWNLOAD_RETRIES=${IPREGION_DOWNLOAD_RETRIES:-3}
DOWNLOAD_RETRY_DELAY=${IPREGION_DOWNLOAD_RETRY_DELAY:-2}
TMP_DIR=${TMPDIR:-/tmp}/ipregion-install.$$
RELEASE_JSON=$TMP_DIR/release.json
DOWNLOAD_ERR=$TMP_DIR/download.err
GITHUB_API=${GITHUB_API%/}
GITHUB_DOWNLOAD_BASE=${GITHUB_DOWNLOAD_BASE%/}
log() {
printf '%s\n' "ipregion-install: $*"
}
die() {
printf '%s\n' "ipregion-install: error: $*" >&2
exit 1
}
cleanup() {
rm -rf "$TMP_DIR"
}
trap cleanup EXIT HUP INT TERM
need_cmd() {
command -v "$1" >/dev/null 2>&1 || die "missing required command: $1"
}
is_luci_enabled() {
case "$INSTALL_LUCI" in
0|false|FALSE|no|NO) return 1 ;;
*) return 0 ;;
esac
}
version_ge() {
left=$1
right=$2
old_ifs=$IFS
IFS=.
set -- $left
l1=${1:-0}; l2=${2:-0}; l3=${3:-0}
set -- $right
r1=${1:-0}; r2=${2:-0}; r3=${3:-0}
IFS=$old_ifs
[ "$l1" -gt "$r1" ] && return 0
[ "$l1" -lt "$r1" ] && return 1
[ "$l2" -gt "$r2" ] && return 0
[ "$l2" -lt "$r2" ] && return 1
[ "$l3" -ge "$r3" ]
}
check_target() {
need_cmd id
need_cmd sed
need_cmd awk
[ "$(id -u)" = 0 ] || die "run this script as root on the router"
need_cmd apk
if [ -r /etc/openwrt_release ]; then
release=$(sed -n "s/^DISTRIB_RELEASE='\([^']*\)'.*/\1/p" /etc/openwrt_release | sed 's/-.*$//' || true)
case "$release" in
''|SNAPSHOT) ;;
*[!0-9.]*) log "warning: could not parse OpenWrt release; expected 25.12.1+ with apk" ;;
*)
version_ge "$release" 25.12.1 || die "OpenWrt $release is unsupported by this APK installer; expected 25.12.1+. For OpenWrt 24.10 use install-ipk.sh"
;;
esac
else
log "warning: /etc/openwrt_release not found; continuing because apk is available"
fi
}
validate_inputs() {
case "$DOWNLOAD_RETRIES" in
''|*[!0-9]*) DOWNLOAD_RETRIES=3 ;;
esac
case "$DOWNLOAD_RETRY_DELAY" in
''|*[!0-9]*) DOWNLOAD_RETRY_DELAY=2 ;;
esac
[ "$DOWNLOAD_RETRIES" -ge 1 ] || DOWNLOAD_RETRIES=1
case "$REPO" in
*/*) ;;
*) die "IPREGION_REPO must look like owner/repo" ;;
esac
case "$REPO" in
*[!A-Za-z0-9._/-]*) die "IPREGION_REPO contains unsupported characters" ;;
esac
case "$RELEASE" in
*[!A-Za-z0-9._-]*) die "IPREGION_RELEASE contains unsupported characters" ;;
esac
case "$GITHUB_API" in
http://*|https://*) ;;
*) die "IPREGION_GITHUB_API must start with http:// or https://" ;;
esac
case "$GITHUB_DOWNLOAD_BASE" in
http://*|https://*) ;;
*) die "IPREGION_GITHUB_DOWNLOAD_BASE must start with http:// or https://" ;;
esac
}
select_downloader() {
if command -v wget >/dev/null 2>&1; then
DOWNLOADER=wget
elif command -v uclient-fetch >/dev/null 2>&1; then
DOWNLOADER=uclient-fetch
else
die "missing downloader: install wget or uclient-fetch"
fi
}
download_once() {
url=$1
out=$2
rm -f "$DOWNLOAD_ERR"
case "$DOWNLOADER" in
wget) wget -q -O "$out" "$url" 2>"$DOWNLOAD_ERR" ;;
uclient-fetch) uclient-fetch -q -O "$out" "$url" 2>"$DOWNLOAD_ERR" ;;
*) die "internal downloader error" ;;
esac
}
download_error() {
sed -n '1p' "$DOWNLOAD_ERR" 2>/dev/null || true
}
download() {
url=$1
out=$2
label=$3
attempt=1
rm -f "$out"
while [ "$attempt" -le "$DOWNLOAD_RETRIES" ]; do
if [ "$DOWNLOAD_RETRIES" -gt 1 ]; then
log "downloading $label (attempt $attempt/$DOWNLOAD_RETRIES)"
fi
if download_once "$url" "$out"; then
return 0
fi
err=$(download_error)
if [ -n "$err" ]; then
log "download failed for $label: $err"
else
log "download failed for $label"
fi
rm -f "$out"
if [ "$attempt" -lt "$DOWNLOAD_RETRIES" ] && [ "$DOWNLOAD_RETRY_DELAY" -gt 0 ]; then
sleep "$DOWNLOAD_RETRY_DELAY"
fi
attempt=$((attempt + 1))
done
return 1
}
release_api_url() {
if [ "$RELEASE" = latest ]; then
printf '%s/repos/%s/releases/latest\n' "$GITHUB_API" "$REPO"
else
printf '%s/repos/%s/releases/tags/%s\n' "$GITHUB_API" "$REPO" "$RELEASE"
fi
}
direct_release_asset_url() {
pkg=$1
if [ "$RELEASE" = latest ]; then
printf '%s/%s/releases/latest/download/%s.apk\n' "$GITHUB_DOWNLOAD_BASE" "$REPO" "$pkg"
else
printf '%s/%s/releases/download/%s/%s.apk\n' "$GITHUB_DOWNLOAD_BASE" "$REPO" "$RELEASE" "$pkg"
fi
}
metadata_has_assets() {
awk '/"browser_download_url"[[:space:]]*:/ { found = 1 } END { exit found ? 0 : 1 }' "$RELEASE_JSON"
}
metadata_error_message() {
awk '
match($0, /"message"[[:space:]]*:[[:space:]]*"[^"]*"/) {
msg = substr($0, RSTART, RLENGTH)
sub(/^"message"[[:space:]]*:[[:space:]]*"/, "", msg)
sub(/"$/, "", msg)
print msg
exit
}
' "$RELEASE_JSON"
}
remove_downloaded_packages() {
rm -f "$TMP_DIR/ipregion.apk" "$TMP_DIR/luci-app-ipregion.apk" "$TMP_DIR/luci-i18n-ipregion-ru.apk"
}
asset_url_for() {
pkg=$1
awk -v pkg="$pkg" '
function basename(url, name) {
name = url
sub(/[?#].*$/, "", name)
sub(/^.*\//, "", name)
return name
}
function wanted(name) {
return name == pkg ".apk" || name ~ ("^" pkg "[-_].*\\.apk$")
}
function consider(url, name) {
gsub(/\\\//, "/", url)
name = basename(url)
if (name == pkg ".apk") {
print url
found = 1
exit
}
if (fallback == "" && wanted(name))
fallback = url
}
{
line = $0
while (match(line, /"browser_download_url"[[:space:]]*:[[:space:]]*"[^"]*"/)) {
url = substr(line, RSTART, RLENGTH)
sub(/^"browser_download_url"[[:space:]]*:[[:space:]]*"/, "", url)
sub(/"$/, "", url)
consider(url)
line = substr(line, RSTART + RLENGTH)
}
}
END {
if (!found && fallback != "")
print fallback
}
' "$RELEASE_JSON"
}
fetch_release_metadata() {
url=$(release_api_url)
log "reading release metadata from $url"
download "$url" "$RELEASE_JSON" "GitHub release metadata" || return 1
if ! metadata_has_assets; then
message=$(metadata_error_message)
if [ -n "$message" ]; then
log "GitHub API response: $message"
else
log "GitHub API response did not contain release assets"
fi
return 1
fi
}
fetch_package() {
pkg=$1
out=$TMP_DIR/$pkg.apk
url=$(asset_url_for "$pkg")
[ -n "$url" ] || { log "release asset not found in metadata: $pkg*.apk"; return 1; }
log "downloading $pkg"
download "$url" "$out" "$pkg" || return 1
[ -s "$out" ] || { log "downloaded empty package: $pkg"; return 1; }
}
fetch_direct_package() {
pkg=$1
out=$TMP_DIR/$pkg.apk
url=$(direct_release_asset_url "$pkg")
log "downloading $pkg from direct release asset"
download "$url" "$out" "$pkg direct release asset" || return 1
[ -s "$out" ] || { log "downloaded empty package: $pkg"; return 1; }
}
fetch_metadata_packages() {
fetch_release_metadata || return 1
fetch_package ipregion || return 1
if is_luci_enabled; then
fetch_package luci-app-ipregion || return 1
fetch_package luci-i18n-ipregion-ru || return 1
fi
}
fetch_direct_packages() {
fetch_direct_package ipregion || return 1
if is_luci_enabled; then
fetch_direct_package luci-app-ipregion || return 1
fetch_direct_package luci-i18n-ipregion-ru || return 1
fi
}
probe_url() {
label=$1
url=$2
if download_once "$url" /dev/null; then
log "$label reachable"
else
err=$(download_error)
if [ -n "$err" ]; then
log "$label not reachable: $err"
else
log "$label not reachable"
fi
fi
}
diagnose_github_access() {
log "checking GitHub connectivity from this router"
probe_url "GitHub release page" "$GITHUB_DOWNLOAD_BASE/$REPO/releases"
probe_url "GitHub API" "$GITHUB_API"
probe_url "GitHub raw content" "https://raw.githubusercontent.com/$REPO/main/install.sh"
log "if GitHub is blocked or rate-limited, retry later or install downloaded APK files manually"
}
install_packages() {
if [ "$APK_UPDATE" = 1 ]; then
log "updating apk repositories"
apk update
fi
if is_luci_enabled; then
log "installing ipregion, luci-app-ipregion and Russian translation"
apk add $APK_FLAGS "$TMP_DIR/ipregion.apk" "$TMP_DIR/luci-app-ipregion.apk" "$TMP_DIR/luci-i18n-ipregion-ru.apk"
else
log "installing ipregion"
apk add $APK_FLAGS "$TMP_DIR/ipregion.apk"
fi
}
post_install() {
if command -v ipregion >/dev/null 2>&1; then
ipregion --help >/dev/null || die "ipregion command was installed but did not start"
fi
if is_luci_enabled && [ -x /etc/init.d/rpcd ]; then
log "restarting rpcd"
/etc/init.d/rpcd restart || log "warning: rpcd restart failed"
fi
if is_luci_enabled && [ -x /etc/init.d/uhttpd ]; then
log "reloading uhttpd"
/etc/init.d/uhttpd reload >/dev/null 2>&1 || true
fi
log "installed successfully"
log "run 'ipregion --self-test --json' to verify router connectivity"
}
mkdir -p "$TMP_DIR"
check_target
validate_inputs
select_downloader
if ! fetch_metadata_packages; then
log "GitHub release metadata path failed; trying direct release asset URLs"
remove_downloaded_packages
if ! fetch_direct_packages; then
diagnose_github_access
die "failed to download GitHub release assets"
fi
fi
install_packages
post_install