Commit 577c4141 authored by 神楽坂玲奈's avatar 神楽坂玲奈

test

parent dbc63c7f
[
{
"id": 3,
"routers": [
1,
3
]
}
]
import config from '../config/config.json'; import config from '../config/config.json';
import routers from '../config/routers.json'; import routers from '../config/routers.json';
import plans from '../config/plans.json';
import child_process from 'child_process'; import child_process from 'child_process';
export class RouteWriter { export class RouteWriter {
static id = parseInt(process.env.RAILGUN_ID);
static input: string[] = []; static input: string[] = [];
static via: Record<number, number> = {}; static via = new Map<number, number>();
static plan: Record<number, number> = {}; static plan = new Map<number, number>();
static reset() { static reset() {
this.input.push(`route flush table ${config.table} proto ${config.proto}`); this.input.push(`route flush table ${config.table} proto ${config.proto}`);
for (const peer of routers.filter((r) => r.id !== parseInt(process.env.RAILGUN_ID) && r.interface)) { for (const peer of routers.filter((r) => r.id !== this.id && r.interface)) {
this.set(peer.id, peer.id); this.setVia(peer.id, peer.id);
}
for (const plan of plans.filter(plan => !plan.routers.includes(this.id))) {
this.setPlan(plan.id, this.id);
} }
} }
static set(toId: number, viaId: number) { static setVia(toId: number, viaId: number) {
console.log(toId, viaId); console.log(toId, viaId);
this.via.set(toId, viaId);
const to = routers.find(r => r.id === toId); const to = routers.find(r => r.id === toId);
const via = routers.find(r => r.id === viaId); const via = routers.find(r => r.id === viaId);
for (const address of [to.address, ...to.subnets]) { for (const address of [to.address, ...to.subnets]) {
this.input.push(`route replace ${address} dev ${via.interface} table ${config.table} proto ${config.proto}`); this.input.push(`route replace ${address} dev ${via.interface} table ${config.table} proto ${config.proto}`);
} }
for (const [table, to] of Object.entries(RouteWriter.plan)) { for (const [table, to] of this.plan.entries()) {
if (to === toId) { if (to === toId) {
this.input.push(`route replace default dev ${via.interface} table ${table} proto ${config.proto}`); this.input.push(`route replace default dev ${via.interface} table ${table} proto ${config.proto}`);
} }
...@@ -29,13 +35,20 @@ export class RouteWriter { ...@@ -29,13 +35,20 @@ export class RouteWriter {
} }
static setPlan(table: number, toId: number) { static setPlan(table: number, toId: number) {
console.log(table, viaId); console.log(table, toId);
const via = routers.find(r => r.id === viaId); this.plan.set(table, toId);
this.input.push(`route replace default dev ${via.interface} table ${table} proto ${config.proto}`);
if (toId === this.id) {
this.input.push(`route del default table ${table} proto ${config.proto}`);
} else {
const viaId = this.via.get(toId);
const via = routers.find(r => r.id === viaId);
this.input.push(`route replace default dev ${via.interface} table ${table} proto ${config.proto}`);
}
} }
static commit() { static commit() {
// 可能改成异步的会更好点 // 可能改成异步的会更好点?
child_process.execFileSync('ip', ['-batch', '-'], { input: this.input.join('\n') }); child_process.execFileSync('ip', ['-batch', '-'], { input: this.input.join('\n') });
this.input = []; this.input = [];
} }
......
...@@ -19,7 +19,7 @@ export class Server { ...@@ -19,7 +19,7 @@ export class Server {
} }
for (const [to, via] of Object.entries(message.via)) { for (const [to, via] of Object.entries(message.via)) {
RouteWriter.set(parseInt(to), via); RouteWriter.setVia(parseInt(to), via);
} }
RouteWriter.commit(); RouteWriter.commit();
this.ack = message.seq + 1; this.ack = message.seq + 1;
......
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