Refresh VPS reverse tunnel on WAN reconnect
This commit is contained in:
1 parent
c98322364d
commit
2c7bfde758
3 files changed
+52
-8
No files matched your search
@@ -75,7 +75,7 @@ printf '%s\n' "$stamp" >"$STAMP_FILE"
|
|||||||
|
|
||||||
log "WAN event $ACTION on ${INTERFACE:-unknown}/${DEVICE:-unknown}, restarting reverse tunnel"
|
log "WAN event $ACTION on ${INTERFACE:-unknown}/${DEVICE:-unknown}, restarting reverse tunnel"
|
||||||
if /etc/init.d/owrt-remote restart >/dev/null 2>&1; then
|
if /etc/init.d/owrt-remote restart >/dev/null 2>&1; then
|
||||||
/usr/sbin/owrt-remote heartbeat >/dev/null 2>&1 || true
|
/usr/sbin/owrt-remote heartbeat wan-reconnect >/dev/null 2>&1 || true
|
||||||
else
|
else
|
||||||
log "restart failed"
|
log "restart failed"
|
||||||
fi
|
fi
|
||||||
|
|||||||
@@ -544,7 +544,8 @@ run_xray() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
heartbeat_json() {
|
heartbeat_json() {
|
||||||
local router_id router_name role public_url hub_url xray_state service_state ssh_state
|
local router_id router_name role public_url hub_url xray_state service_state ssh_state event
|
||||||
|
event="${1:-}"
|
||||||
router_id="$(safe_id "$(uci_get router_id main)")"
|
router_id="$(safe_id "$(uci_get router_id main)")"
|
||||||
router_name="$(uci_get router_name "$router_id")"
|
router_name="$(uci_get router_name "$router_id")"
|
||||||
role="$(uci_get role node)"
|
role="$(uci_get role node)"
|
||||||
@@ -589,6 +590,7 @@ heartbeat_json() {
|
|||||||
"temperature": $(json_string "$(temperature_text)"),
|
"temperature": $(json_string "$(temperature_text)"),
|
||||||
"xray": $(json_string "$xray_state"),
|
"xray": $(json_string "$xray_state"),
|
||||||
"ssh": $(json_string "$ssh_state"),
|
"ssh": $(json_string "$ssh_state"),
|
||||||
|
"event": $(json_string "$event"),
|
||||||
"service": $(json_string "$service_state")
|
"service": $(json_string "$service_state")
|
||||||
}
|
}
|
||||||
EOF
|
EOF
|
||||||
@@ -625,7 +627,8 @@ post_json() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
send_heartbeat() {
|
send_heartbeat() {
|
||||||
local hub_url token endpoint tmp
|
local hub_url token endpoint tmp event
|
||||||
|
event="${1:-}"
|
||||||
hub_url="$(uci_get hub_url '')"
|
hub_url="$(uci_get hub_url '')"
|
||||||
token="$(uci_get hub_token '')"
|
token="$(uci_get hub_token '')"
|
||||||
[ -n "$hub_url" ] || die "hub_url пустой"
|
[ -n "$hub_url" ] || die "hub_url пустой"
|
||||||
@@ -633,7 +636,7 @@ send_heartbeat() {
|
|||||||
|
|
||||||
endpoint="${hub_url%/}/api/heartbeat"
|
endpoint="${hub_url%/}/api/heartbeat"
|
||||||
tmp="${TMPDIR:-/tmp}/owrt-remote-heartbeat.$$"
|
tmp="${TMPDIR:-/tmp}/owrt-remote-heartbeat.$$"
|
||||||
heartbeat_json >"$tmp"
|
heartbeat_json "$event" >"$tmp"
|
||||||
post_json "$endpoint" "$token" "$tmp"
|
post_json "$endpoint" "$token" "$tmp"
|
||||||
rm -f "$tmp"
|
rm -f "$tmp"
|
||||||
}
|
}
|
||||||
@@ -961,7 +964,7 @@ case "$cmd" in
|
|||||||
run_xray
|
run_xray
|
||||||
;;
|
;;
|
||||||
heartbeat)
|
heartbeat)
|
||||||
send_heartbeat
|
send_heartbeat "$@"
|
||||||
;;
|
;;
|
||||||
heartbeat-loop)
|
heartbeat-loop)
|
||||||
heartbeat_loop
|
heartbeat_loop
|
||||||
|
|||||||
+44
-3
@@ -45,6 +45,7 @@ VAPID_PRIVATE_KEY_FILE = STATE_DIR / "hub-vapid-private.pem"
|
|||||||
VAPID_PUBLIC_KEY_FILE = STATE_DIR / "hub-vapid-public.txt"
|
VAPID_PUBLIC_KEY_FILE = STATE_DIR / "hub-vapid-public.txt"
|
||||||
BOOT_ID_FILE = STATE_DIR / "hub-boot.id"
|
BOOT_ID_FILE = STATE_DIR / "hub-boot.id"
|
||||||
AGENT_TOKEN_FILE = STATE_DIR / "agent.token"
|
AGENT_TOKEN_FILE = STATE_DIR / "agent.token"
|
||||||
|
XRAY_WAN_RECONNECT_FILE = STATE_DIR / "xray-wan-reconnect-restart.json"
|
||||||
ACME_WEBROOT = STATE_DIR / "acme-webroot"
|
ACME_WEBROOT = STATE_DIR / "acme-webroot"
|
||||||
ONLINE_AFTER_SECONDS = int(os.environ.get("OWRT_REMOTE_ONLINE_AFTER", "75"))
|
ONLINE_AFTER_SECONDS = int(os.environ.get("OWRT_REMOTE_ONLINE_AFTER", "75"))
|
||||||
DEFAULT_VLESS_PORT = int(os.environ.get("OWRT_REMOTE_VLESS_PORT", "8443"))
|
DEFAULT_VLESS_PORT = int(os.environ.get("OWRT_REMOTE_VLESS_PORT", "8443"))
|
||||||
@@ -78,6 +79,16 @@ def now_ts():
|
|||||||
return int(time.time())
|
return int(time.time())
|
||||||
|
|
||||||
|
|
||||||
|
def atomic_write_text(path, text, mode=None):
|
||||||
|
path = Path(path)
|
||||||
|
path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
tmp = path.with_name(f".{path.name}.{os.getpid()}.tmp")
|
||||||
|
tmp.write_text(text, encoding="utf-8")
|
||||||
|
if mode is not None:
|
||||||
|
os.chmod(tmp, mode)
|
||||||
|
os.replace(tmp, path)
|
||||||
|
|
||||||
|
|
||||||
def iso_time(ts):
|
def iso_time(ts):
|
||||||
if not ts:
|
if not ts:
|
||||||
return ""
|
return ""
|
||||||
@@ -1410,8 +1421,7 @@ def reload_vps_xray(db_path=DB_PATH):
|
|||||||
init_db(conn)
|
init_db(conn)
|
||||||
rows = list_router_rows(conn)
|
rows = list_router_rows(conn)
|
||||||
config = make_server_xray_config(rows)
|
config = make_server_xray_config(rows)
|
||||||
out.parent.mkdir(parents=True, exist_ok=True)
|
atomic_write_text(out, json.dumps(config, ensure_ascii=False, indent=2) + "\n", mode=0o600)
|
||||||
out.write_text(json.dumps(config, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
|
|
||||||
try:
|
try:
|
||||||
os.chmod(out, 0o600)
|
os.chmod(out, 0o600)
|
||||||
except OSError:
|
except OSError:
|
||||||
@@ -1446,6 +1456,30 @@ def restart_vps_xray():
|
|||||||
return {"service": service}
|
return {"service": service}
|
||||||
|
|
||||||
|
|
||||||
|
def maybe_restart_vps_xray_after_wan_reconnect(router_id):
|
||||||
|
if str(os.environ.get("OWRT_REMOTE_RESTART_XRAY_ON_WAN_RECONNECT", "1")).lower() in {"0", "no", "false", "off"}:
|
||||||
|
return {"skipped": "disabled"}
|
||||||
|
cooldown = int(os.environ.get("OWRT_REMOTE_WAN_RECONNECT_XRAY_COOLDOWN", "90"))
|
||||||
|
now = now_ts()
|
||||||
|
last = 0
|
||||||
|
try:
|
||||||
|
data = json.loads(XRAY_WAN_RECONNECT_FILE.read_text(encoding="utf-8"))
|
||||||
|
last = int(data.get("ts") or 0)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
if now - last < cooldown:
|
||||||
|
return {"skipped": "cooldown", "remaining": cooldown - (now - last)}
|
||||||
|
result = restart_vps_xray()
|
||||||
|
XRAY_WAN_RECONNECT_FILE.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
atomic_write_text(
|
||||||
|
XRAY_WAN_RECONNECT_FILE,
|
||||||
|
json.dumps({"ts": now, "router_id": router_id, "result": result}, ensure_ascii=False) + "\n",
|
||||||
|
mode=0o600,
|
||||||
|
)
|
||||||
|
result["reason"] = "wan-reconnect"
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
def make_openwrt_config(row, hub_url):
|
def make_openwrt_config(row, hub_url):
|
||||||
lines = [
|
lines = [
|
||||||
"uci -q delete owrtremote.main",
|
"uci -q delete owrtremote.main",
|
||||||
@@ -4082,7 +4116,14 @@ class Handler(BaseHTTPRequestHandler):
|
|||||||
payload = self.read_payload()
|
payload = self.read_payload()
|
||||||
with self.app.conn() as conn:
|
with self.app.conn() as conn:
|
||||||
router = heartbeat(conn, payload)
|
router = heartbeat(conn, payload)
|
||||||
self.send_json(200, {"ok": True, "router": router})
|
xray_restart = None
|
||||||
|
if payload.get("event") == "wan-reconnect":
|
||||||
|
try:
|
||||||
|
xray_restart = maybe_restart_vps_xray_after_wan_reconnect(router["id"])
|
||||||
|
except Exception as exc:
|
||||||
|
self.log_message("wan reconnect xray restart error: %s", exc)
|
||||||
|
xray_restart = {"error": str(exc)}
|
||||||
|
self.send_json(200, {"ok": True, "router": router, "xray_restart": xray_restart})
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
self.log_message("heartbeat error: %s", exc)
|
self.log_message("heartbeat error: %s", exc)
|
||||||
self.send_json(400, {"ok": False, "error": str(exc)})
|
self.send_json(400, {"ok": False, "error": str(exc)})
|
||||||
|
|||||||
Reference in new issue
Block a user