UNPKG

rpki-validator

Version:

An RPKI validator that works also in the browser.

157 lines (156 loc) 10 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 _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } 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.setAdvancedStatsTimer) { _this.setAdvancedStatsTimer = setInterval(_this._setAdvancedStats, _this.advancedStatsRefreshRateMinutes * 60 * 1000); _this._advancedStatsPromise = _this._setAdvancedStats(); } if (_this.index) { return Promise.resolve(_this.index); } else { return _this._advancedStatsPromise.then(function () { return _this.index; }); } }); _defineProperty(_this, "_setAdvancedStats", function () { var url = _brembo["default"].build(_this.host, { path: ["dump.json"], params: { client: _this.clientId } }); var headers = {}; if (_this.dumpModified) { headers["If-Modified-Since"] = _this.dumpModified.toUTCString(); } return _this.axios({ method: "get", url: url, maxContentLength: Infinity, maxBodyLength: Infinity }).then(function (data) { if (data && data.data) { if (_this.options.verbose) { console.log("Loading new roa data"); } _this.dumpModified = new Date(data.headers["last-modified"]); _this.index = new _advancedStatsParser["default"](); var items = data.data.split("\n"); var _iterator = _createForOfIteratorHelper(items), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var item = _step.value; try { var trimmedItem = item.trim(); if (trimmedItem.length > 1) { _this.index.add(JSON.parse(trimmedItem)); } } catch (e) {} } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } } }); }); _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 } }); var headers = {}; if ((_this$metadata = _this.metadata) !== null && _this$metadata !== void 0 && _this$metadata.lastModified) { headers["If-Modified-Since"] = new Date(_this.metadata.lastModified).toUTCString(); } return _this.axios({ method: "get", url: url, headers: headers, responseType: "json" }).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) { return { prefix: roa.prefix, maxLength: roa.maxLength, asn: parseInt(roa.asn.toString().replace("AS", "")), ta: roa.ta, 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); }); }); _this.index = 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"]);