forked from slobys/openclash-auto-installer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmosdns.sh
More file actions
executable file
·333 lines (284 loc) · 9.44 KB
/
Copy pathmosdns.sh
File metadata and controls
executable file
·333 lines (284 loc) · 9.44 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
#!/bin/sh
set -eu
LOCKDIR="/tmp/mosdns-install.lock"
TMP_ROOT="/tmp/mosdns-install"
MOSDNS_REPO="sbwml/luci-app-mosdns"
MOSDNS_API="https://api.github.com/repos/$MOSDNS_REPO/releases/latest"
MOSDNS_RELEASE_URL="https://github.com/$MOSDNS_REPO/releases/latest"
RESTART_SERVICES="1"
FORCE_PKG_UPDATE="1"
cleanup() {
rm -rf "$TMP_ROOT"
rmdir "$LOCKDIR" 2>/dev/null || true
}
trap cleanup EXIT INT TERM
log() {
printf '%s\n' "==> $*"
}
warn() {
printf '%s\n' "[WARN] $*" >&2
}
die() {
printf '%s\n' "[ERROR] $*" >&2
exit 1
}
need_cmd() {
command -v "$1" >/dev/null 2>&1 || die "Missing command: $1"
}
usage() {
cat <<'EOF_USAGE'
usage:
sh mosdns.sh [Options]
Options:
--skip-restart Don't try to enable when done / Restart mosdns
--skip-pkg-update jump over opkg update / apk update
-h, --help show help
EOF_USAGE
}
parse_args() {
while [ "$#" -gt 0 ]; do
case "$1" in
--skip-restart)
RESTART_SERVICES="0"
;;
--skip-pkg-update)
FORCE_PKG_UPDATE="0"
;;
-h|--help)
usage
exit 0
;;
*)
die "unknown parameters: $1"
;;
esac
shift
done
}
detect_pkg_mgr() {
if command -v opkg >/dev/null 2>&1; then
printf 'opkg'
elif command -v apk >/dev/null 2>&1; then
printf 'apk'
else
die "not detected opkg or apk, the current system does not support it yet"
fi
}
get_distr_arch() {
if [ -f /etc/openwrt_release ]; then
# shellcheck disable=SC1091
. /etc/openwrt_release >/dev/null 2>&1 || true
printf '%s' "${DISTRIB_ARCH:-}"
else
printf ''
fi
}
select_sdk() {
PKG_MGR="$1"
case "$PKG_MGR" in
apk)
printf 'openwrt-25.12'
;;
opkg)
printf 'openwrt-24.10'
;;
*)
die "Unknown package manager: $PKG_MGR"
;;
esac
}
download_url() {
URL="$1"
OUT="$2"
if command -v curl >/dev/null 2>&1; then
curl -fsSL --retry 3 --connect-timeout 15 \
-A "openclaw-openwrt-installer" \
"$URL" -o "$OUT"
elif command -v wget >/dev/null 2>&1; then
wget -qO "$OUT" --user-agent="openclaw-openwrt-installer" "$URL"
else
die "Lack curl or wget, unable to download file"
fi
}
download_github_api() {
URL="$1"
OUT="$2"
if command -v curl >/dev/null 2>&1; then
curl -fsSL --retry 3 --connect-timeout 15 \
-A "openclaw-openwrt-installer" \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"$URL" -o "$OUT"
elif command -v wget >/dev/null 2>&1; then
wget -qO "$OUT" \
--user-agent="openclaw-openwrt-installer" \
--header="Accept: application/vnd.github+json" \
--header="X-GitHub-Api-Version: 2022-11-28" \
"$URL"
else
die "Lack curl or wget, unable to download file"
fi
}
find_asset_url() {
ASSET_NAME="$1"
if [ -f "$TMP_ROOT/release.json" ]; then
JSON_URL="$(sed 's/"browser_download_url"/\
"browser_download_url"/g' "$TMP_ROOT/release.json" |
sed -n 's/^"browser_download_url"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' |
grep "/$ASSET_NAME$" |
head -n1 || true)"
if [ -n "$JSON_URL" ]; then
printf '%s\n' "$JSON_URL"
return 0
fi
fi
for html in "$TMP_ROOT/release-assets.html" "$TMP_ROOT/release.html"; do
[ -f "$html" ] || continue
HTML_URL="$(grep -o "/$MOSDNS_REPO/releases/download/[^\"'<> ]*/$ASSET_NAME" "$html" | head -n1 || true)"
[ -n "$HTML_URL" ] && printf 'https://github.com%s\n' "$HTML_URL"
[ -n "$HTML_URL" ] && return 0
done
return 0
}
fetch_release_meta() {
if download_github_api "$MOSDNS_API" "$TMP_ROOT/release.json"; then
return 0
fi
warn "GitHub API Get MosDNS Release Information failed, use instead releases Page details"
download_url "$MOSDNS_RELEASE_URL" "$TMP_ROOT/release.html" || die "Get MosDNS up to date Release Message failed"
RELEASE_TAG="$(sed -n 's|.*href="/'"$MOSDNS_REPO"'/releases/tag/\([^"/?#]*\)".*|\1|p' "$TMP_ROOT/release.html" | head -n1 || true)"
if [ -n "$RELEASE_TAG" ]; then
download_url "https://github.com/$MOSDNS_REPO/releases/expanded_assets/$RELEASE_TAG" "$TMP_ROOT/release-assets.html" || warn "Get MosDNS Release Asset list failed"
fi
}
get_installed_version() {
PKG_MGR="$1"
case "$PKG_MGR" in
opkg)
opkg status mosdns 2>/dev/null | sed -n 's/^Version: //p' | head -n1 || true
;;
apk)
apk info -a mosdns 2>/dev/null | sed -n 's/^version: //p' | head -n1 || true
;;
esac
}
maybe_update_index() {
PKG_MGR="$1"
if [ "$FORCE_PKG_UPDATE" != "1" ]; then
log "Skip software source updates by parameter"
return 0
fi
case "$PKG_MGR" in
opkg)
log "refresh opkg Software source index"
opkg update || warn "opkg update Failure, will continue to try to install GitHub Release Bag"
;;
apk)
log "refresh apk Software source index"
apk update || warn "apk update Failure, will continue to try to install GitHub Release Bag"
;;
esac
}
check_runtime() {
[ -f /etc/openwrt_release ] || die "not detected /etc/openwrt_release, the current environment is not like OpenWrt"
[ -d /usr/share/luci/menu.d ] || die "current LuCI The version may be too old and not found /usr/share/luci/menu.d"
ROOT_SPACE="$(df -m /usr | awk 'END{print $4}' 2>/dev/null || printf 0)"
case "$ROOT_SPACE" in
''|*[!0-9]*) ROOT_SPACE=0 ;;
esac
if [ "$ROOT_SPACE" -lt 35 ]; then
die "system /usr Available space is less than 35MB, it is not recommended to continue the installation MosDNS"
fi
}
install_release_archive() {
PKG_MGR="$1"
DISTR_ARCH="$2"
SDK="$3"
ASSET_NAME="${DISTR_ARCH}-${SDK}.tar.gz"
fetch_release_meta
ASSET_URL="$(find_asset_url "$ASSET_NAME")"
[ -n "$ASSET_URL" ] || die "Not found for current architecture MosDNS Release Bag: $ASSET_NAME"
ARCHIVE="$TMP_ROOT/$ASSET_NAME"
EXTRACT_DIR="$TMP_ROOT/extract"
mkdir -p "$EXTRACT_DIR"
log "download MosDNS Release Bag: $ASSET_NAME"
download_url "$ASSET_URL" "$ARCHIVE" || die "download MosDNS Release Package failed"
log "Unzip MosDNS Release Bag"
tar -zxf "$ARCHIVE" -C "$EXTRACT_DIR" || die "Unzip MosDNS Release Package failed"
if [ -x /etc/init.d/mosdns ]; then
log "stop MosDNS Serve"
/etc/init.d/mosdns stop >/dev/null 2>&1 || true
fi
case "$PKG_MGR" in
opkg)
INSTALL_CMD="opkg install --force-downgrade"
;;
apk)
INSTALL_CMD="apk add --allow-untrusted"
;;
esac
log "Install / renew MosDNS Related packages"
for pkg in \
"$EXTRACT_DIR"/packages_ci/v2dat*.* \
"$EXTRACT_DIR"/packages_ci/v2ray-geoip*.* \
"$EXTRACT_DIR"/packages_ci/v2ray-geosite*.* \
"$EXTRACT_DIR"/packages_ci/mosdns*.* \
"$EXTRACT_DIR"/packages_ci/luci-app-mosdns*.* \
"$EXTRACT_DIR"/packages_ci/luci-i18n-mosdns-zh-cn*.*; do
[ -f "$pkg" ] || continue
# shellcheck disable=SC2086
$INSTALL_CMD "$pkg" || die "Installation failed: $(basename "$pkg")"
done
}
refresh_luci() {
rm -rf /tmp/luci-* /tmp/.luci* /tmp/etc/config/ucitrack /var/run/luci-indexcache 2>/dev/null || true
if [ -x /etc/init.d/rpcd ]; then
/etc/init.d/rpcd restart >/dev/null 2>&1 || warn "rpcd Restart failed"
fi
}
restart_mosdns() {
if [ "$RESTART_SERVICES" != "1" ]; then
log "Skip by parameter mosdns enable / Restart"
return 0
fi
if [ -x /etc/init.d/mosdns ]; then
/etc/init.d/mosdns enable >/dev/null 2>&1 || warn "mosdns enable fail"
/etc/init.d/mosdns restart >/dev/null 2>&1 || warn "mosdns restart fail"
else
warn "not found /etc/init.d/mosdns, skip service restart"
fi
}
main() {
parse_args "$@"
if ! mkdir "$LOCKDIR" 2>/dev/null; then
die "There is already another MosDNS Task is running"
fi
mkdir -p "$TMP_ROOT"
need_cmd sed
need_cmd grep
need_cmd head
need_cmd basename
need_cmd tar
need_cmd df
need_cmd awk
check_runtime
PKG_MGR="$(detect_pkg_mgr)"
DISTR_ARCH="$(get_distr_arch)"
[ -n "$DISTR_ARCH" ] || die "Unable to read DISTRIB_ARCH, currently does not support the current system"
SDK="$(select_sdk "$PKG_MGR")"
log "Package manager detected: $PKG_MGR"
log "detected MosDNS Architecture: $DISTR_ARCH"
log "Use build version: $SDK"
OLD_VER="$(get_installed_version "$PKG_MGR")"
log "Currently installed version: ${OLD_VER:-not installed}"
maybe_update_index "$PKG_MGR"
install_release_archive "$PKG_MGR" "$DISTR_ARCH" "$SDK"
restart_mosdns
refresh_luci
NEW_VER="$(get_installed_version "$PKG_MGR")"
log "Post-installation version: ${NEW_VER:-unknown}"
warn "Not actively rewritten by default /etc/config/mosdns;please LuCI Click on your network environment to enable or adjust DNS Offload settings"
warn "if LuCI The menu does not appear immediately, please refresh the page or log in again LuCI"
log "MosDNS Processing completed"
}
main "$@"