| 1 | import React, { |
| 2 | createContext, |
| 3 | useCallback, |
| 4 | useContext, |
| 5 | useEffect, |
| 6 | useRef, |
| 7 | useState, |
| 8 | } from "react"; |
| 9 | import { |
| 10 | ArrowLeft, |
| 11 | Check, |
| 12 | Computer, |
| 13 | LoaderCircle, |
| 14 | Plus, |
| 15 | Server, |
| 16 | ShieldCheck, |
| 17 | } from "lucide-react"; |
| 18 | import { api, native, nativeAvailable } from "./api"; |
| 19 | import { setClientContext } from "./client-context.mjs"; |
| 20 | import { Modal } from "./modal"; |
| 21 | import { HostForm } from "./remote-host-form"; |
| 22 | import logo from "./logo.png"; |
| 23 | import "./remote-hosts.css"; |
| 24 | |
| 25 | const Navigation = createContext(null); |
| 26 | export const useSSHNavigation = () => useContext(Navigation); |
| 27 | const identity = (context) => |
| 28 | `${context.mode}:${context.connection_id || "disconnected"}`; |
| 29 | const occupied = (status) => |
| 30 | Boolean(status?.running || status?.busy || status?.live_terminals); |
| 31 | const unknown = () => ({ known: false, message: "本机状态待确认" }); |
| 32 | const recentKey = "cage-recent-environment"; |
| 33 | function recent() { |
| 34 | try { |
| 35 | return localStorage.getItem(recentKey) || ""; |
| 36 | } catch { |
| 37 | return ""; |
| 38 | } |
| 39 | } |
| 40 | function remember(value) { |
| 41 | try { |
| 42 | localStorage.setItem(recentKey, value); |
| 43 | } catch { |
| 44 | |
| 45 | } |
| 46 | } |
| 47 | function localSummary(status) { |
| 48 | if (!status?.known) return "本机状态待确认"; |
| 49 | if (status.busy) return "本机正在处理环境操作"; |
| 50 | if (status.live_terminals) |
| 51 | return `本机有 ${status.live_terminals} 个活动终端`; |
| 52 | if (status.running) return "本机环境正在运行"; |
| 53 | return status.service_running ? "本机控制服务已启动" : "本机环境已停止"; |
| 54 | } |
| 55 | |
| 56 | export function ClientRoot({ children }) { |
| 57 | const [context, setContext] = useState(null); |
| 58 | const [error, setError] = useState(""); |
| 59 | const currentIdentity = useRef(null); |
| 60 | const mounted = useRef(false); |
| 61 | const serial = useRef(0); |
| 62 | const refresh = useCallback(async () => { |
| 63 | const query = ++serial.current; |
| 64 | const value = await api("/client/context"); |
| 65 | if (!mounted.current || query !== serial.current) return value; |
| 66 | if (!["none", "local", "remote"].includes(value.mode)) |
| 67 | throw new Error("客户端未返回有效的工作环境,请更新 Cage。"); |
| 68 | const next = identity(value); |
| 69 | if (currentIdentity.current && currentIdentity.current !== next) { |
| 70 | |
| 71 | location.reload(); |
| 72 | return value; |
| 73 | } |
| 74 | currentIdentity.current = next; |
| 75 | setClientContext(value); |
| 76 | setContext(value); |
| 77 | setError(""); |
| 78 | return value; |
| 79 | }, []); |
| 80 | useEffect(() => { |
| 81 | mounted.current = true; |
| 82 | let timer; |
| 83 | async function poll() { |
| 84 | try { |
| 85 | await refresh(); |
| 86 | } catch (e) { |
| 87 | if (mounted.current) setError(e.message); |
| 88 | } |
| 89 | if (mounted.current) timer = setTimeout(poll, 2000); |
| 90 | } |
| 91 | (async () => { |
| 92 | if (location.hash) { |
| 93 | await api("/session", { token: location.hash.slice(1) }); |
| 94 | history.replaceState(null, "", location.pathname); |
| 95 | } |
| 96 | window.dispatchEvent(new Event("cage-session-ready")); |
| 97 | await poll(); |
| 98 | })().catch((e) => setError(e.message)); |
| 99 | return () => { |
| 100 | mounted.current = false; |
| 101 | clearTimeout(timer); |
| 102 | }; |
| 103 | }, [refresh]); |
| 104 | return ( |
| 105 | <div className="client-shell"> |
| 106 | {error && ( |
| 107 | <div className="client-error error-message" role="alert"> |
| 108 | {error} |
| 109 | <button onClick={() => location.reload()}>重新连接</button> |
| 110 | </div> |
| 111 | )} |
| 112 | {context ? ( |
| 113 | <Environment context={context} refresh={refresh}> |
| 114 | {children} |
| 115 | </Environment> |
| 116 | ) : ( |
| 117 | !error && ( |
| 118 | <div className="client-loading"> |
| 119 | <LoaderCircle className="spin" size={18} /> |
| 120 | 正在连接 Cage… |
| 121 | </div> |
| 122 | ) |
| 123 | )} |
| 124 | </div> |
| 125 | ); |
| 126 | } |
| 127 | |
| 128 | function Environment({ context, refresh, children }) { |
| 129 | const gateway = Boolean(context.desktop_gateway); |
| 130 | const local = context.mode === "local" && !context.client_only; |
| 131 | const remote = context.mode === "remote"; |
| 132 | const active = context.connection?.state === "connected" && remote; |
| 133 | const localLost = |
| 134 | gateway && local && context.connection?.state !== "connected"; |
| 135 | const ready = (local && !localLost) || active; |
| 136 | const [expanded, setExpanded] = useState(false); |
| 137 | const [hosts, setHosts] = useState([]); |
| 138 | const [selected, setSelected] = useState(context.host_id || recent()); |
| 139 | const [form, setForm] = useState(null); |
| 140 | const [remove, setRemove] = useState(null); |
| 141 | const [stop, setStop] = useState(null); |
| 142 | const [decision, setDecision] = useState(null); |
| 143 | const [localLocked, setLocalLocked] = useState(false); |
| 144 | const [localStatus, setLocalStatus] = useState(null); |
| 145 | const [busy, setBusy] = useState(""); |
| 146 | const [error, setError] = useState(""); |
| 147 | const [password, setPassword] = useState(""); |
| 148 | const runningAction = useRef(false); |
| 149 | const serial = useRef(0); |
| 150 | const secretForTrust = useRef(null); |
| 151 | const shellPresent = useRef(true); |
| 152 | const acknowledged = useRef(false); |
| 153 | const awaitingTarget = |
| 154 | !ready && gateway && !context.client_only && context.mode === "none"; |
| 155 | const [stayInSSH, setStayInSSH] = useState(() => |
| 156 | sessionStorage.getItem("cage-disconnected-generation") === context.connection_id |
| 157 | ); |
| 158 | const defaultLocal = awaitingTarget && !stayInSSH && context.candidate?.mode !== "remote"; |
| 159 | const showHosts = expanded || (!ready && !defaultLocal); |
| 160 | const candidate = gateway ? context.candidate : null; |
| 161 | const verifying = candidate?.connection?.state === "connecting"; |
| 162 | const connection = |
| 163 | candidate?.connection || (!gateway ? context.connection : null); |
| 164 | const host = hosts.find((item) => item.id === selected); |
| 165 | const hostConnected = Boolean(active && host?.id === context.host_id); |
| 166 | const connectionHostID = connection?.host_id || candidate?.host_id; |
| 167 | const pinnedFailure = |
| 168 | connectionHostID === selected && |
| 169 | connection?.host_key && |
| 170 | ["host_key_unknown", "host_key_changed"].includes(connection.error_code); |
| 171 | const needsDeploy = connectionHostID === selected && connection?.error_code === "deployment_required"; |
| 172 | const phase = connection?.phase || ""; |
| 173 | const connectingLabel = phase.startsWith("cloud_upload_") |
| 174 | ? `正在上传部署包 ${phase.slice("cloud_upload_".length)}%` |
| 175 | : { cloud_install: "正在安装云端环境…", cloud_detect: "正在检测服务器…", cloud_start: "正在启动服务…" }[phase] || "正在验证…"; |
| 176 | const hostName = remote |
| 177 | ? context.name || "远程主机" |
| 178 | : local |
| 179 | ? "本机环境" |
| 180 | : "选择工作环境"; |
| 181 | const route = () => ({ connection_id: context.connection_id || "" }); |
| 182 | const load = useCallback(async () => { |
| 183 | const { hosts = [] } = await api("/client/hosts"); |
| 184 | setHosts(hosts); |
| 185 | setSelected((id) => |
| 186 | hosts.some((item) => item.id === id) ? id : hosts[0]?.id || "", |
| 187 | ); |
| 188 | }, []); |
| 189 | const statusSerial = useRef(0); |
| 190 | const readLocal = useCallback(async () => { |
| 191 | const id = ++statusSerial.current; |
| 192 | let result; |
| 193 | try { |
| 194 | result = await api("/client/local-status"); |
| 195 | } catch { |
| 196 | result = unknown(); |
| 197 | } |
| 198 | if (shellPresent.current && id === statusSerial.current) |
| 199 | setLocalStatus(result); |
| 200 | return result; |
| 201 | }, []); |
| 202 | useEffect(() => { |
| 203 | shellPresent.current = true; |
| 204 | load().catch((e) => setError(e.message)); |
| 205 | return () => { |
| 206 | shellPresent.current = false; |
| 207 | ++serial.current; |
| 208 | }; |
| 209 | }, [load]); |
| 210 | useEffect(() => { |
| 211 | if (!gateway || (local && !showHosts && !decision)) return; |
| 212 | let live = true, |
| 213 | timer; |
| 214 | async function poll() { |
| 215 | if (document.visibilityState !== "hidden") await readLocal(); |
| 216 | if (live) timer = setTimeout(poll, 4500); |
| 217 | } |
| 218 | poll(); |
| 219 | return () => { |
| 220 | live = false; |
| 221 | clearTimeout(timer); |
| 222 | }; |
| 223 | }, [gateway, local, showHosts, decision, readLocal]); |
| 224 | useEffect(() => { |
| 225 | if (!ready && !defaultLocal && !acknowledged.current && nativeAvailable()) { |
| 226 | native("workbenchReady") |
| 227 | .then((ok) => { |
| 228 | if (ok === true) acknowledged.current = true; |
| 229 | }) |
| 230 | .catch(() => {}); |
| 231 | } |
| 232 | }, [ready, defaultLocal]); |
| 233 | const act = async (name, operation) => { |
| 234 | if (runningAction.current) return; |
| 235 | runningAction.current = true; |
| 236 | setBusy(name); |
| 237 | setError(""); |
| 238 | setLocalLocked(false); |
| 239 | try { |
| 240 | await operation(); |
| 241 | } catch (e) { |
| 242 | setLocalLocked(e.status === 423); |
| 243 | setError( |
| 244 | e.status === 423 |
| 245 | ? "本机环境已锁定。请先返回本机解锁,再停止本机环境。" |
| 246 | : e.message, |
| 247 | ); |
| 248 | } finally { |
| 249 | runningAction.current = false; |
| 250 | setBusy(""); |
| 251 | } |
| 252 | }; |
| 253 | const closeSidebar = () => { |
| 254 | secretForTrust.current = null; |
| 255 | setExpanded(false); |
| 256 | setPassword(""); |
| 257 | setError(""); |
| 258 | }; |
| 259 | const openSidebar = () => { |
| 260 | setExpanded(true); |
| 261 | setError(""); |
| 262 | }; |
| 263 | const cancel = async (pending = candidate) => { |
| 264 | ++serial.current; |
| 265 | if (pending) { |
| 266 | try { |
| 267 | await api("/client/cancel", { ...route(), candidate_id: pending.id }); |
| 268 | } catch (e) { |
| 269 | |
| 270 | |
| 271 | if (e.status !== 409 || (await refresh()).candidate?.id === pending.id) |
| 272 | throw e; |
| 273 | } |
| 274 | } |
| 275 | secretForTrust.current = null; |
| 276 | setDecision(null); |
| 277 | setPassword(""); |
| 278 | await refresh(); |
| 279 | }; |
| 280 | async function commit(pending, action, status) { |
| 281 | try { |
| 282 | await api("/client/commit", { |
| 283 | ...route(), |
| 284 | candidate_id: pending.id, |
| 285 | local_action: action, |
| 286 | ...(action === "stop" |
| 287 | ? { local_controller_id: status?.controller_id } |
| 288 | : {}), |
| 289 | }); |
| 290 | } catch (e) { |
| 291 | if (action === "auto" && e.status === 409 && pending.mode === "remote") { |
| 292 | const result = await readLocal(); |
| 293 | if (occupied(result) || !result.known) { |
| 294 | setDecision({ pending, status: result }); |
| 295 | return; |
| 296 | } |
| 297 | } |
| 298 | if (action === "stop" && e.status !== 423) { |
| 299 | const actual = await readLocal(); |
| 300 | if (actual.known && !actual.service_running && !occupied(actual)) { |
| 301 | setDecision(null); |
| 302 | throw new Error( |
| 303 | "本机环境已停止。目标连接未完成,请重新连接 SSH 主机。", |
| 304 | ); |
| 305 | } |
| 306 | } |
| 307 | throw e; |
| 308 | } |
| 309 | remember( |
| 310 | pending.mode === "local" |
| 311 | ? "local" |
| 312 | : pending.connection?.host_id || pending.host_id || selected, |
| 313 | ); |
| 314 | setDecision(null); |
| 315 | setPassword(""); |
| 316 | await refresh(); |
| 317 | } |
| 318 | async function verified(pending) { |
| 319 | if (pending.mode === "remote") { |
| 320 | const status = await readLocal(); |
| 321 | if (!status.known || occupied(status)) { |
| 322 | setDecision({ pending, status }); |
| 323 | return; |
| 324 | } |
| 325 | } |
| 326 | await commit(pending, "auto"); |
| 327 | } |
| 328 | |
| 329 | |
| 330 | async function requestTarget( |
| 331 | mode, |
| 332 | item = null, |
| 333 | secret = "", |
| 334 | cancelled = false, |
| 335 | deploy = false, |
| 336 | ) { |
| 337 | setPassword(""); |
| 338 | if (!gateway) { |
| 339 | const old = context.connection; |
| 340 | if (mode === "local") |
| 341 | await api("/client/local", { connection_id: old?.connection_id || "" }); |
| 342 | else { |
| 343 | if (old?.connection_id) |
| 344 | await api("/client/disconnect", { connection_id: old.connection_id }); |
| 345 | await api("/client/connect", { host_id: item.id, passphrase: secret, deploy }); |
| 346 | } |
| 347 | await refresh(); |
| 348 | return; |
| 349 | } |
| 350 | if (candidate && !cancelled) await cancel(candidate); |
| 351 | secretForTrust.current = null; |
| 352 | const pending = await api("/client/candidate", { |
| 353 | ...route(), |
| 354 | mode, |
| 355 | ...(item ? { host_id: item.id, passphrase: secret, deploy } : {}), |
| 356 | }); |
| 357 | const request = ++serial.current; |
| 358 | |
| 359 | let progress = pending; |
| 360 | while (shellPresent.current && serial.current === request) { |
| 361 | if (progress.connection?.state === "connected") { |
| 362 | await verified(progress); |
| 363 | return; |
| 364 | } |
| 365 | if ( |
| 366 | progress.connection?.state === "failed" || |
| 367 | progress.connection?.state === "disconnected" |
| 368 | ) { |
| 369 | if (item && secret && (progress.connection?.host_key || progress.connection?.error_code === "deployment_required")) |
| 370 | secretForTrust.current = { id: item.id, secret }; |
| 371 | await refresh(); |
| 372 | return; |
| 373 | } |
| 374 | await new Promise((resolve) => setTimeout(resolve, 350)); |
| 375 | if (!shellPresent.current || request !== serial.current) return; |
| 376 | const value = await refresh(); |
| 377 | if ( |
| 378 | value.connection_id !== context.connection_id || |
| 379 | value.candidate?.id !== pending.id |
| 380 | ) |
| 381 | return; |
| 382 | progress = value.candidate; |
| 383 | } |
| 384 | } |
| 385 | const trust = async () => { |
| 386 | const saved = hosts.find((h) => h.id === connectionHostID); |
| 387 | if (!saved || !connection.host_key) |
| 388 | throw new Error("主机配置已变化,请重新连接。"); |
| 389 | const secret = |
| 390 | password || |
| 391 | (secretForTrust.current?.id === saved.id |
| 392 | ? secretForTrust.current.secret |
| 393 | : ""); |
| 394 | if (candidate) await cancel(candidate); |
| 395 | await api( |
| 396 | `/client/hosts/${saved.id}`, |
| 397 | { ...saved, host_key: connection.host_key }, |
| 398 | "PUT", |
| 399 | ); |
| 400 | await load(); |
| 401 | setSelected(saved.id); |
| 402 | await requestTarget("remote", saved, secret, true); |
| 403 | }; |
| 404 | const disconnect = async () => { |
| 405 | const next = await api( |
| 406 | "/client/disconnect", |
| 407 | gateway |
| 408 | ? route() |
| 409 | : { connection_id: context.connection?.connection_id || "" }, |
| 410 | ); |
| 411 | remember(context.host_id || selected); |
| 412 | setPassword(""); |
| 413 | setExpanded(true); |
| 414 | if (gateway) { |
| 415 | sessionStorage.setItem("cage-disconnected-generation", next.connection_id); |
| 416 | setStayInSSH(true); |
| 417 | } |
| 418 | await refresh(); |
| 419 | }; |
| 420 | const choose = (item) => { |
| 421 | secretForTrust.current = null; |
| 422 | setSelected(item.id); |
| 423 | setPassword(""); |
| 424 | setError(""); |
| 425 | openSidebar(); |
| 426 | }; |
| 427 | |
| 428 | |
| 429 | const attemptedDefault = useRef(false); |
| 430 | useEffect(() => { |
| 431 | if (!defaultLocal || attemptedDefault.current) return; |
| 432 | attemptedDefault.current = true; |
| 433 | void act("local", () => requestTarget("local")); |
| 434 | }, [defaultLocal]); |
| 435 | const localButton = (label) => ( |
| 436 | <button |
| 437 | disabled={ |
| 438 | Boolean(busy) || |
| 439 | (localStatus?.available === false && |
| 440 | localStatus?.known && |
| 441 | !localStatus.service_running) |
| 442 | } |
| 443 | onClick={() => act("local", () => requestTarget("local"))} |
| 444 | > |
| 445 | <Computer size={16} /> |
| 446 | {label} |
| 447 | </button> |
| 448 | ); |
| 449 | const modalOpen = Boolean(form || remove || stop || decision); |
| 450 | const decisionBusy = busy === "commit" || busy === "cancel"; |
| 451 | const cancelDecision = () => act("cancel", () => cancel(decision.pending)); |
| 452 | const unlockButton = localLocked && ( |
| 453 | <button |
| 454 | disabled={Boolean(busy)} |
| 455 | onClick={() => |
| 456 | act("unlock-local", async () => { |
| 457 | const pending = decision?.pending || candidate; |
| 458 | if (pending) await cancel(pending); |
| 459 | setStop(null); |
| 460 | setDecision(null); |
| 461 | if (local && !localLost) { |
| 462 | location.reload(); |
| 463 | return; |
| 464 | } |
| 465 | setExpanded(true); |
| 466 | await requestTarget("local", null, "", true); |
| 467 | }) |
| 468 | } |
| 469 | > |
| 470 | 返回本机解锁 |
| 471 | </button> |
| 472 | ); |
| 473 | const access = { expanded: showHosts, open: openSidebar }; |
| 474 | const showBackground = |
| 475 | gateway && |
| 476 | !local && |
| 477 | localStatus && |
| 478 | (!localStatus.known || |
| 479 | occupied(localStatus) || |
| 480 | localStatus.service_running); |
| 481 | return ( |
| 482 | <Navigation.Provider value={access}> |
| 483 | {!local && !defaultLocal && <header className="client-toolbar"> |
| 484 | {local ? ( |
| 485 | <Computer size={16} /> |
| 486 | ) : remote ? ( |
| 487 | <Server size={16} /> |
| 488 | ) : ( |
| 489 | <img className="brand-logo" src={logo} alt="" /> |
| 490 | )} |
| 491 | <strong>{ready ? hostName : "Cage"}</strong> |
| 492 | {ready && ( |
| 493 | <span className="client-toolbar-hint"> |
| 494 | {remote ? "SSH · 云端运行" : "当前工作环境"} |
| 495 | </span> |
| 496 | )} |
| 497 | {ready && !showHosts && ( |
| 498 | <button className="ssh-fallback" onClick={openSidebar}> |
| 499 | <Server size={15} /> |
| 500 | SSH 主机 |
| 501 | </button> |
| 502 | )} |
| 503 | </header>} |
| 504 | {showBackground && ( |
| 505 | <div className="local-background" role="status"> |
| 506 | <Computer size={16} /> |
| 507 | <div> |
| 508 | <strong> |
| 509 | {occupied(localStatus) |
| 510 | ? "本机也在运行" |
| 511 | : localSummary(localStatus)} |
| 512 | </strong> |
| 513 | {occupied(localStatus) && ( |
| 514 | <span> |
| 515 | {localStatus.live_terminals |
| 516 | ? `${localStatus.live_terminals} 个活动终端` |
| 517 | : localStatus.busy |
| 518 | ? "有操作正在进行" |
| 519 | : "正在占用本机资源"} |
| 520 | </span> |
| 521 | )} |
| 522 | </div> |
| 523 | {localStatus.known && localStatus.controller_id && ( |
| 524 | <button |
| 525 | disabled={Boolean(busy)} |
| 526 | onClick={() => { |
| 527 | setError(""); |
| 528 | setStop(localStatus); |
| 529 | }} |
| 530 | > |
| 531 | 停止本机环境 |
| 532 | </button> |
| 533 | )} |
| 534 | </div> |
| 535 | )} |
| 536 | <div |
| 537 | className="client-stage" |
| 538 | inert={modalOpen || undefined} |
| 539 | data-environment-dialog={modalOpen || undefined} |
| 540 | > |
| 541 | {showHosts && ( |
| 542 | <aside |
| 543 | className="workspace-sidebar ssh-sidebar" |
| 544 | aria-label="SSH 主机" |
| 545 | > |
| 546 | <div className="sidebar-top"> |
| 547 | <span className="sidebar-brand"> |
| 548 | <Server size={18} /> |
| 549 | SSH 主机 |
| 550 | </span> |
| 551 | {ready && ( |
| 552 | <button |
| 553 | className="icon-button" |
| 554 | disabled={Boolean(busy) || verifying} |
| 555 | onClick={closeSidebar} |
| 556 | aria-label="返回工作台侧栏" |
| 557 | > |
| 558 | <ArrowLeft size={16} /> |
| 559 | </button> |
| 560 | )} |
| 561 | </div> |
| 562 | <div className="ssh-sidebar-body"> |
| 563 | <div className="ssh-list-heading"> |
| 564 | <span>已保存的主机</span> |
| 565 | <button |
| 566 | className="icon-button" |
| 567 | aria-label="添加远程主机" |
| 568 | title="添加远程主机" |
| 569 | disabled={Boolean(busy)} |
| 570 | onClick={() => setForm({ host: null })} |
| 571 | > |
| 572 | <Plus size={16} /> |
| 573 | </button> |
| 574 | </div> |
| 575 | {hosts.map((item) => ( |
| 576 | <button |
| 577 | className={`ssh-host-row ${selected === item.id ? "selected" : ""}`} |
| 578 | key={item.id} |
| 579 | aria-pressed={selected === item.id} |
| 580 | disabled={Boolean(busy) || verifying} |
| 581 | onClick={() => choose(item)} |
| 582 | > |
| 583 | <Server size={15} /> |
| 584 | <span> |
| 585 | <strong>{item.name}</strong> |
| 586 | <small> |
| 587 | {item.user}@{item.host}:{item.port} |
| 588 | </small> |
| 589 | </span> |
| 590 | {context.host_id === item.id && active && ( |
| 591 | <Check size={15} aria-label="已连接" /> |
| 592 | )} |
| 593 | </button> |
| 594 | ))} |
| 595 | {!hosts.length && ( |
| 596 | <div className="ssh-empty"> |
| 597 | <p>还没有远程主机</p> |
| 598 | <button onClick={() => setForm({ host: null })}> |
| 599 | <Plus size={14} /> |
| 600 | 添加主机 |
| 601 | </button> |
| 602 | </div> |
| 603 | )} |
| 604 | {host && ( |
| 605 | <section className="ssh-host-actions" aria-label="所选主机操作"> |
| 606 | {host.auth.kind === "key_file" && !hostConnected && ( |
| 607 | <label> |
| 608 | 密钥口令(如果已设置) |
| 609 | <input |
| 610 | type="password" |
| 611 | autoComplete="off" |
| 612 | disabled={Boolean(busy)} |
| 613 | value={password} |
| 614 | onChange={(e) => setPassword(e.target.value)} |
| 615 | /> |
| 616 | </label> |
| 617 | )} |
| 618 | {hostConnected ? ( |
| 619 | <button |
| 620 | disabled={Boolean(busy)} |
| 621 | onClick={() => act("disconnect", disconnect)} |
| 622 | > |
| 623 | 断开连接 |
| 624 | </button> |
| 625 | ) : ( |
| 626 | !pinnedFailure && ( |
| 627 | <button |
| 628 | className="primary" |
| 629 | disabled={ |
| 630 | Boolean(busy) || verifying || Boolean(decision) |
| 631 | } |
| 632 | onClick={() => |
| 633 | act("verify", () => |
| 634 | requestTarget("remote", host, password || (needsDeploy && secretForTrust.current?.id === host.id ? secretForTrust.current.secret : ""), false, needsDeploy), |
| 635 | ) |
| 636 | } |
| 637 | > |
| 638 | {busy === "verify" || verifying |
| 639 | ? connectingLabel |
| 640 | : needsDeploy ? "部署并连接" : "连接此主机"} |
| 641 | </button> |
| 642 | ) |
| 643 | )} |
| 644 | <div className="remote-button-row"> |
| 645 | <button |
| 646 | disabled={Boolean(busy) || hostConnected || verifying} |
| 647 | onClick={() => |
| 648 | act("edit", async () => { |
| 649 | if (candidate?.connection.host_id === host.id) |
| 650 | await cancel(candidate); |
| 651 | setForm({ host }); |
| 652 | }) |
| 653 | } |
| 654 | > |
| 655 | 编辑 |
| 656 | </button> |
| 657 | <button |
| 658 | disabled={Boolean(busy) || hostConnected || verifying} |
| 659 | onClick={() => |
| 660 | act("remove-prompt", async () => { |
| 661 | if (candidate?.connection.host_id === host.id) |
| 662 | await cancel(candidate); |
| 663 | setRemove(host); |
| 664 | }) |
| 665 | } |
| 666 | > |
| 667 | 移除连接 |
| 668 | </button> |
| 669 | </div> |
| 670 | </section> |
| 671 | )} |
| 672 | {!context.client_only && (!local || localLost) && ( |
| 673 | <div className="ssh-local-action"> |
| 674 | {localButton("进入本机工作台")} |
| 675 | </div> |
| 676 | )} |
| 677 | {!context.client_only && local && ( |
| 678 | <p className="ssh-help">云端连接就绪后再切换工作台。</p> |
| 679 | )} |
| 680 | {error && !decision && !stop && !remove && ( |
| 681 | <p className="error-message" role="alert"> |
| 682 | {error} |
| 683 | </p> |
| 684 | )} |
| 685 | {connection?.state === "failed" && |
| 686 | (!connectionHostID || connectionHostID === selected) && ( |
| 687 | <div className="ssh-connection" role="alert"> |
| 688 | <strong> |
| 689 | {connection.message || "连接失败,请重试。"} |
| 690 | </strong> |
| 691 | {pinnedFailure && ( |
| 692 | <> |
| 693 | <p>请与服务器管理员核对“{host?.name}”的主机指纹。</p> |
| 694 | <code> |
| 695 | {connection.host_key.algorithm} |
| 696 | <br /> |
| 697 | {connection.host_key.fingerprint} |
| 698 | </code> |
| 699 | <button |
| 700 | disabled={Boolean(busy)} |
| 701 | className="primary" |
| 702 | onClick={() => act("trust", trust)} |
| 703 | > |
| 704 | <ShieldCheck size={15} /> |
| 705 | 我已核对指纹,信任此主机 |
| 706 | </button> |
| 707 | </> |
| 708 | )} |
| 709 | </div> |
| 710 | )} |
| 711 | {remote && !active && !connection?.message && ( |
| 712 | <p className="ssh-help" role="status"> |
| 713 | 连接已断开。请重新连接并核对云端任务。 |
| 714 | </p> |
| 715 | )} |
| 716 | {gateway && candidate && !decision && ( |
| 717 | <> |
| 718 | {candidate.connection?.state === "connected" && !busy && ( |
| 719 | <button |
| 720 | onClick={() => act("verify", () => verified(candidate))} |
| 721 | > |
| 722 | 继续连接 |
| 723 | </button> |
| 724 | )} |
| 725 | <button |
| 726 | disabled={decisionBusy} |
| 727 | onClick={() => cancel().catch((e) => setError(e.message))} |
| 728 | > |
| 729 | 取消此次连接 |
| 730 | </button> |
| 731 | </> |
| 732 | )} |
| 733 | </div> |
| 734 | </aside> |
| 735 | )} |
| 736 | {ready ? ( |
| 737 | <div className="client-workbench">{children}</div> |
| 738 | ) : defaultLocal && !error && candidate?.connection?.state !== "failed" && !expanded ? ( |
| 739 | <div className="client-loading" role="status">正在打开 Cage 工作台…</div> |
| 740 | ) : ( |
| 741 | <main className="environment-picker"> |
| 742 | <div> |
| 743 | <img className="environment-logo" src={logo} alt="" /> |
| 744 | <h1> |
| 745 | {localLost |
| 746 | ? "重新连接本机工作台" |
| 747 | : remote && !active |
| 748 | ? "重新连接 SSH 主机" |
| 749 | : defaultLocal ? "本机工作台连接未完成" : "连接 SSH 主机"} |
| 750 | </h1> |
| 751 | <p> |
| 752 | {context.client_only |
| 753 | ? "通过 SSH 连接服务器,首次连接可自动部署 Cage。项目、应用和网络由服务器管理。" |
| 754 | : "本机和 SSH 主机使用同一个工作台。项目、应用和网络由选中的环境管理。"} |
| 755 | </p> |
| 756 | {!context.client_only && |
| 757 | localStatus?.known && |
| 758 | localStatus.available === false && |
| 759 | !localStatus.service_running && ( |
| 760 | <p>本机运行组件不可用,可通过 SSH 连接服务器。</p> |
| 761 | )} |
| 762 | {!showHosts && error && ( |
| 763 | <p className="error-message" role="alert"> |
| 764 | {error} |
| 765 | </p> |
| 766 | )} |
| 767 | <div className="environment-actions"> |
| 768 | {!context.client_only && |
| 769 | (!showHosts || localLost) && |
| 770 | localButton("重新连接本机")} |
| 771 | {!showHosts && ( |
| 772 | <button disabled={Boolean(busy)} onClick={openSidebar}> |
| 773 | <Server size={16} /> |
| 774 | 连接 SSH 主机 |
| 775 | </button> |
| 776 | )} |
| 777 | </div> |
| 778 | {!showHosts && hosts.length > 0 && ( |
| 779 | <div className="environment-recent"> |
| 780 | <h2>已保存的主机</h2> |
| 781 | {hosts.map((item) => ( |
| 782 | <button key={item.id} onClick={() => choose(item)}> |
| 783 | <Server size={15} /> |
| 784 | {item.name} |
| 785 | {recent() === item.id && <small>上次使用</small>} |
| 786 | </button> |
| 787 | ))} |
| 788 | </div> |
| 789 | )} |
| 790 | {busy === "local" && <p role="status">正在连接本机工作台…</p>} |
| 791 | {!showHosts && candidate?.connection?.state === "failed" && ( |
| 792 | <p role="alert" className="error-message"> |
| 793 | {candidate.connection.message} |
| 794 | </p> |
| 795 | )} |
| 796 | </div> |
| 797 | </main> |
| 798 | )} |
| 799 | </div> |
| 800 | {form && ( |
| 801 | <HostForm |
| 802 | initial={form.host} |
| 803 | platform={context.client_platform} |
| 804 | onClose={() => setForm(null)} |
| 805 | onSaved={async (saved) => { |
| 806 | await load(); |
| 807 | setSelected(saved.id); |
| 808 | setPassword(""); |
| 809 | setForm(null); |
| 810 | }} |
| 811 | /> |
| 812 | )} |
| 813 | {remove && ( |
| 814 | <Modal |
| 815 | title="移除 SSH 主机" |
| 816 | onClose={busy ? undefined : () => setRemove(null)} |
| 817 | footer={ |
| 818 | <> |
| 819 | <button disabled={Boolean(busy)} onClick={() => setRemove(null)}> |
| 820 | 取消 |
| 821 | </button> |
| 822 | <button |
| 823 | className="primary" |
| 824 | disabled={Boolean(busy)} |
| 825 | onClick={() => |
| 826 | act("remove", async () => { |
| 827 | await api( |
| 828 | `/client/hosts/${remove.id}`, |
| 829 | undefined, |
| 830 | "DELETE", |
| 831 | ); |
| 832 | setRemove(null); |
| 833 | setPassword(""); |
| 834 | await load(); |
| 835 | }) |
| 836 | } |
| 837 | > |
| 838 | 确认移除 |
| 839 | </button> |
| 840 | </> |
| 841 | } |
| 842 | > |
| 843 | <div className="ssh-dialog-body"> |
| 844 | <p>移除“{remove.name}”的本机连接配置?云端文件和任务会保留。</p> |
| 845 | {error && <p role="alert">{error}</p>} |
| 846 | </div> |
| 847 | </Modal> |
| 848 | )} |
| 849 | {decision && ( |
| 850 | <Modal |
| 851 | title="切换到 SSH 主机" |
| 852 | onClose={decisionBusy ? undefined : cancelDecision} |
| 853 | footer={ |
| 854 | <> |
| 855 | <button disabled={decisionBusy} onClick={cancelDecision}> |
| 856 | 取消 |
| 857 | </button> |
| 858 | <button |
| 859 | disabled={decisionBusy} |
| 860 | onClick={() => |
| 861 | act("commit", () => |
| 862 | commit(decision.pending, "keep", decision.status), |
| 863 | ) |
| 864 | } |
| 865 | > |
| 866 | 保留本机运行 |
| 867 | </button> |
| 868 | <button |
| 869 | className="primary" |
| 870 | disabled={ |
| 871 | decisionBusy || |
| 872 | !decision.status.known || |
| 873 | !decision.status.controller_id |
| 874 | } |
| 875 | onClick={() => |
| 876 | act("commit", () => |
| 877 | commit(decision.pending, "stop", decision.status), |
| 878 | ) |
| 879 | } |
| 880 | > |
| 881 | 停止本机并连接 |
| 882 | </button> |
| 883 | </> |
| 884 | } |
| 885 | > |
| 886 | <div className="ssh-dialog-body"> |
| 887 | <p> |
| 888 | <Check size={16} /> 已验证{" "} |
| 889 | {hosts.find( |
| 890 | (h) => |
| 891 | h.id === |
| 892 | (decision.pending.connection?.host_id || |
| 893 | decision.pending.host_id), |
| 894 | )?.name || "目标主机"}{" "} |
| 895 | 的 SSH 与 Cage 服务。 |
| 896 | </p> |
| 897 | <p> |
| 898 | <strong>{localSummary(decision.status)}。</strong>{" "} |
| 899 | 停止本机会结束终端任务并释放资源,未保存的内容可能丢失。选择保留后可以在云端工作台稍后停止。 |
| 900 | </p> |
| 901 | {busy === "commit" && ( |
| 902 | <p role="status">正在确认环境切换,请稍候…</p> |
| 903 | )} |
| 904 | {error && ( |
| 905 | <p role="alert" className="error-message"> |
| 906 | {error} |
| 907 | </p> |
| 908 | )} |
| 909 | {unlockButton} |
| 910 | </div> |
| 911 | </Modal> |
| 912 | )} |
| 913 | {stop && ( |
| 914 | <Modal |
| 915 | title="停止本机环境?" |
| 916 | onClose={busy ? undefined : () => setStop(null)} |
| 917 | footer={ |
| 918 | <> |
| 919 | <button disabled={Boolean(busy)} onClick={() => setStop(null)}> |
| 920 | 取消 |
| 921 | </button> |
| 922 | <button |
| 923 | className="primary" |
| 924 | disabled={Boolean(busy)} |
| 925 | onClick={() => |
| 926 | act("stop", async () => { |
| 927 | await api("/client/local-stop", { |
| 928 | ...route(), |
| 929 | local_controller_id: stop.controller_id, |
| 930 | }); |
| 931 | await readLocal(); |
| 932 | setStop(null); |
| 933 | }) |
| 934 | } |
| 935 | > |
| 936 | 确认停止本机 |
| 937 | </button> |
| 938 | </> |
| 939 | } |
| 940 | > |
| 941 | <div className="ssh-dialog-body"> |
| 942 | <p> |
| 943 | {localSummary(stop)} |
| 944 | 。停止将结束本机终端任务并释放资源,未保存的内容可能丢失。云端任务继续运行。 |
| 945 | </p> |
| 946 | {busy === "stop" && <p role="status">正在停止本机环境…</p>} |
| 947 | {error && ( |
| 948 | <p role="alert" className="error-message"> |
| 949 | {error} |
| 950 | </p> |
| 951 | )} |
| 952 | {unlockButton} |
| 953 | </div> |
| 954 | </Modal> |
| 955 | )} |
| 956 | </Navigation.Provider> |
| 957 | ); |
| 958 | } |