mirror of
https://github.com/Lissy93/web-check.git
synced 2025-06-20 11:47:42 +02:00
Improved robustness of whois endpoint
This commit is contained in:
parent
8d4c09ffd9
commit
2bf7454950
49
api/whois.js
49
api/whois.js
@ -1,5 +1,6 @@
|
|||||||
const net = require('net');
|
const net = require('net');
|
||||||
const psl = require('psl');
|
const psl = require('psl');
|
||||||
|
const axios = require('axios');
|
||||||
const middleware = require('./_common/middleware');
|
const middleware = require('./_common/middleware');
|
||||||
|
|
||||||
const getBaseDomain = (url) => {
|
const getBaseDomain = (url) => {
|
||||||
@ -44,18 +45,7 @@ const parseWhoisData = (data) => {
|
|||||||
return parsedData;
|
return parsedData;
|
||||||
};
|
};
|
||||||
|
|
||||||
const fetchWhoisData = async (url) => {
|
const fetchFromInternic = async (hostname) => {
|
||||||
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}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const client = net.createConnection({ port: 43, host: 'whois.internic.net' }, () => {
|
const client = net.createConnection({ port: 43, host: 'whois.internic.net' }, () => {
|
||||||
client.write(hostname + '\r\n');
|
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);
|
exports.handler = middleware(fetchWhoisData);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user