fix: multi-node online stats, per-node Hy2 ports, and panel reload stability
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+32
-4
@@ -102,6 +102,33 @@ function setButtonBusy(btn, busy, busyText) {
|
||||
}
|
||||
}
|
||||
|
||||
async function readJson(res) {
|
||||
const text = await res.text();
|
||||
try {
|
||||
return JSON.parse(text);
|
||||
} catch {
|
||||
throw new Error(res.ok ? "响应格式错误" : `HTTP ${res.status}`);
|
||||
}
|
||||
}
|
||||
|
||||
async function reloadPanel(maxWaitMs = 15000) {
|
||||
const url = `${panelBase()}/`;
|
||||
const started = Date.now();
|
||||
while (Date.now() - started < maxWaitMs) {
|
||||
await new Promise((r) => setTimeout(r, 1500));
|
||||
try {
|
||||
const res = await fetch(url, { credentials: "same-origin" });
|
||||
if (res.ok) {
|
||||
location.href = url;
|
||||
return;
|
||||
}
|
||||
} catch {
|
||||
/* sing-box 重启期间可能短暂不可用,继续重试 */
|
||||
}
|
||||
}
|
||||
location.href = url;
|
||||
}
|
||||
|
||||
if (confirmAddBtn) {
|
||||
confirmAddBtn.addEventListener("click", async () => {
|
||||
const name = nodeName.value.trim() || "新节点";
|
||||
@@ -114,10 +141,11 @@ if (confirmAddBtn) {
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ name }),
|
||||
});
|
||||
const data = await res.json();
|
||||
const data = await readJson(res);
|
||||
if (!res.ok) throw new Error(data.error || "创建失败");
|
||||
toast("节点已创建,配置生效中…");
|
||||
setTimeout(() => location.reload(), 600);
|
||||
modal.classList.add("hidden");
|
||||
reloadPanel();
|
||||
} catch (err) {
|
||||
toast(err.message || "创建失败");
|
||||
setButtonBusy(confirmAddBtn, false);
|
||||
@@ -136,10 +164,10 @@ document.querySelectorAll(".delete-btn").forEach((btn) => {
|
||||
method: "DELETE",
|
||||
credentials: "same-origin",
|
||||
});
|
||||
const data = await res.json();
|
||||
const data = await readJson(res);
|
||||
if (!res.ok) throw new Error(data.error || "删除失败");
|
||||
toast("已删除,配置生效中…");
|
||||
setTimeout(() => location.reload(), 600);
|
||||
reloadPanel();
|
||||
} catch (err) {
|
||||
toast(err.message || "删除失败");
|
||||
setButtonBusy(btn, false);
|
||||
|
||||
Reference in New Issue
Block a user