Suppress noise by excluding NIC values

This commit is contained in:
KaKi87 2023-12-14 11:08:49 +01:00
parent 96d442c505
commit 6492d66ed6
1 changed files with 73 additions and 45 deletions

View File

@ -2,6 +2,58 @@ import Joi from 'npm:joi@17.6.1';
import exec from '../lib/exec.js';
const whois = async ({
hostname,
format,
excludedValues = []
}) => {
const {
stdout,
stderr
} = await exec(
'whois',
hostname
);
let data;
switch(format){
case 'text': {
data = stdout;
break;
}
case 'json':
case 'nestedJson': {
data = {};
for(const line of stdout.split('\n')){
const [, key, value] = line.match(/^\s*([\w\s]+)[^\w]?\s+(.+)$/) || [];
if(key && value && !excludedValues.includes(value)){
if(format === 'json'){
if(!data[key])
data[key] = [];
data[key].push(value);
}
else if(format === 'nestedJson'){
const keys = key.split(/\s+/g);
let parentObject = data;
for(const key of keys){
if(!parentObject[key])
parentObject[key] = {};
parentObject = parentObject[key];
}
if(!parentObject.$)
parentObject.$ = [];
parentObject.$.push(value);
}
}
}
break;
}
}
return {
success: stdout && !stderr,
data
};
};
/**
* Get WHOIS info
* @path {GET} /whois/:hostname
@ -29,7 +81,10 @@ export default async fastify => {
'json',
'nestedJson'
)
.default('json')
.default('json'),
excludeNicValues: Joi
.boolean()
.default(true)
})
}
},
@ -38,54 +93,27 @@ export default async fastify => {
hostname
},
query: {
format
format,
excludeNicValues
}
}) => {
const {
stdout,
stderr
} = await exec(
'whois',
hostname
);
let data;
switch(format){
case 'text': {
data = stdout;
break;
}
case 'json':
case 'nestedJson': {
data = {};
for(const line of stdout.split('\n')){
const [, key, value] = line.match(/^\s*([\w\s]+)[^\w]?\s+(.+)$/) || [];
if(key && value){
if(format === 'json'){
if(!data[key])
data[key] = [];
data[key].push(value);
}
else if(format === 'nestedJson'){
const keys = key.split(/\s+/g);
let parentObject = data;
for(const key of keys){
if(!parentObject[key])
parentObject[key] = {};
parentObject = parentObject[key];
}
if(!parentObject.$)
parentObject.$ = [];
parentObject.$.push(value);
}
}
}
break;
const excludedValues = [];
if(excludeNicValues && Joi.string().domain().validate(hostname)){
const nicHostname = `nic.${hostname.split('.').slice(-1)[0]}`;
if(hostname !== nicHostname){
const nicWhois = await whois({
hostname: nicHostname,
format: 'json'
});
if(nicWhois.success)
excludedValues.push(...Object.values(nicWhois.data).flat());
}
}
return {
success: stdout && !stderr,
data
};
return whois({
hostname,
format,
excludedValues
});
}
);
};