diff --git a/config-sub-converter/scripts/convert-awg-to-clash.js b/config-sub-converter/scripts/convert-awg-to-clash.js index f82c1ff..9503ddd 100644 --- a/config-sub-converter/scripts/convert-awg-to-clash.js +++ b/config-sub-converter/scripts/convert-awg-to-clash.js @@ -14,83 +14,62 @@ function parseIniWG(text) { section = sec[1]; continue; } - const kv = line.match(/^([^=]+?)\s*=\s*(.+)$/); if (!kv || !section) continue; - const key = kv[1].trim(); - const val = kv[2].trim(); - - if (section === "Interface") data.Interface[key] = val; - if (section === "Peer") data.Peer[key] = val; + data[section][kv[1].trim()] = kv[2].trim(); } return data; } -function splitList(v) { +function split(v) { return String(v || "") .split(",") .map(x => x.trim()) .filter(Boolean); } -function parseEndpoint(ep) { - const m = String(ep || "").match(/^(.+?):(\d+)$/); - if (!m) return { host: "", port: 0 }; - return { host: m[1], port: Number(m[2]) }; -} - function scalar(v) { - if (typeof v === "number") return String(v); - if (typeof v === "boolean") return v ? "true" : "false"; - return `"${String(v).replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"`; + if (typeof v === "number") return v; + if (typeof v === "boolean") return v; + return String(v); } -function arrayInline(arr) { - return `[${arr.map(scalar).join(", ")}]`; -} +const wg = parseIniWG($content); +const i = wg.Interface; +const p = wg.Peer; -function main() { - const input = $content; // Sub Store magic variable +const [server, port] = p.Endpoint.split(":"); - const wg = parseIniWG(input); - const i = wg.Interface; - const p = wg.Peer; - - const dns = splitList(i.DNS); - const allowed = splitList(p.AllowedIPs); - const ep = parseEndpoint(p.Endpoint); - - const amz = {}; - ["Jc","Jmin","Jmax","S1","S2","H1","H2","H3","H4"].forEach(k => { - if (i[k] !== undefined) amz[k.toLowerCase()] = Number(i[k]); - }); - - let out = ""; - out += "proxies:\n"; - out += " - name: \"amz-wg\"\n"; - out += " type: wireguard\n"; - out += ` ip: ${scalar(i.Address)}\n`; - out += ` private-key: ${scalar(i.PrivateKey)}\n`; - out += " peers:\n"; - out += ` - server: ${scalar(ep.host)}\n`; - out += ` port: ${ep.port}\n`; - out += ` public-key: ${scalar(p.PublicKey)}\n`; - if (p.PresharedKey) - out += ` pre-shared-key: ${scalar(p.PresharedKey)}\n`; - out += ` allowed-ips: ${arrayInline(allowed)}\n`; - out += " udp: true\n"; - out += " remote-dns-resolve: true\n"; - if (dns.length) out += ` dns: ${arrayInline(dns)}\n`; - - if (Object.keys(amz).length) { - out += " amnezia-wg-option:\n"; - for (const k in amz) { - out += ` ${k}: ${amz[k]}\n`; - } +const proxy = { + name: "amz-wg", + type: "wireguard", + ip: i.Address, + "private-key": i.PrivateKey, + peers: [{ + server, + port: Number(port), + "public-key": p.PublicKey, + "pre-shared-key": p.PresharedKey, + "allowed-ips": split(p.AllowedIPs) + }], + udp: true, + "remote-dns-resolve": true, + dns: split(i.DNS), + "amnezia-wg-option": { + jc: Number(i.Jc), + jmin: Number(i.Jmin), + jmax: Number(i.Jmax), + s1: Number(i.S1), + s2: Number(i.S2), + h1: Number(i.H1), + h2: Number(i.H2), + h3: Number(i.H3), + h4: Number(i.H4) } +}; - return out; -} - -main(); +// 🔴 ВАЖНО: перезаписываем $content +$content = ProxyUtils.yaml.safeDump({ + proxies: [proxy] +});