Auto-install router WOL support
This commit is contained in:
1 parent
fa4897c647
commit
307cb87712
2 files changed
+163
-5
No files matched your search
+73
@@ -12,6 +12,10 @@ info() {
|
|||||||
printf '%s\n' "$*"
|
printf '%s\n' "$*"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
warn() {
|
||||||
|
printf 'WARN: %s\n' "$*" >&2
|
||||||
|
}
|
||||||
|
|
||||||
die() {
|
die() {
|
||||||
printf 'ERROR: %s\n' "$*" >&2
|
printf 'ERROR: %s\n' "$*" >&2
|
||||||
exit 1
|
exit 1
|
||||||
@@ -134,6 +138,73 @@ package_manager() {
|
|||||||
printf 'unknown'
|
printf 'unknown'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
package_installed() {
|
||||||
|
local pkg manager
|
||||||
|
pkg="$1"
|
||||||
|
manager="$(package_manager)"
|
||||||
|
case "$manager" in
|
||||||
|
opkg)
|
||||||
|
opkg list-installed "$pkg" 2>/dev/null | grep -Fq "$pkg"
|
||||||
|
;;
|
||||||
|
apk)
|
||||||
|
apk info -e "$pkg" >/dev/null 2>&1
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
return 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
install_packages() {
|
||||||
|
local manager
|
||||||
|
manager="$(package_manager)"
|
||||||
|
case "$manager" in
|
||||||
|
opkg)
|
||||||
|
opkg update >/dev/null 2>&1 || warn "не удалось обновить список opkg, пробую ставить пакеты как есть"
|
||||||
|
opkg install "$@"
|
||||||
|
;;
|
||||||
|
apk)
|
||||||
|
apk update >/dev/null 2>&1 || warn "не удалось обновить список apk, пробую ставить пакеты как есть"
|
||||||
|
apk add "$@"
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
return 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
ensure_wol_support() {
|
||||||
|
local need manager tool_pkg candidate
|
||||||
|
[ "${ROOT%/}" = "" ] || return 0
|
||||||
|
manager="$(package_manager)"
|
||||||
|
[ "$manager" != "unknown" ] || {
|
||||||
|
warn "не найден пакетный менеджер для установки WOL-пакетов"
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
need=""
|
||||||
|
if ! package_installed "luci-app-wol"; then
|
||||||
|
need="$need luci-app-wol"
|
||||||
|
fi
|
||||||
|
if ! command -v etherwake >/dev/null 2>&1 && ! command -v wakeonlan >/dev/null 2>&1 && ! command -v wol >/dev/null 2>&1; then
|
||||||
|
tool_pkg="etherwake"
|
||||||
|
for candidate in etherwake wakeonlan wol; do
|
||||||
|
if package_installed "$candidate"; then
|
||||||
|
tool_pkg=""
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
if [ -n "${tool_pkg:-}" ]; then
|
||||||
|
need="$need $tool_pkg"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
need="${need# }"
|
||||||
|
[ -n "$need" ] || return 0
|
||||||
|
info "Устанавливаю поддержку Wake-on-LAN: $need"
|
||||||
|
if ! install_packages $need; then
|
||||||
|
warn "не удалось установить WOL-пакеты автоматически: $need"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
install_xray_runtime() {
|
install_xray_runtime() {
|
||||||
local remote_bin
|
local remote_bin
|
||||||
[ "${ROOT%/}" = "" ] || return 0
|
[ "${ROOT%/}" = "" ] || return 0
|
||||||
@@ -152,6 +223,8 @@ install_file "usr/share/luci/menu.d/luci-app-owrt-remote.json" 0644
|
|||||||
install_file "usr/share/rpcd/acl.d/luci-app-owrt-remote.json" 0644
|
install_file "usr/share/rpcd/acl.d/luci-app-owrt-remote.json" 0644
|
||||||
install_file "www/luci-static/resources/view/owrt_remote.js" 0644
|
install_file "www/luci-static/resources/view/owrt_remote.js" 0644
|
||||||
|
|
||||||
|
ensure_wol_support
|
||||||
|
|
||||||
rm -f "$(target_path usr/lib/lua/luci/controller/owrt_remote.lua)" 2>/dev/null || true
|
rm -f "$(target_path usr/lib/lua/luci/controller/owrt_remote.lua)" 2>/dev/null || true
|
||||||
rm -rf "$(target_path tmp/luci-indexcache)" "$(target_path tmp/luci-modulecache)" "$(target_path tmp/luci-indexcache.)"* "$(target_path tmp/luci-modulecache.)"* 2>/dev/null || true
|
rm -rf "$(target_path tmp/luci-indexcache)" "$(target_path tmp/luci-modulecache)" "$(target_path tmp/luci-indexcache.)"* "$(target_path tmp/luci-modulecache.)"* 2>/dev/null || true
|
||||||
|
|
||||||
|
|||||||
+90
-5
@@ -2477,10 +2477,10 @@ function renderWolPanel(router) {{
|
|||||||
</label>
|
</label>
|
||||||
<label class="wolField">
|
<label class="wolField">
|
||||||
<span>SSH пароль</span>
|
<span>SSH пароль</span>
|
||||||
<input type="password" data-wol-password="${{escapeAttr(router.id)}}" value="${{escapeAttr(state.sshPassword || '')}}" placeholder="Если SSH по паролю"${{state.loading || state.waking ? ' disabled' : ''}}>
|
<input type="password" data-wol-password="${{escapeAttr(router.id)}}" value="${{escapeAttr(state.sshPassword || '')}}" placeholder="Если SSH по паролю" autocomplete="current-password" autocapitalize="off" autocorrect="off" spellcheck="false" inputmode="text" enterkeyhint="done"${{state.loading || state.waking ? ' disabled' : ''}}>
|
||||||
</label>
|
</label>
|
||||||
<button class="btn" type="button" data-wol-refresh="${{escapeAttr(router.id)}}"${{state.loading || state.waking ? ' disabled' : ''}}>Обновить</button>
|
<button class="btn" type="button" data-wol-refresh="${{escapeAttr(router.id)}}"${{state.loading || state.waking ? ' disabled' : ''}}>Обновить</button>
|
||||||
<button class="btn primary" type="button" data-wol-send="${{escapeAttr(router.id)}}"${{state.loading || state.waking || !devices.length ? ' disabled' : ''}}>Разбудить</button>
|
<button class="btn primary" type="button" data-wol-send="${{escapeAttr(router.id)}}"${{wakeDisabled ? ' disabled' : ''}}>Разбудить</button>
|
||||||
</div>
|
</div>
|
||||||
<div class="${{metaClass}}">${{escapeHtml(wolMetaText(state))}}</div>
|
<div class="${{metaClass}}">${{escapeHtml(wolMetaText(state))}}</div>
|
||||||
</div>`;
|
</div>`;
|
||||||
@@ -2526,10 +2526,10 @@ function renderWolPanelRich(router) {{
|
|||||||
</div>
|
</div>
|
||||||
<label class="wolField">
|
<label class="wolField">
|
||||||
<span>SSH пароль</span>
|
<span>SSH пароль</span>
|
||||||
<input type="password" data-wol-password="${{escapeAttr(router.id)}}" value="${{escapeAttr(state.sshPassword || '')}}" placeholder="Если SSH по паролю"${{state.loading || state.waking ? ' disabled' : ''}}>
|
<input type="password" data-wol-password="${{escapeAttr(router.id)}}" value="${{escapeAttr(state.sshPassword || '')}}" placeholder="Если SSH по паролю" autocomplete="current-password" autocapitalize="off" autocorrect="off" spellcheck="false" inputmode="text" enterkeyhint="done"${{state.loading || state.waking ? ' disabled' : ''}}>
|
||||||
</label>
|
</label>
|
||||||
<button class="btn" type="button" data-wol-refresh="${{escapeAttr(router.id)}}"${{state.loading || state.waking ? ' disabled' : ''}}>Обновить</button>
|
<button class="btn" type="button" data-wol-refresh="${{escapeAttr(router.id)}}"${{state.loading || state.waking ? ' disabled' : ''}}>Обновить</button>
|
||||||
<button class="btn primary" type="button" data-wol-send="${{escapeAttr(router.id)}}"${{state.loading || state.waking || !devices.length ? ' disabled' : ''}}>Разбудить</button>
|
<button class="btn primary" type="button" data-wol-send="${{escapeAttr(router.id)}}"${{wakeDisabled ? ' disabled' : ''}}>Разбудить</button>
|
||||||
</div>
|
</div>
|
||||||
<div class="${{metaClass}}">${{escapeHtml(wolMetaText(state))}}</div>
|
<div class="${{metaClass}}">${{escapeHtml(wolMetaText(state))}}</div>
|
||||||
</div>`;
|
</div>`;
|
||||||
@@ -3956,11 +3956,18 @@ cards.addEventListener('input', (ev) => {{
|
|||||||
const routerId = ev.target?.dataset?.wolPassword;
|
const routerId = ev.target?.dataset?.wolPassword;
|
||||||
if (!routerId) return;
|
if (!routerId) return;
|
||||||
const value = ev.target.value || '';
|
const value = ev.target.value || '';
|
||||||
|
ev.stopPropagation();
|
||||||
saveWolPassword(routerId, value);
|
saveWolPassword(routerId, value);
|
||||||
setWolState(routerId, {{sshPassword: value, error: '', message: ''}});
|
const state = getWolState(routerId);
|
||||||
|
wolStateByRouter.set(String(routerId), Object.assign({{}}, state, {{sshPassword: value, error: '', message: ''}}));
|
||||||
}});
|
}});
|
||||||
|
|
||||||
cards.addEventListener('click', async (ev) => {{
|
cards.addEventListener('click', async (ev) => {{
|
||||||
|
const wolPasswordField = ev.target.closest('[data-wol-password]');
|
||||||
|
if (wolPasswordField) {{
|
||||||
|
ev.stopPropagation();
|
||||||
|
return;
|
||||||
|
}}
|
||||||
const toggleId = ev.target?.dataset?.cardToggle;
|
const toggleId = ev.target?.dataset?.cardToggle;
|
||||||
if (toggleId) {{
|
if (toggleId) {{
|
||||||
const body = document.getElementById(toggleId);
|
const body = document.getElementById(toggleId);
|
||||||
@@ -5736,6 +5743,70 @@ cat "$tmp"
|
|||||||
item["source"] = ", ".join(item.pop("sources", []))
|
item["source"] = ", ".join(item.pop("sources", []))
|
||||||
return ordered
|
return ordered
|
||||||
|
|
||||||
|
def ensure_router_wol_support(self, row, ssh_password=""):
|
||||||
|
script = r"""
|
||||||
|
set -eu
|
||||||
|
|
||||||
|
has_wol_tool() {
|
||||||
|
command -v etherwake >/dev/null 2>&1 || command -v wakeonlan >/dev/null 2>&1 || command -v wol >/dev/null 2>&1
|
||||||
|
}
|
||||||
|
|
||||||
|
install_pkg() {
|
||||||
|
pkg="$1"
|
||||||
|
case "$PKG_MANAGER" in
|
||||||
|
opkg)
|
||||||
|
opkg install "$pkg" >/dev/null 2>&1
|
||||||
|
;;
|
||||||
|
apk)
|
||||||
|
apk add "$pkg" >/dev/null 2>&1
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
return 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
if has_wol_tool; then
|
||||||
|
echo "WOL tool already present on router"
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
PKG_MANAGER=""
|
||||||
|
if command -v apk >/dev/null 2>&1; then
|
||||||
|
PKG_MANAGER="apk"
|
||||||
|
elif command -v opkg >/dev/null 2>&1; then
|
||||||
|
PKG_MANAGER="opkg"
|
||||||
|
fi
|
||||||
|
|
||||||
|
[ -n "$PKG_MANAGER" ] || {
|
||||||
|
echo "No package manager found to install WOL support." >&2
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
case "$PKG_MANAGER" in
|
||||||
|
opkg)
|
||||||
|
opkg update >/dev/null 2>&1 || true
|
||||||
|
;;
|
||||||
|
apk)
|
||||||
|
apk update >/dev/null 2>&1 || true
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
install_pkg luci-app-wol || true
|
||||||
|
install_pkg etherwake || true
|
||||||
|
install_pkg wakeonlan || true
|
||||||
|
install_pkg wol || true
|
||||||
|
|
||||||
|
if has_wol_tool; then
|
||||||
|
echo "WOL support installed via $PKG_MANAGER"
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "WOL package installation finished, but no etherwake/wakeonlan/wol command is available." >&2
|
||||||
|
exit 1
|
||||||
|
"""
|
||||||
|
return self.run_router_ssh_script(row, script, timeout=75, ssh_password=ssh_password).strip()
|
||||||
|
|
||||||
def send_router_wol_packet(self, row, mac, iface="", ssh_password=""):
|
def send_router_wol_packet(self, row, mac, iface="", ssh_password=""):
|
||||||
clean_mac = self.normalize_wol_mac(mac)
|
clean_mac = self.normalize_wol_mac(mac)
|
||||||
if not clean_mac:
|
if not clean_mac:
|
||||||
@@ -5766,6 +5837,7 @@ fi
|
|||||||
echo "No WOL tool found on router. Install etherwake, wakeonlan or wol." >&2
|
echo "No WOL tool found on router. Install etherwake, wakeonlan or wol." >&2
|
||||||
exit 127
|
exit 127
|
||||||
"""
|
"""
|
||||||
|
try:
|
||||||
output = self.run_router_ssh_script(
|
output = self.run_router_ssh_script(
|
||||||
row,
|
row,
|
||||||
script,
|
script,
|
||||||
@@ -5773,6 +5845,19 @@ exit 127
|
|||||||
timeout=12,
|
timeout=12,
|
||||||
ssh_password=ssh_password,
|
ssh_password=ssh_password,
|
||||||
)
|
)
|
||||||
|
except RuntimeError as exc:
|
||||||
|
if "No WOL tool found on router" not in str(exc):
|
||||||
|
raise
|
||||||
|
install_output = self.ensure_router_wol_support(row, ssh_password=ssh_password)
|
||||||
|
output = self.run_router_ssh_script(
|
||||||
|
row,
|
||||||
|
script,
|
||||||
|
[clean_mac, str(iface or "")],
|
||||||
|
timeout=18,
|
||||||
|
ssh_password=ssh_password,
|
||||||
|
)
|
||||||
|
if install_output:
|
||||||
|
output = (install_output + "\n" + output).strip()
|
||||||
return {"mac": clean_mac, "iface": str(iface or ""), "output": output.strip() or "Wake packet sent"}
|
return {"mac": clean_mac, "iface": str(iface or ""), "output": output.strip() or "Wake packet sent"}
|
||||||
|
|
||||||
def vps_shell_args(self):
|
def vps_shell_args(self):
|
||||||
|
|||||||
Reference in new issue
Block a user