Commit 74a8dc13 authored by 神楽坂玲奈's avatar 神楽坂玲奈

plan, not work

parent 95846ae4
Pipeline #16308 passed with stages
in 51 seconds
/.idea/
/node_modules/
/import/
[
{
"id": 3,
"routers": [
1,
3
]
}
]
[
{"id":1, "interface":"mc-yangtze", "address":"10.198.0.1","subnets": []},
{"id":3, "interface":"mc-south", "address":"10.198.0.3","subnets": []},
{"id":4, "interface":"mc-ayane", "address":"10.198.0.4","subnets": []},
{"id":5, "interface":"mc-satori", "address":"10.198.0.5","subnets": []},
{"id":6, "interface":"mc-koishi", "address":"10.198.0.6","subnets": []},
{"id":7, "interface":"mc-fractal", "address":"10.198.0.7","subnets": []},
{"id":8, "interface":"mc-nanahira", "address":"10.198.0.22","subnets": []},
{"id":9, "interface":"mc-marcia", "address":"10.198.0.9","subnets": []},
{"id":10, "interface":"mc-anillc", "address":"10.198.0.10","subnets": []},
{"id":11, "interface":"mc-crest", "address":"10.198.0.15","subnets": []},
{"id":12, "interface":"mc-hk-hkg-alql2", "address":"10.198.0.12","subnets": []},
{"id":13, "interface":"mc-hk-hkg-alql3", "address":"10.198.0.20","subnets": []},
{"id":14, "interface":"mc-frog", "address":"10.198.0.8","subnets": []},
{"id":15, "interface":"mc-ailizijiang", "address":"10.198.0.41","subnets": []},
{"id":16, "interface":"mc-kano", "address":"10.198.0.14","subnets": []},
{"id":17, "interface":"mc-ge-muc-uma", "address":"10.198.0.18","subnets": []},
{"id":18, "interface":"mc-sakuya", "address":"10.198.0.16","subnets": []},
{"id":19, "interface":"mc-monika", "address":"10.198.0.21","subnets": []},
{"id":24, "interface":"mc-palm", "address":"10.198.0.34","subnets": []},
{"id":25, "interface":"mc-pekunicom", "address":"10.198.0.40","subnets": []},
{"id":26, "interface":"mc-jue", "address":"10.198.0.32","subnets": []},
{"id":28, "interface":"mc-bluebird", "address":"10.198.0.36","subnets": []},
{"id":29, "interface":"mc-chikaanna", "address":"10.198.0.35","subnets": []},
{"id":30, "interface":"mc-cain", "address":"10.198.0.17","subnets": []},
{"id":32, "interface":"mc-hime", "address":"10.198.0.25","subnets": []},
{"id":33, "interface":"mc-shirayuki", "address":"10.198.0.28","subnets": []},
{"id":35, "interface":"mc-whitelight", "address":"10.198.0.42","subnets": []},
{"id":37, "interface":"mc-nekoyuki", "address":"10.198.0.37","subnets": []},
{"id":38, "interface":"mc-rossa", "address":"10.198.0.38","subnets": []},
{"id":39, "interface":"mc-tsan", "address":"10.198.0.39","subnets": []},
{"id":40, "interface":"mc-budingocg", "address":"10.198.0.13","subnets": []},
{"id":41, "interface":"mc-hlsj-vpn-3", "address":"10.198.0.53","subnets": []},
{"id":42, "interface":"mc-kino-enter", "address":"10.198.0.23","subnets": []},
{"id":44, "interface":"mc-tx-sha-xs", "address":"10.198.0.44","subnets": []},
{"id":45, "interface":"mc-tx-sha-hd", "address":"10.198.0.45","subnets": []},
{"id":46, "interface":"mc-tx-pek-wl", "address":"10.198.0.46","subnets": []},
{"id":47, "interface":"mc-tx-can-hr", "address":"10.198.0.47","subnets": []},
{"id":48, "interface":"mc-tx-can-qiao", "address":"10.198.0.48","subnets": []},
{"id":49, "interface":"mc-tx-sha-saku", "address":"10.198.0.49","subnets": []},
{"id":63, "interface":"mc-kr-chu-cain1", "address":"10.198.0.26","subnets": []},
{"id":64, "interface":"mc-kr-chu-cain2", "address":"10.198.0.27","subnets": []},
{"id":65, "interface":"mc-kr-chu-caina", "address":"10.198.0.29","subnets": []},
{"id":100, "interface":"mc-mini", "address":"10.198.0.100","subnets": []},
{"id":101, "interface":"mc-genz", "address":"10.198.0.101","subnets": []}
]
\ No newline at end of file
......@@ -17,7 +17,7 @@ export interface Report {
export interface Change {
seq: number,
via: Record<number, number>,
// plan: Record<number, number>
plan: Record<number, number>
}
// 路由器向路由器发送的消息
......
import config from '../config/config.json';
import routers from '../config/routers.json';
import plans from '../config/plans.json';
import { parseInt } from 'lodash';
import { Router } from './Router';
import child_process from 'child_process';
export class RouteWriter {
......@@ -10,26 +11,29 @@ export class RouteWriter {
static plan = new Map<number, number>();
static reset() {
console.log("reset");
console.log('reset');
const self = Router.all.find(r => r.id === this.id);
this.input.push(`route flush table ${config.table} proto ${config.proto}`);
for (const peer of routers.filter((r) => r.id !== this.id && r.interface)) {
for (const peer of Router.all.filter((r) => r.id !== this.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);
for (const [_groupId, groupRouters] of Object.entries(Router.groups).filter(([_, g]) => !g.includes(self))) {
const groupId = parseInt(_groupId);
this.setPlan(groupId, this.id);
}
}
static setVia(toId: number, viaId: number) {
this.via.set(toId, viaId);
const to = routers.find(r => r.id === toId);
const via = routers.find(r => r.id === viaId);
const to = Router.all.find(r => r.id === toId);
const via = Router.all.find(r => r.id === viaId);
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} via ${via.linkAddress} table ${config.table} proto ${config.proto}`);
}
for (const [table, to] of this.plan.entries()) {
if (to === toId) {
this.input.push(`route replace default dev ${via.interface} table ${table} proto ${config.proto}`);
this.input.push(`route replace default via ${via.linkAddress} table ${table} proto ${config.proto}`);
}
}
}
......@@ -41,8 +45,8 @@ export class RouteWriter {
this.input.push(`route flush 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}`);
const via = Router.all.find(r => r.id === viaId);
this.input.push(`route replace default via ${via.linkAddress} table ${table} proto ${config.proto}`);
}
}
......@@ -51,7 +55,7 @@ export class RouteWriter {
// 可能改成异步的会更好点?
const input = this.input.join('\n');
console.log(input);
// child_process.execFileSync('ip', ['-batch', '-'], { input });
child_process.execFileSync('ip', ['-batch', '-'], { input });
this.input = [];
}
}
import * as _ from 'lodash';
import config from '../config/config.json';
import { Hello, PeerQuality } from '../protocol';
import routers from '../import/data/Router.json';
import { GatewayGroup } from '../import/scripts/GatewayGroup';
import gatewayGroups from '../import/data/GatewayGroup.json';
import subnets from '../import/data/Subnet.json';
export interface RouterConfig {
id: number;
address: string;
subnets: string[];
}
export class Router implements Hello, PeerQuality {
static all: Router[] = routers.filter(r => r.id !== parseInt(process.env.RAILGUN_ID)).map(r => new Router(r.id, r));
static groups: Record<number, Router[]>;
export class Peer implements Hello, PeerQuality, RouterConfig {
id: number;
address: string;
linkAddress: string;
subnets: string[];
delay: number = 0;
......@@ -21,8 +22,10 @@ export class Peer implements Hello, PeerQuality, RouterConfig {
history: (number | undefined)[] = [];
constructor(config: RouterConfig) {
Object.assign(this, config);
constructor(public id: number, r: any) {
this.address = r.address;
this.linkAddress = `10.200.${id}.${parseInt(process.env.RAILGUN_ID)}`;
this.subnets = subnets.filter(s => r.name).map(s => s.subnet);
}
reset() {
......@@ -88,3 +91,17 @@ export class Peer implements Hello, PeerQuality, RouterConfig {
};
}
}
function groupRouters(g: GatewayGroup): Router[] {
return _.uniq(
g.locationPrefix
.flatMap((p) => routers.filter((r) => r.location.startsWith(p)))
.concat(routers.filter((r) => g.includeRouters.includes(r.name)))
.filter((r) => !g.excludeRouters.includes(r.name))
.map((r) => Router.all.find((r1) => r1.id === r.id)!)
.concat(gatewayGroups.filter((g1) => g.children.includes(g1.name)).flatMap((c) => groupRouters(c)))
);
}
Router.groups = Object.fromEntries(gatewayGroups.map((g) => [g.id, groupRouters(g)]));
console.log(Router.groups);
......@@ -3,7 +3,7 @@ import { Socket } from 'dgram';
import config from '../config/config.json';
import { RouteWriter } from './RouteWriter';
import { Peer } from './Peer';
import { Router } from './Router';
import { Change, Hello, Report } from '../protocol';
export class Server {
......@@ -23,9 +23,9 @@ export class Server {
for (const [to, via] of Object.entries(message.via)) {
RouteWriter.setVia(parseInt(to), via);
}
// for (const [plan, to] of Object.entries(message.plan)) {
// RouteWriter.setVia(parseInt(plan), to);
// }
for (const [plan, to] of Object.entries(message.plan)) {
RouteWriter.setVia(parseInt(plan), to);
}
RouteWriter.commit();
this.ack++;
......@@ -37,11 +37,11 @@ export class Server {
socket.send(JSON.stringify(response), config.server_port, config.server_address);
}
update(socket: Socket, self: Hello, peers: Peer[]) {
update(socket: Socket, self: Hello) {
const message: Report = {
id: self.id,
ack: this.ack,
peers: Object.fromEntries(peers.map(peer => [peer.id, peer.update(self.time)]))
peers: Object.fromEntries(Router.all.map(peer => [peer.id, peer.update(self.time)]))
};
socket.send(JSON.stringify(message), config.server_port, config.server_address);
}
......
import dgram from 'dgram';
import assert from 'assert';
import config from '../config/config.json';
import routers from '../config/routers.json';
import { Server } from './Server';
import { Peer } from './Peer';
import { Router } from './Router';
import { Change, Hello } from '../protocol';
import routers from '../import/data/Router.json';
const self: Hello = { id: parseInt(process.env.RAILGUN_ID), seq: 0, time: 0 };
const server = new Server();
const peers = routers.filter(r => r.id !== self.id && r.interface).map(r => new Peer(r));
const socket = dgram
.createSocket('udp4')
......@@ -26,7 +26,7 @@ const socket = dgram
// from client
const message: Hello = JSON.parse(msg.toString());
assert(message.id);
const peer = peers.find(p => p.id === message.id);
const peer = Router.all.find(p => p.id === message.id);
// assert(peer && rinfo.address === `10.200.${self.id}.${peer.id}` && rinfo.port === config.port);
peer.onMessage(message);
}
......@@ -40,9 +40,9 @@ socket.bind(config.port);
setInterval(() => {
self.time = Date.now();
const message = JSON.stringify(self);
for (const peer of peers) {
socket.send(message, config.port, `10.200.${peer.id}.${self.id}`);
for (const peer of Router.all) {
socket.send(message, config.port, peer.linkAddress);
}
server.update(socket, self, peers);
server.update(socket, self);
self.seq++;
}, config.interval);
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