Commit 2c15b777 authored by nanahira's avatar nanahira

update gateway compare

parent 6319628e
......@@ -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