UNPKG

rpki-validator

Version:

An RPKI validator that works also in the browser.

147 lines (144 loc) 10 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _RpkiClientConnector2 = _interopRequireDefault(require("./RpkiClientConnector")); var _brembo = _interopRequireDefault(require("brembo")); var _ExternalConnector = _interopRequireDefault(require("./ExternalConnector")); 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 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 api = "https://api.packetvis.com/v1/rpki/"; var PacketVisConnector = exports["default"] = /*#__PURE__*/function (_RpkiClientConnector) { function PacketVisConnector(options) { var _options$host, _options$host2; var _this; _classCallCheck(this, PacketVisConnector); _this = _callSuper(this, PacketVisConnector, [_objectSpread(_objectSpread({}, options), {}, { verbose: true, host: _brembo["default"].build((_options$host = options.host) !== null && _options$host !== void 0 ? _options$host : api, { path: ["static"] }) })]); _defineProperty(_this, "_setAdvancedStats", function () { try { var fs = require("fs"); // Load only for node var file = ".cache/dump.json"; if (fs.existsSync(file)) { var stats = fs.statSync(file); if (_this.cacheModified.dump < stats.mtime) { // Newer than last time var payload = fs.readFileSync(file, "utf8"); _this.index = new _advancedStatsParser["default"](); var items = payload.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(); } _this.cacheModified.dump = stats.mtime; } } else { throw new Error("RPKI cache missing ".concat(file)); } } catch (error) { return Promise.reject(error); } }); _defineProperty(_this, "getVRPs", function () { try { var fs = require("fs"); // Load only for node var file = ".cache/vrps.json"; if (fs.existsSync(file)) { var stats = fs.statSync(file); if (_this.cacheModified.vrps < stats.mtime) { if (new Date() - stats.mtime < 30 * 60 * 1000) { // Newer than 30 min var payload = JSON.parse(fs.readFileSync(file, "utf8")); _this.cacheConnector.setVRPs(payload.roas); _this._applyRpkiClientMetadata(payload === null || payload === void 0 ? void 0 : payload.metadata); _this.metadata.lastModified = stats.mtime.toISOString(); _this.cacheModified.vrps = stats.mtime; return _this.cacheConnector.getVRPs(); } else { throw new Error("RPKI cache too old ".concat(file)); } } else { return Promise.reject(); } } else { throw new Error("RPKI cache missing ".concat(file)); } } catch (error) { return Promise.reject(error); } }); _defineProperty(_this, "getExpiringElements", function (vrp, expires, now) { var _this$options; if (_this.index) { return Promise.resolve(_this.index.getExpiring(vrp, expires, now)); } else if (!((_this$options = _this.options) !== null && _this$options !== void 0 && _this$options.localConnector)) { var url = _brembo["default"].build(_this.vrpHost, { path: ["meta", "expiring"], params: { prefix: vrp.prefix, asn: vrp.asn, maxlength: vrp.maxLength, expires: expires, client: _this.clientId } }); return _this.axios({ method: "get", url: url }).then(function (_ref) { var data = _ref.data; return data.data; }); } else { return Promise.reject("Cannot load index"); } }); _this.minimumRefreshRateMinutes = 1; _this.vrpHost = (_options$host2 = options.host) !== null && _options$host2 !== void 0 ? _options$host2 : api; _this.cacheModified = { vrps: 0, dump: 0 }; _this.cacheConnector = new _ExternalConnector["default"]({}); return _this; } _inherits(PacketVisConnector, _RpkiClientConnector); return _createClass(PacketVisConnector); }(_RpkiClientConnector2["default"]);