mirror of
https://github.com/nttgin/BGPalerter.git
synced 2024-05-19 06:50:08 +00:00
added argument p and pf to generate complete prefix list from simple prefix list (#43)
* added argument p and pf to generate complete prefix list from simple prefix list * updated documentation with -p and -pf arguments
This commit is contained in:
@@ -14,10 +14,12 @@ Below the list of possible parameters. **Remember to prepend them with a `--` in
|
||||
|
||||
| Parameter | Description | Expected format | Example | Required |
|
||||
|---|---|---|---|---|
|
||||
| -a | The AS number(s) you want to generate the list for | A comma-separated list of integers | 2914,3333 | Yes |
|
||||
| -o | The YAML output file | A string ending in ".yml" | prefixes.yml | Yes
|
||||
| -o | The YAML output file | A string ending in ".yml" | prefixes.yml | Yes |
|
||||
| -a | The AS number(s) you want to generate the list for | A comma-separated list of integers | 2914,3333 | No (one among -a, -p, -pf is required) |
|
||||
| -e | Prefixes to exclude from the list | A comma-separated list of prefixes | 165.254.255.0/24,192.147.168.0/24 | No |
|
||||
| -i | Avoid monitoring delegated prefixes. If a more specific prefix is found and it results announced by an AS different from the one declared in -a, then set `ignore: true` and `ignoreMorespecifics: true` | Nothing | | No
|
||||
| -p | Prefixes for which the list will be generated | A comma-separated list of prefixes | 165.254.255.0/24,192.147.168.0/24 | No (one among -a, -p, -pf is required) |
|
||||
| -pf | A file containing the prefixes for which the list will be generated | A text file having a prefix for each line | prefixes.txt | No (one among -a, -p, -pf is required) |
|
||||
|
||||
|
||||
## <a name="prefixes-fields"></a>Prefixes list fields
|
||||
|
@@ -4,20 +4,22 @@ import yaml from "js-yaml";
|
||||
import fs from "fs";
|
||||
const batchPromises = require('batch-promises');
|
||||
|
||||
module.exports = function generatePrefixes(asns, outputFile, exclude, excludeDelegated) {
|
||||
module.exports = function generatePrefixes(asnList, outputFile, exclude, excludeDelegated, prefixes) {
|
||||
const generateList = {};
|
||||
let someNotValidatedPrefixes = false;
|
||||
|
||||
if (!asns) {
|
||||
throw new Error("One or more comma-separated AS numbers have to be specified");
|
||||
if (!asnList && !prefixes) {
|
||||
throw new Error("You need to specify at least an AS number or a list of prefixes.");
|
||||
}
|
||||
|
||||
if (asnList && prefixes) {
|
||||
throw new Error("You can specify an AS number or a list of prefixes, not both.");
|
||||
}
|
||||
|
||||
if (!outputFile) {
|
||||
throw new Error("Output file not specified");
|
||||
}
|
||||
|
||||
const asnList = asns.split(",");
|
||||
|
||||
const getMultipleOrigins = (prefix) => {
|
||||
const url = brembo.build("https://stat.ripe.net", {
|
||||
path: ["data", "prefix-overview", "data.json"],
|
||||
@@ -73,7 +75,6 @@ module.exports = function generatePrefixes(asns, outputFile, exclude, excludeDel
|
||||
|
||||
};
|
||||
|
||||
|
||||
const generateRule = (prefix, asn, ignoreMorespecifics, description, excludeDelegated) =>
|
||||
getMultipleOrigins(prefix)
|
||||
.then(asns => {
|
||||
@@ -152,8 +153,17 @@ module.exports = function generatePrefixes(asns, outputFile, exclude, excludeDel
|
||||
})
|
||||
};
|
||||
|
||||
return Promise
|
||||
.all(asnList.map(getAnnouncedPrefixes))
|
||||
const getBaseRules = () => {
|
||||
if (prefixes) {
|
||||
return Promise
|
||||
.all(prefixes.map(p => generateRule(p, null, false, null, false)))
|
||||
.then(() => prefixes);
|
||||
} else {
|
||||
return Promise.all(asnList.map(getAnnouncedPrefixes));
|
||||
}
|
||||
};
|
||||
|
||||
return getBaseRules()
|
||||
.then(items => [].concat.apply([], items))
|
||||
.then(prefixes => {
|
||||
return batchPromises(20, prefixes, prefix => {
|
||||
@@ -179,7 +189,7 @@ module.exports = function generatePrefixes(asns, outputFile, exclude, excludeDel
|
||||
fs.writeFileSync(outputFile, yamlContent);
|
||||
|
||||
if (someNotValidatedPrefixes) {
|
||||
console.log("WARNING: The generated configuration is a snapshot of what is currently announced by " + asns + " but some of the prefixes don't have ROA objects associated. Please, verify the config file by hand!");
|
||||
console.log("WARNING: The generated configuration is a snapshot of what is currently announced. Some of the prefixes don't have ROA objects associated or are RPKI invalid. Please, verify the config file by hand!");
|
||||
}
|
||||
console.log("Done!");
|
||||
})
|
||||
|
30
index.js
30
index.js
@@ -53,11 +53,19 @@ const params = yargs
|
||||
.nargs('e', 1)
|
||||
.describe('e', 'Prefixes to exclude')
|
||||
|
||||
.alias('p', 'prefixes')
|
||||
.nargs('p', 1)
|
||||
.describe('p', 'Prefixes to include')
|
||||
|
||||
.alias('pf', 'prefixes-file')
|
||||
.nargs('pf', 1)
|
||||
.describe('pf', 'File containing the prefixes to include')
|
||||
|
||||
.alias('i', 'ignore-delegated')
|
||||
.nargs('i', 0)
|
||||
.describe('i', 'Ignore delegated prefixes')
|
||||
|
||||
.demandOption(['o', 'a'])
|
||||
.demandOption(['o']);
|
||||
})
|
||||
.example('$0 generate -a 2914 -o prefixes.yml', 'Generate prefixes for AS2914')
|
||||
|
||||
@@ -70,12 +78,28 @@ const params = yargs
|
||||
switch(params._[0]) {
|
||||
case "generate":
|
||||
const generatePrefixes = require("./generatePrefixesList");
|
||||
let prefixes = null;
|
||||
if (params.p && params.pf) {
|
||||
throw new Error("The argument -p is not compatible with the argument -pf");
|
||||
} else if (params.p) {
|
||||
prefixes = params.p.split(",");
|
||||
} else if (params.pf) {
|
||||
const fs = require("fs");
|
||||
if (fs.existsSync(params.pf)) {
|
||||
prefixes = fs.readFileSync(params.pf, 'utf8').split(/\r?\n/).filter(i => i && true);
|
||||
} else {
|
||||
throw new Error("The prefix list file (-pf) is not readable");
|
||||
}
|
||||
}
|
||||
|
||||
generatePrefixes(
|
||||
params.a.toString(),
|
||||
(params.a) ? params.a.toString().split(",") : null,
|
||||
params.o,
|
||||
(params.e || "").split(","),
|
||||
params.i || false
|
||||
params.i || false,
|
||||
prefixes
|
||||
);
|
||||
|
||||
break;
|
||||
|
||||
default: // Run monitor
|
||||
|
Reference in New Issue
Block a user