| 1 | import { InlineSelect } from "./inline-select"; |
| 2 | import React, { useEffect, useRef, useState } from "react"; |
| 3 | import { |
| 4 | Download, |
| 5 | Plus, |
| 6 | Search, |
| 7 | RefreshCw, |
| 8 | Check, |
| 9 | Activity, |
| 10 | Server, |
| 11 | Trash2, |
| 12 | Pencil, |
| 13 | LoaderCircle, |
| 14 | Square, |
| 15 | CircleHelp, |
| 16 | ArrowRight, |
| 17 | X, |
| 18 | Route, |
| 19 | } from "lucide-react"; |
| 20 | import { api } from "./api"; |
| 21 | import { SafetyMessage } from "./safety-message"; |
| 22 | import { ProxyYAMLInput } from "./proxy-yaml-input"; |
| 23 | import "./proxy.css"; |
| 24 | |
| 25 | const bytes = (v) => { |
| 26 | const n = Math.max(0, Number(v) || 0); |
| 27 | if (n < 1024) return `${n.toFixed(0)} B`; |
| 28 | if (n < 1024 ** 2) return `${(n / 1024).toFixed(1)} KiB`; |
| 29 | if (n < 1024 ** 3) return `${(n / 1024 ** 2).toFixed(1)} MiB`; |
| 30 | return `${(n / 1024 ** 3).toFixed(2)} GiB`; |
| 31 | }; |
| 32 | function usageLabel(raw) { |
| 33 | const values = Object.fromEntries( |
| 34 | (raw || "").split(";").map((v) => v.trim().split("=")), |
| 35 | ); |
| 36 | const used = Number(values.upload || 0) + Number(values.download || 0); |
| 37 | return [ |
| 38 | values.total ? `${bytes(used)} / ${bytes(values.total)}` : "", |
| 39 | Number(values.expire) > 0 |
| 40 | ? `到期 ${new Date(Number(values.expire) * 1000).toLocaleDateString()}` |
| 41 | : "", |
| 42 | ] |
| 43 | .filter(Boolean) |
| 44 | .join(" · "); |
| 45 | } |
| 46 | const emptyForm = { |
| 47 | name: "", |
| 48 | url: "", |
| 49 | content: "", |
| 50 | interval_hours: 24, |
| 51 | host_download: false, |
| 52 | }; |
| 53 | export function ProxySettings({ status, onApplied, onConfirm, onDirty, initializing = false }) { |
| 54 | const [proxy, setProxy] = useState(null), |
| 55 | [tab, setTab] = useState("nodes"), |
| 56 | [search, setSearch] = useState(""), |
| 57 | [source, setSource] = useState(""), |
| 58 | [form, setForm] = useState(null), |
| 59 | [mode, setMode] = useState("url"), |
| 60 | [error, setError] = useState(""), |
| 61 | [pending, setPending] = useState(""), |
| 62 | [notice, setNotice] = useState(""), |
| 63 | [delays, setDelays] = useState({}), |
| 64 | [connections, setConnections] = useState({ connections: [] }), |
| 65 | [logs, setLogs] = useState([]), |
| 66 | [port, setPort] = useState("17896"), |
| 67 | [rates, setRates] = useState({ up: 0, down: 0 }); |
| 68 | const [manual, setManual] = useState({ |
| 69 | name: "", |
| 70 | type: "socks5", |
| 71 | server: "", |
| 72 | port: "1080", |
| 73 | username: "", |
| 74 | password: "", |
| 75 | }); |
| 76 | const [routeDraft, setRouteDraft] = useState(null); |
| 77 | const [hostRefresh, setHostRefresh] = useState(false); |
| 78 | const [helpOpen, setHelpOpen] = useState(false); |
| 79 | const [readingFile, setReadingFile] = useState(false); |
| 80 | const alive = useRef(true), |
| 81 | loading = useRef(false), |
| 82 | latest = useRef(null), |
| 83 | measuring = useRef(false), |
| 84 | portDirty = useRef(false); |
| 85 | const reload = async () => { |
| 86 | const value = await api("/proxy"); |
| 87 | if (alive.current) { |
| 88 | setProxy(value); |
| 89 | if (!portDirty.current) setPort(String(value.port)); |
| 90 | } |
| 91 | return value; |
| 92 | }; |
| 93 | useEffect(() => { |
| 94 | alive.current = true; |
| 95 | const tick = async () => { |
| 96 | if (loading.current) return; |
| 97 | loading.current = true; |
| 98 | try { |
| 99 | await reload(); |
| 100 | } catch (e) { |
| 101 | if (alive.current) setError(e.message); |
| 102 | } finally { |
| 103 | loading.current = false; |
| 104 | } |
| 105 | }; |
| 106 | tick(); |
| 107 | const timer = setInterval(tick, 4000); |
| 108 | return () => { |
| 109 | alive.current = false; |
| 110 | clearInterval(timer); |
| 111 | }; |
| 112 | }, []); |
| 113 | useEffect(() => { |
| 114 | if (tab !== "connections" && tab !== "diagnostics") return; |
| 115 | let cancelled = false; |
| 116 | const tick = async () => { |
| 117 | try { |
| 118 | if (tab === "diagnostics") { |
| 119 | const result = await api("/proxy/logs"); |
| 120 | if (!cancelled) setLogs(result.lines); |
| 121 | } else { |
| 122 | const result = await api("/proxy/connections"), |
| 123 | now = Date.now(); |
| 124 | if (cancelled) return; |
| 125 | if (latest.current) { |
| 126 | const seconds = (now - latest.current.time) / 1000; |
| 127 | if (seconds > 0) |
| 128 | setRates({ |
| 129 | up: Math.max( |
| 130 | 0, |
| 131 | (result.uploadTotal - latest.current.uploadTotal) / seconds, |
| 132 | ), |
| 133 | down: Math.max( |
| 134 | 0, |
| 135 | (result.downloadTotal - latest.current.downloadTotal) / |
| 136 | seconds, |
| 137 | ), |
| 138 | }); |
| 139 | } |
| 140 | latest.current = { ...result, time: now }; |
| 141 | setConnections(result); |
| 142 | } |
| 143 | } catch (e) { |
| 144 | if (!cancelled) setError(e.message); |
| 145 | } |
| 146 | }; |
| 147 | tick(); |
| 148 | const timer = setInterval(tick, 3000); |
| 149 | return () => { |
| 150 | cancelled = true; |
| 151 | clearInterval(timer); |
| 152 | latest.current = null; |
| 153 | }; |
| 154 | }, [tab]); |
| 155 | const busy = Boolean(pending) || Boolean(status.job?.running); |
| 156 | const mutate = async (action, input = {}) => { |
| 157 | setError(""); |
| 158 | setNotice(""); |
| 159 | setPending(action); |
| 160 | try { |
| 161 | const job = await api(`/proxy/${action}`, input); |
| 162 | let current; |
| 163 | do { |
| 164 | await new Promise((resolve) => setTimeout(resolve, 500)); |
| 165 | current = (await api("/status")).job; |
| 166 | if (current.id !== job.id) |
| 167 | throw new Error("操作已被其他任务替代,请查看当前状态"); |
| 168 | } while (current.running); |
| 169 | portDirty.current = false; |
| 170 | const updated = await reload(); |
| 171 | await onApplied(); |
| 172 | if (current.error) throw new Error(current.error); |
| 173 | if (alive.current) { |
| 174 | setForm(null); |
| 175 | if (action === "route" || action === "select") setRouteDraft(null); |
| 176 | const importedSource = |
| 177 | action === "import" |
| 178 | ? updated.sources.at(-1) |
| 179 | : action === "refresh" |
| 180 | ? updated.sources.find((source) => source.id === input.id) |
| 181 | : null; |
| 182 | setNotice( |
| 183 | importedSource |
| 184 | ? `已导入 ${importedSource.nodes.length} 个节点,跳过 ${importedSource.skipped?.length || 0} 个${importedSource.skipped?.length ? ";可在来源下查看跳过原因" : ""}` |
| 185 | : current.message || "已完成", |
| 186 | ); |
| 187 | setDelays({}); |
| 188 | } |
| 189 | } catch (e) { |
| 190 | if (alive.current) setError(e.message); |
| 191 | } finally { |
| 192 | if (alive.current) setPending(""); |
| 193 | } |
| 194 | }; |
| 195 | const applyRoute = async (nodeIDs) => { |
| 196 | if ( |
| 197 | !proxy.active && |
| 198 | status.vm_running && |
| 199 | !(await onConfirm({ |
| 200 | title: "启用内置代理并重启环境?", |
| 201 | body: "首次启用或修改代理端口需要重启,当前终端会结束。后续只切换节点会保留终端进程,但网络连接会重新建立。", |
| 202 | confirm: "启用并重启", |
| 203 | })) |
| 204 | ) |
| 205 | return; |
| 206 | mutate("route", { node_ids: nodeIDs, port: Number(port) }); |
| 207 | }; |
| 208 | const measure = async (ids) => { |
| 209 | if (measuring.current) return; |
| 210 | measuring.current = true; |
| 211 | setPending("delay"); |
| 212 | setError(""); |
| 213 | try { |
| 214 | |
| 215 | let index = 0; |
| 216 | await Promise.all( |
| 217 | Array.from({ length: Math.min(4, ids.length) }, async () => { |
| 218 | while (index < ids.length && alive.current) { |
| 219 | const id = ids[index++]; |
| 220 | try { |
| 221 | const result = await api("/proxy/delay", { node_id: id }); |
| 222 | if (alive.current) |
| 223 | setDelays((old) => ({ ...old, [id]: `${result.delay} ms` })); |
| 224 | } catch { |
| 225 | if (alive.current) |
| 226 | setDelays((old) => ({ ...old, [id]: "不可用 / 未载入" })); |
| 227 | } |
| 228 | } |
| 229 | }), |
| 230 | ); |
| 231 | } finally { |
| 232 | measuring.current = false; |
| 233 | if (alive.current) setPending(""); |
| 234 | } |
| 235 | }; |
| 236 | const submit = () => { |
| 237 | let value = { |
| 238 | id: form.id || "", |
| 239 | name: form.name, |
| 240 | url: form.url, |
| 241 | content: form.content, |
| 242 | interval_hours: form.interval_hours, |
| 243 | host_download: form.host_download, |
| 244 | }; |
| 245 | if (mode === "manual" && !form.id) { |
| 246 | const node = { ...manual, port: Number(manual.port) }; |
| 247 | value = { |
| 248 | ...form, |
| 249 | name: form.name || manual.name, |
| 250 | url: "", |
| 251 | content: JSON.stringify({ proxies: [node] }), |
| 252 | interval_hours: 0, |
| 253 | }; |
| 254 | } |
| 255 | mutate(form.id ? "edit" : "import", value); |
| 256 | }; |
| 257 | const savedRoute = proxy?.route?.length |
| 258 | ? proxy.route |
| 259 | : proxy?.selected |
| 260 | ? [proxy.selected] |
| 261 | : [""]; |
| 262 | const route = routeDraft ?? savedRoute; |
| 263 | const routeChanged = JSON.stringify(route) !== JSON.stringify(savedRoute); |
| 264 | useEffect(() => { |
| 265 | onDirty?.(routeChanged || initializing && Boolean(form)); |
| 266 | return () => onDirty?.(false); |
| 267 | }, [onDirty, routeChanged, initializing, Boolean(form)]); |
| 268 | if (!proxy) |
| 269 | return <div className="proxy-loading" role={error ? "alert" : "status"}>{error || "正在读取代理配置…"}</div>; |
| 270 | const allNodes = proxy.sources.flatMap((s) => |
| 271 | s.nodes.map((n) => ({ ...n, source: s.name, sourceID: s.id })), |
| 272 | ); |
| 273 | const selected = allNodes.find((n) => n.id === proxy.selected); |
| 274 | const routeValid = |
| 275 | route.every((id) => allNodes.some((n) => n.id === id)) && |
| 276 | new Set(route).size === route.length; |
| 277 | const routeName = (id) => allNodes.find((n) => n.id === id)?.name || "待选择"; |
| 278 | const updateHop = (index, id) => |
| 279 | setRouteDraft(route.map((current, i) => (i === index ? id : current))); |
| 280 | const nodes = allNodes.filter( |
| 281 | (n) => |
| 282 | (!source || n.sourceID === source) && |
| 283 | `${n.name} ${n.source} ${n.type}` |
| 284 | .toLowerCase() |
| 285 | .includes(search.toLowerCase()), |
| 286 | ); |
| 287 | return ( |
| 288 | <div className="proxy-manager"> |
| 289 | <div className="section-title"> |
| 290 | <div className="proxy-heading"> |
| 291 | <h3>内置代理</h3> |
| 292 | <button |
| 293 | type="button" |
| 294 | className="icon-button proxy-help-toggle" |
| 295 | aria-label="代理使用说明" |
| 296 | title={helpOpen ? "收起代理使用说明" : "查看代理使用说明"} |
| 297 | aria-expanded={helpOpen} |
| 298 | aria-controls="proxy-help" |
| 299 | onClick={() => setHelpOpen((open) => !open)} |
| 300 | > |
| 301 | <CircleHelp size={17} aria-hidden="true" /> |
| 302 | </button> |
| 303 | </div> |
| 304 | <p>选择最终出口,按需添加中转,供 Cage 内所有程序共用。</p> |
| 305 | </div> |
| 306 | {helpOpen && ( |
| 307 | <section |
| 308 | id="proxy-help" |
| 309 | className="proxy-help" |
| 310 | aria-labelledby="proxy-help-title" |
| 311 | > |
| 312 | <h4 id="proxy-help-title">代理使用说明</h4> |
| 313 | <p className="proxy-help-intro"> |
| 314 | Cage 自带代理核心,无需另外安装 |
| 315 | Clash。请先准备你已有的代理订阅或节点信息,Cage 不提供代理节点。 |
| 316 | </p> |
| 317 | <ol className="proxy-help-steps"> |
| 318 | <li> |
| 319 | <strong>导入你的代理</strong> |
| 320 | <p>打开「订阅」→「添加来源」,填写来源名称,再选择一种方式:</p> |
| 321 | <ul> |
| 322 | <li> |
| 323 | <strong>订阅链接:</strong>从代理服务商处复制 Clash/Mihomo |
| 324 | 格式的 HTTPS |
| 325 | 订阅地址,粘贴后点击「导入并验证」。首次还没有可用节点时,勾选「通过主机网络下载这份订阅」;主机需要能访问该地址。 |
| 326 | </li> |
| 327 | <li> |
| 328 | <strong>本地 YAML:</strong>拖入或选择配置文件,或粘贴含节点的 |
| 329 | YAML 配置,再点击「导入并验证」。可从已有代理客户端导出配置。 |
| 330 | </li> |
| 331 | <li> |
| 332 | <strong>手动节点:</strong>适用于 HTTP CONNECT 或 |
| 333 | SOCKS5。填写节点名称、服务器地址、端口,以及服务商提供的用户名和密码(如有),再点击「导入并验证」。 |
| 334 | </li> |
| 335 | </ul> |
| 336 | </li> |
| 337 | <li> |
| 338 | <strong>选择节点并启用</strong> |
| 339 | <p> |
| 340 | 导入完成后切到「节点」,选择最终出口,再点击「应用代理」。住宅代理无法从国内连接时,选择「链式代理」,在它前面添加可连接的中转节点;节点可以来自不同订阅。首次启用时若环境正在运行,需要重启;后续切换只重建网络连接。 |
| 341 | </p> |
| 342 | </li> |
| 343 | <li> |
| 344 | <strong>确认可以联网</strong> |
| 345 | <p> |
| 346 | {initializing ? "点击“开始安装”,等待客机安装和网络检查完成。以初始化界面的检查结果为准;" : "回到工作台启动环境,等待网络检查通过。以工作台的联网状态为准;"}「测速」只检测节点延迟。Cage |
| 347 | 内的浏览器、终端和开发工具会共用所选节点,无需分别配置代理。 |
| 348 | </p> |
| 349 | </li> |
| 350 | </ol> |
| 351 | <div className="proxy-help-notes"> |
| 352 | <strong>导入失败或无法联网?</strong> |
| 353 | <p> |
| 354 | 确认复制的是订阅地址,而非服务商登录页或管理页。暂不支持单条分享链接、Base64 |
| 355 | 节点列表,以及只引用远程节点列表的配置;请向服务商获取含节点的 |
| 356 | Clash/Mihomo YAML。 |
| 357 | </p> |
| 358 | <p> |
| 359 | 订阅下载失败时,可检查主机是否能访问订阅地址,或改用本地 |
| 360 | YAML。节点不可用时,换一个节点并查看「诊断」;更换节点后仍需通过出口 |
| 361 | IP 和地区检查。 |
| 362 | </p> |
| 363 | </div> |
| 364 | </section> |
| 365 | )} |
| 366 | <div className="proxy-summary"> |
| 367 | <Server size={22} /> |
| 368 | <div> |
| 369 | <strong> |
| 370 | {selected ? `最终出口 · ${selected.name}` : "尚未配置代理节点"} |
| 371 | </strong> |
| 372 | {savedRoute.length > 1 && ( |
| 373 | <span className="proxy-current-route"> |
| 374 | 当前链路:{savedRoute.map(routeName).join(" → ")} |
| 375 | </span> |
| 376 | )} |
| 377 | <span> |
| 378 | {proxy.active |
| 379 | ? proxy.running |
| 380 | ? "Cage 自带核心运行中" |
| 381 | : "核心未就绪" |
| 382 | : "请选择节点后应用,未配置时无法联网"}{" "} |
| 383 | · 独立端口 {proxy.port} |
| 384 | </span> |
| 385 | </div> |
| 386 | <span className={`proxy-badge ${proxy.network_open ? "online" : ""}`}> |
| 387 | {proxy.state_label} |
| 388 | </span> |
| 389 | </div> |
| 390 | {proxy.active && status.location && ( |
| 391 | <p className="field-help"> |
| 392 | 最近检测出口:{status.location.ip} · {status.location.country}{" "} |
| 393 | {status.location.city}。联网是否放行以工作台检查状态为准。 |
| 394 | </p> |
| 395 | )} |
| 396 | {!proxy.core_available && ( |
| 397 | <div role="alert" className="error-message"> |
| 398 | 安装包缺少随附代理核心,请重新构建或安装完整 Cage 应用。 |
| 399 | </div> |
| 400 | )} |
| 401 | <p className="field-help"> |
| 402 | 仅允许经节点转发的 IPv4 TCP 和 DNS;公网 UDP、IPv6、ICMP |
| 403 | 持续阻断。切换会关闭旧网络连接并重新检查出口。 |
| 404 | </p> |
| 405 | <div role="tablist" aria-label="代理管理" className="proxy-tabs"> |
| 406 | {[ |
| 407 | ["nodes", "节点"], |
| 408 | ["sources", "订阅"], |
| 409 | ["connections", "连接"], |
| 410 | ["diagnostics", "诊断"], |
| 411 | ].map(([id, label]) => ( |
| 412 | <button |
| 413 | key={id} |
| 414 | role="tab" |
| 415 | aria-selected={tab === id} |
| 416 | onClick={() => setTab(id)} |
| 417 | > |
| 418 | {label} |
| 419 | </button> |
| 420 | ))} |
| 421 | </div> |
| 422 | {tab === "nodes" && ( |
| 423 | <> |
| 424 | {allNodes.length > 0 && ( |
| 425 | <section |
| 426 | className="proxy-route" |
| 427 | aria-labelledby="proxy-route-title" |
| 428 | > |
| 429 | <div className="proxy-route-heading"> |
| 430 | <div> |
| 431 | <h4 id="proxy-route-title">配置连接方式</h4> |
| 432 | <p>直接使用一个出口,或经中转连接住宅代理。</p> |
| 433 | </div> |
| 434 | <div |
| 435 | className="proxy-route-mode" |
| 436 | role="group" |
| 437 | aria-label="连接方式" |
| 438 | > |
| 439 | <button |
| 440 | type="button" |
| 441 | aria-pressed={route.length === 1} |
| 442 | disabled={busy} |
| 443 | onClick={() => setRouteDraft([route.at(-1)])} |
| 444 | > |
| 445 | 单节点 |
| 446 | </button> |
| 447 | <button |
| 448 | type="button" |
| 449 | aria-pressed={route.length > 1} |
| 450 | disabled={busy} |
| 451 | onClick={() => { |
| 452 | if (route.length === 1) setRouteDraft(["", route[0]]); |
| 453 | }} |
| 454 | > |
| 455 | 链式代理 |
| 456 | </button> |
| 457 | </div> |
| 458 | </div> |
| 459 | <ol className="proxy-route-hops"> |
| 460 | {route.map((id, index) => { |
| 461 | const final = index === route.length - 1; |
| 462 | const label = final |
| 463 | ? "最终出口" |
| 464 | : `中转节点${route.length > 2 ? ` ${index + 1}` : ""}`; |
| 465 | return ( |
| 466 | <li |
| 467 | key={index} |
| 468 | className={`proxy-route-hop ${final ? "is-exit" : ""}`} |
| 469 | > |
| 470 | <span className="proxy-hop-number" aria-hidden="true"> |
| 471 | {index + 1} |
| 472 | </span> |
| 473 | <div className="proxy-hop-control"> |
| 474 | <label htmlFor={`proxy-hop-${index}`}>{label}</label> |
| 475 | <InlineSelect |
| 476 | id={`proxy-hop-${index}`} |
| 477 | aria-label={label} |
| 478 | value={id} |
| 479 | disabled={busy} |
| 480 | onValueChange={(next) => updateHop(index, next)} |
| 481 | > |
| 482 | <option value="" disabled> |
| 483 | 请选择{label} |
| 484 | </option> |
| 485 | {Boolean(id) && |
| 486 | !allNodes.some((n) => n.id === id) && ( |
| 487 | <option value={id} disabled> |
| 488 | 节点已被移除,请重新选择 |
| 489 | </option> |
| 490 | )} |
| 491 | {proxy.sources.map((src) => ( |
| 492 | <optgroup key={src.id} label={src.name}> |
| 493 | {src.nodes.map((n) => ( |
| 494 | <option |
| 495 | key={n.id} |
| 496 | value={n.id} |
| 497 | disabled={route.some( |
| 498 | (hop, i) => i !== index && hop === n.id, |
| 499 | )} |
| 500 | > |
| 501 | {n.name} |
| 502 | </option> |
| 503 | ))} |
| 504 | </optgroup> |
| 505 | ))} |
| 506 | </InlineSelect> |
| 507 | {final && <small>目标网站看到此节点的出口 IP</small>} |
| 508 | </div> |
| 509 | {!final && ( |
| 510 | <button |
| 511 | className="icon-button proxy-hop-remove" |
| 512 | type="button" |
| 513 | disabled={busy} |
| 514 | aria-label={`移除${label}`} |
| 515 | title={`移除${label}`} |
| 516 | onClick={() => |
| 517 | setRouteDraft(route.filter((_, i) => i !== index)) |
| 518 | } |
| 519 | > |
| 520 | <X size={15} /> |
| 521 | </button> |
| 522 | )} |
| 523 | </li> |
| 524 | ); |
| 525 | })} |
| 526 | </ol> |
| 527 | {route.length > 1 && route.length < allNodes.length && ( |
| 528 | <button |
| 529 | type="button" |
| 530 | className="proxy-route-add" |
| 531 | disabled={busy || route.length >= allNodes.length} |
| 532 | onClick={() => |
| 533 | setRouteDraft([...route.slice(0, -1), "", route.at(-1)]) |
| 534 | } |
| 535 | > |
| 536 | <Plus size={14} /> |
| 537 | 添加中转 |
| 538 | </button> |
| 539 | )} |
| 540 | <div className="proxy-route-preview" aria-label="待应用链路"> |
| 541 | <Route size={16} aria-hidden="true" /> |
| 542 | <span>Cage</span> |
| 543 | {route.map((id, index) => ( |
| 544 | <React.Fragment key={index}> |
| 545 | <ArrowRight size={13} aria-hidden="true" /> |
| 546 | <strong>{routeName(id)}</strong> |
| 547 | </React.Fragment> |
| 548 | ))} |
| 549 | <ArrowRight size={13} aria-hidden="true" /> |
| 550 | <span>目标网站</span> |
| 551 | </div> |
| 552 | <div className="proxy-route-actions"> |
| 553 | <span aria-live="polite"> |
| 554 | {routeChanged |
| 555 | ? "有未应用的代理修改" |
| 556 | : proxy.active |
| 557 | ? "当前配置已应用" |
| 558 | : "选择完成后应用代理"} |
| 559 | </span> |
| 560 | <div> |
| 561 | {routeDraft !== null && ( |
| 562 | <button |
| 563 | type="button" |
| 564 | disabled={busy} |
| 565 | onClick={() => setRouteDraft(null)} |
| 566 | > |
| 567 | 撤销修改 |
| 568 | </button> |
| 569 | )} |
| 570 | <button |
| 571 | type="button" |
| 572 | className={routeChanged || !proxy.active ? "primary" : ""} |
| 573 | disabled={busy || !proxy.core_available || !routeValid} |
| 574 | onClick={() => applyRoute(route)} |
| 575 | > |
| 576 | {pending === "route" ? ( |
| 577 | <LoaderCircle size={14} className="spin" /> |
| 578 | ) : !routeChanged && proxy.active ? ( |
| 579 | <RefreshCw size={14} /> |
| 580 | ) : ( |
| 581 | <Check size={14} /> |
| 582 | )} |
| 583 | {routeChanged || !proxy.active ? "应用代理" : "重新应用"} |
| 584 | </button> |
| 585 | </div> |
| 586 | </div> |
| 587 | {proxy.route_error && ( |
| 588 | <p role="alert" className="proxy-source-error"> |
| 589 | <SafetyMessage message={proxy.route_error} /> |
| 590 | </p> |
| 591 | )} |
| 592 | {error && ( |
| 593 | <div role="alert" className="error-message"> |
| 594 | <SafetyMessage message={error} /> |
| 595 | </div> |
| 596 | )} |
| 597 | </section> |
| 598 | )} |
| 599 | {allNodes.length > 0 && ( |
| 600 | <div className="proxy-node-heading"> |
| 601 | <h4>可用节点</h4> |
| 602 | <button |
| 603 | type="button" |
| 604 | disabled={busy} |
| 605 | onClick={() => { |
| 606 | setTab("sources"); |
| 607 | setForm({ ...emptyForm }); |
| 608 | setMode("url"); |
| 609 | }} |
| 610 | > |
| 611 | <Plus size={14} /> |
| 612 | 添加来源 |
| 613 | </button> |
| 614 | </div> |
| 615 | )} |
| 616 | <div className="proxy-toolbar"> |
| 617 | <label className="proxy-search"> |
| 618 | <Search size={15} /> |
| 619 | <input |
| 620 | aria-label="搜索节点" |
| 621 | value={search} |
| 622 | onChange={(e) => setSearch(e.target.value)} |
| 623 | placeholder="搜索节点、来源或协议" |
| 624 | /> |
| 625 | </label> |
| 626 | <InlineSelect |
| 627 | aria-label="节点来源" |
| 628 | value={source} |
| 629 | onValueChange={(next) => setSource(next)} |
| 630 | > |
| 631 | <option value="">全部来源</option> |
| 632 | {proxy.sources.map((s) => ( |
| 633 | <option key={s.id} value={s.id}> |
| 634 | {s.name} |
| 635 | </option> |
| 636 | ))} |
| 637 | </InlineSelect> |
| 638 | <button |
| 639 | disabled={busy || !proxy.running || !nodes.length} |
| 640 | title="检测 HTTP 往返延迟,通常不含建连开销,不代表网页首开耗时" |
| 641 | onClick={() => measure(nodes.map((n) => n.id))} |
| 642 | > |
| 643 | <Activity size={14} /> |
| 644 | 测速 |
| 645 | </button> |
| 646 | </div> |
| 647 | {!allNodes.length ? ( |
| 648 | <div className="proxy-empty"> |
| 649 | <Download size={26} /> |
| 650 | <strong>添加第一份节点来源</strong> |
| 651 | <p>支持 Clash/Mihomo YAML 订阅、本地配置或手动节点。</p> |
| 652 | <button |
| 653 | onClick={() => { |
| 654 | setTab("sources"); |
| 655 | setForm({ ...emptyForm }); |
| 656 | setMode("url"); |
| 657 | }} |
| 658 | > |
| 659 | <Plus size={15} /> |
| 660 | 添加来源 |
| 661 | </button> |
| 662 | </div> |
| 663 | ) : ( |
| 664 | <div className="proxy-node-list"> |
| 665 | {nodes.map((n) => ( |
| 666 | <div |
| 667 | className={`proxy-node ${n.id === route.at(-1) ? "selected" : ""}`} |
| 668 | key={n.id} |
| 669 | > |
| 670 | <button |
| 671 | className="proxy-node-choice" |
| 672 | disabled={ |
| 673 | busy || |
| 674 | !proxy.core_available || |
| 675 | route.slice(0, -1).includes(n.id) |
| 676 | } |
| 677 | title={ |
| 678 | route.slice(0, -1).includes(n.id) |
| 679 | ? "此节点已用作中转" |
| 680 | : "选为最终出口,点击应用代理后生效" |
| 681 | } |
| 682 | onClick={() => updateHop(route.length - 1, n.id)} |
| 683 | aria-pressed={n.id === route.at(-1)} |
| 684 | aria-label={`选择节点 ${n.name}`} |
| 685 | > |
| 686 | <span className="proxy-radio"> |
| 687 | {n.id === route.at(-1) && <Check size={12} />} |
| 688 | </span> |
| 689 | <span> |
| 690 | <strong>{n.name}</strong> |
| 691 | <small> |
| 692 | {n.source} · {n.type.toUpperCase()} |
| 693 | </small> |
| 694 | </span> |
| 695 | </button> |
| 696 | {savedRoute.includes(n.id) && ( |
| 697 | <span className="proxy-node-role"> |
| 698 | {n.id === proxy.selected ? "当前出口" : "当前中转"} |
| 699 | </span> |
| 700 | )} |
| 701 | <span className="proxy-delay"> |
| 702 | {delays[n.id] || "未测速"} |
| 703 | </span> |
| 704 | <button |
| 705 | aria-label={`测速 ${n.name}`} |
| 706 | title="检测此节点到 www.gstatic.com 的 HTTP 往返延迟,通常不含建连开销,不代表网页首开耗时" |
| 707 | disabled={busy || !proxy.running} |
| 708 | onClick={() => measure([n.id])} |
| 709 | > |
| 710 | <Activity size={14} /> |
| 711 | </button> |
| 712 | </div> |
| 713 | ))} |
| 714 | {!nodes.length && ( |
| 715 | <p className="field-help">没有符合搜索条件的节点。</p> |
| 716 | )} |
| 717 | </div> |
| 718 | )} |
| 719 | </> |
| 720 | )} |
| 721 | {tab === "sources" && ( |
| 722 | <> |
| 723 | <label className="check-line"> |
| 724 | <input |
| 725 | type="checkbox" |
| 726 | checked={hostRefresh} |
| 727 | onChange={(e) => setHostRefresh(e.target.checked)} |
| 728 | /> |
| 729 | 手动更新订阅时通过主机网络下载 |
| 730 | </label> |
| 731 | <p className="field-help"> |
| 732 | 自动更新始终使用当前配置的代理,不会切换到主机下载。 |
| 733 | </p> |
| 734 | <div className="proxy-toolbar"> |
| 735 | <span> |
| 736 | {proxy.sources.length} 个来源 · {allNodes.length} 个节点 |
| 737 | </span> |
| 738 | <button |
| 739 | disabled={busy} |
| 740 | onClick={() => { |
| 741 | setForm({ ...emptyForm }); |
| 742 | setMode("url"); |
| 743 | }} |
| 744 | > |
| 745 | <Plus size={14} /> |
| 746 | 添加来源 |
| 747 | </button> |
| 748 | </div> |
| 749 | {proxy.sources.map((s) => ( |
| 750 | <div className="proxy-source" key={s.id}> |
| 751 | <div> |
| 752 | <strong>{s.name}</strong> |
| 753 | <small> |
| 754 | {s.remote ? s.host : "本地导入"} · {s.nodes.length} 个节点 ·{" "} |
| 755 | {s.interval_hours && s.remote |
| 756 | ? `每 ${s.interval_hours} 小时更新` |
| 757 | : "手动更新"} |
| 758 | </small> |
| 759 | <small> |
| 760 | 更新于 {new Date(s.updated_at).toLocaleString()} |
| 761 | {usageLabel(s.usage) && ` · ${usageLabel(s.usage)}`} |
| 762 | </small> |
| 763 | {s.error && ( |
| 764 | <span className="proxy-source-error">{s.error}</span> |
| 765 | )} |
| 766 | {s.skipped?.length > 0 && ( |
| 767 | <details className="proxy-source-skipped"> |
| 768 | <summary> |
| 769 | 已导入 {s.nodes.length} 个节点,跳过 {s.skipped.length} 个 |
| 770 | · 查看原因 |
| 771 | </summary> |
| 772 | <ul> |
| 773 | {s.skipped.map((issue) => ( |
| 774 | <li key={issue.index}> |
| 775 | 第 {issue.index} 个节点 |
| 776 | {issue.name && `「${issue.name}」`} |
| 777 | {issue.type && `(${issue.type})`}:{issue.reason} |
| 778 | </li> |
| 779 | ))} |
| 780 | </ul> |
| 781 | </details> |
| 782 | )} |
| 783 | </div> |
| 784 | <div className="proxy-source-actions"> |
| 785 | {s.remote && ( |
| 786 | <button |
| 787 | disabled={busy} |
| 788 | aria-label={`更新 ${s.name}`} |
| 789 | onClick={() => |
| 790 | mutate("refresh", { |
| 791 | id: s.id, |
| 792 | host_download: hostRefresh, |
| 793 | }) |
| 794 | } |
| 795 | > |
| 796 | <RefreshCw size={14} /> |
| 797 | </button> |
| 798 | )} |
| 799 | <button |
| 800 | disabled={busy} |
| 801 | aria-label={`编辑 ${s.name}`} |
| 802 | onClick={() => { |
| 803 | setForm({ |
| 804 | ...emptyForm, |
| 805 | id: s.id, |
| 806 | name: s.name, |
| 807 | interval_hours: s.interval_hours, |
| 808 | remote: s.remote, |
| 809 | }); |
| 810 | setMode("url"); |
| 811 | }} |
| 812 | > |
| 813 | <Pencil size={14} /> |
| 814 | </button> |
| 815 | <button |
| 816 | disabled={busy} |
| 817 | aria-label={`删除 ${s.name}`} |
| 818 | onClick={async () => { |
| 819 | if ( |
| 820 | await onConfirm({ |
| 821 | title: `删除来源“${s.name}”?`, |
| 822 | body: s.nodes.some((n) => n.id === proxy.selected) |
| 823 | ? "当前节点来自此来源。删除后 Cage 会保持离线,直到你选择其他节点。" |
| 824 | : "将移除此来源及其节点,本地项目文件不受影响。", |
| 825 | confirm: "删除来源", |
| 826 | }) |
| 827 | ) |
| 828 | mutate("delete", { id: s.id }); |
| 829 | }} |
| 830 | > |
| 831 | <Trash2 size={14} /> |
| 832 | </button> |
| 833 | </div> |
| 834 | </div> |
| 835 | ))} |
| 836 | {form && ( |
| 837 | <div className="proxy-import"> |
| 838 | <h4>{form.id ? "编辑来源" : "添加节点来源"}</h4> |
| 839 | {!form.id && ( |
| 840 | <div className="proxy-tabs"> |
| 841 | {[ |
| 842 | ["url", "订阅链接"], |
| 843 | ["file", "本地 YAML"], |
| 844 | ["manual", "手动节点"], |
| 845 | ].map(([id, label]) => ( |
| 846 | <button |
| 847 | key={id} |
| 848 | className={mode === id ? "selected" : ""} |
| 849 | onClick={() => { |
| 850 | setMode(id); |
| 851 | setForm((old) => ({ ...old, content: "", url: "" })); |
| 852 | }} |
| 853 | > |
| 854 | {label} |
| 855 | </button> |
| 856 | ))} |
| 857 | </div> |
| 858 | )} |
| 859 | <label> |
| 860 | 来源名称 |
| 861 | <input |
| 862 | value={form.name} |
| 863 | maxLength={100} |
| 864 | onChange={(e) => setForm({ ...form, name: e.target.value })} |
| 865 | placeholder="例如:日常代理" |
| 866 | /> |
| 867 | </label> |
| 868 | {mode === "url" && (!form.id || form.remote) && ( |
| 869 | <> |
| 870 | <label> |
| 871 | {form.id ? "新订阅链接(留空保留)" : "订阅链接"} |
| 872 | <input |
| 873 | value={form.url} |
| 874 | type="url" |
| 875 | autoComplete="off" |
| 876 | onChange={(e) => |
| 877 | setForm({ ...form, url: e.target.value }) |
| 878 | } |
| 879 | placeholder="https://…" |
| 880 | /> |
| 881 | </label> |
| 882 | <label> |
| 883 | 自动更新间隔 |
| 884 | <InlineSelect |
| 885 | aria-label="自动更新间隔" |
| 886 | value={form.interval_hours} |
| 887 | onValueChange={(next) => |
| 888 | setForm({ |
| 889 | ...form, |
| 890 | interval_hours: Number(next), |
| 891 | }) |
| 892 | } |
| 893 | > |
| 894 | {[ |
| 895 | [0, "仅手动更新"], |
| 896 | [1, "每小时"], |
| 897 | [6, "每 6 小时"], |
| 898 | [24, "每天"], |
| 899 | [168, "每周"], |
| 900 | ].map(([v, label]) => ( |
| 901 | <option value={v} key={v}> |
| 902 | {label} |
| 903 | </option> |
| 904 | ))} |
| 905 | </InlineSelect> |
| 906 | </label> |
| 907 | {!form.id && ( |
| 908 | <label className="check-line"> |
| 909 | <input |
| 910 | type="checkbox" |
| 911 | checked={form.host_download} |
| 912 | onChange={(e) => |
| 913 | setForm({ ...form, host_download: e.target.checked }) |
| 914 | } |
| 915 | /> |
| 916 | 通过主机网络下载这份订阅 |
| 917 | </label> |
| 918 | )} |
| 919 | <p className="field-help"> |
| 920 | 默认经当前配置的代理下载。首次没有可用节点时,可勾选主机下载或导入本地文件;失败不会自动改用其他路径。 |
| 921 | </p> |
| 922 | </> |
| 923 | )} |
| 924 | {mode === "file" && !form.id && ( |
| 925 | <ProxyYAMLInput |
| 926 | content={form.content} |
| 927 | disabled={busy} |
| 928 | onReadingChange={setReadingFile} |
| 929 | onContentChange={(content) => |
| 930 | setForm((old) => ({ ...old, content })) |
| 931 | } |
| 932 | onFileRead={(value, content) => |
| 933 | setForm((old) => ({ |
| 934 | ...old, |
| 935 | content, |
| 936 | name: |
| 937 | old.name || |
| 938 | value.name.replace(/\.[^.]+$/, "").slice(0, 100), |
| 939 | interval_hours: 0, |
| 940 | })) |
| 941 | } |
| 942 | /> |
| 943 | )} |
| 944 | {mode === "manual" && !form.id && ( |
| 945 | <> |
| 946 | <div className="field-row"> |
| 947 | <label> |
| 948 | 节点名称 |
| 949 | <input |
| 950 | value={manual.name} |
| 951 | onChange={(e) => |
| 952 | setManual({ ...manual, name: e.target.value }) |
| 953 | } |
| 954 | /> |
| 955 | </label> |
| 956 | <label> |
| 957 | 协议 |
| 958 | <InlineSelect |
| 959 | aria-label="协议" |
| 960 | value={manual.type} |
| 961 | onValueChange={(next) => |
| 962 | setManual({ ...manual, type: next }) |
| 963 | } |
| 964 | > |
| 965 | <option value="socks5">SOCKS5</option> |
| 966 | <option value="http">HTTP CONNECT</option> |
| 967 | </InlineSelect> |
| 968 | </label> |
| 969 | </div> |
| 970 | <div className="field-row"> |
| 971 | <label> |
| 972 | 服务器 |
| 973 | <input |
| 974 | value={manual.server} |
| 975 | onChange={(e) => |
| 976 | setManual({ ...manual, server: e.target.value }) |
| 977 | } |
| 978 | placeholder="域名或 IP" |
| 979 | /> |
| 980 | </label> |
| 981 | <label> |
| 982 | 端口 |
| 983 | <input |
| 984 | type="number" |
| 985 | min="1" |
| 986 | max="65535" |
| 987 | value={manual.port} |
| 988 | onChange={(e) => |
| 989 | setManual({ ...manual, port: e.target.value }) |
| 990 | } |
| 991 | /> |
| 992 | </label> |
| 993 | </div> |
| 994 | <div className="field-row"> |
| 995 | <label> |
| 996 | 用户名 |
| 997 | <input |
| 998 | value={manual.username} |
| 999 | autoComplete="off" |
| 1000 | onChange={(e) => |
| 1001 | setManual({ ...manual, username: e.target.value }) |
| 1002 | } |
| 1003 | /> |
| 1004 | </label> |
| 1005 | <label> |
| 1006 | 密码 |
| 1007 | <input |
| 1008 | type="password" |
| 1009 | value={manual.password} |
| 1010 | autoComplete="new-password" |
| 1011 | onChange={(e) => |
| 1012 | setManual({ ...manual, password: e.target.value }) |
| 1013 | } |
| 1014 | /> |
| 1015 | </label> |
| 1016 | </div> |
| 1017 | </> |
| 1018 | )} |
| 1019 | <div className="proxy-form-actions"> |
| 1020 | <button disabled={busy} onClick={() => setForm(null)}> |
| 1021 | 取消 |
| 1022 | </button> |
| 1023 | <button |
| 1024 | className="primary" |
| 1025 | disabled={ |
| 1026 | busy || |
| 1027 | readingFile || |
| 1028 | (!form.id && |
| 1029 | (mode === "url" |
| 1030 | ? !form.url || !form.name |
| 1031 | : mode === "file" |
| 1032 | ? !form.content.trim() || !form.name |
| 1033 | : !manual.name || !manual.server)) |
| 1034 | } |
| 1035 | onClick={submit} |
| 1036 | > |
| 1037 | {form.id ? "保存来源" : "导入并验证"} |
| 1038 | </button> |
| 1039 | </div> |
| 1040 | </div> |
| 1041 | )} |
| 1042 | </> |
| 1043 | )} |
| 1044 | {tab === "connections" && ( |
| 1045 | <> |
| 1046 | <div className="proxy-metrics"> |
| 1047 | <span> |
| 1048 | 上传 <strong>{bytes(rates.up)}/s</strong> |
| 1049 | <small>累计 {bytes(connections.uploadTotal)}</small> |
| 1050 | </span> |
| 1051 | <span> |
| 1052 | 下载 <strong>{bytes(rates.down)}/s</strong> |
| 1053 | <small>累计 {bytes(connections.downloadTotal)}</small> |
| 1054 | </span> |
| 1055 | </div> |
| 1056 | <p className="field-help"> |
| 1057 | 显示当前核心的连接与流量,包含 Cage |
| 1058 | 自身检测请求;重启核心后累计值重置。 |
| 1059 | </p> |
| 1060 | <div className="proxy-node-list"> |
| 1061 | {connections.connections.map((c) => ( |
| 1062 | <div className="proxy-connection" key={c.id}> |
| 1063 | <span> |
| 1064 | {c.host}:{c.port} |
| 1065 | <small>{c.network}</small> |
| 1066 | </span> |
| 1067 | <small> |
| 1068 | ↑ {bytes(c.upload)} · ↓ {bytes(c.download)} |
| 1069 | </small> |
| 1070 | </div> |
| 1071 | ))} |
| 1072 | {!connections.connections.length && ( |
| 1073 | <p className="proxy-empty">暂无活动连接</p> |
| 1074 | )} |
| 1075 | </div> |
| 1076 | </> |
| 1077 | )} |
| 1078 | {tab === "diagnostics" && ( |
| 1079 | <> |
| 1080 | <p className="field-help"> |
| 1081 | 随附核心 {proxy.version || "未运行"}。以下为 Cage |
| 1082 | 状态摘要;含凭据风险的核心原始日志不向页面公开。 |
| 1083 | </p> |
| 1084 | <div className="proxy-diagnostics" role="log"> |
| 1085 | {logs.map((line, i) => ( |
| 1086 | <div key={i}>{line}</div> |
| 1087 | ))} |
| 1088 | </div> |
| 1089 | <label> |
| 1090 | 独立代理端口 |
| 1091 | <input |
| 1092 | type="number" |
| 1093 | min="1024" |
| 1094 | max="65535" |
| 1095 | value={port} |
| 1096 | onChange={(e) => { |
| 1097 | portDirty.current = true; |
| 1098 | setPort(e.target.value); |
| 1099 | }} |
| 1100 | /> |
| 1101 | </label> |
| 1102 | <div className="proxy-toolbar"> |
| 1103 | <button |
| 1104 | disabled={busy || !selected} |
| 1105 | onClick={async () => { |
| 1106 | if ( |
| 1107 | Number(port) !== proxy.port && |
| 1108 | status.vm_running && |
| 1109 | !(await onConfirm({ |
| 1110 | title: "更换代理端口并重启环境?", |
| 1111 | body: "端口变化需要重启环境,当前终端会结束。", |
| 1112 | confirm: "更换并重启", |
| 1113 | })) |
| 1114 | ) |
| 1115 | return; |
| 1116 | mutate("restart", { port: Number(port) }); |
| 1117 | }} |
| 1118 | > |
| 1119 | <RefreshCw size={14} /> |
| 1120 | 应用端口并重启核心 |
| 1121 | </button> |
| 1122 | <button |
| 1123 | disabled={busy || !proxy.running} |
| 1124 | onClick={() => mutate("stop")} |
| 1125 | > |
| 1126 | <Square size={14} /> |
| 1127 | 停止核心 |
| 1128 | </button> |
| 1129 | </div> |
| 1130 | <p className="field-help"> |
| 1131 | Cage 不设置系统代理或主机 TUN。本机其他代理的 TUN |
| 1132 | 仍可能影响核心出站,需要结合实际路由验证。 |
| 1133 | </p> |
| 1134 | </> |
| 1135 | )} |
| 1136 | {pending && ( |
| 1137 | <p className="proxy-progress" role="status"> |
| 1138 | <LoaderCircle size={14} className="spin" /> |
| 1139 | {pending === "delay" |
| 1140 | ? "正在检测节点延迟…" |
| 1141 | : status.job?.message || "正在处理…"} |
| 1142 | </p> |
| 1143 | )} |
| 1144 | {error && !(tab === "nodes" && allNodes.length > 0) && ( |
| 1145 | <div role="alert" className="error-message"> |
| 1146 | <SafetyMessage message={error} /> |
| 1147 | </div> |
| 1148 | )} |
| 1149 | {notice && ( |
| 1150 | <p role="status" className="proxy-notice"> |
| 1151 | <Check size={14} /> |
| 1152 | {notice} |
| 1153 | </p> |
| 1154 | )} |
| 1155 | </div> |
| 1156 | ); |
| 1157 | } |