Skip to content

Commit

Permalink
Fix parsing of whois records
Browse files Browse the repository at this point in the history
  • Loading branch information
Lissy93 committed Jul 18, 2023
1 parent 8878062 commit 9a5af64
Showing 1 changed file with 37 additions and 18 deletions.
55 changes: 37 additions & 18 deletions server/lambda/whois-lookup.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,42 @@
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) {
let url = event.queryStringParameters.url;

if (!url) {
return {
statusCode: 400,
body: JSON.stringify({ message: 'A url query parameter is required' }),
};
return errorResponse('URL query parameter is required.', 400);
}

if (!url.startsWith('http://') && !url.startsWith('https://')) {
Expand All @@ -17,12 +45,9 @@ exports.handler = async function(event, context) {

let hostname;
try {
hostname = new URL(url).hostname;
hostname = getBaseDomain(new URL(url).hostname);
} catch (error) {
return {
statusCode: 400,
body: JSON.stringify({ message: 'Invalid url provided' }),
};
return errorResponse(`Unable to parse URL: ${error}`, 400);
}

return new Promise((resolve, reject) => {
Expand All @@ -43,18 +68,12 @@ exports.handler = async function(event, context) {
body: JSON.stringify(parsedData),
});
} catch (error) {
resolve({
statusCode: 500,
body: JSON.stringify({ error: error.message }),
});
resolve(errorResponse(error.message));
}
});

client.on('error', (err) => {
resolve({
statusCode: 500,
body: JSON.stringify({ message: err.message }),
});
resolve(errorResponse(err.message, 500));
});
});
};
Expand Down

0 comments on commit 9a5af64

Please sign in to comment.