2019-07-03 17:56:07 +02:00
|
|
|
import WebSocket from "ws";
|
2019-07-05 00:09:59 +02:00
|
|
|
import Connector from "./connector";
|
2019-07-03 17:56:07 +02:00
|
|
|
|
|
|
|
|
export default class ConnectorRIS extends Connector{
|
|
|
|
|
|
2019-07-05 12:01:38 +02:00
|
|
|
constructor(name, params, env) {
|
|
|
|
|
super(name, params, env);
|
2019-07-05 00:09:59 +02:00
|
|
|
this.ws = null;
|
2019-07-03 17:56:07 +02:00
|
|
|
}
|
|
|
|
|
|
2019-07-05 00:09:59 +02:00
|
|
|
connect = () =>
|
|
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
this.ws = new WebSocket(this.params.url);
|
|
|
|
|
this.ws.on('message', this.message);
|
|
|
|
|
this.ws.on('close', this.error);
|
|
|
|
|
this.ws.on('open', () => {
|
|
|
|
|
resolve(true);
|
2019-07-05 12:01:38 +02:00
|
|
|
this.connected(this.name + ' connector connected');
|
2019-07-05 00:09:59 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
} catch(error) {
|
2019-07-09 02:46:08 +02:00
|
|
|
this.error(error);
|
2019-07-05 00:09:59 +02:00
|
|
|
resolve(false);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
subscribe = (input) =>
|
|
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
|
try {
|
|
|
|
|
this.ws.send(JSON.stringify({
|
|
|
|
|
type: "ris_subscribe",
|
2019-07-09 02:46:08 +02:00
|
|
|
data: this.params.subscription
|
2019-07-05 00:09:59 +02:00
|
|
|
}));
|
|
|
|
|
resolve(true);
|
|
|
|
|
} catch(error) {
|
2019-07-09 02:46:08 +02:00
|
|
|
this.error(error);
|
2019-07-05 00:09:59 +02:00
|
|
|
resolve(false);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2019-07-05 12:01:38 +02:00
|
|
|
static transform = (message) => {
|
|
|
|
|
if (message.type === 'ris_message') {
|
|
|
|
|
message = message.data;
|
|
|
|
|
const components = [];
|
|
|
|
|
const announcements = message["announcements"] || [];
|
|
|
|
|
const withdrawals = message["withdrawals"] || [];
|
|
|
|
|
const peer = message["peer"];
|
|
|
|
|
const path = message["path"];
|
2019-07-03 17:56:07 +02:00
|
|
|
|
2019-07-09 02:46:08 +02:00
|
|
|
for (let announcement of announcements) {
|
2019-07-05 12:01:38 +02:00
|
|
|
const nextHop = announcement["next_hop"];
|
|
|
|
|
const prefixes = announcement["prefixes"] || [];
|
2019-07-03 17:56:07 +02:00
|
|
|
|
2019-07-09 02:46:08 +02:00
|
|
|
for (let prefix of prefixes) {
|
2019-07-05 12:01:38 +02:00
|
|
|
components.push({
|
|
|
|
|
type: "announcement",
|
|
|
|
|
prefix,
|
|
|
|
|
peer,
|
|
|
|
|
path,
|
|
|
|
|
originAs: path[path.length - 1],
|
|
|
|
|
nextHop
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-03 17:56:07 +02:00
|
|
|
|
2019-07-09 02:46:08 +02:00
|
|
|
for (let prefix of withdrawals) {
|
2019-07-03 17:56:07 +02:00
|
|
|
components.push({
|
2019-07-05 12:01:38 +02:00
|
|
|
type: "withdrawal",
|
2019-07-03 17:56:07 +02:00
|
|
|
prefix,
|
2019-07-05 12:01:38 +02:00
|
|
|
peer
|
2019-07-03 17:56:07 +02:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-05 12:01:38 +02:00
|
|
|
return components;
|
2019-07-03 17:56:07 +02:00
|
|
|
}
|
2019-07-09 02:46:08 +02:00
|
|
|
}
|
|
|
|
|
};
|