|
| 1 | +export type WarpImportSummary = { |
| 2 | + status: "idle" | "ok" | "warning" | "error"; |
| 3 | + message: string; |
| 4 | + lines: number; |
| 5 | + hasInterface: boolean; |
| 6 | + hasPeer: boolean; |
| 7 | + hasPrivateKey: boolean; |
| 8 | + hasPublicKey: boolean; |
| 9 | + hasAddress: boolean; |
| 10 | + hasEndpoint: boolean; |
| 11 | + allowedIps: number; |
| 12 | + dnsServers: number; |
| 13 | + mtu: string; |
| 14 | + keepalive: string; |
| 15 | + endpointHost: string; |
| 16 | + endpointPort: string; |
| 17 | + looksImportable: boolean; |
| 18 | +}; |
| 19 | + |
| 20 | +type SectionName = "interface" | "peer" | ""; |
| 21 | + |
| 22 | +export function summarizeWarpImport(text: string): WarpImportSummary { |
| 23 | + const trimmed = text.trim(); |
| 24 | + if (!trimmed) return summary("idle", "等待粘贴 WireGuard/WARP 配置。", 0, {}, {}); |
| 25 | + const parsed = parseWireGuard(trimmed); |
| 26 | + const endpoint = splitEndpoint(parsed.peer.endpoint || ""); |
| 27 | + const missing = [ |
| 28 | + parsed.hasInterface ? "" : "[Interface]", |
| 29 | + parsed.hasPeer ? "" : "[Peer]", |
| 30 | + parsed.interface.privatekey ? "" : "PrivateKey", |
| 31 | + parsed.interface.address ? "" : "Address", |
| 32 | + parsed.peer.publickey ? "" : "PublicKey", |
| 33 | + parsed.peer.endpoint ? "" : "Endpoint" |
| 34 | + ].filter(Boolean); |
| 35 | + if (missing.length) { |
| 36 | + return summary("error", `缺少 ${missing.join(", ")},CLI 会拒绝导入。`, parsed.lines, parsed.interface, parsed.peer, parsed.hasInterface, parsed.hasPeer); |
| 37 | + } |
| 38 | + if (!endpoint.host || !endpoint.port) { |
| 39 | + return summary("error", "Endpoint 需要包含 host:port。", parsed.lines, parsed.interface, parsed.peer, parsed.hasInterface, parsed.hasPeer); |
| 40 | + } |
| 41 | + if (!validPort(endpoint.port)) { |
| 42 | + return summary("error", "Endpoint 端口必须是 0-65535 的数字。", parsed.lines, parsed.interface, parsed.peer, parsed.hasInterface, parsed.hasPeer); |
| 43 | + } |
| 44 | + if (!parsed.peer.allowedips) { |
| 45 | + return summary("warning", "AllowedIPs 未填写,CLI 会使用 0.0.0.0/0 和 ::/0 默认值。", parsed.lines, parsed.interface, parsed.peer, parsed.hasInterface, parsed.hasPeer); |
| 46 | + } |
| 47 | + return summary("ok", "配置字段齐全,可交给 CLI 导入。", parsed.lines, parsed.interface, parsed.peer, parsed.hasInterface, parsed.hasPeer); |
| 48 | +} |
| 49 | + |
| 50 | +export function formatWarpImportSummaryReport(summary: WarpImportSummary): string { |
| 51 | + return [ |
| 52 | + "MagicNet WARP import summary", |
| 53 | + "privacy_note=private/public keys and raw endpoint are omitted", |
| 54 | + `status=${summary.status}`, |
| 55 | + `message=${summary.message}`, |
| 56 | + `lines=${summary.lines}`, |
| 57 | + `has_interface=${summary.hasInterface ? 1 : 0}`, |
| 58 | + `has_peer=${summary.hasPeer ? 1 : 0}`, |
| 59 | + `has_private_key=${summary.hasPrivateKey ? 1 : 0}`, |
| 60 | + `has_public_key=${summary.hasPublicKey ? 1 : 0}`, |
| 61 | + `has_address=${summary.hasAddress ? 1 : 0}`, |
| 62 | + `has_endpoint=${summary.hasEndpoint ? 1 : 0}`, |
| 63 | + `endpoint_host_present=${summary.endpointHost ? 1 : 0}`, |
| 64 | + `endpoint_port=${summary.endpointPort || "none"}`, |
| 65 | + `allowed_ips=${summary.allowedIps}`, |
| 66 | + `dns_servers=${summary.dnsServers}`, |
| 67 | + `mtu=${summary.mtu || "default"}`, |
| 68 | + `keepalive=${summary.keepalive || "default"}` |
| 69 | + ].join("\n"); |
| 70 | +} |
| 71 | + |
| 72 | +export function warpImportTone(status: WarpImportSummary["status"]): string { |
| 73 | + if (status === "error") return "border-red-400/30 bg-red-400/10 text-red-100"; |
| 74 | + if (status === "warning") return "border-amber-400/30 bg-amber-400/10 text-amber-100"; |
| 75 | + if (status === "ok") return "border-emerald-400/25 bg-emerald-400/10 text-emerald-100"; |
| 76 | + return "border-zinc-800 bg-zinc-950 text-zinc-400"; |
| 77 | +} |
| 78 | + |
| 79 | +function parseWireGuard(text: string): { |
| 80 | + lines: number; |
| 81 | + hasInterface: boolean; |
| 82 | + hasPeer: boolean; |
| 83 | + interface: Record<string, string>; |
| 84 | + peer: Record<string, string>; |
| 85 | +} { |
| 86 | + let section: SectionName = ""; |
| 87 | + const iface: Record<string, string> = {}; |
| 88 | + const peer: Record<string, string> = {}; |
| 89 | + let hasInterface = false; |
| 90 | + let hasPeer = false; |
| 91 | + const lines = text.split(/\r?\n/); |
| 92 | + lines.forEach((raw) => { |
| 93 | + const line = raw.split("#", 1)[0].split(";", 1)[0].trim(); |
| 94 | + if (!line) return; |
| 95 | + if (line.startsWith("[") && line.endsWith("]")) { |
| 96 | + const sectionName = line.slice(1, -1).trim(); |
| 97 | + section = sectionName === "Interface" ? "interface" : sectionName === "Peer" ? "peer" : ""; |
| 98 | + hasInterface ||= section === "interface"; |
| 99 | + hasPeer ||= section === "peer"; |
| 100 | + return; |
| 101 | + } |
| 102 | + const [key, ...rest] = line.split("="); |
| 103 | + const value = rest.join("=").trim(); |
| 104 | + const normalizedKey = key?.trim().toLowerCase(); |
| 105 | + if (!normalizedKey) return; |
| 106 | + if (section === "interface") iface[normalizedKey] = value; |
| 107 | + if (section === "peer") peer[normalizedKey] = value; |
| 108 | + }); |
| 109 | + return { lines: lines.filter((line) => line.trim()).length, hasInterface, hasPeer, interface: iface, peer }; |
| 110 | +} |
| 111 | + |
| 112 | +function summary( |
| 113 | + status: WarpImportSummary["status"], |
| 114 | + message: string, |
| 115 | + lines: number, |
| 116 | + iface: Record<string, string>, |
| 117 | + peer: Record<string, string>, |
| 118 | + hasInterface = false, |
| 119 | + hasPeer = false |
| 120 | +): WarpImportSummary { |
| 121 | + const endpoint = splitEndpoint(peer.endpoint || ""); |
| 122 | + return { |
| 123 | + status, |
| 124 | + message, |
| 125 | + lines, |
| 126 | + hasInterface, |
| 127 | + hasPeer, |
| 128 | + hasPrivateKey: Boolean(iface.privatekey), |
| 129 | + hasPublicKey: Boolean(peer.publickey), |
| 130 | + hasAddress: Boolean(iface.address), |
| 131 | + hasEndpoint: Boolean(peer.endpoint), |
| 132 | + allowedIps: splitCsv(peer.allowedips).length, |
| 133 | + dnsServers: splitCsv(iface.dns).length, |
| 134 | + mtu: iface.mtu || "", |
| 135 | + keepalive: peer.persistentkeepalive || "", |
| 136 | + endpointHost: endpoint.host, |
| 137 | + endpointPort: endpoint.port, |
| 138 | + looksImportable: status === "ok" || status === "warning" |
| 139 | + }; |
| 140 | +} |
| 141 | + |
| 142 | +function splitEndpoint(endpoint: string): { host: string; port: string } { |
| 143 | + if (endpoint.startsWith("[")) { |
| 144 | + const end = endpoint.indexOf("]:"); |
| 145 | + if (end < 0) return { host: "", port: "" }; |
| 146 | + return { host: endpoint.slice(1, end), port: endpoint.slice(end + 2) }; |
| 147 | + } |
| 148 | + const index = endpoint.lastIndexOf(":"); |
| 149 | + if (index <= 0 || index >= endpoint.length - 1) return { host: "", port: "" }; |
| 150 | + return { host: endpoint.slice(0, index), port: endpoint.slice(index + 1) }; |
| 151 | +} |
| 152 | + |
| 153 | +function validPort(port: string): boolean { |
| 154 | + if (!/^\d+$/.test(port)) return false; |
| 155 | + const parsed = Number(port); |
| 156 | + return Number.isInteger(parsed) && parsed >= 0 && parsed <= 65535; |
| 157 | +} |
| 158 | + |
| 159 | +function splitCsv(value: string): string[] { |
| 160 | + return value.split(",").map((item) => item.trim()).filter(Boolean); |
| 161 | +} |
0 commit comments