Commit 231370d9 authored by nanahira's avatar nanahira

fix lif and rif

parent ee82a268
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
"description": "", "description": "",
"scripts": { "scripts": {
"build": "tsc", "build": "tsc",
"start": "node -r ts-node/register --unhandled-rejections=strict src/inventory.ts " "start": "node build/inventory.ts "
}, },
"dependencies": { "dependencies": {
"@types/lodash": "^4.14.149", "@types/lodash": "^4.14.149",
......
...@@ -39,12 +39,12 @@ class InventoryBuilder { ...@@ -39,12 +39,12 @@ class InventoryBuilder {
// 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)])); const hosts = Object.fromEntries(Object.values(this.hosts).map(h => [h.host, this.host_vars(h)]));
// console.log(hosts); // console.log(hosts);
const vars = await this.loadUtilities(hosts); const vars = await this.loadUtilities();
const result = YAML.stringify({ wg: { hosts, vars } }); const result = YAML.stringify({ wg: { hosts, vars } });
return fs.promises.writeFile('result/inventory.yaml', result); return fs.promises.writeFile('result/inventory.yaml', result);
} }
async loadUtilities(hosts) { async loadUtilities() {
const raw_utility = _.keyBy(await this.load('configurations'), 'key'); const raw_utility = _.keyBy(await this.load('configurations'), 'key');
this.routeLists = YAML.parse(fs.readFileSync(path.join('lists', 'result.yaml'), "utf8")); this.routeLists = YAML.parse(fs.readFileSync(path.join('lists', 'result.yaml'), "utf8"));
// 所有内网网段 // 所有内网网段
...@@ -90,20 +90,21 @@ class InventoryBuilder { ...@@ -90,20 +90,21 @@ class InventoryBuilder {
const routePlans = []; const routePlans = [];
for (const h of this.connections) { for (const h of this.connections) {
if (h != host.name) { if (h != host.name) {
const to = host[h]; const to = host[h]; // 当前主机的条目
const from = this.hosts[h][host.name]; const from = this.hosts[h][host.name]; // 其他主机的这个主机的条目
if (from && to) { if (from && to) {
// 非对称连接 // 非对称连接
connections.push(this.parse_connection(host, this.hosts[h], to, false, true)); connections.push(this.parse_connection(host, this.hosts[h], to, false, true, false));
connections.push(this.parse_connection(host, this.hosts[h], from, true, false)); connections.push(this.parse_connection(host, this.hosts[h], from, true, false, true));
} else if (from || to) { } else if (from || to) {
// 对称连接 // 对称连接
connections.push(this.parse_connection(host, this.hosts[h], from || to, true, true)); const connectionString = from || to;
connections.push(this.parse_connection(host, this.hosts[h], null_connection, false, false)); connections.push(this.parse_connection(host, this.hosts[h], connectionString, true, true, connectionString === from));
connections.push(this.parse_connection(host, this.hosts[h], null_connection, false, false, false));
} else { } else {
// 不连接 // 不连接
connections.push(this.parse_connection(host, this.hosts[h], null_connection, true, false)); connections.push(this.parse_connection(host, this.hosts[h], null_connection, true, false, false));
connections.push(this.parse_connection(host, this.hosts[h], null_connection, false, true)); connections.push(this.parse_connection(host, this.hosts[h], null_connection, false, true, false));
} }
routePlans.push({ routePlans.push({
name: h.replace(/-/g, "_"), name: h.replace(/-/g, "_"),
...@@ -128,17 +129,19 @@ class InventoryBuilder { ...@@ -128,17 +129,19 @@ class InventoryBuilder {
}; };
} }
parse_connection(local: any, remote: any, connstr: string, inbound: boolean, outbound: boolean) { parse_connection(local: any, remote: any, connstr: string, inbound: boolean, outbound: boolean, reverse: boolean) {
const leftbottom = local.id > remote.id; // true 条目位于左下,false 条目位于右上 const leftbottom = local.id > remote.id; // true 条目位于左下,false 条目位于右上
const cis = !(!leftbottom && inbound && outbound); // true 无需翻转,false 需要翻转。 const cis = !reverse; // true 无需翻转,false 需要翻转。
const primary = leftbottom ? outbound : inbound; // true 使用 peerAddress、port, false 使用peerAddress2、port2 const primary = leftbottom ? outbound : inbound; // true 使用 peerAddress、port, false 使用peerAddress2、port2
const connStrSplited = connstr.split(',');
const [_metric, protocol, _params] = connstr.split(','); const [_metric, protocol] = connStrSplited;
const paramsString = connStrSplited.slice(2).join("&");
const metric = parseInt(_metric); const metric = parseInt(_metric);
const params = Object.fromEntries(new URLSearchParams(_params).entries()); const params = Object.fromEntries(new URLSearchParams(paramsString).entries());
const name = `mc${!outbound ? 'i' : '-'}${remote.name}`; const name = `mc${!outbound ? 'i' : '-'}${remote.name}`;
const localGatewayName = (cis ? params.lif : params.rif) || params.if; const localGatewayName = (cis ? params.lif : params.rif) || params.if;
const localGateway = localGatewayName ? this.gateways[local.name][localGatewayName] : _.find(this.gateways[local.name]); const localGateway = localGatewayName ? this.gateways[local.name][localGatewayName] : _.find(this.gateways[local.name]);
//console.log(local.name, paramsString, params, localGatewayName, localGateway.name)
const localGatewayMark = localGatewayName ? localGateway.selectionMark : ""; const localGatewayMark = localGatewayName ? localGateway.selectionMark : "";
const remoteGatewayName = (cis ? params.rif : params.lif) || 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 remoteGateway = remoteGatewayName ? this.gateways[remote.name][remoteGatewayName] : _.find(this.gateways[remote.name]);
...@@ -181,6 +184,10 @@ class InventoryBuilder { ...@@ -181,6 +184,10 @@ class InventoryBuilder {
const mtu = Math.min(localGateway ? localGateway.mtu : 1500, remoteGateway ? remoteGateway.mtu : 1500); const mtu = Math.min(localGateway ? localGateway.mtu : 1500, remoteGateway ? remoteGateway.mtu : 1500);
//console.log(local.name, name, mtu); //console.log(local.name, name, mtu);
if (outbound) {
console.log(`${local.name} GW ${localGateway.isp} ${inbound ? "<" : "="}==[${protocol}]==> ${remote.name} GW ${remoteGateway.isp}`);
}
return { return {
name, name,
metric, metric,
......
...@@ -10,6 +10,10 @@ set -e ...@@ -10,6 +10,10 @@ set -e
mkdir -p result mkdir -p result
npm start npm start
cd lists
./run.sh
cd ..
cd ansible || exit cd ansible || exit
sed -r -e '/^Address/d' \ sed -r -e '/^Address/d' \
......
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