1
0
mirror of https://github.com/nttgin/BGPalerter.git synced 2024-05-19 06:50:08 +00:00
Files
nttgin-BGPalerter/connectors/connectorRIS.js

164 lines
5.6 KiB
JavaScript
Raw Normal View History

2019-07-10 18:29:01 +02:00
/*
* BSD 3-Clause License
*
* Copyright (c) 2019, NTT Ltd.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
2019-07-03 17:56:07 +02:00
import WebSocket from "ws";
2019-07-05 00:09:59 +02:00
import Connector from "./connector";
import { AS, Path } from "../model";
2019-07-03 17:56:07 +02:00
export default class ConnectorRIS extends Connector{
constructor(name, params, env) {
super(name, params, env);
2019-07-05 00:09:59 +02:00
this.ws = null;
2019-08-18 01:02:45 +02:00
this.subscription = null;
this.pingTimer = 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);
2019-08-18 01:02:45 +02:00
this.pingTimer = setInterval(() => {
this.ws.ping(() => {})
}, 5000);
2019-08-18 16:05:53 +02:00
this.ws.on('message', this._message);
2019-08-18 01:02:45 +02:00
this.ws.on('close', this.close);
2019-08-18 16:05:53 +02:00
this.ws.on('error', this._error);
2019-07-05 00:09:59 +02:00
this.ws.on('open', () => {
resolve(true);
2019-08-18 16:05:53 +02:00
this._connect(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);
}
});
2019-08-18 01:02:45 +02:00
close = (error) => {
2019-08-18 16:05:53 +02:00
this._disconnect(error);
2019-08-18 01:02:45 +02:00
clearInterval(this.pingTimer);
2019-08-18 16:05:53 +02:00
setTimeout(() => {
try {
this.ws.terminate();
} catch(e) {
}
this.connect()
.then(() => this.subscribe(this.subscription));
}, 5000);
2019-08-18 01:02:45 +02:00
};
2019-07-10 18:07:55 +02:00
_subscribeToAll = (input) => {
console.log("Subscribing to everything");
this.ws.send(JSON.stringify({
type: "ris_subscribe",
data: this.params.subscription
}));
};
_subscribeToPrefixes = (input) => {
2019-09-14 20:57:59 +02:00
const monitoredPrefixes = input.getMonitoredLessSpecifics().map(item => item.prefix);
2019-07-10 18:07:55 +02:00
const params = JSON.parse(JSON.stringify(this.params.subscription));
for (let prefix of monitoredPrefixes){
params.prefix = prefix;
console.log("Subscribing to:", prefix);
this.ws.send(JSON.stringify({
type: "ris_subscribe",
data: params
}));
}
};
2019-07-05 00:09:59 +02:00
subscribe = (input) =>
new Promise((resolve, reject) => {
2019-08-18 01:02:45 +02:00
this.subscription = input;
2019-07-05 00:09:59 +02:00
try {
(this.params.carefulSubscription) ?
2019-07-10 18:07:55 +02:00
this._subscribeToPrefixes(input) :
this._subscribeToAll(input);
2019-07-05 00:09:59 +02:00
resolve(true);
} catch(error) {
2019-08-18 16:05:53 +02:00
this._error(error);
2019-07-05 00:09:59 +02:00
resolve(false);
}
});
static transform = (message) => {
if (message.type === 'ris_message') {
message = message.data;
const components = [];
const announcements = message["announcements"] || [];
const aggregator = message["aggregator"] || null;
const withdrawals = message["withdrawals"] || [];
const peer = message["peer"];
let path, originAS;
if (message["path"] && message["path"].length) {
path = new Path(message["path"].map(i => new AS(i)));
originAS = path.getLast();
}
2019-07-03 17:56:07 +02:00
2019-07-09 02:46:08 +02:00
for (let announcement of announcements) {
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) {
components.push({
type: "announcement",
prefix,
peer,
path,
originAS,
nextHop,
aggregator
})
}
}
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({
type: "withdrawal",
2019-07-03 17:56:07 +02:00
prefix,
peer
2019-07-03 17:56:07 +02:00
})
}
return components;
2019-07-03 17:56:07 +02:00
}
2019-07-09 02:46:08 +02:00
}
};