| 1 | import React, { useState } from "react"; |
| 2 | import { |
| 3 | ShieldCheck, |
| 4 | ShieldAlert, |
| 5 | Check, |
| 6 | CircleHelp, |
| 7 | X, |
| 8 | LoaderCircle, |
| 9 | ArrowUpRight, |
| 10 | Copy, |
| 11 | Settings, |
| 12 | ChevronDown, |
| 13 | } from "lucide-react"; |
| 14 | import { api, writeClipboard } from "./api"; |
| 15 | import { Modal } from "./views"; |
| 16 | import { networkState, reportForSharing } from "./safety-state.mjs"; |
| 17 | |
| 18 | const diagnostics = [ |
| 19 | { |
| 20 | name: "IP 质量", |
| 21 | source: "ip.net.coffee", |
| 22 | url: "https://ip.net.coffee/claude/", |
| 23 | description: "查看 IP 属性、ASN、信誉评分与网络诊断。", |
| 24 | }, |
| 25 | { |
| 26 | name: "浏览器信息", |
| 27 | source: "fuck-claude.com", |
| 28 | url: "https://fuck-claude.com/", |
| 29 | description: "查看时区、语言、字体等浏览器信号的社区评估。", |
| 30 | }, |
| 31 | ]; |
| 32 | |
| 33 | export async function bindDetectedMacOSExit(ip, onConfirm) { |
| 34 | if (!(await onConfirm({ title: "绑定固定出口?", body: `客机终端、浏览器和代理均测得 ${ip}。保存后请重启客机并重新检测。`, confirm: "绑定固定出口" }))) return null; |
| 35 | return api("/macos/exit-policy", { ip }); |
| 36 | } |
| 37 | |
| 38 | export function SafetyPanel({ |
| 39 | status, |
| 40 | onClose, |
| 41 | onSettings, |
| 42 | onOpenPage, |
| 43 | onConfirm, |
| 44 | }) { |
| 45 | const [error, setError] = useState(""), |
| 46 | [notice, setNotice] = useState(""); |
| 47 | const network = networkState(status), |
| 48 | safety = status.safety || {}, |
| 49 | report = safety.report; |
| 50 | const busy = Boolean(status.job?.running), |
| 51 | online = network.mode === "open"; |
| 52 | const passed = |
| 53 | report?.checks?.filter((c) => c.critical && c.state === "passed").length || |
| 54 | 0; |
| 55 | const total = report?.checks?.filter((c) => c.critical).length || 0; |
| 56 | const run = async () => { |
| 57 | setError(""); |
| 58 | if ( |
| 59 | status.vm_running && |
| 60 | !safety.gate && |
| 61 | !(await onConfirm({ |
| 62 | title: "重启环境并加载检测组件?", |
| 63 | body: "当前终端会话将结束。请先保存文件,浏览器登录与项目文件会保留。", |
| 64 | confirm: "重启并检查", |
| 65 | })) |
| 66 | ) |
| 67 | return; |
| 68 | try { |
| 69 | await api(status.ready && safety.gate ? "/safety/check" : "/start", {}); |
| 70 | } catch (e) { |
| 71 | setError(e.message); |
| 72 | } |
| 73 | }; |
| 74 | const pause = async () => { |
| 75 | setError(""); |
| 76 | try { |
| 77 | await api("/safety/close", {}); |
| 78 | } catch (e) { |
| 79 | setError(e.message); |
| 80 | } |
| 81 | }; |
| 82 | const copy = async () => { |
| 83 | try { |
| 84 | await writeClipboard(JSON.stringify(reportForSharing(report), null, 2)); |
| 85 | setNotice("已复制报告,不含代理凭据、IP 地址和本机路径"); |
| 86 | } catch { |
| 87 | setError("无法写入本机剪贴板,请检查浏览器的剪贴板权限。"); |
| 88 | } |
| 89 | }; |
| 90 | const open = async (url) => { |
| 91 | if (await onOpenPage(url)) onClose(); |
| 92 | }; |
| 93 | return ( |
| 94 | <Modal |
| 95 | title="网络与隔离检查" |
| 96 | wide |
| 97 | onClose={onClose} |
| 98 | footer={ |
| 99 | <> |
| 100 | <span className="save-note"> |
| 101 | {busy ? status.job.message : "外网关闭时,离线终端和文件仍可使用"} |
| 102 | </span> |
| 103 | {(online || network.mode === "checking") && ( |
| 104 | <button onClick={pause}> |
| 105 | {network.mode === "checking" ? "取消检测并关闭外网" : "暂停外网"} |
| 106 | </button> |
| 107 | )} |
| 108 | <button className="primary" disabled={busy} onClick={run}> |
| 109 | {busy ? ( |
| 110 | <LoaderCircle size={15} className="spin" /> |
| 111 | ) : ( |
| 112 | <ShieldCheck size={15} /> |
| 113 | )} |
| 114 | {busy |
| 115 | ? "检查进行中" |
| 116 | : online |
| 117 | ? "暂停外网并重检" |
| 118 | : status.ready && safety.gate |
| 119 | ? "重新检测" |
| 120 | : status.vm_running |
| 121 | ? "重启并检查" |
| 122 | : "启动并检查"} |
| 123 | </button> |
| 124 | </> |
| 125 | } |
| 126 | > |
| 127 | <div className="modal-body safety-body"> |
| 128 | {safety.macos_exit_candidate?.ip && <button disabled={busy} onClick={async () => { |
| 129 | try { if (await bindDetectedMacOSExit(safety.macos_exit_candidate.ip, onConfirm)) setNotice("固定出口已提交,请在保存完成后重启客机并重新检测。"); } |
| 130 | catch (e) { setError(e.message); } |
| 131 | }}>绑定检测到的固定出口 {safety.macos_exit_candidate.ip}</button>} |
| 132 | <div className={`safety-summary ${network.mode}`}> |
| 133 | {online ? ( |
| 134 | <ShieldCheck size={27} /> |
| 135 | ) : network.mode === "checking" ? ( |
| 136 | <LoaderCircle size={27} className="spin" /> |
| 137 | ) : ( |
| 138 | <ShieldAlert size={27} /> |
| 139 | )} |
| 140 | <div> |
| 141 | <h3>{network.label}</h3> |
| 142 | <p> |
| 143 | {busy |
| 144 | ? status.job.message |
| 145 | : online |
| 146 | ? status.network_error || "关键检查已通过,后台继续核对出口规则与环境配置。" |
| 147 | : status.network_error || safety.error || |
| 148 | safety.gate?.reason || |
| 149 | "完成关键检查后,才允许连接外部网站。"} |
| 150 | </p> |
| 151 | </div> |
| 152 | <button className="text-button" onClick={() => onSettings("network")}> |
| 153 | <Settings size={15} /> |
| 154 | 出口规则 |
| 155 | </button> |
| 156 | </div> |
| 157 | {(error || notice) && ( |
| 158 | <div |
| 159 | role="status" |
| 160 | className={error ? "error-message" : "inline-info"} |
| 161 | > |
| 162 | {error || notice} |
| 163 | </div> |
| 164 | )} |
| 165 | <div className="safety-section-heading"> |
| 166 | <h4>关键检查</h4> |
| 167 | <span>{total ? `${passed} / ${total} 通过` : "尚无检测记录"}</span> |
| 168 | {report && ( |
| 169 | <button className="text-button" onClick={copy}> |
| 170 | <Copy size={14} /> |
| 171 | 复制报告 |
| 172 | </button> |
| 173 | )} |
| 174 | </div> |
| 175 | {report ? ( |
| 176 | <> |
| 177 | <p className="safety-timestamp"> |
| 178 | 检测于 {new Date(report.created_at).toLocaleString("zh-CN")} ·{" "} |
| 179 | {online ? "当前运行实例" : "报告不代表当前已放行"} |
| 180 | </p> |
| 181 | <div className="safety-checks"> |
| 182 | {report.checks |
| 183 | .filter((check) => check.critical) |
| 184 | .map((check) => { |
| 185 | const Icon = |
| 186 | check.state === "passed" |
| 187 | ? Check |
| 188 | : check.state === "failed" |
| 189 | ? X |
| 190 | : CircleHelp; |
| 191 | return ( |
| 192 | <details |
| 193 | className={`safety-check ${check.state}`} |
| 194 | key={check.id} |
| 195 | > |
| 196 | <summary> |
| 197 | <Icon size={16} /> |
| 198 | <strong>{check.title}</strong> |
| 199 | <span> |
| 200 | {check.state === "passed" |
| 201 | ? "通过" |
| 202 | : check.state === "failed" |
| 203 | ? "未通过" |
| 204 | : busy && check.checked_at === report.created_at |
| 205 | ? "待检查" |
| 206 | : "未知"} |
| 207 | </span> |
| 208 | <ChevronDown size={14} /> |
| 209 | </summary> |
| 210 | <div> |
| 211 | <p>{check.evidence}</p> |
| 212 | {check.state !== "passed" && check.remedy && ( |
| 213 | <p className="check-remedy">{check.remedy}</p> |
| 214 | )} |
| 215 | </div> |
| 216 | </details> |
| 217 | ); |
| 218 | })} |
| 219 | </div> |
| 220 | </> |
| 221 | ) : ( |
| 222 | <p className="safety-empty"> |
| 223 | 检查涵盖出站规则、代理故障关闭、DNS、共享目录、宿主管理接口,以及系统和浏览器的实际配置。关键项失败或未知时保持外网关闭。 |
| 224 | </p> |
| 225 | )} |
| 226 | {report?.checks?.some((check) => !check.critical) && ( |
| 227 | <div className="safety-advisories"> |
| 228 | {report.checks |
| 229 | .filter((check) => !check.critical) |
| 230 | .map((check) => ( |
| 231 | <p key={check.id}> |
| 232 | <CircleHelp size={15} /> |
| 233 | <span> |
| 234 | <strong>{check.title}:</strong> |
| 235 | {check.evidence} |
| 236 | </span> |
| 237 | </p> |
| 238 | ))} |
| 239 | </div> |
| 240 | )} |
| 241 | <div className="safety-section-heading"> |
| 242 | <h4>社区补充检查</h4> |
| 243 | <span>提示项,不参与联网准入</span> |
| 244 | </div> |
| 245 | <div className="diagnostic-sites"> |
| 246 | {diagnostics.map((site) => ( |
| 247 | <div key={site.url}> |
| 248 | <strong>{site.name}</strong> |
| 249 | <small>{site.source}</small> |
| 250 | <p>{site.description}</p> |
| 251 | <button disabled={!online || busy} onClick={() => open(site.url)}> |
| 252 | 在 Cage 中打开 |
| 253 | <ArrowUpRight size={14} /> |
| 254 | </button> |
| 255 | </div> |
| 256 | ))} |
| 257 | </div> |
| 258 | <p className="field-help"> |
| 259 | 点击后才访问第三方站点。站点会收到 Cage 的出口 |
| 260 | IP,并可读取该页面可访问的浏览器信息。评分属于社区参考。 |
| 261 | </p> |
| 262 | <details className="safety-scope"> |
| 263 | <summary>检查范围与来源</summary> |
| 264 | <p> |
| 265 | 当前版本验证 IPv4 出口,客机公网 IPv6 与 UDP 关闭。出口监测约每 30 |
| 266 | 秒执行;位置来自 IP |
| 267 | 数据库,城市判断具有误差。代理自身按目标分流的行为仍需在代理侧约束。 |
| 268 | </p> |
| 269 | <p> |
| 270 | 报告包含运行时策略和采样证据,不能代替完整虚拟化审计,也不代表平台账号资格或封禁预测。全面检查有效期为 |
| 271 | 24 小时;控制服务失联后,联网授权最多保留 90 秒。 |
| 272 | </p> |
| 273 | <div className="safety-sources"> |
| 274 | <span>方法参考</span> |
| 275 | <button |
| 276 | className="text-button" |
| 277 | disabled={!online} |
| 278 | onClick={() => |
| 279 | open("https://github.com/YOURSJEYO/claude-switcher") |
| 280 | } |
| 281 | > |
| 282 | claude-switcher |
| 283 | <ArrowUpRight size={12} /> |
| 284 | </button> |
| 285 | <button |
| 286 | className="text-button" |
| 287 | disabled={!online} |
| 288 | onClick={() => |
| 289 | open("https://github.com/DUNHKpcc/Claude-Guardian") |
| 290 | } |
| 291 | > |
| 292 | Claude-Guardian |
| 293 | <ArrowUpRight size={12} /> |
| 294 | </button> |
| 295 | </div> |
| 296 | </details> |
| 297 | </div> |
| 298 | </Modal> |
| 299 | ); |
| 300 | } |