SSH 隧道管理器:pywebview+paramiko 桌面应用

- 本地端口转发隧道管理(密码/私钥认证,自动重连,随应用启动)
- 主机与隧道可视化配置,DPAPI 加密存储机密,known_hosts 校验
- 自定义无边框窗口,深色界面,托盘驻留(最小化/关闭到托盘)
- 内置本地测试 SSH 服务端与端到端自测脚本
This commit is contained in:
leefer
2026-09-24 17:14:59 +08:00
commit 27a3b4c6a8
15 changed files with 2745 additions and 0 deletions
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

+709
View File
@@ -0,0 +1,709 @@
/* SSH 隧道管理器 — 前端逻辑
运行于 pywebview(调用 Python API)或浏览器(Mock,便于调试界面) */
"use strict";
/* ---------- 工具 ---------- */
const $ = (s, el = document) => el.querySelector(s);
const $$ = (s, el = document) => [...el.querySelectorAll(s)];
const esc = (s) => String(s ?? "").replace(/[&<>"']/g,
(c) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;" }[c]));
const isPort = (v) => /^\d+$/.test(String(v).trim()) && +v >= 1 && +v <= 65535;
const withTimeout = (p, ms, msg) => Promise.race([
p,
new Promise((_, rej) => setTimeout(() => rej(new Error(msg)), ms)),
]);
function fmtUptime(s) {
s = Math.max(0, Math.floor(s));
const h = String(Math.floor(s / 3600)).padStart(2, "0");
const m = String(Math.floor((s % 3600) / 60)).padStart(2, "0");
const sec = String(s % 60).padStart(2, "0");
return `${h}:${m}:${sec}`;
}
/* ---------- 状态 ---------- */
const S = {
hosts: [],
tunnels: [], // 每项含 cfg 字段 + runtime + logs + host_label
feed: [],
view: "tunnels",
openLogs: new Set(),
};
/* ---------- API 桥 ---------- */
function nativeApi() {
return (typeof window.pywebview === "object" && window.pywebview && window.pywebview.api) || null;
}
async function call(name, ...args) {
const a = nativeApi();
if (a) {
const r = await a[name](...args);
if (r && r.ok === false) throw new Error(r.error || "操作失败");
return r;
}
return MockApi[name](...args);
}
/* Python 事件入口 */
window.__bus = (ev) => {
try { handleEvent(ev); } catch (e) { console.error(e); }
};
function handleEvent(ev) {
if (ev.type === "state") { applyState(ev.state); return; }
if (ev.type === "tunnel") {
const t = S.tunnels.find((x) => x.id === ev.id);
if (t) { t.runtime = { status: ev.status, error: ev.error, started_at: ev.started_at, conn_count: ev.conn_count }; renderTunnels(); renderSummary(); }
return;
}
if (ev.type === "log") {
const t = S.tunnels.find((x) => x.id === ev.tunnel_id);
if (t) {
t.logs.push(ev.entry);
if (S.openLogs.has(t.id)) appendLogBox(t);
}
appendFeedRow({ tunnel_name: t ? t.name : "?", ...ev.entry });
if (S.view === "logs") maybeScrollFeed();
}
}
function applyState(state) {
S.hosts = state.hosts || [];
S.tunnels = state.tunnels || [];
S.feed = state.feed || [];
renderAll();
}
/* ---------- Mock(浏览器调试用) ---------- */
const MockApi = (() => {
let hosts = [
{ id: "h1", name: "开发服务器", hostname: "192.168.8.20", port: 22, username: "deploy", auth: "password", key_path: "", note: "", has_password: true, has_passphrase: false },
{ id: "h2", name: "跳板机", hostname: "gw.example.com", port: 2222, username: "root", auth: "key", key_path: "C:\\Users\\me\\.ssh\\id_ed25519", note: "", has_password: false, has_passphrase: true },
];
let tunnels = [
{ id: "t1", name: "grafana", host_id: "h1", local_port: 3000, remote_host: "127.0.0.1", remote_port: 3000, autostart: false, autoreconnect: true, host_label: "deploy@192.168.8.20:22", host_auth: "password", runtime: { id: "t1", status: "connected", error: "", started_at: Date.now() / 1000 - 4372, conn_count: 12 }, logs: [
{ t: "10:21:07", level: "info", msg: "正在连接 deploy@192.168.8.20:22 ..." },
{ t: "10:21:08", level: "success", msg: "隧道已建立: 127.0.0.1:3000 → 127.0.0.1:3000" },
{ t: "10:24:11", level: "info", msg: "接受连接 → 127.0.0.1:3000" },
] },
{ id: "t2", name: "mysql-debug", host_id: "h2", local_port: 3307, remote_host: "127.0.0.1", remote_port: 3306, autostart: true, autoreconnect: true, host_label: "root@gw.example.com:2222", host_auth: "key", runtime: { id: "t2", status: "stopped", error: "", started_at: null, conn_count: 0 }, logs: [] },
];
let feed = tunnels.flatMap((t) => t.logs.map((e) => ({ tunnel_name: t.name, ...e })));
const snap = () => ({ hosts: structuredClone(hosts), tunnels: structuredClone(tunnels), feed: feed.slice(-300) });
const wait = (ms) => new Promise((r) => setTimeout(r, ms));
function pushLog(id, level, msg) {
const t = tunnels.find((x) => x.id === id);
if (!t) return;
const e = { t: new Date().toTimeString().slice(0, 8), level, msg };
t.logs.push(e);
feed.push({ tunnel_name: t.name, ...e });
window.__bus({ type: "log", tunnel_id: id, entry: e });
}
return {
async get_state() { return { ok: true, state: snap() }; },
async save_host(p) {
const rec = { id: p.id || "h" + Math.random().toString(36).slice(2, 6), name: p.name || p.hostname, hostname: p.hostname, port: +p.port || 22, username: p.username, auth: p.auth, key_path: p.key_path || "", note: p.note || "", has_password: !!p.password || (p.id && hosts.find((h) => h.id === p.id)?.has_password && !p.password === false), has_passphrase: false };
if (!p.id && p.auth === "password") rec.has_password = !!p.password;
if (p.id && !p.password) rec.has_password = hosts.find((h) => h.id === p.id).has_password;
const i = hosts.findIndex((h) => h.id === rec.id);
if (i >= 0) hosts[i] = rec; else hosts.push(rec);
tunnels.forEach((t) => { if (t.host_id === rec.id) t.host_label = `${rec.username}@${rec.hostname}:${rec.port}`; });
return { ok: true, id: rec.id, state: snap() };
},
async delete_host(id) { hosts = hosts.filter((h) => h.id !== id); return { ok: true, state: snap() }; },
async save_tunnel(p) {
const host = hosts.find((h) => h.id === p.host_id);
const rec = { id: p.id || "t" + Math.random().toString(36).slice(2, 6), name: p.name || `${p.remote_host}:${p.remote_port}`, host_id: p.host_id, local_port: +p.local_port, remote_host: p.remote_host || "127.0.0.1", remote_port: +p.remote_port, autostart: !!p.autostart, autoreconnect: p.autoreconnect !== false, host_label: host ? `${host.username}@${host.hostname}:${host.port}` : "?", host_auth: host?.auth, runtime: { id: p.id || "t0", status: "stopped", error: "", started_at: null, conn_count: 0 } };
const i = tunnels.findIndex((t) => t.id === rec.id);
if (i >= 0) { rec.runtime = tunnels[i].runtime; tunnels[i] = rec; } else tunnels.push(rec);
return { ok: true, id: rec.id, state: snap() };
},
async delete_tunnel(id) { tunnels = tunnels.filter((t) => t.id !== id); feed = feed.filter((f) => f.tunnel_id !== id); return { ok: true, state: snap() }; },
async start_tunnel(id) {
const t = tunnels.find((x) => x.id === id);
t.runtime = { ...t.runtime, status: "connecting", error: "" };
window.__bus({ type: "tunnel", ...t.runtime });
pushLog(id, "info", `正在连接 ${t.host_label} ...`);
await wait(1200);
if (t.runtime.status !== "connecting") return { ok: true };
t.runtime = { ...t.runtime, status: "connected", started_at: Date.now() / 1000 };
window.__bus({ type: "tunnel", ...t.runtime });
pushLog(id, "success", `隧道已建立: 127.0.0.1:${t.local_port} → ${t.remote_host}:${t.remote_port}`);
return { ok: true };
},
async stop_tunnel(id) {
const t = tunnels.find((x) => x.id === id);
t.runtime = { ...t.runtime, status: "stopped", started_at: null };
window.__bus({ type: "tunnel", ...t.runtime });
pushLog(id, "info", "隧道已断开");
return { ok: true };
},
async probe_host(p) { await wait(900); return p.hostname && p.username ? { ok: true, message: "认证成功 · 服务端 SSH-2.0-OpenSSH_9.6" } : { ok: false, message: "请先填写主机地址" }; },
async open_url(url) { window.open(url, "_blank"); return { ok: true }; },
async copy_text() { return { ok: true }; },
async pick_key_file() { return { ok: true, path: "C:\\Users\\me\\.ssh\\id_ed25519" }; },
async win_minimize() { return { ok: true }; },
async win_maximize() { return { ok: true }; },
async win_close() { return { ok: true }; },
};
})();
/* ---------- 图标 ---------- */
const I = {
edit: '<path d="M17 3a2.85 2.83 0 1 1 4 4L7.5 20.5 2 22l1.5-5.5Z"/><path d="m15 5 4 4"/>',
trash: '<path d="M3 6h18"/><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6"/><path d="M8 6V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/><path d="M10 11v6M14 11v6"/>',
copy: '<rect x="9" y="9" width="13" height="13" rx="2"/><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"/>',
external: '<path d="M15 3h6v6"/><path d="M10 14 21 3"/><path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6"/>',
play: '<path d="m7 4 13 8-13 8Z"/>',
stop: '<rect x="6" y="6" width="12" height="12" rx="1.5"/>',
chevron: '<path d="m6 9 6 6 6-6"/>',
key: '<circle cx="8" cy="16" r="4"/><path d="m10.8 13.2 9.7-9.7"/><path d="M17 7l3 3"/>',
lock: '<rect x="4" y="11" width="16" height="10" rx="2"/><path d="M8 11V7a4 4 0 0 1 8 0v4"/>',
check: '<path d="M20 6 9 17l-5-5"/>',
alert: '<circle cx="12" cy="12" r="9"/><path d="M12 8v4.5"/><path d="M12 16h.01"/>',
folder: '<path d="M3 7V5a2 2 0 0 1 2-2h4l2 2h8a2 2 0 0 1 2 2v10a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2Z"/>',
x: '<path d="M6 6l12 12M18 6 6 18"/>',
};
const icon = (n, s = 14, w = 1.8) =>
`<svg class="ic" width="${s}" height="${s}" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="${w}" stroke-linecap="round" stroke-linejoin="round">${I[n]}</svg>`;
/* ---------- 渲染 ---------- */
const STATUS_META = {
stopped: { text: "已停止", cls: "", dot: "" },
connecting: { text: "连接中…", cls: "amber", dot: "amber pulse" },
connected: { text: "已连接", cls: "green", dot: "green" },
error: { text: "连接失败", cls: "red", dot: "red" },
};
function renderAll() {
renderTunnels();
renderHosts();
renderFeed(true);
renderSummary();
$("#badgeHosts").textContent = S.hosts.length || "";
$("#logCount").textContent = S.feed.length ? `${S.feed.length} 条` : "";
}
function renderTunnels() {
const grid = $("#tunnelGrid");
const running = S.tunnels.filter((t) => t.runtime.status === "connected").length;
$("#tunnelCount").textContent = S.tunnels.length ? `${S.tunnels.length} 条 · ${running} 条运行中` : "";
$("#tunnelsEmpty").style.display = S.tunnels.length ? "none" : "";
grid.style.display = S.tunnels.length ? "" : "none";
const b = $("#badgeTunnels");
b.textContent = running || "";
b.classList.toggle("live", running > 0);
grid.innerHTML = S.tunnels.map((t) => {
const rt = t.runtime;
const meta = STATUS_META[rt.status] || STATUS_META.stopped;
const live = rt.status === "connected";
const conn = rt.status === "connecting";
const up = live && rt.started_at ? ` · ${fmtUptime(Date.now() / 1000 - rt.started_at)}` : "";
const authIc = t.host_auth === "key" ? "key" : "lock";
const open = S.openLogs.has(t.id);
return `
<article class="tcard ${live ? "live" : ""}" data-id="${t.id}">
<div class="head">
<span class="dot ${meta.dot}"></span>
<span class="grow name" title="${esc(t.name)}">${esc(t.name)}</span>
<span class="status-text ${meta.cls}" data-status>${meta.text}${up}</span>
<button class="ibtn" data-act="edit" title="编辑">${icon("edit")}</button>
<button class="ibtn red" data-act="del" title="删除">${icon("trash")}</button>
</div>
${rt.status === "error" && rt.error ? `<div class="err-line" title="${esc(rt.error)}">${esc(rt.error)}</div>` : ""}
<div class="mapline">
<span class="addr">127.0.0.1:${t.local_port}</span>
<svg class="ic arrow" width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M5 12h14"/><path d="m13 6 6 6-6 6"/></svg>
<span class="addr">${esc(t.remote_host)}:${t.remote_port}</span>
<span class="grow"></span>
<button class="ibtn" data-act="copy" title="复制本地地址">${icon("copy", 13)}</button>
<button class="ibtn" data-act="open" title="在浏览器打开 http://127.0.0.1:${t.local_port}">${icon("external", 13)}</button>
</div>
<div class="meta-row">
${icon(authIc, 12)}
<span>${esc(t.host_label)}</span>
<span class="sep">|</span>
<span>累计连接 ${rt.conn_count || 0}</span>
<span class="grow"></span>
<button data-act="log" class="${open ? "open" : ""}">${icon("chevron", 12)} 日志</button>
</div>
<div class="acts">
${live || conn
? `<button class="btn sm subtle" data-act="stop">${icon("stop", 12, 2)} 断开</button>`
: `<button class="btn sm primary" data-act="start">${icon("play", 12, 2)} 启动</button>`}
</div>
${open ? `<div class="logbox" data-logbox>${(t.logs || []).map(logLine).join("")}</div>` : ""}
</article>`;
}).join("");
}
const logLine = (e) =>
`<div><span class="lt">${esc(e.t)}</span><span class="l-${esc(e.level)}">${esc(e.msg)}</span></div>`;
function appendLogBox(t) {
const box = $(`#tunnelGrid .tcard[data-id="${t.id}"] [data-logbox]`);
if (!box) return;
box.innerHTML = (t.logs || []).map(logLine).join("");
box.scrollTop = box.scrollHeight;
}
function renderHosts() {
const list = $("#hostList");
$("#hostCount").textContent = S.hosts.length ? `${S.hosts.length} 台` : "";
$("#hostsEmpty").style.display = S.hosts.length ? "none" : "";
list.style.display = S.hosts.length ? "" : "none";
list.innerHTML = S.hosts.map((h) => {
const used = S.tunnels.filter((t) => t.host_id === h.id);
return `
<div class="hcard" data-id="${h.id}">
<span class="icon-box">
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round"><rect x="3" y="4" width="18" height="7" rx="2"/><rect x="3" y="13" width="18" height="7" rx="2"/><path d="M7 7.5h.01M7 16.5h.01"/></svg>
</span>
<div class="info">
<div class="hname">${esc(h.name)}</div>
<div class="haddr">${esc(h.username)}@${esc(h.hostname)}:${h.port}</div>
<div class="tags">
<span class="tag">${icon(h.auth === "key" ? "key" : "lock", 11)} ${h.auth === "key" ? "私钥" : "密码"}</span>
${h.note ? `<span class="tag">${esc(h.note)}</span>` : ""}
</div>
${used.length ? `<div class="used-by">被 ${used.length} 条隧道使用:${esc(used.map((t) => t.name).join("、"))}</div>` : ""}
</div>
<button class="ibtn" data-act="edit" title="编辑">${icon("edit")}</button>
<button class="ibtn red" data-act="del" title="删除">${icon("trash")}</button>
</div>`;
}).join("");
}
/* ---------- 日志视图 ---------- */
const feedRow = (e) =>
`<div class="row l-${esc(e.level)}"><span class="t">${esc(e.t)}</span><span class="src" title="${esc(e.tunnel_name)}">${esc(e.tunnel_name)}</span><span class="m">${esc(e.msg)}</span></div>`;
function renderFeed(clear) {
const el = $("#logFeed");
el.innerHTML = S.feed.length
? S.feed.map(feedRow).join("")
: `<div class="empty-feed">暂无日志 —— 启动一条隧道后,这里会记录连接与转发事件</div>`;
maybeScrollFeed(true);
}
function appendFeedRow(e) {
S.feed.push(e);
if (S.feed.length > 400) S.feed.shift();
const el = $("#logFeed");
const empty = $(".empty-feed", el);
if (empty) empty.remove();
el.insertAdjacentHTML("beforeend", feedRow(e));
while (el.children.length > 400) el.firstChild.remove();
if (S.view === "logs") maybeScrollFeed();
}
function maybeScrollFeed(force) {
if (!$("#autoScroll").checked && !force) return;
const el = $("#logFeed");
el.scrollTop = el.scrollHeight;
}
/* ---------- 顶部/侧栏摘要 ---------- */
function renderSummary() {
const running = S.tunnels.filter((t) => t.runtime.status === "connected").length;
const connecting = S.tunnels.filter((t) => t.runtime.status === "connecting").length;
const dot = $("#connDot"), text = $("#connText");
dot.className = "dot" + (running ? " green" : connecting ? " amber pulse" : "");
text.textContent = running
? `${running} 条隧道运行中`
: connecting ? "正在连接…" : "没有运行中的隧道";
}
/* ---------- Toast ---------- */
function toast(msg, type = "ok") {
const el = document.createElement("div");
el.className = `toast ${type}`;
el.innerHTML = `<span class="t-ic">${icon(type === "error" ? "alert" : "check", 14, 2)}</span><span class="t-msg">${esc(msg)}</span>`;
$("#toastRoot").appendChild(el);
setTimeout(() => { el.classList.add("out"); setTimeout(() => el.remove(), 300); }, 3600);
}
/* ---------- 弹窗框架(支持叠加) ---------- */
const modalStack = [];
function openModal({ title, body, footer }) {
const overlay = document.createElement("div");
overlay.className = "overlay";
overlay.innerHTML = `
<div class="modal" role="dialog">
<div class="m-head"><h2>${esc(title)}</h2>
<button class="ibtn" data-close>${icon("x", 14)}</button></div>
<form novalidate>
<div class="m-body">${body}</div>
<div class="m-foot">${footer}</div>
</form>
</div>`;
$("#modalRoot").appendChild(overlay);
const ctx = { overlay, onOk: null, onClose: null };
modalStack.push(ctx);
const close = () => closeModal(ctx);
overlay.addEventListener("mousedown", (e) => { if (e.target === overlay) close(); });
$("[data-close]", overlay).addEventListener("click", close);
$$("[data-cancel]", overlay).forEach((b) => b.addEventListener("click", close));
$("form", overlay).addEventListener("submit", (e) => {
e.preventDefault();
if (ctx.onOk) ctx.onOk();
});
const first = $("input, select", overlay);
if (first) first.focus();
ctx.close = close;
return ctx;
}
function closeModal(ctx) {
const c = ctx || modalStack[modalStack.length - 1];
if (!c) return;
const i = modalStack.indexOf(c);
if (i >= 0) modalStack.splice(i, 1);
if (c.onClose) c.onClose();
c.overlay.remove();
}
document.addEventListener("keydown", (e) => {
if (e.key === "Escape" && modalStack.length) closeModal();
});
const footBtns = (okText, opts = {}) => `
${opts.dangerSide ? `<button type="button" class="btn danger sm" data-side>${opts.dangerSide}</button><span class="grow"></span>` : `<span class="grow"></span>`}
<button type="button" class="btn sm subtle" data-cancel>取消</button>
<button type="submit" class="btn sm ${opts.okClass || "primary"}">${esc(okText)}</button>`;
/* ---------- 主机编辑弹窗 ---------- */
function openHostModal(existing, opts = {}) {
const h = existing || {};
const ctx = openModal({
title: existing ? "编辑主机" : "新建主机",
body: `
<div class="field"><label>备注名称</label>
<input class="ipt" name="name" placeholder="例如:公司跳板机" value="${esc(h.name || "")}"></div>
<div class="row2-uneven">
<div class="field"><label>主机地址</label>
<input class="ipt mono" name="hostname" placeholder="例如 203.0.113.10 或 gw.example.com" value="${esc(h.hostname || "")}"></div>
<div class="field"><label>SSH 端口</label>
<input class="ipt mono" name="port" value="${esc(h.port || 22)}"></div>
</div>
<div class="field"><label>用户名</label>
<input class="ipt" name="username" placeholder="SSH 登录用户" value="${esc(h.username || "")}"></div>
<div class="field"><label>认证方式</label>
<div class="seg" name="auth">
<button type="button" data-v="password" class="${(h.auth || "password") === "password" ? "on" : ""}">${icon("lock", 13)} 密码</button>
<button type="button" data-v="key" class="${h.auth === "key" ? "on" : ""}">${icon("key", 13)} 私钥</button>
</div></div>
<div data-auth="password" style="${h.auth === "key" ? "display:none" : ""}">
<div class="field"><label>密码</label>
<input class="ipt" type="password" name="password" placeholder="${h.has_password ? "已保存 — 留空则不修改" : "登录密码"}"
autocomplete="new-password"></div>
</div>
<div data-auth="key" style="${h.auth === "key" ? "" : "display:none"}">
<div class="field"><label>私钥文件</label>
<div class="row2-uneven">
<input class="ipt mono" name="key_path" placeholder="例如 C:\\Users\\你\\.ssh\\id_ed25519" value="${esc(h.key_path || "")}">
<button type="button" class="btn" data-pick style="width:100%">浏览…</button>
</div>
<div class="hint">支持 OpenSSH 格式的私钥(id_ed25519 / id_rsa 等)</div></div>
<div class="field"><label>私钥口令(可选)</label>
<input class="ipt" type="password" name="key_passphrase" placeholder="${h.has_passphrase ? "已保存 — 留空则不修改" : "私钥若有口令"}"
autocomplete="new-password"></div>
</div>
<div class="field"><label>备注(可选)</label>
<input class="ipt" name="note" placeholder="" value="${esc(h.note || "")}"></div>
<div class="probe-result" data-probe style="display:none"></div>
`,
footer: `<button type="button" class="btn sm subtle" data-probe-btn style="margin-right:auto">${icon("play", 12, 2)} 测试连接</button>
${footBtns(existing ? "保存" : "添加")}`,
});
const ov = ctx.overlay;
let auth = h.auth || "password";
const segBtns = $$("[name=auth] button", ov);
segBtns.forEach((b) => b.addEventListener("click", () => {
auth = b.dataset.v;
segBtns.forEach((x) => x.classList.toggle("on", x === b));
$("[data-auth=password]", ov).style.display = auth === "password" ? "" : "none";
$("[data-auth=key]", ov).style.display = auth === "key" ? "" : "none";
}));
$("[data-pick]", ov).addEventListener("click", async () => {
try {
const r = await call("pick_key_file");
if (r && r.path) $("input[name=key_path]", ov).value = r.path;
} catch (e) { toast(e.message, "error"); }
});
const collect = () => ({
id: h.id, name: v("name"), hostname: v("hostname"), port: v("port"),
username: v("username"), auth,
password: $("input[name=password]", ov).value,
key_path: v("key_path"), key_passphrase: $("input[name=key_passphrase]", ov).value,
note: v("note"),
has_password: !!h.has_password, has_passphrase: !!h.has_passphrase,
});
function v(name) { return $(`input[name=${name}]`, ov).value.trim(); }
const showProbe = (ok, msg) => {
const el = $("[data-probe]", ov);
el.style.display = "";
el.className = `probe-result ${ok ? "ok" : "bad"}`;
el.innerHTML = `${icon(ok ? "check" : "alert", 14, 2)}<span>${esc(msg)}</span>`;
};
$("[data-probe-btn]", ov).addEventListener("click", async () => {
const btn = $("[data-probe-btn]", ov);
btn.disabled = true;
btn.textContent = "测试中…";
try { const r = await call("probe_host", collect()); showProbe(r.ok, r.message || r.error || ""); }
catch (e) { showProbe(false, e.message); }
btn.disabled = false;
btn.innerHTML = `${icon("play", 12, 2)} 测试连接`;
});
ctx.onOk = async () => {
const p = collect();
let bad = false;
if (!p.hostname) { markErr("hostname"), bad = true; }
if (!p.username) { markErr("username"), bad = true; }
if (!isPort(p.port)) { markErr("port"), bad = true; }
if (bad) return;
try {
const r = await call("save_host", p);
applyState(r.state);
toast(existing ? "主机已更新" : "主机已添加");
closeModal(ctx);
if (opts.onSaved) opts.onSaved(r.id);
} catch (e) { toast(e.message, "error"); }
};
function markErr(name) { $(`input[name=${name}]`, ov).classList.add("err"); }
return ctx;
}
/* ---------- 隧道编辑弹窗 ---------- */
function openTunnelModal(existing) {
const t = existing || {};
const hostOpts = () => S.hosts.map((h) =>
`<option value="${h.id}" ${t.host_id === h.id ? "selected" : ""}>${esc(h.name)}(${esc(h.username)}@${esc(h.hostname)})</option>`).join("");
const ctx = openModal({
title: existing ? "编辑隧道" : "新建隧道",
body: `
<div class="field"><label>名称</label>
<input class="ipt" name="name" placeholder="${esc(t.name || "例如 dev-web")}" value="${esc(existing ? t.name : "")}"></div>
<div class="field"><label>SSH 主机</label>
<div class="row2-uneven">
<div class="sel-wrap"><select class="ipt" name="host_id">${hostOpts()}</select></div>
<button type="button" class="btn" data-newhost style="width:100%">新主机…</button>
</div></div>
<div class="row2">
<div class="field"><label>远程地址</label>
<input class="ipt mono" name="remote_host" value="${esc(t.remote_host || "127.0.0.1")}">
<div class="hint">服务在远程主机上监听的地址,只对本机开放的服务填 127.0.0.1</div></div>
<div class="field"><label>远程端口</label>
<input class="ipt mono" name="remote_port" placeholder="例如 3000" value="${esc(t.remote_port || "")}"></div>
</div>
<div class="field"><label>本地端口</label>
<input class="ipt mono" name="local_port" placeholder="本机访问用的端口" value="${esc(t.local_port || "")}">
<div class="hint">建立后,在本机打开 <b>127.0.0.1:本地端口</b>,等同于直接访问远程的这个服务</div></div>
<div class="switch-row">
<span class="sw-text"><span class="sw-title">自动重连</span><br>
<span class="sw-desc">连接断开后自动重试,重试间隔逐渐拉长</span></span>
<input type="checkbox" class="switch" name="autoreconnect" ${t.autoreconnect !== false && !("autoreconnect" in t) || t.autoreconnect ? "checked" : ""}>
</div>
<div class="switch-row">
<span class="sw-text"><span class="sw-title">随应用启动</span><br>
<span class="sw-desc">打开管理器时自动建立这条隧道</span></span>
<input type="checkbox" class="switch" name="autostart" ${t.autostart ? "checked" : ""}>
</div>
`,
footer: footBtns(existing ? "保存" : "创建隧道"),
});
const ov = ctx.overlay;
if (!S.hosts.length) $("select[name=host_id]", ov).innerHTML = `<option value="">—— 请先添加主机 ——</option>`;
// 远程端口 → 本地端口跟随
const rp = $("input[name=remote_port]", ov), lp = $("input[name=local_port]", ov);
rp.addEventListener("input", () => { if (!lp.dataset.touched && isPort(rp.value)) lp.value = rp.value; });
lp.addEventListener("input", () => { lp.dataset.touched = "1"; });
$("[data-newhost]", ov).addEventListener("click", () => {
openHostModal(null, {
onSaved: (id) => {
const sel = $("select[name=host_id]", ov);
sel.innerHTML = S.hosts.map((h) =>
`<option value="${h.id}" ${h.id === id ? "selected" : ""}>${esc(h.name)}(${esc(h.username)}@${esc(h.hostname)})</option>`).join("");
},
});
});
ctx.onOk = async () => {
const v = (n) => $(`[name=${n}]`, ov).value.trim();
let bad = false;
if (!isPort(v("remote_port"))) { markErr("remote_port"); bad = true; }
if (!isPort(v("local_port"))) { markErr("local_port"); bad = true; }
if (!v("host_id")) { toast("请选择主机(或先新建一台)", "error"); bad = true; }
if (bad) return;
try {
const r = await call("save_tunnel", {
id: t.id, name: v("name"), host_id: v("host_id"),
remote_host: v("remote_host") || "127.0.0.1", remote_port: v("remote_port"),
local_port: v("local_port"),
autostart: $("[name=autostart]", ov).checked,
autoreconnect: $("[name=autoreconnect]", ov).checked,
});
applyState(r.state);
toast(existing ? "隧道已更新" : "隧道已创建,点「启动」建立连接");
closeModal(ctx);
} catch (e) { toast(e.message, "error"); }
};
function markErr(n) { $(`[name=${n}]`, ov).classList.add("err"); }
}
/* ---------- 删除确认 ---------- */
function confirmDanger(title, html) {
return new Promise((resolve) => {
const ctx = openModal({
title,
body: `<div class="confirm-text">${html}</div>`,
footer: `<span class="grow"></span>
<button type="button" class="btn sm subtle" data-cancel>取消</button>
<button type="button" class="btn sm danger" data-ok>删除</button>`,
});
ctx.onClose = () => resolve(false);
$("[data-ok]", ctx.overlay).addEventListener("click", () => {
ctx.onClose = null; resolve(true); closeModal(ctx);
});
});
}
/* ---------- 事件绑定 ---------- */
function switchView(name) {
S.view = name;
$$(".nav-item").forEach((b) => b.classList.toggle("on", b.dataset.view === name));
$$(".view").forEach((v) => v.classList.toggle("on", v.id === `view-${name}`));
if (name === "logs") maybeScrollFeed(true);
}
function bindOnce() {
$$(".nav-item").forEach((b) => b.addEventListener("click", () => switchView(b.dataset.view)));
$("#btnNewTunnel").addEventListener("click", () => openTunnelModal());
$("#btnEmptyNewTunnel").addEventListener("click", () => openTunnelModal());
$("#btnNewHost").addEventListener("click", () => openHostModal());
$("#btnEmptyNewHost").addEventListener("click", () => openHostModal());
$("#btnClearLogs").addEventListener("click", () => { S.feed = []; renderFeed(); });
// 隧道卡片(事件委托)
$("#tunnelGrid").addEventListener("click", async (e) => {
const card = e.target.closest(".tcard");
if (!card) return;
const t = S.tunnels.find((x) => x.id === card.dataset.id);
if (!t) return;
const act = e.target.closest("[data-act]")?.dataset.act;
if (!act) return;
try {
if (act === "start") await call("start_tunnel", t.id);
else if (act === "stop") await call("stop_tunnel", t.id);
else if (act === "edit") openTunnelModal(t);
else if (act === "del") {
const ok = await confirmDanger("删除隧道",
`确定删除隧道 <b>${esc(t.name)}</b>?<br>映射关系 127.0.0.1:${t.local_port} → ${esc(t.remote_host)}:${t.remote_port} 将被移除。`);
if (!ok) return;
const r = await call("delete_tunnel", t.id);
applyState(r.state);
toast("隧道已删除");
} else if (act === "copy") {
await call("copy_text", `http://127.0.0.1:${t.local_port}`);
toast("已复制 " + `http://127.0.0.1:${t.local_port}`);
} else if (act === "open") {
await call("open_url", `http://127.0.0.1:${t.local_port}`);
} else if (act === "log") {
S.openLogs.has(t.id) ? S.openLogs.delete(t.id) : S.openLogs.add(t.id);
renderTunnels();
}
} catch (err) { toast(err.message, "error"); }
});
// 主机卡片
$("#hostList").addEventListener("click", async (e) => {
const card = e.target.closest(".hcard");
if (!card) return;
const h = S.hosts.find((x) => x.id === card.dataset.id);
if (!h) return;
const act = e.target.closest("[data-act]")?.dataset.act;
if (!act) return;
try {
if (act === "edit") openHostModal(h);
else if (act === "del") {
const used = S.tunnels.filter((t) => t.host_id === h.id);
if (used.length) { toast(`该主机正被「${used.map((t) => t.name).join("、")}」使用,请先删除相关隧道`, "error"); return; }
const ok = await confirmDanger("删除主机",
`确定删除主机 <b>${esc(h.name)}</b>(${esc(h.username)}@${esc(h.hostname)})?`);
if (!ok) return;
const r = await call("delete_host", h.id);
applyState(r.state);
toast("主机已删除");
}
} catch (err) { toast(err.message, "error"); }
});
// 窗口控制
$("#btnMin").addEventListener("click", () => call("win_minimize").catch(() => {}));
$("#btnMax").addEventListener("click", () => call("win_maximize").catch(() => {}));
$("#btnClose").addEventListener("click", () => call("win_close").catch(() => {}));
$("#dragZone").addEventListener("dblclick", () => call("win_maximize").catch(() => {}));
// 运行时长刷新
setInterval(() => {
$$("#tunnelGrid .tcard").forEach((card) => {
const t = S.tunnels.find((x) => x.id === card.dataset.id);
if (!t || t.runtime.status !== "connected" || !t.runtime.started_at) return;
const st = $("[data-status]", card);
st.textContent = `已连接 · ${fmtUptime(Date.now() / 1000 - t.runtime.started_at)}`;
});
}, 1000);
}
/* ---------- 启动 ---------- */
function whenReady() {
return new Promise((resolve) => {
if (nativeApi()) return resolve();
window.addEventListener("pywebviewready", () => resolve(), { once: true });
setTimeout(resolve, 1500); // 浏览器 Mock 兜底
});
}
(async function init() {
bindOnce();
renderAll();
await whenReady();
try {
const r = await withTimeout(call("get_state"), 5000, "连接应用后端超时");
applyState(r.state);
} catch (e) {
toast("加载配置失败:" + e.message, "error");
}
})();
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 8.5 KiB

+117
View File
@@ -0,0 +1,117 @@
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>SSH 隧道管理器</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<header id="titlebar" class="titlebar">
<div class="drag-zone pywebview-drag-region" id="dragZone">
<span class="logo"><img src="logo.svg" alt=""></span>
<span class="title">SSH 隧道管理器</span>
</div>
<div class="win-controls">
<button class="wbtn" id="btnMin" title="最小化">
<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><path d="M5 12h14"/></svg>
</button>
<button class="wbtn" id="btnMax" title="最大化 / 还原">
<svg width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="5" y="5" width="14" height="14" rx="2"/></svg>
</button>
<button class="wbtn close" id="btnClose" title="关闭">
<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><path d="M6 6l12 12M18 6L6 18"/></svg>
</button>
</div>
</header>
<div id="app">
<nav id="nav">
<button class="nav-item on" data-view="tunnels">
<svg class="ic" width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round"><circle cx="12" cy="12" r="3.2"/><path d="M3 12h5.6M15.4 12H21"/></svg>
隧道
<span class="nav-badge" id="badgeTunnels"></span>
</button>
<button class="nav-item" data-view="hosts">
<svg class="ic" width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round"><rect x="3" y="4" width="18" height="7" rx="2"/><rect x="3" y="13" width="18" height="7" rx="2"/><path d="M7 7.5h.01M7 16.5h.01"/></svg>
主机
<span class="nav-badge" id="badgeHosts"></span>
</button>
<button class="nav-item" data-view="logs">
<svg class="ic" width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M14 3H7a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V8z"/><path d="M14 3v5h5"/><path d="M9.5 13h5M9.5 17h3"/></svg>
日志
</button>
<div class="nav-foot">
<div class="conn-summary" id="connSummary">
<span class="dot" id="connDot"></span>
<span id="connText">没有运行中的隧道</span>
</div>
</div>
</nav>
<main id="main">
<section class="view on" id="view-tunnels">
<header class="view-head">
<h1>隧道</h1>
<span class="count" id="tunnelCount"></span>
<span class="spacer"></span>
<button class="btn primary" id="btnNewTunnel">
<svg class="ic" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><path d="M12 5v14M5 12h14"/></svg>
新建隧道
</button>
</header>
<div class="card-grid" id="tunnelGrid"></div>
<div class="empty" id="tunnelsEmpty" style="display:none">
<div class="glyph">
<svg width="38" height="38" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.4" stroke-linecap="round"><circle cx="12" cy="12" r="3.4"/><path d="M2.5 12h4.2M17.3 12h4.2"/></svg>
</div>
<h2>还没有隧道</h2>
<p>把远程主机上只对本机开放的服务,映射到你本机的一个端口</p>
<button class="btn primary" id="btnEmptyNewTunnel">新建第一条隧道</button>
</div>
</section>
<section class="view" id="view-hosts">
<header class="view-head">
<h1>主机</h1>
<span class="count" id="hostCount"></span>
<span class="spacer"></span>
<button class="btn" id="btnNewHost">
<svg class="ic" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><path d="M12 5v14M5 12h14"/></svg>
新建主机
</button>
</header>
<div class="host-list" id="hostList"></div>
<div class="empty" id="hostsEmpty" style="display:none">
<div class="glyph">
<svg width="38" height="38" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.4" stroke-linecap="round"><rect x="3" y="4" width="18" height="7" rx="2"/><rect x="3" y="13" width="18" height="7" rx="2"/><path d="M7 7.5h.01M7 16.5h.01"/></svg>
</div>
<h2>还没有主机</h2>
<p>添加一台 SSH 主机后,就可以基于它建立端口隧道</p>
<button class="btn primary" id="btnEmptyNewHost">添加主机</button>
</div>
</section>
<section class="view" id="view-logs">
<header class="view-head">
<h1>日志</h1>
<span class="count" id="logCount"></span>
<span class="spacer"></span>
<label class="switch-row" style="padding:0">
<span class="sw-title" style="font-size:12px;color:var(--dim)">自动滚动</span>
<input type="checkbox" class="switch" id="autoScroll" checked>
</label>
<button class="btn subtle sm" id="btnClearLogs">清空显示</button>
</header>
<div class="log-feed" id="logFeed"></div>
</section>
</main>
</div>
<div id="modalRoot"></div>
<div id="toastRoot"></div>
<script src="app.js"></script>
</body>
</html>
+9
View File
@@ -0,0 +1,9 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256">
<g fill="none">
<rect width="256" height="256" fill="#242938" rx="60" />
<path fill="#242938" fill-rule="evenodd" d="m203.819 68.835l-63.14-37.48a23.79 23.79 0 0 0-24.361 0l-63.14 37.48C45.642 73.31 41 81.575 41 90.522v74.961c0 8.945 4.643 17.215 12.18 21.689l63.14 37.473a23.8 23.8 0 0 0 12.179 3.354a23.8 23.8 0 0 0 12.178-3.354l63.14-37.473c7.536-4.474 12.182-12.744 12.182-21.689v-74.96c0-8.948-4.646-17.214-12.18-21.688" clip-rule="evenodd" />
<path fill="#fff" fill-rule="evenodd" d="m118.527 220.808l-63.14-37.474c-6.176-3.666-10.013-10.506-10.013-17.852V90.523c0-7.346 3.837-14.186 10.01-17.85l63.143-37.48a19.55 19.55 0 0 1 9.972-2.747c3.495 0 6.943.95 9.973 2.747l63.14 37.48c5.204 3.089 8.714 8.438 9.701 14.437c-2.094-4.469-6.817-5.684-12.32-2.47l-59.734 36.897c-7.448 4.354-12.94 9.24-12.945 18.221v73.604c-.004 5.378 2.168 8.861 5.504 9.871c-1.096.19-2.201.322-3.319.322a19.55 19.55 0 0 1-9.972-2.747m85.292-151.974l-63.14-37.478A23.8 23.8 0 0 0 128.499 28a23.8 23.8 0 0 0-12.181 3.356l-63.14 37.478C45.642 73.308 41 81.576 41 90.524v74.958c0 8.945 4.643 17.215 12.18 21.689l63.14 37.475A23.84 23.84 0 0 0 128.499 228a23.83 23.83 0 0 0 12.178-3.354l63.142-37.475c7.536-4.474 12.18-12.744 12.18-21.689V90.523c0-8.947-4.644-17.215-12.18-21.689" clip-rule="evenodd" />
<path fill="#47b353" fill-rule="evenodd" d="m187.267 172.729l-15.722 9.41c-.417.243-.723.516-.726 1.017v4.114c0 .503.338.712.754.467l15.966-9.703c.416-.243.48-.708.483-1.209v-3.629c0-.5-.338-.71-.755-.467" clip-rule="evenodd" />
<path fill="#242938" fill-rule="evenodd" d="M153.788 138.098c.509-.258.928.059.935.725l.053 5.439c2.277-.906 4.255-1.148 6.047-.734c.389.104.561.633.402 1.261l-1.197 4.82c-.093.364-.298.732-.545.961a1.3 1.3 0 0 1-.315.234a.7.7 0 0 1-.472.077c-.818-.185-2.763-.61-5.823.94c-3.21 1.625-4.333 4.414-4.311 6.484c.027 2.472 1.295 3.221 5.673 3.296c5.834.097 8.355 2.646 8.416 8.522c.06 5.77-3.02 11.966-7.732 15.763l.104 5.384c.006.648-.415 1.391-.924 1.649l-3.189 1.837c-.511.258-.93-.06-.937-.708l-.055-5.296c-2.731 1.135-5.499 1.409-7.267.699c-.333-.13-.476-.622-.344-1.182l1.156-4.868c.092-.384.295-.768.571-1.012q.147-.142.299-.219c.183-.092.362-.112.514-.055c1.905.642 4.342.342 6.685-.844c2.977-1.506 4.968-4.542 4.937-7.558c-.029-2.737-1.51-3.874-5.113-3.901c-4.586.013-8.861-.891-8.932-7.642c-.057-5.558 2.833-11.342 7.408-14.999l-.057-5.435c-.007-.668.401-1.403.926-1.667z" clip-rule="evenodd" />
</g>
</svg>

After

Width:  |  Height:  |  Size: 2.4 KiB

+630
View File
@@ -0,0 +1,630 @@
/* SSH 隧道管理器 — 界面样式
原则:石墨深色底、单一点缀色、无渐变无发光、系统字体 */
:root {
--bg: #111418;
--surface: #171b21;
--surface-2: #1d232b;
--surface-3: #242b34;
--border: #28303a;
--border-strong: #384250;
--text: #e2e7ee;
--dim: #97a1ad;
--faint: #5f6a76;
--accent: #2fbfa1;
--accent-strong: #43d6b5;
--accent-dim: rgba(47, 191, 161, 0.13);
--on-accent: #07130f;
--green: #46c46e;
--green-dim: rgba(70, 196, 110, 0.12);
--amber: #d9a23c;
--amber-dim: rgba(217, 162, 60, 0.12);
--red: #e2564d;
--red-dim: rgba(226, 86, 77, 0.12);
--r-lg: 10px;
--r-md: 8px;
--r-sm: 6px;
--mono: "Cascadia Mono", "JetBrains Mono", "Consolas", ui-monospace, monospace;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
font: 13px/1.55 "Segoe UI Variable Text", "Segoe UI", "Microsoft YaHei UI",
system-ui, sans-serif;
background: var(--bg);
color: var(--text);
overflow: hidden;
user-select: none;
cursor: default;
}
::selection { background: var(--accent-dim); color: var(--text); }
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb {
background: var(--surface-3);
border: 3px solid transparent;
border-radius: 6px;
background-clip: padding-box;
}
::-webkit-scrollbar-thumb:hover { background-color: var(--border-strong); }
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; }
input, select { font: inherit; color: var(--text); }
svg.ic { flex: none; vertical-align: -2px; }
.mono { font-family: var(--mono); }
/* ---------- 标题栏 ---------- */
.titlebar {
height: 42px;
display: flex;
align-items: center;
-webkit-app-region: no-drag;
}
.titlebar .drag-zone {
display: flex;
align-items: center;
gap: 9px;
flex: 1;
height: 100%;
padding-left: 14px;
}
.titlebar .logo {
width: 24px;
height: 24px;
border-radius: 7px;
overflow: hidden;
flex: none;
}
.titlebar .logo img { width: 100%; height: 100%; display: block; }
.titlebar .title {
font-size: 12.5px;
font-weight: 600;
letter-spacing: 0.2px;
color: var(--dim);
}
.win-controls { display: flex; height: 100%; }
.wbtn {
width: 44px;
height: 100%;
display: grid;
place-items: center;
color: var(--dim);
transition: background 0.12s, color 0.12s;
}
.wbtn:hover { background: var(--surface-2); color: var(--text); }
.wbtn.close:hover { background: #d0342c; color: #fff; }
/* ---------- 布局 ---------- */
#app { display: flex; height: calc(100% - 42px); }
nav#nav {
width: 172px;
flex: none;
display: flex;
flex-direction: column;
padding: 10px 10px 12px;
border-right: 1px solid var(--border);
}
.nav-item {
display: flex;
align-items: center;
gap: 9px;
padding: 8px 10px;
border-radius: var(--r-md);
color: var(--dim);
font-size: 13px;
margin-bottom: 2px;
transition: background 0.12s, color 0.12s;
text-align: left;
}
.nav-item:hover { background: var(--surface); color: var(--text); }
.nav-item.on {
background: var(--surface-2);
color: var(--text);
font-weight: 500;
}
.nav-item.on .ic { color: var(--accent); }
.nav-item .nav-badge {
margin-left: auto;
font-size: 11px;
font-family: var(--mono);
color: var(--faint);
}
.nav-item .nav-badge.live { color: var(--green); }
.nav-foot { margin-top: auto; padding: 0 6px; }
.conn-summary {
display: flex;
align-items: center;
gap: 7px;
font-size: 11.5px;
color: var(--faint);
border-top: 1px solid var(--border);
padding-top: 12px;
}
main#main { flex: 1; min-width: 0; overflow-y: auto; }
.view { display: none; padding: 22px 26px 30px; }
.view.on { display: block; }
.view-head {
display: flex;
align-items: center;
margin-bottom: 16px;
gap: 12px;
}
.view-head h1 { font-size: 16px; font-weight: 600; }
.view-head .count {
font-family: var(--mono);
font-size: 12px;
color: var(--faint);
margin-top: 1px;
}
.view-head .spacer { flex: 1; }
/* ---------- 通用控件 ---------- */
.btn {
display: inline-flex;
align-items: center;
gap: 6px;
height: 32px;
padding: 0 14px;
border-radius: var(--r-md);
background: var(--surface-2);
border: 1px solid var(--border-strong);
font-size: 12.5px;
font-weight: 500;
color: var(--text);
transition: background 0.12s, border-color 0.12s, color 0.12s;
white-space: nowrap;
}
.btn:hover { background: var(--surface-3); }
.btn.primary {
background: var(--accent);
border-color: var(--accent);
color: var(--on-accent);
font-weight: 600;
}
.btn.primary:hover { background: var(--accent-strong); border-color: var(--accent-strong); }
.btn.sm { height: 28px; padding: 0 11px; font-size: 12px; border-radius: var(--r-sm); }
.btn.subtle { background: transparent; border-color: var(--border); color: var(--dim); }
.btn.subtle:hover { color: var(--text); background: var(--surface-2); }
.btn.danger { color: var(--red); border-color: rgba(226, 86, 77, 0.4); }
.btn.danger:hover { background: var(--red-dim); border-color: var(--red); }
.btn:disabled { opacity: 0.45; pointer-events: none; }
.ibtn {
width: 28px;
height: 28px;
border-radius: var(--r-sm);
display: inline-grid;
place-items: center;
color: var(--faint);
transition: background 0.12s, color 0.12s;
flex: none;
}
.ibtn:hover { background: var(--surface-3); color: var(--text); }
.ibtn.red:hover { background: var(--red-dim); color: var(--red); }
.field { margin-bottom: 13px; }
.field > label {
display: block;
font-size: 12px;
font-weight: 500;
color: var(--dim);
margin-bottom: 6px;
}
.field .hint {
font-size: 11.5px;
color: var(--faint);
margin-top: 5px;
line-height: 1.45;
}
.ipt {
width: 100%;
height: 34px;
padding: 0 10px;
background: var(--surface-2);
border: 1px solid var(--border);
border-radius: var(--r-md);
outline: none;
transition: border-color 0.12s, box-shadow 0.12s;
}
textarea.ipt { height: auto; padding: 8px 10px; resize: vertical; }
.ipt:hover { border-color: var(--border-strong); }
.ipt:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-dim); }
.ipt::placeholder { color: var(--faint); }
.ipt.err { border-color: var(--red); }
select.ipt { appearance: none; padding-right: 28px; cursor: pointer; }
.sel-wrap { position: relative; }
.sel-wrap::after {
content: "";
position: absolute;
right: 11px; top: 13px;
width: 7px; height: 7px;
border-right: 1.6px solid var(--faint);
border-bottom: 1.6px solid var(--faint);
transform: rotate(45deg);
pointer-events: none;
}
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 12px; }
.row2-uneven { display: grid; grid-template-columns: 1fr 110px; gap: 0 12px; }
.seg {
display: flex;
background: var(--surface-2);
border: 1px solid var(--border);
border-radius: var(--r-md);
padding: 2px;
gap: 2px;
}
.seg button {
flex: 1;
height: 28px;
border-radius: var(--r-sm);
color: var(--dim);
font-size: 12.5px;
display: inline-flex;
align-items: center;
justify-content: center;
gap: 6px;
transition: background 0.12s, color 0.12s;
}
.seg button:hover { color: var(--text); }
.seg button.on {
background: var(--surface-3);
color: var(--text);
box-shadow: inset 0 0 0 1px var(--border-strong);
}
.switch-row {
display: flex;
align-items: center;
gap: 10px;
padding: 7px 0;
}
.switch-row .sw-text { flex: 1; }
.switch-row .sw-title { font-size: 12.5px; }
.switch-row .sw-desc { font-size: 11.5px; color: var(--faint); }
.switch {
position: relative;
width: 34px; height: 20px;
flex: none;
appearance: none;
background: var(--surface-3);
border: 1px solid var(--border-strong);
border-radius: 20px;
cursor: pointer;
transition: background 0.15s, border-color 0.15s;
}
.switch::after {
content: "";
position: absolute;
top: 2px; left: 2px;
width: 14px; height: 14px;
border-radius: 50%;
background: var(--dim);
transition: left 0.15s, background 0.15s;
}
.switch:checked { background: var(--accent-dim); border-color: var(--accent); }
.switch:checked::after { left: 16px; background: var(--accent); }
/* ---------- 隧道卡片 ---------- */
.card-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(430px, 1fr));
gap: 14px;
}
.tcard {
background: var(--surface);
border: 1px solid var(--border);
border-radius: var(--r-lg);
padding: 14px 16px 13px;
display: flex;
flex-direction: column;
gap: 10px;
transition: border-color 0.15s;
}
.tcard:hover { border-color: var(--border-strong); }
.tcard.live { border-color: rgba(70, 196, 110, 0.25); }
.tcard .head { display: flex; align-items: center; gap: 8px; }
.tcard .name {
font-size: 13.5px;
font-weight: 600;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.tcard .head .grow { flex: 1; min-width: 0; }
.dot {
width: 8px; height: 8px;
border-radius: 50%;
flex: none;
background: var(--faint);
}
.dot.green { background: var(--green); box-shadow: 0 0 0 3px var(--green-dim); }
.dot.amber { background: var(--amber); box-shadow: 0 0 0 3px var(--amber-dim); }
.dot.red { background: var(--red); box-shadow: 0 0 0 3px var(--red-dim); }
.dot.pulse { animation: pulse 1.6s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: 0.35; } }
.status-text { font-size: 12px; color: var(--dim); white-space: nowrap; }
.status-text.green { color: var(--green); }
.status-text.amber { color: var(--amber); }
.status-text.red { color: var(--red); }
.err-line {
font-size: 11.5px;
color: var(--red);
background: var(--red-dim);
border: 1px solid rgba(226, 86, 77, 0.25);
border-radius: var(--r-sm);
padding: 6px 9px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.mapline {
display: flex;
align-items: center;
gap: 8px;
font-family: var(--mono);
font-size: 12px;
color: var(--dim);
background: var(--surface-2);
border: 1px solid var(--border);
border-radius: var(--r-md);
padding: 7px 8px 7px 11px;
}
.mapline .addr { color: var(--text); }
.mapline .arrow { color: var(--accent); flex: none; }
.mapline .grow { flex: 1; }
.meta-row {
display: flex;
align-items: center;
gap: 5px;
font-size: 11.5px;
color: var(--faint);
}
.meta-row .ic { color: var(--faint); }
.meta-row .sep { margin: 0 4px; color: var(--border-strong); }
.meta-row .grow { flex: 1; }
.meta-row button {
color: var(--faint);
display: inline-flex;
align-items: center;
gap: 4px;
font-size: 11.5px;
padding: 2px 6px;
border-radius: var(--r-sm);
transition: color 0.12s, background 0.12s;
}
.meta-row button:hover { color: var(--text); background: var(--surface-2); }
.meta-row button .ic { transition: transform 0.15s; }
.meta-row button.open .ic { transform: rotate(180deg); }
.acts { display: flex; align-items: center; gap: 8px; }
.acts .grow { flex: 1; }
.logbox {
font-family: var(--mono);
font-size: 11px;
line-height: 1.7;
color: var(--dim);
background: #0d1013;
border: 1px solid var(--border);
border-radius: var(--r-md);
padding: 8px 11px;
max-height: 130px;
overflow-y: auto;
user-select: text;
}
.logbox .lt { color: var(--faint); margin-right: 8px; }
.logbox .l-warn { color: var(--amber); }
.logbox .l-error { color: var(--red); }
/* ---------- 主机列表 ---------- */
.host-list { display: flex; flex-direction: column; gap: 10px; max-width: 860px; }
.hcard {
background: var(--surface);
border: 1px solid var(--border);
border-radius: var(--r-lg);
padding: 12px 16px;
display: flex;
align-items: center;
gap: 12px;
transition: border-color 0.15s;
}
.hcard:hover { border-color: var(--border-strong); }
.hcard .icon-box {
width: 34px; height: 34px;
border-radius: var(--r-md);
background: var(--surface-2);
border: 1px solid var(--border);
display: grid;
place-items: center;
color: var(--dim);
flex: none;
}
.hcard .info { flex: 1; min-width: 0; }
.hcard .hname { font-weight: 600; font-size: 13px; }
.hcard .haddr {
font-family: var(--mono);
font-size: 11.5px;
color: var(--dim);
margin-top: 2px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.hcard .tags { display: flex; gap: 6px; margin-top: 5px; }
.hcard .used-by { font-size: 11.5px; color: var(--faint); margin-top: 5px; }
.tag {
display: inline-flex;
align-items: center;
gap: 4px;
font-size: 10.5px;
padding: 1px 7px;
border-radius: 20px;
border: 1px solid var(--border-strong);
color: var(--dim);
background: var(--surface-2);
}
/* ---------- 日志视图 ---------- */
.log-feed {
background: #0d1013;
border: 1px solid var(--border);
border-radius: var(--r-lg);
padding: 12px 16px;
font-family: var(--mono);
font-size: 11.5px;
line-height: 1.85;
height: calc(100vh - 175px);
overflow-y: auto;
user-select: text;
}
.log-feed .row { display: flex; gap: 12px; }
.log-feed .t { color: var(--faint); flex: none; width: 62px; }
.log-feed .src { color: var(--dim); flex: none; width: 130px; overflow: hidden; text-overflow: ellipsis; }
.log-feed .m { color: var(--dim); word-break: break-all; }
.log-feed .l-info .m { color: var(--dim); }
.log-feed .l-success .m { color: var(--green); }
.log-feed .l-warn .m { color: var(--amber); }
.log-feed .l-error .m { color: var(--red); }
.log-feed .empty-feed { color: var(--faint); text-align: center; padding-top: 60px; }
/* ---------- 空状态 ---------- */
.empty {
border: 1px dashed var(--border-strong);
border-radius: var(--r-lg);
padding: 56px 20px;
text-align: center;
color: var(--faint);
}
.empty .glyph { color: var(--faint); opacity: 0.75; margin-bottom: 12px; }
.empty h2 { font-size: 13.5px; font-weight: 600; color: var(--dim); margin-bottom: 5px; }
.empty p { font-size: 12px; margin-bottom: 16px; }
/* ---------- 弹窗 ---------- */
.overlay {
position: fixed;
inset: 0;
background: rgba(5, 7, 9, 0.62);
display: grid;
place-items: center;
z-index: 50;
animation: fadein 0.14s ease;
}
@keyframes fadein { from { opacity: 0; } }
.modal {
width: 480px;
max-width: calc(100vw - 48px);
max-height: calc(100vh - 80px);
overflow-y: auto;
background: var(--surface);
border: 1px solid var(--border-strong);
border-radius: 12px;
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.45);
animation: rise 0.16s ease;
}
@keyframes rise { from { transform: translateY(8px); opacity: 0; } }
.modal .m-head {
display: flex;
align-items: center;
padding: 15px 18px 13px;
border-bottom: 1px solid var(--border);
}
.modal .m-head h2 { font-size: 14px; font-weight: 600; flex: 1; }
.modal .m-body { padding: 16px 18px 4px; }
.modal .m-foot {
display: flex;
align-items: center;
gap: 8px;
padding: 14px 18px 16px;
}
.modal .m-foot .grow { flex: 1; }
.probe-result {
display: flex;
gap: 7px;
align-items: flex-start;
font-size: 12px;
border-radius: var(--r-md);
padding: 8px 10px;
margin-bottom: 13px;
line-height: 1.5;
word-break: break-all;
}
.probe-result.ok { background: var(--green-dim); color: var(--green); }
.probe-result.bad { background: var(--red-dim); color: var(--red); }
.confirm-text { font-size: 13px; color: var(--dim); line-height: 1.6; padding-bottom: 10px; }
.confirm-text b { color: var(--text); }
/* ---------- Toast ---------- */
#toastRoot {
position: fixed;
right: 16px;
bottom: 16px;
display: flex;
flex-direction: column;
gap: 8px;
z-index: 100;
}
.toast {
display: flex;
align-items: flex-start;
gap: 8px;
min-width: 220px;
max-width: 380px;
background: var(--surface-2);
border: 1px solid var(--border-strong);
border-left: 3px solid var(--accent);
border-radius: var(--r-md);
padding: 10px 13px;
font-size: 12.5px;
box-shadow: 0 8px 26px rgba(0, 0, 0, 0.35);
animation: rise 0.18s ease;
}
.toast.error { border-left-color: var(--red); }
.toast .t-msg { flex: 1; word-break: break-all; }
.toast .t-ic { margin-top: 1px; }
.toast.ok .t-ic { color: var(--accent); }
.toast.error .t-ic { color: var(--red); }
.toast.out { transition: opacity 0.25s, transform 0.25s; opacity: 0; transform: translateX(12px); }