Update config-sub-converter/scripts/convert-awg-to-clash.js
This commit is contained in:
@@ -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(", ")}]`;
|
||||
}
|
||||
|
||||
function main() {
|
||||
const input = $content; // Sub Store magic variable
|
||||
|
||||
const wg = parseIniWG(input);
|
||||
const wg = parseIniWG($content);
|
||||
const i = wg.Interface;
|
||||
const p = wg.Peer;
|
||||
|
||||
const dns = splitList(i.DNS);
|
||||
const allowed = splitList(p.AllowedIPs);
|
||||
const ep = parseEndpoint(p.Endpoint);
|
||||
const [server, port] = p.Endpoint.split(":");
|
||||
|
||||
const amz = {};
|
||||
["Jc","Jmin","Jmax","S1","S2","H1","H2","H3","H4"].forEach(k => {
|
||||
if (i[k] !== undefined) amz[k.toLowerCase()] = Number(i[k]);
|
||||
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)
|
||||
}
|
||||
};
|
||||
|
||||
// 🔴 ВАЖНО: перезаписываем $content
|
||||
$content = ProxyUtils.yaml.safeDump({
|
||||
proxies: [proxy]
|
||||
});
|
||||
|
||||
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`;
|
||||
}
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
main();
|
||||
|
||||
Reference in New Issue
Block a user