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