Skip to content

Commit

Permalink
introduced error logs on for non standard iso codes
Browse files Browse the repository at this point in the history
  • Loading branch information
massimocandela committed Jun 30, 2021
1 parent 337d23c commit 6caaef5
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 10 deletions.
22 changes: 22 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
"cli-progress": "^3.9.0",
"ip-address": "^8.0.0",
"ip-sub": "^1.0.25",
"iso-3166-1": "^2.0.1",
"iso-3166-2": "^1.0.0",
"md5": "^2.3.0",
"moment": "^2.29.1",
"readline": "^1.3.0",
Expand Down
34 changes: 34 additions & 0 deletions src/csvParser.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import ipUtils from "ip-sub";
import iso3166a from "iso-3166-1";
import iso3166b from "iso-3166-2";

const checkPrefix = (prefix) => {
if (!prefix.includes("/")) {
Expand All @@ -17,8 +19,40 @@ class Geofeed {
this.zip = zip || null;
this.city = city;
this.valid = true;
};

validate = () => {
const out = [];
const validCountry = !this.country || iso3166a.whereAlpha2(this.country.toLowerCase());
const validRegion = !this.region || iso3166b.subdivision(this.region);
const validCombination = !this.country || !this.region || !!validCountry && !!validRegion && validRegion.countryCode === this.country;

if (!validCountry) {
out.push("Not valid Country Code (iso-3166-1)");
}

if (!validRegion) {
out.push("Not valid Subdivision Code (iso-3166-2)");
}

if (validCountry && validRegion && !validCombination) {
out.push("The Subdivision is not inside the Country");
}

return out;
};

toString = () => {
return [
this.prefix,
this.country || "",
this.region || "",
this.city || "",
this.zip || ""
].join(",");
}
}

export default class CsvParser {

parse = (inetnum, content) => {
Expand Down
35 changes: 26 additions & 9 deletions src/finder.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,19 +134,29 @@ export default class Finder {
return [].concat.apply([], out);
})
.then(data => {
if (!this.params.includeZip) {
data.forEach(i => i.zip = null);
}
if (!this.params.includeZip) {
data.forEach(i => i.zip = null);
}

return data;
return data;
});
};

validateGeofeeds = (geofeeds) => {
return geofeeds
.filter(geofeed => {
return geofeed && !!geofeed.inetnum && !!geofeed.prefix &&
(ipUtils.isEqualPrefix(geofeed.inetnum, geofeed.prefix) || ipUtils.isSubnet(geofeed.inetnum, geofeed.prefix));
const errors = geofeed.validate();

if (errors.length > 0) {
console.log(`Error: ${geofeed} ${errors.join(", ")}`);
}

if (this.params.keepNonIso || errors.length === 0) {
return geofeed && !!geofeed.inetnum && !!geofeed.prefix &&
(ipUtils.isEqualPrefix(geofeed.inetnum, geofeed.prefix) || ipUtils.isSubnet(geofeed.inetnum, geofeed.prefix));
}

return false;
});

};
Expand Down Expand Up @@ -249,14 +259,21 @@ export default class Finder {
}
};

getInetnum = (prefix) =>
new Promise((resolve, reject) => {
webWhois.lookup(prefix, (error, data) => {
_whois = (prefix, server) => {
return new Promise((resolve, reject) => {
webWhois.lookup(prefix, { follow: 4, verbose: true }, (error, data) => {
if (error) {
reject(error)
} else {
resolve(data);
}
})
});
}

getInetnum = (prefix) =>
this._whois(prefix)
.then(list => {
return list.map(i => i.data).join("\n")
});
}
7 changes: 6 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import yargs from 'yargs';

const toGeofeed = (geofeedsObjects) => {
return geofeedsObjects
.map(g => `${g.prefix || ""},${g.country || ""},${g.region || ""},${g.city || ""},${g.zip || ""},`)
// .map(g => `${g.prefix || ""},${g.country || ""},${g.region || ""},${g.city || ""},${g.zip || ""},`)
.join("\n");
};

Expand Down Expand Up @@ -34,6 +34,10 @@ const params = yargs
// .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('i', 'include')
.nargs('i', 1)
.default('i', 'ripe,apnic,lacnic,afrinic,arin')
Expand All @@ -48,6 +52,7 @@ const options = {
defaultCacheDays: 7,
arinBulk: params.b,
includeZip: params.z || false,
keepNonIso: params.k || false,
include: ((params.i) ? params.i : "ripe,apnic,lacnic,afrinic,arin").split(","),
output: params.o || "result.csv",
test: params.t || null,
Expand Down

0 comments on commit 6caaef5

Please sign in to comment.