|
| 1 | +import type { ParsedNode } from "../types/node"; |
| 2 | + |
| 3 | +export type NodeNameRenameMap = ReadonlyMap<string, string> | Readonly<Record<string, string>>; |
| 4 | + |
| 5 | +type ReconcileNodeNameReferencesOptions = { |
| 6 | + nodes: ParsedNode[]; |
| 7 | + renameMap?: NodeNameRenameMap; |
| 8 | +}; |
| 9 | + |
| 10 | +function isRecord(value: unknown): value is Record<string, unknown> { |
| 11 | + return Boolean(value) && typeof value === "object" && !Array.isArray(value); |
| 12 | +} |
| 13 | + |
| 14 | +function toRenameMap(value?: NodeNameRenameMap): Map<string, string> { |
| 15 | + const entries = value instanceof Map ? value.entries() : Object.entries(value ?? {}); |
| 16 | + const out = new Map<string, string>(); |
| 17 | + for (const [rawFrom, rawTo] of entries) { |
| 18 | + const from = rawFrom.trim(); |
| 19 | + const to = rawTo.trim(); |
| 20 | + if (!from || !to || from === to) continue; |
| 21 | + out.set(from, to); |
| 22 | + } |
| 23 | + return out; |
| 24 | +} |
| 25 | + |
| 26 | +function resolveRenamedNodeName(name: string, renameMap: ReadonlyMap<string, string>): string { |
| 27 | + let current = name; |
| 28 | + const visited = new Set<string>(); |
| 29 | + while (!visited.has(current)) { |
| 30 | + visited.add(current); |
| 31 | + const next = renameMap.get(current); |
| 32 | + if (!next) break; |
| 33 | + current = next; |
| 34 | + } |
| 35 | + return current; |
| 36 | +} |
| 37 | + |
| 38 | +export function composeNodeNameRenameMaps( |
| 39 | + existing?: NodeNameRenameMap, |
| 40 | + next?: NodeNameRenameMap |
| 41 | +): Map<string, string> { |
| 42 | + const existingMap = toRenameMap(existing); |
| 43 | + const nextMap = toRenameMap(next); |
| 44 | + const out = new Map<string, string>(); |
| 45 | + |
| 46 | + for (const [from, to] of existingMap) { |
| 47 | + const resolved = resolveRenamedNodeName(resolveRenamedNodeName(to, existingMap), nextMap); |
| 48 | + if (from !== resolved) out.set(from, resolved); |
| 49 | + } |
| 50 | + for (const [from, to] of nextMap) { |
| 51 | + if (out.has(from)) continue; |
| 52 | + const resolved = resolveRenamedNodeName(to, nextMap); |
| 53 | + out.set(from, resolved); |
| 54 | + } |
| 55 | + |
| 56 | + return out; |
| 57 | +} |
| 58 | + |
| 59 | +function remapNameList( |
| 60 | + value: unknown, |
| 61 | + renameMap: ReadonlyMap<string, string>, |
| 62 | + availableNames: ReadonlySet<string>, |
| 63 | + options: { keepDirect?: boolean } = {} |
| 64 | +): unknown { |
| 65 | + if (!Array.isArray(value)) return value; |
| 66 | + const out: unknown[] = []; |
| 67 | + const seenNames = new Set<string>(); |
| 68 | + for (const item of value) { |
| 69 | + if (typeof item !== "string") { |
| 70 | + out.push(item); |
| 71 | + continue; |
| 72 | + } |
| 73 | + const name = item.trim(); |
| 74 | + if (!name) continue; |
| 75 | + if (options.keepDirect && name === "DIRECT") { |
| 76 | + if (!seenNames.has(name)) out.push(name); |
| 77 | + seenNames.add(name); |
| 78 | + continue; |
| 79 | + } |
| 80 | + const nextName = resolveRenamedNodeName(name, renameMap); |
| 81 | + if (!availableNames.has(nextName) || seenNames.has(nextName)) continue; |
| 82 | + seenNames.add(nextName); |
| 83 | + out.push(nextName); |
| 84 | + } |
| 85 | + return out; |
| 86 | +} |
| 87 | + |
| 88 | +function remapAdvancedMemberList( |
| 89 | + value: unknown, |
| 90 | + renameMap: ReadonlyMap<string, string>, |
| 91 | + availableNames: ReadonlySet<string> |
| 92 | +): unknown { |
| 93 | + if (!Array.isArray(value)) return value; |
| 94 | + const out: unknown[] = []; |
| 95 | + const seenNodeNames = new Set<string>(); |
| 96 | + for (const item of value) { |
| 97 | + if (!isRecord(item) || item.kind !== "node" || typeof item.name !== "string") { |
| 98 | + out.push(item); |
| 99 | + continue; |
| 100 | + } |
| 101 | + const nextName = resolveRenamedNodeName(item.name.trim(), renameMap); |
| 102 | + if (!nextName || !availableNames.has(nextName) || seenNodeNames.has(nextName)) continue; |
| 103 | + seenNodeNames.add(nextName); |
| 104 | + out.push(nextName === item.name ? item : { ...item, name: nextName }); |
| 105 | + } |
| 106 | + return out; |
| 107 | +} |
| 108 | + |
| 109 | +function remapProxyGroupAdvanced( |
| 110 | + value: unknown, |
| 111 | + renameMap: ReadonlyMap<string, string>, |
| 112 | + availableNames: ReadonlySet<string> |
| 113 | +): unknown { |
| 114 | + if (!isRecord(value)) return value; |
| 115 | + return Object.fromEntries( |
| 116 | + Object.entries(value).map(([groupId, rawAdvanced]) => { |
| 117 | + if (!isRecord(rawAdvanced)) return [groupId, rawAdvanced]; |
| 118 | + return [ |
| 119 | + groupId, |
| 120 | + { |
| 121 | + ...rawAdvanced, |
| 122 | + ...(Object.hasOwn(rawAdvanced, "extraMembers") |
| 123 | + ? { extraMembers: remapAdvancedMemberList(rawAdvanced.extraMembers, renameMap, availableNames) } |
| 124 | + : {}), |
| 125 | + ...(Object.hasOwn(rawAdvanced, "excludedMembers") |
| 126 | + ? { excludedMembers: remapAdvancedMemberList(rawAdvanced.excludedMembers, renameMap, availableNames) } |
| 127 | + : {}), |
| 128 | + ...(Object.hasOwn(rawAdvanced, "memberOrder") |
| 129 | + ? { memberOrder: remapAdvancedMemberList(rawAdvanced.memberOrder, renameMap, availableNames) } |
| 130 | + : {}), |
| 131 | + }, |
| 132 | + ]; |
| 133 | + }) |
| 134 | + ); |
| 135 | +} |
| 136 | + |
| 137 | +export function reconcileNodeNameReferences<T extends object>( |
| 138 | + config: T, |
| 139 | + options: ReconcileNodeNameReferencesOptions |
| 140 | +): T { |
| 141 | + const rawConfig = config as Record<string, unknown>; |
| 142 | + const renameMap = toRenameMap(options.renameMap); |
| 143 | + const availableNames = new Set(options.nodes.map((node) => node.name.trim()).filter(Boolean)); |
| 144 | + |
| 145 | + const listenerPorts = isRecord(rawConfig.listenerPorts) |
| 146 | + ? Object.fromEntries( |
| 147 | + Object.entries(rawConfig.listenerPorts) |
| 148 | + .map(([name, port]) => [resolveRenamedNodeName(name, renameMap), port] as const) |
| 149 | + .filter( |
| 150 | + ([name, port]) => |
| 151 | + availableNames.has(name) && |
| 152 | + typeof port === "number" && |
| 153 | + Number.isInteger(port) && |
| 154 | + port >= 1 && |
| 155 | + port <= 65535 |
| 156 | + ) |
| 157 | + ) |
| 158 | + : rawConfig.listenerPorts; |
| 159 | + |
| 160 | + const dialerProxyGroups = Array.isArray(rawConfig.dialerProxyGroups) |
| 161 | + ? rawConfig.dialerProxyGroups.map((rawGroup) => { |
| 162 | + if (!isRecord(rawGroup)) return rawGroup; |
| 163 | + return { |
| 164 | + ...rawGroup, |
| 165 | + relayNodes: remapNameList(rawGroup.relayNodes, renameMap, availableNames, { keepDirect: true }), |
| 166 | + targetNodes: remapNameList(rawGroup.targetNodes, renameMap, availableNames), |
| 167 | + }; |
| 168 | + }) |
| 169 | + : rawConfig.dialerProxyGroups; |
| 170 | + |
| 171 | + return { |
| 172 | + ...rawConfig, |
| 173 | + ...(Object.hasOwn(rawConfig, "listenerPorts") ? { listenerPorts } : {}), |
| 174 | + ...(Object.hasOwn(rawConfig, "dialerProxyGroups") ? { dialerProxyGroups } : {}), |
| 175 | + ...(Object.hasOwn(rawConfig, "proxyGroupAdvanced") |
| 176 | + ? { proxyGroupAdvanced: remapProxyGroupAdvanced(rawConfig.proxyGroupAdvanced, renameMap, availableNames) } |
| 177 | + : {}), |
| 178 | + } as T; |
| 179 | +} |
0 commit comments