Commit 2c15b777 authored by nanahira's avatar nanahira

update gateway compare

parent 6319628e
......@@ -19,9 +19,9 @@ class InventoryBuilder {
return (await util.promisify(parse)(data, { columns: true, cast: true })).filter(h => h.id);
}
async loadGateways() {
async loadGateways() {
let gateways = await this.load('gateways');
for (let gateway of gateways) {
for (let gateway of gateways) {
if (!gateway.dev_or_via.length)
continue;
gateway.isTun = !gateway.dev_or_via.match(/^(\d{1,3}\.){1,3}\d{1,3}$/);
......@@ -46,21 +46,21 @@ class InventoryBuilder {
return fs.promises.writeFile('result/inventory.yaml', result);
}
async loadUtilities(hosts) {
async loadUtilities(hosts) {
const raw_utility = _.keyBy(await this.load('configurations'), 'key');
let route_plans = await this.load('route tables');
this.route_lists = YAML.parse(fs.readFileSync(path.join('lists', 'result.yaml'), "utf8"));
for (let plan of route_plans) {
for (let plan of route_plans) {
plan.name = plan.list + "_" + plan.gateway.replace(/-/g, "_").split(".")[0]
//plan.list = JSON.parse(JSON.stringify(this.route_lists[plan.list]));
plan.gatewayAddress = hosts[plan.gateway].address;
}
// 所有内网网段
this.route_lists.mycard = ["10.198.0.0/16", "10.200.0.0/15"];
for (const h in this.hosts) {
for (const h in this.hosts) {
const host = this.hosts[h]
for (const c of host.subnets.split(",")) {
if (!c.length) {
for (const c of host.subnets.split(",")) {
if (!c.length) {
continue;
}
this.route_lists.mycard.push(c);
......@@ -73,10 +73,10 @@ class InventoryBuilder {
route_list_names: Object.keys(this.route_lists),
route_plans
};
for (let col in raw_utility) {
for (let col in raw_utility) {
vars[col] = raw_utility[col].value;
}
return vars;
}
host_vars(host) {
......@@ -174,20 +174,39 @@ class InventoryBuilder {
};
}
// frps还是frpc的积分,NAT越有利分越高
gatewayCompareScore(gateway: any): number {
let score: number = 0xff - gateway.id;
const ipv4Score = ({
"static": 2,
"dynamic": 1
})[gateway.ipv4] || 0;
score |= ipv4Score << 12;
const ipv4NatScore = ({
"ports": 0,
"dmz": 1
})[gateway.ipv4_nat] || 2;
score |= ipv4NatScore << 10;
const globalSSHScore = ({
"globalssh": 1
})[gateway.ssh] || 0;
score |= globalSSHScore << 13;
return score;
}
// true: 本地做 server,false: 远端做server
// 如果都不能做,抛异常
// 两个参数对调返回的结果必须相反
gatewayCompare(localGateway: any, remoteGateway: any): boolean {
// 两边至少一个有IPv4地址才能连
assert(localGateway.ipv4 !== '' || remoteGateway.ipv4 !== '');
// 如果都static,就让列表中更靠前的做s
if (localGateway.ipv4 == remoteGateway.ipv4) {
return localGateway.id < remoteGateway.id;
} else if (localGateway.ipv4 === 'static' || remoteGateway.ipv4 === 'static') {
return localGateway.ipv4 === 'static';
} else {
return localGateway.ipv4 === 'dynamic';
}
// 两边必须相反
assert(this.gatewayCompareScore(localGateway) !== this.gatewayCompareScore(remoteGateway));
// 两边只有一边是GlobalSSH的,GlobalSSH做s
// 只有一边有static的,就static做s
// 如果都static,那么没有NAT的或者dmz的做s
// 如果都还相同,就让列表中更靠前的做s
return this.gatewayCompareScore(localGateway) > this.gatewayCompareScore(remoteGateway);
}
async wgPublickey(privateKey) {
......
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