Fix VPS host persistence and direct WAN routing in v111
This commit is contained in:
1 parent
411f95d6ba
commit
b854d3a001
20 files changed
+2097
-49
No files matched your search
@@ -0,0 +1,258 @@
|
||||
param(
|
||||
[Parameter(Mandatory = $true)][ValidateSet('Router', 'Vps')][string]$Target,
|
||||
[Parameter(Mandatory = $true)][ValidateNotNullOrEmpty()][string]$Machine,
|
||||
[ValidateSet('Install', 'Rollback')][string]$Action = 'Install',
|
||||
[string]$PinnedVpsHost = '193.233.82.38',
|
||||
[ValidatePattern('^[a-zA-Z0-9_-]+$')][string]$WanInterface = 'wan',
|
||||
[switch]$PreserveConnectionSettings,
|
||||
[ValidatePattern('^[a-zA-Z0-9_-]+$')][string]$BackupName = 'host-policy-v111',
|
||||
[switch]$PrepareOnly
|
||||
)
|
||||
|
||||
$ErrorActionPreference = 'Stop'
|
||||
$RepoRoot = Split-Path -Parent $PSScriptRoot
|
||||
$UploadName = 'owrt-host-policy-' + [guid]::NewGuid().ToString('N')
|
||||
$StageRoot = Join-Path ([System.IO.Path]::GetTempPath()) $UploadName
|
||||
$Utf8 = [System.Text.UTF8Encoding]::new($false)
|
||||
$Payloads = @{}
|
||||
if ($PinnedVpsHost -notmatch '^[a-zA-Z0-9][a-zA-Z0-9.:-]*$') { throw 'Invalid VPS host. Use an IP or domain without URL or port.' }
|
||||
|
||||
if ($Target -eq 'Router') {
|
||||
$Payloads = @{
|
||||
'agent' = 'files\usr\sbin\owrt-remote'
|
||||
'wan.sh' = 'files\usr\lib\owrt-remote\wan.sh'
|
||||
'cgi' = 'files\www\cgi-bin\owrt-remote'
|
||||
'luci' = 'files\www\luci-static\resources\view\owrt_remote.js'
|
||||
'init' = 'files\etc\init.d\owrt-remote'
|
||||
}
|
||||
$RemoteScript = @'
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
stage=/tmp/__UPLOAD_NAME__
|
||||
backup=/root/owrt-remote-backup-before-__BACKUP_NAME__
|
||||
config=/etc/config/owrtremote
|
||||
changed=0
|
||||
save_files() {
|
||||
dir=$1
|
||||
mkdir -p "$dir"
|
||||
chmod 700 "$dir"
|
||||
cp -p "$config" "$dir/config"
|
||||
for item in agent wan.sh cgi luci init; do
|
||||
case "$item" in
|
||||
agent) path=/usr/sbin/owrt-remote ;;
|
||||
wan.sh) path=/usr/lib/owrt-remote/wan.sh ;;
|
||||
cgi) path=/www/cgi-bin/owrt-remote ;;
|
||||
luci) path=/www/luci-static/resources/view/owrt_remote.js ;;
|
||||
init) path=/etc/init.d/owrt-remote ;;
|
||||
esac
|
||||
if [ -f "$path" ]; then cp -p "$path" "$dir/$item"; else touch "$dir/$item.absent"; fi
|
||||
done
|
||||
xray_config=$(uci -q get owrtremote.main.xray_config || echo /etc/xray/owrt-remote-client.json)
|
||||
printf '%s' "$xray_config" >"$dir/xray-path"
|
||||
if [ -f "$xray_config" ]; then cp -p "$xray_config" "$dir/xray"; else touch "$dir/xray.absent"; fi
|
||||
chmod 600 "$dir/config" "$dir/xray-path"
|
||||
[ ! -f "$dir/xray" ] || chmod 600 "$dir/xray"
|
||||
touch "$dir/complete"
|
||||
}
|
||||
restore_files() {
|
||||
dir=$1
|
||||
test -f "$dir/complete"
|
||||
/etc/init.d/owrt-remote stop >/dev/null 2>&1 || true
|
||||
/usr/sbin/owrt-remote wan-cleanup >/dev/null 2>&1 || true
|
||||
cp -p "$dir/config" "$config"
|
||||
for item in agent wan.sh cgi luci init; do
|
||||
case "$item" in
|
||||
agent) path=/usr/sbin/owrt-remote ;;
|
||||
wan.sh) path=/usr/lib/owrt-remote/wan.sh ;;
|
||||
cgi) path=/www/cgi-bin/owrt-remote ;;
|
||||
luci) path=/www/luci-static/resources/view/owrt_remote.js ;;
|
||||
init) path=/etc/init.d/owrt-remote ;;
|
||||
esac
|
||||
if [ -f "$dir/$item.absent" ]; then rm -f "$path"; else cp -p "$dir/$item" "$path"; fi
|
||||
done
|
||||
xray_config=$(cat "$dir/xray-path")
|
||||
if [ -f "$dir/xray.absent" ]; then rm -f "$xray_config"; else cp -p "$dir/xray" "$xray_config"; fi
|
||||
/etc/init.d/owrt-remote restart
|
||||
}
|
||||
cleanup() {
|
||||
code=$?
|
||||
trap - EXIT
|
||||
if [ "$code" -ne 0 ] && [ "$changed" = 1 ]; then
|
||||
echo 'Operation failed; restoring this attempt.' >&2
|
||||
logread -e owrt-remote | tail -n 25 >&2 || true
|
||||
restore_files "$stage/previous" || true
|
||||
fi
|
||||
rm -rf "$stage"
|
||||
exit "$code"
|
||||
}
|
||||
trap cleanup EXIT
|
||||
test -s "$config"
|
||||
if [ '__ACTION__' = Install ]; then
|
||||
for item in agent wan.sh cgi init; do test -s "$stage/$item"; sh -n "$stage/$item"; done
|
||||
test -s "$stage/luci"
|
||||
for item in cgi luci; do grep -F -q 'v111' "$stage/$item"; done
|
||||
# curl is needed to preserve HTTPS SNI and bypass the local DNS/proxy.
|
||||
if ! command -v curl >/dev/null 2>&1; then
|
||||
if command -v opkg >/dev/null 2>&1; then opkg update && opkg install curl ca-bundle
|
||||
elif command -v apk >/dev/null 2>&1; then apk add curl ca-certificates
|
||||
else echo 'Install curl and CA certificates first.' >&2; exit 1; fi
|
||||
fi
|
||||
for tool in ip nft ubus jsonfilter nslookup curl; do command -v "$tool" >/dev/null 2>&1 || { echo "Missing tool: $tool" >&2; exit 1; }; done
|
||||
# Check the router's real ip implementation and WAN before stopping Remote.
|
||||
if [ '__PRESERVE_CONNECTION__' = 1 ]; then
|
||||
expected_host=$(uci -q get owrtremote.main.vps_host)
|
||||
expected_mode=$(uci -q get owrtremote.main.vps_host_mode || echo manual)
|
||||
expected_direct=$(uci -q get owrtremote.main.wan_direct || echo 0)
|
||||
if [ "$expected_direct" = 1 ]; then OWRT_REMOTE_WAN_HELPER="$stage/wan.sh" sh "$stage/agent" wan-preflight; fi
|
||||
else
|
||||
expected_host='__PINNED_HOST__'
|
||||
expected_mode=manual
|
||||
expected_direct=1
|
||||
OWRT_REMOTE_WAN_HELPER="$stage/wan.sh" OWRT_REMOTE_WAN_INTERFACE='__WAN_INTERFACE__' sh "$stage/agent" wan-preflight
|
||||
fi
|
||||
if [ ! -e "$backup" ]; then
|
||||
save_files "$stage/retained-backup"
|
||||
mv "$stage/retained-backup" "$backup"
|
||||
fi
|
||||
test -f "$backup/complete"
|
||||
save_files "$stage/previous"
|
||||
changed=1
|
||||
# procd may already have no instance after a previous failed installation.
|
||||
/etc/init.d/owrt-remote stop || true
|
||||
mkdir -p /usr/lib/owrt-remote /www/luci-static/resources/view
|
||||
for item in agent wan.sh cgi luci init; do
|
||||
case "$item" in
|
||||
agent) path=/usr/sbin/owrt-remote; mode=755 ;;
|
||||
wan.sh) path=/usr/lib/owrt-remote/wan.sh; mode=644 ;;
|
||||
cgi) path=/www/cgi-bin/owrt-remote; mode=755 ;;
|
||||
luci) path=/www/luci-static/resources/view/owrt_remote.js; mode=644 ;;
|
||||
init) path=/etc/init.d/owrt-remote; mode=755 ;;
|
||||
esac
|
||||
cp "$stage/$item" "$path.new"
|
||||
chmod "$mode" "$path.new"
|
||||
mv "$path.new" "$path"
|
||||
done
|
||||
if [ '__PRESERVE_CONNECTION__' != 1 ]; then
|
||||
uci set 'owrtremote.main.vps_host=__PINNED_HOST__'
|
||||
uci set owrtremote.main.vps_host_mode=manual
|
||||
uci set owrtremote.main.wan_direct=1
|
||||
uci set 'owrtremote.main.wan_interface=__WAN_INTERFACE__'
|
||||
uci commit owrtremote
|
||||
fi
|
||||
/usr/sbin/owrt-remote render-client >/dev/null
|
||||
/etc/init.d/owrt-remote restart
|
||||
ready=0
|
||||
for attempt in 1 2 3 4 5 6 7 8 9 10; do
|
||||
if /usr/sbin/owrt-remote status | grep -q '^xray_state: running'; then ready=1; break; fi
|
||||
sleep 2
|
||||
done
|
||||
test "$ready" = 1
|
||||
/usr/sbin/owrt-remote heartbeat
|
||||
if [ "$expected_mode" = manual ]; then test "$(uci -q get owrtremote.main.vps_host)" = "$expected_host"; fi
|
||||
test "$(uci -q get owrtremote.main.vps_host_mode || echo manual)" = "$expected_mode"
|
||||
test "$(uci -q get owrtremote.main.wan_direct || echo 0)" = "$expected_direct"
|
||||
if nft list table inet owrt_remote_wan >/dev/null 2>&1; then
|
||||
echo 'Legacy Remote marking table still exists.' >&2
|
||||
exit 1
|
||||
fi
|
||||
/usr/sbin/owrt-remote status
|
||||
grep -F -q 'v111' /www/cgi-bin/owrt-remote
|
||||
grep -F -q 'v111' /www/luci-static/resources/view/owrt_remote.js
|
||||
echo HOST_POLICY_ROUTER_V111_OK
|
||||
else
|
||||
test -f "$backup/complete"
|
||||
save_files "$stage/previous"
|
||||
changed=1
|
||||
restore_files "$backup"
|
||||
/usr/sbin/owrt-remote status
|
||||
echo HOST_POLICY_ROUTER_V111_ROLLBACK_OK
|
||||
fi
|
||||
'@
|
||||
} else {
|
||||
$Payloads = @{
|
||||
'hub.py' = 'vps\owrt-remote-hub.py'
|
||||
'test_host_policy.py' = 'tests\test_host_policy.py'
|
||||
'resources.py' = 'tests\test_resource_lifecycle.py'
|
||||
'recaptcha.py' = 'tests\test_recaptcha.py'
|
||||
}
|
||||
$RemoteScript = @'
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
stage=/tmp/__UPLOAD_NAME__
|
||||
target=/opt/owrt-remote/owrt-remote-hub.py
|
||||
backup=/opt/owrt-remote/owrt-remote-hub.py.bak-before-host-policy-v111
|
||||
changed=0
|
||||
cleanup() {
|
||||
code=$?
|
||||
trap - EXIT
|
||||
if [ "$code" -ne 0 ] && [ "$changed" = 1 ]; then
|
||||
echo 'Operation failed; restoring this attempt.' >&2
|
||||
journalctl -u owrt-remote -n 30 --no-pager >&2 || true
|
||||
cp -p "$stage/previous.py" "$target.new"
|
||||
mv "$target.new" "$target"
|
||||
systemctl restart owrt-remote || true
|
||||
fi
|
||||
rm -rf "$stage"
|
||||
exit "$code"
|
||||
}
|
||||
trap cleanup EXIT
|
||||
test -s "$target"
|
||||
python_bin=/opt/owrt-remote/venv/bin/python
|
||||
[ -x "$python_bin" ] || python_bin=python3
|
||||
if [ '__ACTION__' = Install ]; then
|
||||
grep -F -q 'v111' "$stage/hub.py"
|
||||
"$python_bin" -m py_compile "$stage/hub.py"
|
||||
OWRT_HOST_POLICY_HUB_SOURCE="$stage/hub.py" "$python_bin" "$stage/test_host_policy.py" HostPolicyTests -v
|
||||
OWRT_RESOURCE_HUB_SOURCE="$stage/hub.py" "$python_bin" "$stage/resources.py" -v
|
||||
OWRT_RECAPTCHA_HUB_SOURCE="$stage/hub.py" "$python_bin" "$stage/recaptcha.py" -v
|
||||
if [ ! -e "$backup" ]; then cp -p "$target" "$backup"; fi
|
||||
test -s "$backup"
|
||||
candidate="$stage/hub.py"
|
||||
else
|
||||
test -s "$backup"
|
||||
"$python_bin" -m py_compile "$backup"
|
||||
candidate="$backup"
|
||||
fi
|
||||
cp -p "$target" "$stage/previous.py"
|
||||
cp "$candidate" "$target.new"
|
||||
chmod 755 "$target.new"
|
||||
changed=1
|
||||
mv "$target.new" "$target"
|
||||
systemctl restart owrt-remote
|
||||
healthy=0
|
||||
for attempt in 1 2 3 4 5 6 7 8 9 10; do
|
||||
if systemctl is-active --quiet owrt-remote && curl -fsS --max-time 5 http://127.0.0.1:8088/health; then healthy=1; break; fi
|
||||
sleep 2
|
||||
done
|
||||
test "$healthy" = 1
|
||||
cmp "$candidate" "$target"
|
||||
printf '\nHOST_POLICY_VPS_V111_%s_OK\n' '__ACTION__'
|
||||
'@
|
||||
}
|
||||
|
||||
$PreserveValue = if ($PreserveConnectionSettings) { '1' } else { '0' }
|
||||
$RemoteScript = $RemoteScript.Replace('__UPLOAD_NAME__', $UploadName).Replace('__ACTION__', $Action).Replace('__PINNED_HOST__', $PinnedVpsHost).Replace('__WAN_INTERFACE__', $WanInterface).Replace('__PRESERVE_CONNECTION__', $PreserveValue).Replace('__BACKUP_NAME__', $BackupName)
|
||||
try {
|
||||
[System.IO.Directory]::CreateDirectory($StageRoot) | Out-Null
|
||||
[System.IO.File]::WriteAllText((Join-Path $StageRoot 'run.sh'), $RemoteScript.Replace("`r`n", "`n") + "`n", $Utf8)
|
||||
if ($Action -eq 'Install') {
|
||||
foreach ($Item in $Payloads.GetEnumerator()) {
|
||||
$Source = Join-Path $RepoRoot $Item.Value
|
||||
$Content = [System.IO.File]::ReadAllText($Source).Replace("`r`n", "`n")
|
||||
[System.IO.File]::WriteAllText((Join-Path $StageRoot $Item.Key), $Content, $Utf8)
|
||||
}
|
||||
}
|
||||
if ($PrepareOnly) { Write-Host "Prepared only: $StageRoot"; return }
|
||||
& scp -O -r -- $StageRoot "${Machine}:/tmp/"
|
||||
if ($LASTEXITCODE -ne 0) { throw 'Upload failed. The installation has not started.' }
|
||||
& ssh -T -o ConnectTimeout=10 $Machine "sh /tmp/$UploadName/run.sh"
|
||||
if ($LASTEXITCODE -ne 0) { throw "$Action failed. See the output above; success was not confirmed." }
|
||||
Write-Host "$Target $Action finished successfully."
|
||||
} finally {
|
||||
if (-not $PrepareOnly) {
|
||||
$Resolved = [System.IO.Path]::GetFullPath($StageRoot)
|
||||
$Expected = [System.IO.Path]::GetFullPath((Join-Path ([System.IO.Path]::GetTempPath()) $UploadName))
|
||||
if ($Resolved -ne $Expected -or (Split-Path -Leaf $Resolved) -ne $UploadName) { throw 'Unsafe staging cleanup path.' }
|
||||
if (Test-Path -LiteralPath $Resolved) { Remove-Item -LiteralPath $Resolved -Recurse -Force }
|
||||
}
|
||||
}
|
||||
@@ -17,8 +17,8 @@ if ($Action -eq 'Install') {
|
||||
set -Eeuo pipefail
|
||||
stage=/tmp/__UPLOAD_NAME__
|
||||
target=/opt/owrt-remote/owrt-remote-hub.py
|
||||
backup=/opt/owrt-remote/owrt-remote-hub.py.bak-before-recaptcha-v3-v110
|
||||
captcha_backup=/opt/owrt-remote/captcha-before-recaptcha-v3-v110.json
|
||||
backup=/opt/owrt-remote/owrt-remote-hub.py.bak-before-recaptcha-v3-v111
|
||||
captcha_backup=/opt/owrt-remote/captcha-before-recaptcha-v3-v111.json
|
||||
candidate=$target.new-__UPLOAD_NAME__
|
||||
changed=0
|
||||
cleanup() {
|
||||
@@ -45,7 +45,7 @@ if [ ! -x "$python_bin" ]; then python_bin=python3; fi
|
||||
import ast, pathlib, sys
|
||||
source = pathlib.Path(sys.argv[1]).read_text(encoding='utf-8')
|
||||
tree = ast.parse(source)
|
||||
assert 'v110' in source
|
||||
assert 'v111' in source
|
||||
assert 'v108' not in source
|
||||
assert 'CAPTCHA_MODE_RECAPTCHA_V3' in source
|
||||
assert 'prepareLoginRecaptcha' in source
|
||||
@@ -88,7 +88,7 @@ pid=$(systemctl show owrt-remote -p MainPID --value)
|
||||
test "$pid" -gt 0
|
||||
printf 'PID=%s FD=%s\n' "$pid" "$(find /proc/$pid/fd -maxdepth 1 -type l | wc -l)"
|
||||
systemctl --no-pager --full status owrt-remote | sed -n '1,16p'
|
||||
echo RECAPTCHA_V3_V110_OK
|
||||
echo RECAPTCHA_V3_V111_OK
|
||||
'@
|
||||
} elseif ($Action -eq 'Rollback') {
|
||||
$RemoteScript = @'
|
||||
@@ -96,8 +96,8 @@ echo RECAPTCHA_V3_V110_OK
|
||||
set -Eeuo pipefail
|
||||
stage=/tmp/__UPLOAD_NAME__
|
||||
target=/opt/owrt-remote/owrt-remote-hub.py
|
||||
backup=/opt/owrt-remote/owrt-remote-hub.py.bak-before-recaptcha-v3-v110
|
||||
captcha_backup=/opt/owrt-remote/captcha-before-recaptcha-v3-v110.json
|
||||
backup=/opt/owrt-remote/owrt-remote-hub.py.bak-before-recaptcha-v3-v111
|
||||
captcha_backup=/opt/owrt-remote/captcha-before-recaptcha-v3-v111.json
|
||||
candidate=$target.rollback-__UPLOAD_NAME__
|
||||
changed=0
|
||||
restore_captcha() {
|
||||
|
||||
@@ -18,7 +18,7 @@ if ($Action -eq 'Install') {
|
||||
set -Eeuo pipefail
|
||||
stage=/tmp/__UPLOAD_NAME__
|
||||
target=/opt/owrt-remote/owrt-remote-hub.py
|
||||
backup=/opt/owrt-remote/owrt-remote-hub.py.bak-before-resource-fix-v110
|
||||
backup=/opt/owrt-remote/owrt-remote-hub.py.bak-before-resource-fix-v111
|
||||
candidate=$target.new-__UPLOAD_NAME__
|
||||
changed=0
|
||||
cleanup() {
|
||||
@@ -45,7 +45,7 @@ if [ ! -x "$python_bin" ]; then python_bin=python3; fi
|
||||
import ast, pathlib, sys
|
||||
source = pathlib.Path(sys.argv[1]).read_text(encoding='utf-8')
|
||||
tree = ast.parse(source)
|
||||
assert 'v110' in source
|
||||
assert 'v111' in source
|
||||
assert 'v108' not in source
|
||||
factory = next(node for node in tree.body if isinstance(node, ast.FunctionDef) and node.name == 'connect')
|
||||
assert any(isinstance(item, ast.Name) and item.id == 'contextmanager' for item in factory.decorator_list)
|
||||
@@ -78,7 +78,7 @@ pid=$(systemctl show owrt-remote -p MainPID --value)
|
||||
test "$pid" -gt 0
|
||||
printf 'PID=%s FD=%s\n' "$pid" "$(find /proc/$pid/fd -maxdepth 1 -type l | wc -l)"
|
||||
systemctl --no-pager --full status owrt-remote | sed -n '1,16p'
|
||||
echo RESOURCE_FIX_V110_OK
|
||||
echo RESOURCE_FIX_V111_OK
|
||||
'@
|
||||
} elseif ($Action -eq 'Rollback') {
|
||||
$RemoteScript = @'
|
||||
@@ -86,7 +86,7 @@ echo RESOURCE_FIX_V110_OK
|
||||
set -Eeuo pipefail
|
||||
stage=/tmp/__UPLOAD_NAME__
|
||||
target=/opt/owrt-remote/owrt-remote-hub.py
|
||||
backup=/opt/owrt-remote/owrt-remote-hub.py.bak-before-resource-fix-v110
|
||||
backup=/opt/owrt-remote/owrt-remote-hub.py.bak-before-resource-fix-v111
|
||||
candidate=$target.rollback-__UPLOAD_NAME__
|
||||
changed=0
|
||||
cleanup() {
|
||||
|
||||
+189
-11
@@ -12,6 +12,7 @@ import hmac
|
||||
import hashlib
|
||||
import html
|
||||
import http.client
|
||||
import ipaddress
|
||||
import json
|
||||
import math
|
||||
import os
|
||||
@@ -1364,6 +1365,70 @@ def row_int_value(row, key, default=0):
|
||||
return int(default)
|
||||
|
||||
|
||||
def router_reported_connection(router, *, legacy_mode="auto"):
|
||||
row = dict(router or {})
|
||||
status = row.get("status")
|
||||
if not isinstance(status, dict):
|
||||
try:
|
||||
status = json.loads(row.get("status_json") or "{}")
|
||||
except (ValueError, TypeError):
|
||||
status = {}
|
||||
if not isinstance(status, dict):
|
||||
status = {}
|
||||
# Legacy agents had automatic endpoint updates. Keep that server behavior
|
||||
# until they report a policy; the patched agent itself defaults to manual.
|
||||
mode = legacy_mode if status.get("vps_host_mode") is None else ("auto" if status.get("vps_host_mode") == "auto" else "manual")
|
||||
host = str(status.get("vps_host") or row.get("vps_host") or "").strip()
|
||||
return mode, host
|
||||
|
||||
|
||||
def validate_router_vps_host(value):
|
||||
host = str(value or "").strip()
|
||||
try:
|
||||
return str(ipaddress.ip_address(host))
|
||||
except ValueError:
|
||||
pass
|
||||
if re.fullmatch(r"[0-9.]+", host):
|
||||
raise ValueError("Некорректный IP-адрес VPS host")
|
||||
if len(host) > 253 or not re.fullmatch(r"[A-Za-z0-9](?:[A-Za-z0-9.-]*[A-Za-z0-9])?", host):
|
||||
raise ValueError("VPS host должен быть IP-адресом или доменом без URL и порта")
|
||||
if any(not label or len(label) > 63 or label.startswith("-") or label.endswith("-") for label in host.split(".")):
|
||||
raise ValueError("Некорректный домен VPS host")
|
||||
return host.lower()
|
||||
|
||||
|
||||
def make_router_vps_host_script(host, mode):
|
||||
host = validate_router_vps_host(host)
|
||||
if mode not in ("manual", "auto"):
|
||||
raise ValueError("VPS host mode должен быть manual или auto")
|
||||
return f'''set -eu
|
||||
owrt-remote status | grep -q '^vps_host_mode:' || {{ echo 'Сначала обнови агент на роутере' >&2; exit 1; }}
|
||||
old_host="$(uci -q get owrtremote.main.vps_host || true)"
|
||||
old_mode="$(uci -q get owrtremote.main.vps_host_mode || true)"
|
||||
applied=0
|
||||
cleanup() {{
|
||||
code=$?
|
||||
trap - EXIT
|
||||
if [ "$applied" = 0 ]; then
|
||||
uci set "owrtremote.main.vps_host=$old_host"
|
||||
if [ -n "$old_mode" ]; then uci set "owrtremote.main.vps_host_mode=$old_mode"; else uci -q delete owrtremote.main.vps_host_mode || true; fi
|
||||
uci commit owrtremote
|
||||
owrt-remote render-client >/dev/null 2>&1 || true
|
||||
fi
|
||||
exit "$code"
|
||||
}}
|
||||
trap cleanup EXIT
|
||||
uci set 'owrtremote.main.vps_host={sh_quote(host)}'
|
||||
uci set 'owrtremote.main.vps_host_mode={mode}'
|
||||
uci commit owrtremote
|
||||
owrt-remote render-client >/dev/null
|
||||
applied=1
|
||||
logger -t owrt-remote "VPS host changed by Hub UI: old=$old_host new={host} mode={mode}" || true
|
||||
( sleep 2; /etc/init.d/owrt-remote restart >/dev/null 2>&1 ) </dev/null >/dev/null 2>&1 &
|
||||
echo VPS_HOST_APPLIED
|
||||
'''
|
||||
|
||||
|
||||
def canonical_router_hub_update(router, app_public_url="", request_origin=""):
|
||||
router = router or {}
|
||||
router_public_url = public_url_origin(router.get("public_url", ""))
|
||||
@@ -1378,11 +1443,15 @@ def canonical_router_hub_update(router, app_public_url="", request_origin=""):
|
||||
vps_host = urllib.parse.urlsplit(hub_url).hostname or ""
|
||||
except Exception:
|
||||
vps_host = ""
|
||||
return {
|
||||
update = {
|
||||
"hub_url": hub_url,
|
||||
"public_url": public_url,
|
||||
"vps_host": vps_host,
|
||||
}
|
||||
# Router is authoritative for a manually fixed tunnel address. Patched
|
||||
# agents also protect it locally while the Hub is still on an old version.
|
||||
if router_reported_connection(router)[0] == "auto":
|
||||
update["vps_host"] = vps_host
|
||||
return update
|
||||
|
||||
|
||||
def verify_password_login(username, password, otp=""):
|
||||
@@ -3619,9 +3688,11 @@ def router_current_hub_bundle(router, app_public_url="", request_origin="", fall
|
||||
public_url = prefer_origin_with_explicit_port(router_public_url, app_origin)
|
||||
public_url = prefer_origin_with_explicit_port(public_url, request_origin)
|
||||
hub_url = public_url or request_origin or router_fallback_hub_url(row, fallback_host, 8088)
|
||||
reported_mode, reported_host = router_reported_connection(row)
|
||||
config_vps_host = (
|
||||
vps_host_name(public_url or hub_url)
|
||||
(reported_host if reported_mode == "manual" else "")
|
||||
or str(row.get("vps_host") or "").strip()
|
||||
or vps_host_name(public_url or hub_url)
|
||||
or vps_host_name(app_origin or request_origin)
|
||||
or vps_host_name(router_fallback_hub_url(row, fallback_host, 8088))
|
||||
or ""
|
||||
@@ -4266,6 +4337,7 @@ def build_openwrt_config_payload(row, hub_url, vps_host_override="", public_url_
|
||||
"role": row_str_value(row, "role", "node"),
|
||||
"hub_url": str(hub_url or ""),
|
||||
"vps_host": effective_vps_host,
|
||||
"vps_host_mode": router_reported_connection(row, legacy_mode="manual")[0],
|
||||
"vps_port": row_int_value(row, "vless_port", DEFAULT_VLESS_PORT),
|
||||
"vless_uuid": row_str_value(row, "vless_uuid"),
|
||||
"vless_encryption": row_str_value(row, "vless_encryption", "none"),
|
||||
@@ -4288,8 +4360,9 @@ def build_openwrt_config_payload(row, hub_url, vps_host_override="", public_url_
|
||||
|
||||
def build_openwrt_config_lines(payload, mode="manual"):
|
||||
lines = [
|
||||
"uci -q delete owrtremote.main",
|
||||
"uci set owrtremote.main=remote",
|
||||
"owrt_saved_host=\"$(uci -q get owrtremote.main.vps_host 2>/dev/null || true)\"",
|
||||
"owrt_saved_mode=\"$(uci -q get owrtremote.main.vps_host_mode 2>/dev/null || echo manual)\"",
|
||||
"uci -q get owrtremote.main >/dev/null 2>&1 || uci set owrtremote.main=remote",
|
||||
"uci set owrtremote.main.enabled='1'",
|
||||
f"uci set owrtremote.main.router_id='{sh_quote(payload['id'])}'",
|
||||
f"uci set owrtremote.main.router_name='{sh_quote(payload['name'])}'",
|
||||
@@ -4299,7 +4372,12 @@ def build_openwrt_config_lines(payload, mode="manual"):
|
||||
"uci set owrtremote.main.heartbeat_interval='30'",
|
||||
"uci set owrtremote.main.xray_bin='/usr/bin/xray'",
|
||||
"uci set owrtremote.main.xray_config='/etc/xray/owrt-remote-client.json'",
|
||||
f"uci set owrtremote.main.vps_host='{sh_quote(payload['vps_host'])}'",
|
||||
"if [ \"$owrt_saved_mode\" = auto ] || [ -z \"$owrt_saved_host\" ]; then",
|
||||
f" uci set owrtremote.main.vps_host='{sh_quote(payload['vps_host'])}'",
|
||||
f" uci set owrtremote.main.vps_host_mode='{sh_quote(payload.get('vps_host_mode', 'manual'))}'",
|
||||
"else",
|
||||
" uci set owrtremote.main.vps_host_mode='manual'",
|
||||
"fi",
|
||||
f"uci set owrtremote.main.vps_port='{payload['vps_port']}'",
|
||||
f"uci set owrtremote.main.vless_uuid='{sh_quote(payload['vless_uuid'])}'",
|
||||
f"uci set owrtremote.main.vless_encryption='{sh_quote(payload['vless_encryption'])}'",
|
||||
@@ -4661,7 +4739,7 @@ input,select{{min-width:0;border:1px solid var(--line);border-radius:8px;padding
|
||||
<div class="brand">
|
||||
<div class="desktopHeader">
|
||||
<div class="desktopHeaderTop">
|
||||
<h1 class="appBanner"><span>OpenWrt Remote Hub <span class="appBannerVersion">v110</span></span></h1>
|
||||
<h1 class="appBanner"><span>OpenWrt Remote Hub <span class="appBannerVersion">v111</span></span></h1>
|
||||
<div class="routerSearchDock" id="routerSearchDock">
|
||||
<button class="routerSearchToggle" id="routerSearchToggle" type="button" aria-expanded="false" aria-controls="routerSearchPanel" data-active="false">
|
||||
<span>Поиск роутеров</span>
|
||||
@@ -5013,7 +5091,7 @@ systemctl restart owrt-remote-xray</pre>
|
||||
</div>
|
||||
</div>
|
||||
<div class="mobileSearchDock" id="mobileRouterSearchDock">
|
||||
<span class="mobileSearchVersion">v110</span>
|
||||
<span class="mobileSearchVersion">v111</span>
|
||||
<button class="routerSearchToggle mobilePanelToggle primary" id="mobileRouterSearchToggle" type="button" aria-expanded="false" aria-controls="mobileRouterSearchPanel" data-active="false">
|
||||
<span>Поиск роутеров</span>
|
||||
</button>
|
||||
@@ -5500,7 +5578,7 @@ function hasOpenWolPicker() {{
|
||||
|
||||
function activeWolInteractiveField() {{
|
||||
const active = document.activeElement;
|
||||
return active && typeof active.matches === 'function' && active.matches('[data-wol-password],[data-wol-select],[data-traffic-password],[data-router-notes-input]') ? active : null;
|
||||
return active && typeof active.matches === 'function' && active.matches('[data-wol-password],[data-wol-select],[data-traffic-password],[data-router-notes-input],[data-vps-field]') ? active : null;
|
||||
}}
|
||||
|
||||
function shouldDeferRouterRender() {{
|
||||
@@ -7721,6 +7799,36 @@ function normalizeRouterName(value) {{
|
||||
return String(value || '').trim().replace(/\\s+/g, ' ').slice(0, 80);
|
||||
}}
|
||||
|
||||
const vpsHostDrafts = new Map();
|
||||
function getVpsHostDraft(router) {{
|
||||
if (!vpsHostDrafts.has(router.id)) {{
|
||||
const status = router.status || {{}};
|
||||
vpsHostDrafts.set(router.id, {{open: false, host: status.vps_host || router.vps_host || '', auto: status.vps_host_mode === 'auto', password: '', dirty: false, saving: false, message: ''}});
|
||||
}}
|
||||
const state = vpsHostDrafts.get(router.id);
|
||||
if (!state.dirty && !state.saving) {{
|
||||
state.host = (router.status || {{}}).vps_host || router.vps_host || '';
|
||||
state.auto = (router.status || {{}}).vps_host_mode === 'auto';
|
||||
}}
|
||||
return state;
|
||||
}}
|
||||
function renderVpsHostSettings(router) {{
|
||||
if (!(router.permissions || {{}}).owner) return '';
|
||||
const state = getVpsHostDraft(router);
|
||||
const key = escapeAttr(router.id);
|
||||
return `<div class="wolPanel" style="margin-top:10px">
|
||||
<button class="btn" type="button" data-vps-toggle="${{key}}" aria-expanded="${{state.open ? 'true' : 'false'}}">${{state.open ? 'Скрыть VPS host' : 'Настроить VPS host'}}</button>
|
||||
${{state.open ? `<div style="display:grid;gap:10px;margin-top:10px;min-width:0">
|
||||
<label style="display:grid;gap:5px;min-width:0">VPS host<input style="width:100%;min-width:0" data-vps-field="host" data-vps-router="${{key}}" value="${{escapeAttr(state.host)}}" placeholder="IP или домен VPS"></label>
|
||||
<label style="display:flex;align-items:center;gap:8px"><input type="checkbox" data-vps-field="auto" data-vps-router="${{key}}"${{state.auto ? ' checked' : ''}}> Автоматически обновлять VPS host</label>
|
||||
<label style="display:grid;gap:5px;min-width:0">Пароль SSH роутера<input style="width:100%;min-width:0" type="password" autocomplete="off" data-vps-field="password" data-vps-router="${{key}}" value="${{escapeAttr(state.password)}}" placeholder="Пусто, если доступ по ключу"></label>
|
||||
<span>Выключено: heartbeat сохраняет указанный адрес. Сохранение применяется на роутере через SSH.</span>
|
||||
<button class="btn" type="button" data-vps-save="${{key}}"${{state.saving || !router.online ? ' disabled' : ''}}>${{state.saving ? 'Сохраняю...' : 'Сохранить VPS host'}}</button>
|
||||
<span>${{escapeHtml(state.message)}}</span>
|
||||
</div>` : ''}}
|
||||
</div>`;
|
||||
}}
|
||||
|
||||
function renderRouterNotesPanel(router) {{
|
||||
const permissions = router && router.permissions && typeof router.permissions === 'object' ? router.permissions : {{}};
|
||||
const canEdit = Boolean(permissions.notes_edit || permissions.manage);
|
||||
@@ -8536,6 +8644,46 @@ routerForm.addEventListener('submit', async (ev) => {{
|
||||
|
||||
|
||||
|
||||
cards.addEventListener('input', (ev) => {{
|
||||
const field = ev.target.closest('[data-vps-field]');
|
||||
if (!field) return;
|
||||
const router = selectedRouter(field.dataset.vpsRouter);
|
||||
if (!router) return;
|
||||
const state = getVpsHostDraft(router);
|
||||
state[field.dataset.vpsField] = field.type === 'checkbox' ? field.checked : field.value;
|
||||
state.dirty = true;
|
||||
}});
|
||||
cards.addEventListener('click', async (ev) => {{
|
||||
const toggle = ev.target.closest('[data-vps-toggle]');
|
||||
if (toggle) {{
|
||||
const router = selectedRouter(toggle.dataset.vpsToggle);
|
||||
if (!router) return;
|
||||
const state = getVpsHostDraft(router);
|
||||
state.open = !state.open;
|
||||
requestRouterRender();
|
||||
return;
|
||||
}}
|
||||
const save = ev.target.closest('[data-vps-save]');
|
||||
if (!save) return;
|
||||
const key = save.dataset.vpsSave;
|
||||
const router = selectedRouter(key);
|
||||
if (!router) return;
|
||||
const state = getVpsHostDraft(router);
|
||||
if (state.saving) return;
|
||||
state.saving = true;
|
||||
state.message = '';
|
||||
requestRouterRender();
|
||||
try {{
|
||||
const res = await fetch('/api/router/' + encodeURIComponent(key) + '/vps-host', {{method: 'POST', headers: {{'Content-Type': 'application/json'}}, body: JSON.stringify({{vps_host: state.host, vps_host_mode: state.auto ? 'auto' : 'manual', ssh_password: state.password}})}});
|
||||
const data = await res.json();
|
||||
if (!res.ok || !data.ok) throw new Error(data.error || 'Не удалось сохранить VPS host');
|
||||
replaceRouterInList(data.router);
|
||||
state.dirty = false;
|
||||
state.message = 'Настройки сохранены на роутере.';
|
||||
}} catch (error) {{ state.message = error.message; }}
|
||||
finally {{ state.saving = false; state.password = ''; requestRouterRender(); }}
|
||||
}});
|
||||
|
||||
cards.addEventListener('click', async (ev) => {{
|
||||
const pickerToggle = ev.target.closest('[data-router-group-picker-toggle]');
|
||||
if (pickerToggle) {{
|
||||
@@ -12405,7 +12553,7 @@ button:hover{{filter:brightness(1.06)}}
|
||||
<form class="login" method="post" action="/login">
|
||||
{error_html}
|
||||
<span class="brand">
|
||||
<h1 class="appBanner"><span>OpenWrt Remote Hub <span class="appBannerVersion">v110</span></span></h1>
|
||||
<h1 class="appBanner"><span>OpenWrt Remote Hub <span class="appBannerVersion">v111</span></span></h1>
|
||||
</span>
|
||||
<label for="hubUsername">Логин</label>
|
||||
<input id="hubUsername" name="username" autocomplete="off" autofocus required>
|
||||
@@ -12877,7 +13025,7 @@ body::after{{content:"";position:fixed;inset:0;pointer-events:none;background:li
|
||||
<circle cx="65" cy="59" r="3" fill="#E5F2FF"/>
|
||||
</svg>
|
||||
</div>
|
||||
<h2 class="brandTitle">OpenWrt Remote Hub <span class="brandVersion">v110</span></h2>
|
||||
<h2 class="brandTitle">OpenWrt Remote Hub <span class="brandVersion">v111</span></h2>
|
||||
</div>
|
||||
</div>
|
||||
<div class="brandBottom">
|
||||
@@ -17428,6 +17576,36 @@ exit 127
|
||||
except Exception as exc:
|
||||
self.send_json(400, {"ok": False, "error": str(exc)})
|
||||
return
|
||||
if path.startswith("/api/router/") and path.endswith("/vps-host"):
|
||||
if not self.require_owner(json_mode=True):
|
||||
return
|
||||
try:
|
||||
parts = path.strip("/").split("/")
|
||||
if len(parts) != 4:
|
||||
raise ValueError("Некорректный путь")
|
||||
router_id = urllib.parse.unquote(parts[2])
|
||||
payload = self.read_payload()
|
||||
host = validate_router_vps_host(payload.get("vps_host"))
|
||||
mode = str(payload.get("vps_host_mode") or "manual")
|
||||
script = make_router_vps_host_script(host, mode)
|
||||
with self.app.conn() as conn:
|
||||
row = get_active_router(conn, router_id)
|
||||
if not row:
|
||||
raise ValueError("Роутер не найден")
|
||||
output = self.run_router_ssh_script(row, script, timeout=35, ssh_password=payload.get("ssh_password", ""))
|
||||
if "VPS_HOST_APPLIED" not in output:
|
||||
raise ValueError("Роутер не подтвердил применение настройки")
|
||||
with self.app.conn() as conn:
|
||||
fresh = get_active_router(conn, router_id)
|
||||
status = row_to_router(fresh).get("status", {})
|
||||
status.update({"vps_host": host, "vps_host_mode": mode})
|
||||
conn.execute("update routers set vps_host = ?, status_json = ?, updated_at = ? where id = ?", (host, json.dumps(status, ensure_ascii=False), now_ts(), router_id))
|
||||
conn.commit()
|
||||
router = row_to_router(get_router(conn, router_id))
|
||||
self.send_json(200, {"ok": True, "router": router})
|
||||
except Exception as exc:
|
||||
self.send_json(400, {"ok": False, "error": str(exc)})
|
||||
return
|
||||
if path == "/api/router":
|
||||
if not self.require_add_router(json_mode=True):
|
||||
return
|
||||
|
||||
Reference in new issue
Block a user