76 lines
1.5 KiB
JavaScript
76 lines
1.5 KiB
JavaScript
function parseIniWG(text) {
|
|
const lines = text
|
|
.replace(/\r\n/g, "\n")
|
|
.split("\n")
|
|
.map(l => l.trim())
|
|
.filter(l => l && !l.startsWith("#") && !l.startsWith(";"));
|
|
|
|
let section = null;
|
|
const data = { Interface: {}, Peer: {} };
|
|
|
|
for (const line of lines) {
|
|
const sec = line.match(/^\[(.+?)\]$/);
|
|
if (sec) {
|
|
section = sec[1];
|
|
continue;
|
|
}
|
|
const kv = line.match(/^([^=]+?)\s*=\s*(.+)$/);
|
|
if (!kv || !section) continue;
|
|
|
|
data[section][kv[1].trim()] = kv[2].trim();
|
|
}
|
|
return data;
|
|
}
|
|
|
|
function split(v) {
|
|
return String(v || "")
|
|
.split(",")
|
|
.map(x => x.trim())
|
|
.filter(Boolean);
|
|
}
|
|
|
|
function scalar(v) {
|
|
if (typeof v === "number") return v;
|
|
if (typeof v === "boolean") return v;
|
|
return String(v);
|
|
}
|
|
|
|
const wg = parseIniWG($content);
|
|
const i = wg.Interface;
|
|
const p = wg.Peer;
|
|
|
|
const [server, port] = p.Endpoint.split(":");
|
|
|
|
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]
|
|
});
|