mirror of
https://github.com/Lissy93/web-check.git
synced 2025-06-19 03:16:45 +02:00
Fix parsing of whois records
This commit is contained in:
parent
8878062d2a
commit
9a5af64d1b
@ -1,14 +1,42 @@
|
|||||||
const net = require('net');
|
const net = require('net');
|
||||||
const { URL } = require('url');
|
// const { URL } = require('url');
|
||||||
|
|
||||||
|
const errorResponse = (message, statusCode = 444) => {
|
||||||
|
return {
|
||||||
|
statusCode: statusCode,
|
||||||
|
body: JSON.stringify({ error: message }),
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
const getBaseDomain = (url) => {
|
||||||
|
// Determine whether a protocol is present
|
||||||
|
let protocol = '';
|
||||||
|
if (url.startsWith('http://')) {
|
||||||
|
protocol = 'http://';
|
||||||
|
} else if (url.startsWith('https://')) {
|
||||||
|
protocol = 'https://';
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove protocol for domain parsing but keep it for final output
|
||||||
|
let noProtocolUrl = url.replace(protocol, '');
|
||||||
|
|
||||||
|
// Split on '.' and get the last two sections
|
||||||
|
const domainParts = noProtocolUrl.split('.');
|
||||||
|
|
||||||
|
// If there's more than one '.'
|
||||||
|
// then get only the last two parts to ignore subdomains
|
||||||
|
if (domainParts.length > 2) {
|
||||||
|
return protocol + domainParts.slice(-2).join('.');
|
||||||
|
} else {
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
exports.handler = async function(event, context) {
|
exports.handler = async function(event, context) {
|
||||||
let url = event.queryStringParameters.url;
|
let url = event.queryStringParameters.url;
|
||||||
|
|
||||||
if (!url) {
|
if (!url) {
|
||||||
return {
|
return errorResponse('URL query parameter is required.', 400);
|
||||||
statusCode: 400,
|
|
||||||
body: JSON.stringify({ message: 'A url query parameter is required' }),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!url.startsWith('http://') && !url.startsWith('https://')) {
|
if (!url.startsWith('http://') && !url.startsWith('https://')) {
|
||||||
@ -17,12 +45,9 @@ exports.handler = async function(event, context) {
|
|||||||
|
|
||||||
let hostname;
|
let hostname;
|
||||||
try {
|
try {
|
||||||
hostname = new URL(url).hostname;
|
hostname = getBaseDomain(new URL(url).hostname);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return {
|
return errorResponse(`Unable to parse URL: ${error}`, 400);
|
||||||
statusCode: 400,
|
|
||||||
body: JSON.stringify({ message: 'Invalid url provided' }),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
@ -43,18 +68,12 @@ exports.handler = async function(event, context) {
|
|||||||
body: JSON.stringify(parsedData),
|
body: JSON.stringify(parsedData),
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
resolve({
|
resolve(errorResponse(error.message));
|
||||||
statusCode: 500,
|
|
||||||
body: JSON.stringify({ error: error.message }),
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
client.on('error', (err) => {
|
client.on('error', (err) => {
|
||||||
resolve({
|
resolve(errorResponse(err.message, 500));
|
||||||
statusCode: 500,
|
|
||||||
body: JSON.stringify({ message: err.message }),
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user