From 1026f1384342c49c659c99e2fe4ec78867e9f497 Mon Sep 17 00:00:00 2001 From: Xargana Date: Fri, 11 Apr 2025 19:13:02 +0300 Subject: [PATCH] added WHOIS --- api/server.js | 2 ++ api/status/status.js | 2 +- api/whois/whois.js | 69 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 api/whois/whois.js diff --git a/api/server.js b/api/server.js index 4c2c45e..6437f76 100644 --- a/api/server.js +++ b/api/server.js @@ -9,6 +9,7 @@ require('dotenv').config({ path: path.join(__dirname, '../.env') }); const status = require("./status/status"); const exchangeRate = require("./exchange-rate/exchange-rate"); +const whois = require("./whois/whois"); const app = express(); const PORT = process.env.PORT || 2589; @@ -19,6 +20,7 @@ const cert = process.env.SSL_CERT_PATH || "/etc/letsencrypt/live/blahaj.tr/fullc app.use(cors()); app.use("/status", status); app.use("/exchange-rate", exchangeRate); +app.use("/whois", whois); // try to load certificates try { diff --git a/api/status/status.js b/api/status/status.js index 0f1b01e..6f4662c 100644 --- a/api/status/status.js +++ b/api/status/status.js @@ -30,7 +30,7 @@ async function checkServers() { for (const server of REMOTE_SERVERS) { try { const res = await ping.promise.probe(server.host, { - timeout: 2, // Set a timeout of 2 seconds + timeout: 4, // Set a timeout of 4 seconds }); serversStatus[server.name].online = res.alive; serversStatus[server.name].responseTime = res.time; diff --git a/api/whois/whois.js b/api/whois/whois.js new file mode 100644 index 0000000..b7165c4 --- /dev/null +++ b/api/whois/whois.js @@ -0,0 +1,69 @@ +const express = require('express'); +const whois = require('whois-json'); +const router = express.Router(); + +// GET endpoint for WHOIS lookup +router.get('/:domain', async (req, res) => { + try { + const domain = req.params.domain; + + if (!domain) { + return res.status(400).json({ error: 'Domain parameter is required' }); + } + + const result = await whois(domain); + + // Format the response in a clean structure + const whoisData = { + domain: domain, + registrar: result.registrar || "Not available", + creationDate: result.creationDate ? new Date(result.creationDate).toISOString() : "Not available", + expirationDate: result.expirationDate ? new Date(result.expirationDate).toISOString() : "Not available", + nameServers: Array.isArray(result.nameServers) ? result.nameServers : (result.nameServers ? [result.nameServers] : ["Not available"]), + status: Array.isArray(result.status) ? result.status : (result.status ? [result.status] : ["Not available"]), + raw: result // Include the full raw data for advanced usage + }; + + res.json(whoisData); + } catch (error) { + console.error('WHOIS API Error:', error); + res.status(500).json({ + error: 'Failed to fetch WHOIS information', + message: error.message + }); + } +}); + +// POST endpoint for WHOIS lookup (alternative to GET with request body) +router.post('/', async (req, res) => { + try { + const domain = req.body.domain; + + if (!domain) { + return res.status(400).json({ error: 'Domain parameter is required in request body' }); + } + + const result = await whois(domain); + + // Format the response in a clean structure + const whoisData = { + domain: domain, + registrar: result.registrar || "Not available", + creationDate: result.creationDate ? new Date(result.creationDate).toISOString() : "Not available", + expirationDate: result.expirationDate ? new Date(result.expirationDate).toISOString() : "Not available", + nameServers: Array.isArray(result.nameServers) ? result.nameServers : (result.nameServers ? [result.nameServers] : ["Not available"]), + status: Array.isArray(result.status) ? result.status : (result.status ? [result.status] : ["Not available"]), + raw: result // Include the full raw data for advanced usage + }; + + res.json(whoisData); + } catch (error) { + console.error('WHOIS API Error:', error); + res.status(500).json({ + error: 'Failed to fetch WHOIS information', + message: error.message + }); + } +}); + +module.exports = router;