Commit 2c15b777 authored by nanahira's avatar nanahira

update gateway compare

parent 6319628e
...@@ -174,20 +174,39 @@ class InventoryBuilder { ...@@ -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 // true: 本地做 server,false: 远端做server
// 如果都不能做,抛异常 // 如果都不能做,抛异常
// 两个参数对调返回的结果必须相反 // 两个参数对调返回的结果必须相反
gatewayCompare(localGateway: any, remoteGateway: any): boolean { gatewayCompare(localGateway: any, remoteGateway: any): boolean {
// 两边至少一个有IPv4地址才能连 // 两边至少一个有IPv4地址才能连
assert(localGateway.ipv4 !== '' || remoteGateway.ipv4 !== ''); assert(localGateway.ipv4 !== '' || remoteGateway.ipv4 !== '');
// 如果都static,就让列表中更靠前的做s // 两边必须相反
if (localGateway.ipv4 == remoteGateway.ipv4) { assert(this.gatewayCompareScore(localGateway) !== this.gatewayCompareScore(remoteGateway));
return localGateway.id < remoteGateway.id; // 两边只有一边是GlobalSSH的,GlobalSSH做s
} else if (localGateway.ipv4 === 'static' || remoteGateway.ipv4 === 'static') { // 只有一边有static的,就static做s
return localGateway.ipv4 === 'static'; // 如果都static,那么没有NAT的或者dmz的做s
} else { // 如果都还相同,就让列表中更靠前的做s
return localGateway.ipv4 === 'dynamic'; return this.gatewayCompareScore(localGateway) > this.gatewayCompareScore(remoteGateway);
}
} }
async wgPublickey(privateKey) { 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