Commit cca4bb54 authored by 神楽坂玲奈's avatar 神楽坂玲奈

scripts

parent 595e28d9
......@@ -24,9 +24,9 @@ class InventoryBuilder {
for (const host of Object.values(this.hosts)) {
host.wgPublickey = await this.wgPublickey(host.wgPrivateKey);
}
console.log(Object.values(this.hosts));
// console.log(Object.values(this.hosts));
const hosts = Object.fromEntries(Object.values(this.hosts).map(h => [h.host, this.host_vars(h)]));
console.log(hosts);
// console.log(hosts);
const result = YAML.stringify({ wg: { hosts } });
return fs.promises.writeFile('result/inventory.yaml', result);
}
......@@ -39,11 +39,11 @@ class InventoryBuilder {
const from = this.hosts[h][host.name];
if (from && to) {
// 非对称连接
connections.push(this.parse_connection(host, this.hosts[h], to, false, host.id > this.hosts[h].id));
connections.push(this.parse_connection(host, this.hosts[h], from, false, host.id < this.hosts[h].id));
connections.push(this.parse_connection(host, this.hosts[h], to, false, true));
connections.push(this.parse_connection(host, this.hosts[h], from, true, false));
} else if (from || to) {
// 对称连接
connections.push(this.parse_connection(host, this.hosts[h], from || to, !!to));
connections.push(this.parse_connection(host, this.hosts[h], from || to, true, true));
}
// 不连接
}
......@@ -58,23 +58,30 @@ class InventoryBuilder {
};
}
parse_connection(local: any, remote: any, connstr: string, mirror = false, inbound = false) {
parse_connection(local: any, remote: any, connstr: string, inbound: boolean, outbound: boolean) {
const leftbottom = local.id > remote.id; // true 条目位于左下,false 条目位于右上
const cis = !(!leftbottom && inbound && outbound); // true 无需翻转,false 需要翻转。
const primary = leftbottom ? outbound : inbound; // true 使用 peerAddress、port, false 使用peerAddress2、port2
if (local.name === 'hk-hkg-alql2' && remote.name === 'halozy') {
console.log(primary);
}
const [_metric, protocol, _params] = connstr.split(',');
const metric = parseInt(_metric);
const params = Object.fromEntries(new URLSearchParams(_params).entries());
const name = inbound ? `${remote.name}-in` : remote.name;
const localGatewayName = (mirror ? params.rif : params.lif) || params.if;
const name = `mc${!outbound ? 'i' : '-'}${remote.name}`;
const localGatewayName = (cis ? params.lif : params.rif) || params.if;
const localGateway = localGatewayName ? this.gateways[local.name][localGatewayName] : undefined;
const localGatewayMark = localGatewayName ? localGateway.mark : undefined;
const remoteGatewayName = (mirror ? params.lif : params.rif) || params.if;
const remoteGatewayName = (cis ? params.rif : params.lif) || params.if;
const remoteGateway = remoteGatewayName ? this.gateways[remote.name][remoteGatewayName] : _.find(this.gateways[remote.name]);
const remoteAddress = remoteGateway.address;
const remoteMark = remote.mark;
const localPort = inbound ? remote.port2 : remote.port;
const remotePort = inbound ? local.port2 : local.port;
const localPort = primary ? remote.port : remote.port2;
const remotePort = primary ? local.port : local.port2;
const wgPublicKey = remote.wgPublickey;
const localPeerAddress = inbound ? local.peerAddress2 : local.peerAddress;
const remotePeerAddress = inbound ? remote.peerAddress2 : remote.peerAddress;
const localPeerAddress = primary ? local.peerAddress : local.peerAddress2;
const remotePeerAddress = primary ? remote.peerAddress : remote.peerAddress2;
return {
name,
metric,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment