geofeed-finder
Version:
A tool to find geofeed files in rpsl and parse them correctly according to draft-ietf-opsawg-finding-geofeeds
101 lines (100 loc) • 6.83 kB
JavaScript
;
var _finder = _interopRequireDefault(require("./finder"));
var _fs = _interopRequireDefault(require("fs"));
var _yargs = _interopRequireDefault(require("yargs"));
var _fastFileLogger = _interopRequireDefault(require("fast-file-logger"));
var _params$i;
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
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; }
var toGeofeed = function toGeofeed(geofeedsObjects) {
return geofeedsObjects.join("\n");
};
var logger = new _fastFileLogger["default"]({
logRotatePattern: "YYYY-MM-DD",
filename: "error-%DATE%.log",
symLink: false,
directory: "./logs",
maxRetainedFiles: 100,
maxFileSizeMB: 100,
compressOnRotation: false,
label: "geofeed-finder",
useUTC: true,
format: function format(_ref) {
var data = _ref.data,
timestamp = _ref.timestamp;
return "".concat(timestamp, " ").concat(data);
}
});
var epilog = "Copyright (c) 2020-".concat(new Date().getFullYear(), ", Massimo Candela (https://massimocandela.com)");
var params = _yargs["default"].usage("Usage: $0 <command> [options]").command("$0", "Run Geofeed finder (default)", function () {
_yargs["default"].alias("v", "version").nargs("v", 0).describe("v", "Show version number").alias("a", "af").nargs("a", 1)["default"]("a", "4,6").describe("a", "Address family").alias("o", "output").nargs("o", 1)["default"]("o", "result.csv").describe("o", "Output file").alias("t", "test").nargs("t", 1).describe("t", "Test specific inetnum using RDAP").alias("c", "cache-whois").nargs("c", 1)["default"]("c", 3).describe("c", "Number of days whois cache validity").alias("g", "cache-geofeed").nargs("g", 1)["default"]("g", 3).describe("g", "Number of days geofeed file cache validity").alias("l", "cache-location").nargs("l", 1)["default"]("l", ".cache/").describe("l", "Cache directory location").alias("s", "silent").nargs("s", 0).describe("s", "Silent mode, don't print errors").alias("u", "keep-invalid-subdivisions").nargs("u", 0).describe("u", "Keep invalid subdivisions (accept invalid ISO regions/subdivisions)").alias("r", "remove-invalid-subdivisions").nargs("r", 0).describe("r", "Remove invalid subdivisions but keep the rest of the geofeed if valid").alias("b", "arin-bulk").nargs("b", 0).describe("b", "Use bulk whois data for ARIN: https://www.arin.net/reference/research/bulkwhois/").alias("p", "arin-skip-suballocations").nargs("p", 0).describe("p", "Do not fetch ARIN's sub allocations. You will save considerable time but have a potentially partial output.").alias("q", "detect-suballocations-locally").nargs("q", 0).describe("q", "Detect ARIN's sub allocations locally instead of downloading a dump file.").alias("z", "include-zip").nargs("z", 0).describe("z", "Zip codes are deprecated in geofeed and by default are excluded from the output.").alias("k", "keep-non-iso").nargs("k", 0).describe("k", "Keep entries with invalid ISO codes").alias("d", "download-timeout").nargs("d", 1).describe("d", "Interrupt downloading a geofeed file after seconds").alias("x", "non-strict").nargs("x", 0).describe("x", "Accept malformed remarks").alias("i", "include").nargs("i", 1)["default"]("i", "ripe,apnic,lacnic,afrinic,arin").describe("i", "Include RIRs (comma-separated list)");
}).help("h").alias("h", "help").epilog(epilog).argv;
var options = {
logger: logger,
cacheDir: params.l || ".cache/",
whoisCacheDays: parseInt(params.c),
daysWhoisSuballocationsCache: 30,
// Cannot be less than 7
compileSuballocationLocally: !!params.q,
skipSuballocations: !!params.p,
geofeedCacheDays: parseInt(params.g),
arinBulk: params.b,
af: params.a.toString().split(",").map(function (i) {
return parseInt(i);
}),
includeZip: !!params.z,
silent: !!params.s,
keepNonIso: !!params.k,
keepInvalidSubdivisions: !!params.u,
removeInvalidSubdivisions: !!params.r,
include: ((_params$i = params.i) !== null && _params$i !== void 0 ? _params$i : "ripe,apnic,lacnic,afrinic,arin").split(","),
output: params.o || "result.csv",
test: params.t || null,
strictGeofeedRemarks: !params.x,
exitOnError: true,
downloadTimeout: params.d || 10 // 0 is not a valid value
};
console.log("geofeed-finder - ".concat(epilog));
new _finder["default"](options).getGeofeeds().then(function (data) {
return new Promise(function (resolve, reject) {
try {
if (!!options.test) {
if (/<a|<div|<span|<style|<link/gi.test(data)) {
console.log("Error: is not CSV but HTML, stop with this nonsense!");
} else {
console.log(toGeofeed(data));
}
resolve();
} else {
_fs["default"].writeFileSync(options.output, "");
var out = _fs["default"].createWriteStream(options.output, {
flags: "a"
});
var _iterator = _createForOfIteratorHelper(data !== null && data !== void 0 ? data : []),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var line = _step.value;
out.write(line + "\n", "UTF8");
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
out.end();
out.on("finish", function () {
console.log("Done! See ".concat(options.output));
resolve();
});
out.on("error", reject);
}
} catch (error) {
reject(error);
}
});
})["catch"](function (error) {
logger.log(error.message);
});