From 2bf7454950e7525cc91dafa352dc9d20b7d7193d Mon Sep 17 00:00:00 2001 From: Alicia Sykes Date: Mon, 14 Aug 2023 22:34:38 +0100 Subject: [PATCH] Improved robustness of whois endpoint --- api/whois.js | 49 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 12 deletions(-) diff --git a/api/whois.js b/api/whois.js index 69e5674..9cac306 100644 --- a/api/whois.js +++ b/api/whois.js @@ -1,5 +1,6 @@ const net = require('net'); const psl = require('psl'); +const axios = require('axios'); const middleware = require('./_common/middleware'); const getBaseDomain = (url) => { @@ -44,18 +45,7 @@ const parseWhoisData = (data) => { return parsedData; }; -const fetchWhoisData = async (url) => { - if (!url.startsWith('http://') && !url.startsWith('https://')) { - url = 'http://' + url; - } - - let hostname; - try { - hostname = getBaseDomain(new URL(url).hostname); - } catch (error) { - throw new Error(`Unable to parse URL: ${error}`); - } - +const fetchFromInternic = async (hostname) => { return new Promise((resolve, reject) => { const client = net.createConnection({ port: 43, host: 'whois.internic.net' }, () => { client.write(hostname + '\r\n'); @@ -81,4 +71,39 @@ const fetchWhoisData = async (url) => { }); }; +const fetchFromMyAPI = async (hostname) => { + try { + const response = await axios.post('https://whois-api-zeta.vercel.app/', { + domain: hostname + }); + return response.data; + } catch (error) { + console.error('Error fetching data from your API:', error.message); + return null; + } +}; + +const fetchWhoisData = async (url) => { + if (!url.startsWith('http://') && !url.startsWith('https://')) { + url = 'http://' + url; + } + + let hostname; + try { + hostname = getBaseDomain(new URL(url).hostname); + } catch (error) { + throw new Error(`Unable to parse URL: ${error}`); + } + + const [internicData, whoisData] = await Promise.all([ + fetchFromInternic(hostname), + fetchFromMyAPI(hostname) + ]); + + return { + internicData, + whoisData + }; +}; + exports.handler = middleware(fetchWhoisData);