parse whois data dfferent

This commit is contained in:
HF 2022-08-07 11:07:05 +02:00
parent b857ddba48
commit a2147eef8c
3 changed files with 18 additions and 7 deletions

View File

@ -208,7 +208,7 @@ export default class MString {
cIter += 3; cIter += 3;
for (; cIter < this.txt.length for (; cIter < this.txt.length
&& !MString.isWhiteSpace(this.txt[cIter]) && !MString.isWhiteSpace(this.txt[cIter])
&& (!enclosure || this.txt[cIter] !== ')'); cIter += 1 && (!enclosure || this.txt[cIter] !== ')'); cIter += 1
); );
if (cIter < this.iter + 4) { if (cIter < this.iter + 4) {
return null; return null;

View File

@ -119,6 +119,9 @@ export function getIPv6Subnet(ip) {
* @return [start, end] with numerical IPs (32bit integer) * @return [start, end] with numerical IPs (32bit integer)
*/ */
function ip4RangeStrToRangeNum(range) { function ip4RangeStrToRangeNum(range) {
if (!range) {
return null;
}
const [start, end] = range.split('-') const [start, end] = range.split('-')
.map(ip4ToNum); .map(ip4ToNum);
if (!start || !end || start > end) { if (!start || !end || start > end) {

View File

@ -18,13 +18,13 @@ function cIDRofWhois(ip, whoisData) {
return whoisData.inet6num return whoisData.inet6num
|| (whoisData.range && !whoisData.range.includes('-') && whoisData.range) || (whoisData.range && !whoisData.range.includes('-') && whoisData.range)
|| whoisData.route || whoisData.route
|| 'N/A'; || null;
} }
const { range } = whoisData; const { range } = whoisData;
if (range.includes('/') && !range.includes('-')) { if (range && range.includes('/') && !range.includes('-')) {
return range; return range;
} }
return ip4InRangeToCIDR(ip, range) || 'N/A'; return ip4InRangeToCIDR(ip, range) || null;
} }
/* /*
@ -63,7 +63,7 @@ function parseWhois(ip, whoisData) {
return { return {
ip, ip,
country: countryFromWhois(whoisData), country: countryFromWhois(whoisData),
cidr: cIDRofWhois(ip, whoisData), cidr: cIDRofWhois(ip, whoisData) || 'N/A',
org: orgFromWhois(whoisData), org: orgFromWhois(whoisData),
descr: whoisData.descr || 'N/A', descr: whoisData.descr || 'N/A',
asn: whoisData.asn || 'N/A', asn: whoisData.asn || 'N/A',
@ -71,7 +71,7 @@ function parseWhois(ip, whoisData) {
} }
async function whois(ip) { async function whois(ip) {
let whoisData = await whoiser.ip(ip); const whoisData = await whoiser.ip(ip);
if (whoisData.ReferralServer) { if (whoisData.ReferralServer) {
let referral = whoisData.ReferralServer; let referral = whoisData.ReferralServer;
const prot = referral.indexOf('://'); const prot = referral.indexOf('://');
@ -79,9 +79,17 @@ async function whois(ip) {
referral = referral.slice(prot + 3); referral = referral.slice(prot + 3);
} }
try { try {
whoisData = await whoiser.ip(ip, { /*
* if referral whois server produces any error
* fallback to initial one
*/
const refWhoisData = await whoiser.ip(ip, {
host: referral, host: referral,
}); });
const refParsedData = parseWhois(ip, refWhoisData);
if (refParsedData.cidr !== 'N/A') {
return refParsedData;
}
} catch { } catch {
// nothing // nothing
} }