UNPKG

rpki-validator

Version:

An RPKI validator that works also in the browser.

176 lines (175 loc) 10.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _brembo = _interopRequireDefault(require("brembo")); var _Connector2 = _interopRequireDefault(require("./Connector")); var _advancedStatsParser = _interopRequireDefault(require("../util/advancedStatsParser")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; } function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; } function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); } function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); } function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); } function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } var RpkiClientConnector = exports["default"] = /*#__PURE__*/function (_Connector) { function RpkiClientConnector(options) { var _this$options$host; var _this; _classCallCheck(this, RpkiClientConnector); _this = _callSuper(this, RpkiClientConnector, [options]); _defineProperty(_this, "getAdvancedStats", function () { if (!_this._advancedStatsPromise) { _this._advancedStatsTimer = setInterval(function () { _this._advancedStatsPromise = _this._setAdvancedStats(); }, _this.advancedStatsRefreshRateMinutes * 60 * 1000); _this._advancedStatsPromise = _this._setAdvancedStats(); } return _this._advancedStatsPromise["catch"](console.log).then(function () { return _this.index; }); }); _defineProperty(_this, "fetchAndParseGz", function (url) { var headers = _objectSpread({ "User-Agent": _this.clientId }, _this.dumpModified ? { "If-Modified-Since": _this.dumpModified.toUTCString() } : {}); return fetch(url, { headers: headers }).then(function (res) { var newLastModified = new Date(res.headers.get("last-modified")); if (_this.dumpModified && newLastModified.toUTCString() === _this.dumpModified.toUTCString()) { return Promise.resolve(); } else { _this.dumpModified = newLastModified; var stream = res.body.pipeThrough(new DecompressionStream("gzip")).pipeThrough(new TextDecoderStream()); var reader = stream.getReader(); _this.index = new _advancedStatsParser["default"](); var buffer = ""; var _process = function process(result) { if (result.done) { buffer.split("\n").forEach(function (l) { if (l.trim().length > 1) { try { _this.index.add(JSON.parse(l)); } catch (_unused) {} } }); return _this.index; } buffer += result.value; var parts = buffer.split("\n"); buffer = parts.pop(); parts.forEach(function (l) { if (l.trim().length > 1) { try { _this.index.add(JSON.parse(l)); } catch (_unused2) {} } }); return reader.read().then(_process); }; return reader.read().then(_process); } }); }); _defineProperty(_this, "_setAdvancedStats", function () { var url = _brembo["default"].build(_this.host, { path: ["dump.json.gz"] }); return _this.fetchAndParseGz(url); }); _defineProperty(_this, "_applyRpkiClientMetadata", function () { var metadata = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; _this.metadata = { buildmachine: metadata === null || metadata === void 0 ? void 0 : metadata.buildmachine, buildtime: metadata !== null && metadata !== void 0 && metadata.buildtime ? new Date(metadata === null || metadata === void 0 ? void 0 : metadata.buildtime).toISOString() : null, // used for if-modified-since elapsedtime: metadata === null || metadata === void 0 ? void 0 : metadata.elapsedtime }; }); _defineProperty(_this, "getVRPs", function () { var _this$metadata; var url = _brembo["default"].build(_this.host, { path: ["vrps.json"], params: { client: _this.clientId } }); return _this.axios({ method: "get", url: url, responseType: "json", headers: _objectSpread({ "User-Agent": _this.clientId, "Accept-Encoding": "gzip" }, (_this$metadata = _this.metadata) !== null && _this$metadata !== void 0 && _this$metadata.lastModified ? { "If-Modified-Since": new Date(_this.metadata.lastModified).toUTCString() } : {}) }).then(function (data) { if (data && data.data && data.data.roas) { var _data$data; var roas = data.data.roas; var metadata = (_data$data = data.data) === null || _data$data === void 0 ? void 0 : _data$data.metadata; var headers = data.headers; _this._applyRpkiClientMetadata(metadata); _this.metadata.lastModified = headers["last-modified"] ? new Date(headers["last-modified"]).toISOString() : null; return roas.map(function (roa) { var _roa$ta; return { prefix: roa.prefix, maxLength: roa.maxLength, asn: parseInt(roa.asn.toString().replace("AS", "")), ta: _this.toStandardTa((_roa$ta = roa.ta) === null || _roa$ta === void 0 ? void 0 : _roa$ta.toLowerCase()), expires: roa.expires || null }; }); } })["catch"](function (error) { var _error$response; if (((_error$response = error.response) === null || _error$response === void 0 ? void 0 : _error$response.status) !== 304) { return Promise.reject(error); } return null; }); }); _defineProperty(_this, "getExpiringElements", function (vrp, expires, now) { return _this.getAdvancedStats().then(function (index) { return index.getExpiring(vrp, expires, now); }); }); _defineProperty(_this, "destroy", function () { var _this$index; if (_this._advancedStatsTimer) { clearInterval(_this._advancedStatsTimer); _this._advancedStatsTimer = null; } if ((_this$index = _this.index) !== null && _this$index !== void 0 && _this$index.destroy) { _this.index.destroy(); } _this.index = null; _this._advancedStatsPromise = null; }); _this.index = null; _this._advancedStatsTimer = null; _this.host = (_this$options$host = _this.options.host) !== null && _this$options$host !== void 0 ? _this$options$host : "https://console.rpki-client.org"; _this.dumpModified = null; _this.minimumRefreshRateMinutes = 5; _this.advancedStatsRefreshRateMinutes = Math.max(10, _this.options.advancedStatsRefreshRateMinutes); return _this; } _inherits(RpkiClientConnector, _Connector); return _createClass(RpkiClientConnector); }(_Connector2["default"]);