| 1 | import React, { useEffect, useRef, useState } from "react"; |
| 2 | import { Check, ChevronDown, Copy, ExternalLink, KeyRound, LoaderCircle, Monitor, Network, RefreshCw, Search, Smartphone, Terminal, Trash2 } from "lucide-react"; |
| 3 | import { api, writeClipboard } from "./api"; |
| 4 | import { Modal } from "./modal"; |
| 5 | import "./tailscale.css"; |
| 6 | |
| 7 | const labels = { Running: "已连接", NeedsLogin: "等待登录", NeedsMachineAuth: "等待管理员批准", Starting: "正在连接", Stopped: "已断开", NotInstalled: "尚未安装" }; |
| 8 | const descriptions = { |
| 9 | Stopped: "登录 Tailscale,让 Cage 加入你的私网。其他设备加入同一网络后,会显示在这里。", |
| 10 | NeedsLogin: "在打开的 Tailscale 官方页面完成登录并授权此设备,然后返回这里。", |
| 11 | NeedsMachineAuth: "登录已完成。请让此网络的管理员在 Tailscale 管理台批准 Cage 设备。", |
| 12 | Starting: "正在连接 Tailscale,设备清单会在连接成功后自动更新。", |
| 13 | }; |
| 14 | const deviceName = peer => peer.hostname || peer.name?.split(".")[0] || peer.ip; |
| 15 | function DeviceIcon({ os }) { return ["android", "iOS"].includes(os) ? <Smartphone size={20} /> : <Monitor size={20} />; } |
| 16 | |
| 17 | export function TailscalePanel({ status, onClose, onOpenBrowser, onInstall, onConfirm }) { |
| 18 | const [state, setState] = useState(null), [error, setError] = useState(""), [busy, setBusy] = useState(false); |
| 19 | const [key, setKey] = useState(""), [notice, setNotice] = useState(""), [query, setQuery] = useState(""); |
| 20 | const [sshOpen, setSSHOpen] = useState(false), [selected, setSelected] = useState(""), [username, setUsername] = useState(""); |
| 21 | const [port, setPort] = useState("80"), [appsOpen, setAppsOpen] = useState(false); |
| 22 | const mounted = useRef(true), inflight = useRef(false), loginRequested = useRef(false), revision = useRef(0); |
| 23 | const ready = status.ready && !status.job?.running; |
| 24 | const connected = ready && state?.backend === "Running"; |
| 25 | const address = state?.ips?.find(ip => !ip.includes(":")); |
| 26 | const peers = [...(state?.peers || [])].sort((a, b) => Number(b.online) - Number(a.online) || deviceName(a).localeCompare(deviceName(b))); |
| 27 | const visible = peers.filter(peer => [deviceName(peer), peer.name, peer.ip, peer.os].some(value => value?.toLowerCase().includes(query.trim().toLowerCase()))); |
| 28 | const openLogin = async url => { |
| 29 | loginRequested.current = false; |
| 30 | try { if (await onOpenBrowser(url) === false && mounted.current) setError("登录页面未能打开,请点击“打开登录页面”重试。"); } |
| 31 | catch { if (mounted.current) setError("登录页面未能打开,请点击“打开登录页面”重试。"); } |
| 32 | }; |
| 33 | const refresh = async () => { |
| 34 | if (inflight.current || !ready) return; |
| 35 | inflight.current = true; |
| 36 | const current = ++revision.current; |
| 37 | try { |
| 38 | const value = await api("/tailscale"); |
| 39 | if (mounted.current && current === revision.current) { |
| 40 | setState(value); setError(""); |
| 41 | if (loginRequested.current && value.auth_url) await openLogin(value.auth_url); |
| 42 | if (value.backend === "Running") loginRequested.current = false; |
| 43 | } |
| 44 | return value; |
| 45 | } catch (e) { if (mounted.current) setError(e.message); } |
| 46 | finally { inflight.current = false; } |
| 47 | }; |
| 48 | useEffect(() => { |
| 49 | mounted.current = true; |
| 50 | void refresh(); |
| 51 | const timer = setInterval(() => void refresh(), 3000); |
| 52 | return () => { mounted.current = false; revision.current++; clearInterval(timer); }; |
| 53 | }, [ready]); |
| 54 | const act = async (action, body = {}) => { |
| 55 | if (busy) return; |
| 56 | setBusy(true); setError(""); setNotice(""); revision.current++; |
| 57 | try { |
| 58 | inflight.current = true; |
| 59 | const value = await api(`/tailscale/${action}`, body); |
| 60 | if (mounted.current) { setState(value); if (action === "add-key") setKey(""); } |
| 61 | return value; |
| 62 | } catch (e) { if (mounted.current) setError(e.message); } |
| 63 | finally { inflight.current = false; if (mounted.current) setBusy(false); } |
| 64 | }; |
| 65 | const login = async () => { |
| 66 | loginRequested.current = true; |
| 67 | const result = await act(state?.enabled ? "login" : "connect"); |
| 68 | if (result?.auth_url) await openLogin(result.auth_url); |
| 69 | else if (!result || result.backend === "Running") loginRequested.current = false; |
| 70 | }; |
| 71 | const copy = async (text, message = "已复制") => { |
| 72 | try { await writeClipboard(text); setNotice(message); } |
| 73 | catch { setNotice("复制失败,请选中文本手动复制。"); } |
| 74 | }; |
| 75 | const stop = async logout => { |
| 76 | if (!await onConfirm({ title: logout ? "退出 Tailscale 登录?" : "断开 Tailscale?", body: logout ? "私网连接会中断,下次连接需要重新登录。已添加的 SSH 公钥保留。" : "正在使用的私网连接会中断,登录状态和 SSH 公钥保留。", confirm: logout ? "退出登录" : "断开连接" })) return; |
| 77 | loginRequested.current = false; |
| 78 | if (await act(logout ? "logout" : "disconnect")) { setSelected(""); setSSHOpen(false); } |
| 79 | }; |
| 80 | return <Modal title="Tailscale" onClose={onClose} wide> |
| 81 | <div className="modal-body tailscale-panel"> |
| 82 | <div className={`tailscale-overview${connected ? " connected" : ""}`}> |
| 83 | <div className="tailscale-state" role="status"> |
| 84 | <div className="tailscale-network-icon"><Network size={24} /></div> |
| 85 | <div><strong>{!ready ? "等待 Cage 环境就绪" : labels[state?.backend] || (state ? "连接状态待确认" : "正在读取状态…")}</strong> |
| 86 | <span>{connected ? (state.tailnet || "Tailscale 私网") : "你的设备,连接在一起"}</span> |
| 87 | </div> |
| 88 | <button className="icon-button" aria-label="刷新 Tailscale 状态" disabled={!ready || busy} onClick={refresh}><RefreshCw size={16} /></button> |
| 89 | </div> |
| 90 | <p className="field-help">{!ready ? "启动 Cage 环境后,即可登录并连接私网。" : connected ? "Cage 已加入私网。选择设备,查看地址并使用它的服务。" : descriptions[state?.backend] || "正在读取当前环境的 Tailscale 状态。"}</p> |
| 91 | {state?.account && connected && <p className="tailscale-account">登录账号 · {state.account}</p>} |
| 92 | {ready && state && <div className="tailscale-actions"> |
| 93 | {!state.installed ? <button className="primary-button" disabled={busy} onClick={async () => { |
| 94 | setBusy(true); setError(""); |
| 95 | try { await onInstall(); } catch (e) { setError(e.message); } |
| 96 | finally { if (mounted.current) setBusy(false); } |
| 97 | }}>{["macos", "windows-11"].includes(status.guest_system) ? "更新环境组件以安装" : "安装 Tailscale"}</button> : <> |
| 98 | {!connected && !["NeedsMachineAuth", "Starting"].includes(state.backend) && <button className="primary-button" disabled={busy} onClick={login}>{busy ? <LoaderCircle size={15} className="spin" /> : <ExternalLink size={15} />}{state.enabled ? "重新发起登录" : "连接并登录"}</button>} |
| 99 | {state.auth_url && !connected && <button className="quiet-button" disabled={busy} onClick={() => openLogin(state.auth_url)}>打开登录页面</button>} |
| 100 | {state.enabled && <button className="quiet-button" disabled={busy} onClick={() => stop(false)}>断开连接</button>} |
| 101 | {connected && <button className="quiet-button" disabled={busy} onClick={() => stop(true)}>退出登录</button>} |
| 102 | </>} |
| 103 | </div>} |
| 104 | </div> |
| 105 | {error && <p className="error-message" role="alert">{error}</p>} |
| 106 | {connected && state.health?.length > 0 && <div className="tailscale-health" role="status"><strong>网络需要留意</strong>{state.health.map((message, i) => <p key={i}>{message}</p>)}</div>} |
| 107 | {connected && <section className="tailscale-self" aria-label="当前 Cage 设备"> |
| 108 | <DeviceIcon os={state.os} /> |
| 109 | <div><strong>{state.hostname || state.name?.split(".")[0] || "Cage"} <span className="tailscale-badge">当前环境</span></strong><span className="tailscale-address">{address || "正在分配地址…"}{state.os ? ` · ${state.os}` : ""}</span></div> |
| 110 | {address && <button className="quiet-button" onClick={() => copy(address, "已复制 Cage 私网地址")}><Copy size={14} />复制地址</button>} |
| 111 | </section>} |
| 112 | <section className="tailscale-section tailscale-devices" aria-labelledby="tailscale-devices-title"> |
| 113 | <div className="tailscale-section-heading"><h3 id="tailscale-devices-title">网络中的设备{connected && <span>{peers.length}</span>}</h3>{connected && peers.length > 0 && <span className="field-help">{peers.filter(peer => peer.online).length} 台在线</span>}</div> |
| 114 | {connected && peers.length > 0 && <label className="tailscale-search"><Search size={16} /><input aria-label="搜索设备" type="search" placeholder="搜索设备名称或地址" value={query} onChange={e => setQuery(e.target.value)} /></label>} |
| 115 | {!connected ? <div className="tailscale-empty"><Monitor size={26} /><strong>连接后查看你的设备</strong><p>在浏览器完成登录,设备清单会自动出现。</p></div> |
| 116 | : peers.length === 0 ? <div className="tailscale-empty"><Monitor size={26} /><strong>私网已连接,还没有发现其他设备</strong><p>在电脑或手机安装 Tailscale,并加入同一网络。这里只显示此网络允许 Cage 发现的设备。</p></div> |
| 117 | : visible.length === 0 ? <p className="tailscale-empty">没有匹配的设备,请尝试其他名称或地址。</p> |
| 118 | : <div className="tailscale-peers">{visible.map(peer => <article className="tailscale-peer" key={peer.id || peer.ip}> |
| 119 | <div className="tailscale-peer-row"><DeviceIcon os={peer.os} /><div className="tailscale-peer-info"><strong>{deviceName(peer)}</strong><span>{peer.ip}{peer.os ? ` · ${peer.os}` : ""}</span></div> |
| 120 | <span className={`tailscale-presence${peer.online ? " online" : ""}`}><i />{peer.online ? "在线" : "离线"}</span> |
| 121 | <button className="icon-button" aria-label={`复制 ${deviceName(peer)} 的地址`} onClick={() => copy(peer.ip, "已复制设备私网地址")}><Copy size={15} /></button> |
| 122 | <button className="quiet-button" aria-expanded={selected === peer.ip} aria-controls={`tailnet-device-${peer.ip}`} onClick={() => { setSelected(selected === peer.ip ? "" : peer.ip); setUsername(""); setPort("80"); }}>使用设备<ChevronDown size={14} /></button> |
| 123 | </div> |
| 124 | {selected === peer.ip && <div className="tailscale-device-detail" id={`tailnet-device-${peer.ip}`}> |
| 125 | {peer.name && <p className="field-help tailscale-dns">{peer.name}</p>} |
| 126 | {!peer.online && <p className="field-help">设备当前离线,请先在该设备上连接 Tailscale。</p>} |
| 127 | <label className="field-label" htmlFor="tailnet-service-port">访问设备上的服务</label> |
| 128 | <p className="field-help">填写服务端口,为 Cage 内的浏览器、数据库客户端或其他应用创建本地连接地址。</p> |
| 129 | <div className="tailscale-connect-form"><input id="tailnet-service-port" aria-label="服务端口" type="number" min="1" max="65535" inputMode="numeric" value={port} onChange={e => setPort(e.target.value)} /><button className="quiet-button" disabled={busy || !Number.isInteger(Number(port)) || Number(port) < 1 || Number(port) > 65535} onClick={async () => { if (await act("open-service", { host: peer.ip, port: Number(port) })) setNotice("本地连接地址已就绪,请在 Cage 环境内使用。"); }}>创建连接地址</button></div> |
| 130 | {(state.forwards || []).filter(entry => entry.host === peer.ip).map(entry => <div className="tailscale-forward" key={`${entry.host}:${entry.port}`}><span><strong>端口 {entry.port}</strong><code>{entry.address}</code></span><button className="icon-button" aria-label={`复制端口 ${entry.port} 的连接地址`} onClick={() => copy(entry.address, "已复制 Cage 内可用的连接地址")}><Copy size={14} /></button><button className="quiet-button" onClick={async () => { try { if (await onOpenBrowser(`http://${entry.address}`) === false) setError("服务页面未能打开,请重试。"); } catch { setError("服务页面未能打开,请重试。"); } }}>打开 HTTP 页面</button></div>)} |
| 131 | <label className="field-label" htmlFor="tailnet-ssh-user"><Terminal size={14} />SSH 连接</label> |
| 132 | <p className="field-help">填写这台设备的系统用户名。目标设备需开启 SSH,并允许当前账号登录。</p> |
| 133 | <div className="tailscale-connect-form"><input id="tailnet-ssh-user" value={username} onChange={e => setUsername(e.target.value)} placeholder="设备的系统用户名" autoComplete="off" maxLength={128} /><button className="quiet-button" disabled={!username.trim() || !/^[a-zA-Z0-9_.@\\-]+$/.test(username.trim())} onClick={() => copy(`ssh -l '${username.trim()}' ${peer.ip}`, "已复制 SSH 命令,请在 Cage 终端运行")}>复制 SSH 命令</button></div> |
| 134 | <p className="field-help">SSH、SFTP 与使用 OpenSSH 的工具可直接使用以上私网地址。其他应用的接入方式见下方“应用访问私网”。</p> |
| 135 | </div>} |
| 136 | </article>)}</div>} |
| 137 | </section> |
| 138 | {connected && <section className="tailscale-section"> |
| 139 | <button className="tailscale-disclosure" aria-expanded={sshOpen} aria-controls="tailnet-inbound-ssh" onClick={() => setSSHOpen(!sshOpen)}><span><KeyRound size={16} />从其他设备 SSH 到 Cage</span><ChevronDown size={16} /></button> |
| 140 | {sshOpen && <div id="tailnet-inbound-ssh" className="tailscale-detail"> |
| 141 | <p className="field-help">仅在需要远程终端、SFTP 或编辑器连接时配置。将连接设备的 SSH 公钥添加到这里,私钥保留在原设备。</p> |
| 142 | {address && <div className="tailscale-command"><code>{`ssh ${state.user}@${address}`}</code><button className="icon-button" aria-label="复制连接 Cage 命令" onClick={() => copy(`ssh ${state.user}@${address}`)}><Copy size={15} /></button></div>} |
| 143 | <label className="field-label" htmlFor="tailscale-key">连接设备的 SSH 公钥</label> |
| 144 | <textarea id="tailscale-key" rows={3} placeholder="ssh-ed25519 AAAA…" value={key} onChange={e => setKey(e.target.value)} maxLength={8192} spellCheck={false} /> |
| 145 | <button className="quiet-button" disabled={busy || !key.trim()} onClick={() => act("add-key", { key: key.trim() })}>添加公钥</button> |
| 146 | <div className="tailscale-keys">{(state.keys || []).map(entry => <div className="tailscale-key" key={entry}><code title={entry}>{entry}</code><button className="icon-button" disabled={busy} aria-label="移除 SSH 公钥" onClick={() => act("remove-key", { key: entry })}><Trash2 size={14} /></button></div>)}</div> |
| 147 | </div>} |
| 148 | </section>} |
| 149 | {connected && state.proxy_url && <section className="tailscale-section"> |
| 150 | <button className="tailscale-disclosure" aria-expanded={appsOpen} aria-controls="tailnet-apps" onClick={() => setAppsOpen(!appsOpen)}><span><Network size={16} />应用访问私网</span><ChevronDown size={16} /></button> |
| 151 | {appsOpen && <div id="tailnet-apps" className="tailscale-detail"><p className="field-help">需要直接使用设备域名、HTTPS 或多个端口时,在 Cage 内的应用中配置 HTTP 代理。仅允许访问此私网中可见的设备,公网继续使用原有出口。</p><button className="quiet-button" onClick={() => copy(state.proxy_url, "已复制私网 HTTP 代理地址(含本地访问凭据)")}>复制私网代理地址</button><p className="field-help">代理地址含本地访问凭据,请仅用于 Cage 内的应用。此接入方式支持 TCP 服务;UDP、系统 VPN 和子网路由未启用。</p></div>} |
| 152 | </section>} |
| 153 | <p className="field-help tailscale-footer">其他设备需安装 Tailscale 并加入同一网络。设备访问受网络权限控制;暂停 Cage 联网或停止环境会中断私网连接。</p> |
| 154 | {notice && <p className="tailscale-notice" role="status"><Check size={14} />{notice}</p>} |
| 155 | </div> |
| 156 | </Modal>; |
| 157 | } |