Commit 6d6295ae authored by 神楽坂玲奈's avatar 神楽坂玲奈

frp

parent 35f83424
......@@ -51,7 +51,7 @@ protocol ospf v2 {
{% if connection.outbound %}
cost {{connection.metric}};
{% else %}
cost 99999;
cost 65535;
{% endif %}
hello 5;
};
......
......@@ -13,4 +13,3 @@
name: 'wg-quick@{{conn.name}}'
state: started
enabled: yes
......@@ -4,7 +4,8 @@ import fs from 'fs';
import path from 'path';
import YAML from 'yaml';
import _ from 'lodash';
import * as child_process from 'child_process';
import child_process from 'child_process';
import assert from 'assert';
class InventoryBuilder {
hosts: { [key: string]: any };
......@@ -53,6 +54,8 @@ class InventoryBuilder {
ansible_ssh_user: host.user,
address: host.address,
key: host.wgPrivateKey,
frpsPort: host.frpsPort,
frpToken: host.frpToken,
gateways: _.mapValues(this.gateways[host.name], gw => _.pick(gw, ['mark_gateway'])),
connections
};
......@@ -76,9 +79,13 @@ class InventoryBuilder {
const remoteMark = remote.mark;
const localPort = primary ? remote.port : remote.port2;
const remotePort = primary ? local.port : local.port2;
const remoteFrpsPort = remote.frpsPort;
const wgPublicKey = remote.wgPublickey;
const localPeerAddress = primary ? `10.200.${local.id}.${remote.id}` : `10.201.${local.id}.${remote.id}`;
const remotePeerAddress = primary ? `10.200.${remote.id}.${local.id}` : `10.201.${remote.id}.${local.id}`;
const frpType = protocol === 'frp' ? (this.gatewayCompare(local, remote) ? 'frps' : 'frpc') : undefined;
return {
name,
metric,
......@@ -92,11 +99,27 @@ class InventoryBuilder {
wgPublicKey,
localPeerAddress,
remotePeerAddress,
remoteFrpsPort,
frpType,
inbound,
outbound
};
}
// true: 本地做 server,false: 远端做server
// 如果都不能做,抛异常
// 两个参数对调返回的结果必须相反
gatewayCompare(localGateway: any, remoteGateway: any): boolean {
// 两边至少一个静态地址才能连
assert(localGateway.ipv4 === 'static' || remoteGateway.ipv4 === 'static');
// 如果都static,就让列表中更靠前的做s
if (localGateway.ipv4 == remoteGateway.ipv4) {
return localGateway.id < remoteGateway.id;
} else {
return localGateway.ipv4 === 'static';
}
}
async wgPublickey(privateKey) {
return new Promise((resolve, reject) => {
const child = child_process.execFile('wg', ['pubkey'], { encoding: 'utf8' }, (error, stdout, stderr) => {
......
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