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

frp

parent 35f83424
...@@ -51,7 +51,7 @@ protocol ospf v2 { ...@@ -51,7 +51,7 @@ protocol ospf v2 {
{% if connection.outbound %} {% if connection.outbound %}
cost {{connection.metric}}; cost {{connection.metric}};
{% else %} {% else %}
cost 99999; cost 65535;
{% endif %} {% endif %}
hello 5; hello 5;
}; };
......
...@@ -6,4 +6,4 @@ ...@@ -6,4 +6,4 @@
systemd: systemd:
name: 'wg-quick@{{conn.name}}' name: 'wg-quick@{{conn.name}}'
state: started state: started
enabled: yes enabled: yes
\ No newline at end of file
...@@ -13,4 +13,3 @@ ...@@ -13,4 +13,3 @@
name: 'wg-quick@{{conn.name}}' name: 'wg-quick@{{conn.name}}'
state: started state: started
enabled: yes enabled: yes
...@@ -4,7 +4,8 @@ import fs from 'fs'; ...@@ -4,7 +4,8 @@ import fs from 'fs';
import path from 'path'; import path from 'path';
import YAML from 'yaml'; import YAML from 'yaml';
import _ from 'lodash'; import _ from 'lodash';
import * as child_process from 'child_process'; import child_process from 'child_process';
import assert from 'assert';
class InventoryBuilder { class InventoryBuilder {
hosts: { [key: string]: any }; hosts: { [key: string]: any };
...@@ -53,6 +54,8 @@ class InventoryBuilder { ...@@ -53,6 +54,8 @@ class InventoryBuilder {
ansible_ssh_user: host.user, ansible_ssh_user: host.user,
address: host.address, address: host.address,
key: host.wgPrivateKey, key: host.wgPrivateKey,
frpsPort: host.frpsPort,
frpToken: host.frpToken,
gateways: _.mapValues(this.gateways[host.name], gw => _.pick(gw, ['mark_gateway'])), gateways: _.mapValues(this.gateways[host.name], gw => _.pick(gw, ['mark_gateway'])),
connections connections
}; };
...@@ -76,9 +79,13 @@ class InventoryBuilder { ...@@ -76,9 +79,13 @@ class InventoryBuilder {
const remoteMark = remote.mark; const remoteMark = remote.mark;
const localPort = primary ? remote.port : remote.port2; const localPort = primary ? remote.port : remote.port2;
const remotePort = primary ? local.port : local.port2; const remotePort = primary ? local.port : local.port2;
const remoteFrpsPort = remote.frpsPort;
const wgPublicKey = remote.wgPublickey; const wgPublicKey = remote.wgPublickey;
const localPeerAddress = primary ? `10.200.${local.id}.${remote.id}` : `10.201.${local.id}.${remote.id}`; 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 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 { return {
name, name,
metric, metric,
...@@ -92,11 +99,27 @@ class InventoryBuilder { ...@@ -92,11 +99,27 @@ class InventoryBuilder {
wgPublicKey, wgPublicKey,
localPeerAddress, localPeerAddress,
remotePeerAddress, remotePeerAddress,
remoteFrpsPort,
frpType,
inbound, inbound,
outbound 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) { async wgPublickey(privateKey) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const child = child_process.execFile('wg', ['pubkey'], { encoding: 'utf8' }, (error, stdout, stderr) => { 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