From 10f9e1b5f2e0048cfbd043da7d19771f3af2e20f Mon Sep 17 00:00:00 2001 From: Dorian Niemiec Date: Mon, 28 Aug 2023 03:44:04 +0200 Subject: [PATCH] Add reverse DNS lookup support --- config.json | 4 +-- svr.js | 77 +++++++++++++++++++++++++++++++++++------------------ 2 files changed, 53 insertions(+), 28 deletions(-) diff --git a/config.json b/config.json index 76fd53d..e8d3ae7 100644 --- a/config.json +++ b/config.json @@ -3,7 +3,7 @@ "port": 80, "pubport": 80, "page404": "404.html", - "timestamp": 1692915666757, + "timestamp": 1693186939816, "blacklist": [], "nonStandardCodes": [], "enableCompression": true, @@ -90,4 +90,4 @@ "enableETag": true, "disableUnusedWorkerTermination": false, "rewriteDirtyURLs": true -} \ No newline at end of file +} diff --git a/svr.js b/svr.js index 85d1e30..8f6d783 100644 --- a/svr.js +++ b/svr.js @@ -357,6 +357,7 @@ http.STATUS_CODES[497] = "HTTP Request Sent to HTTPS Port"; http.STATUS_CODES[598] = "Network Read Timeout Error"; http.STATUS_CODES[599] = "Network Connect Timeout Error"; var url = require("url"); +var dns = require("dns"); try { var gracefulFs = require("graceful-fs"); if (!process.isBun) gracefulFs.gracefulify(fs); //Bun + graceful-fs + SVR.JS + requests about static content = 500 Internal Server Error! @@ -865,21 +866,33 @@ if (host != "[offline]" || ifaceEx) { return; } pubip = d.toString(); - if (!domain) { - if (pubip.indexOf(":") == -1) { - var parts = pubip.split("."); - var p1 = parseInt(parts[0]).toString(16); - var p2 = parseInt(parts[1]).toString(16); - var p3 = parseInt(parts[2]).toString(16); - var p4 = parseInt(parts[3]).toString(16); - var pp = parseInt(pubport).toString(16); - domain = p1 + p2 + p3 + p4 + pp + ".nodesvr.doriantech.com"; - } else { - domain = pubip.replace(/[^0-9a-zA-Z]/gi, "").toLowerCase() + ".nodesvrip6.doriantech.com"; + if (domain) { + ipRequestCompleted = true; + process.emit("ipRequestCompleted"); + } else { + var callbackDone = false; + + var dnsTimeout = setTimeout(function() { + callbackDone = true; + ipRequestCompleted = true; + process.emit("ipRequestCompleted"); + }, 3000); + + try { + dns.reverse(pubip, function(err, hostnames) { + if(callbackDone) return; + clearTimeout(dnsTimeout); + if(!err && hostnames.length > 0) domain = hostnames[0]; + ipRequestCompleted = true; + process.emit("ipRequestCompleted"); + }); + } catch(err) { + clearTimeout(dnsTimeout); + callbackDone = true; + ipRequestCompleted = true; + process.emit("ipRequestCompleted"); } } - ipRequestCompleted = true; - process.emit("ipRequestCompleted"); }); }); ipRequest.on("error", function () { @@ -917,21 +930,33 @@ if (host != "[offline]" || ifaceEx) { return; } pubip = d.toString(); - if (!domain) { - if (pubip.indexOf(":") == -1) { - var parts = pubip.split("."); - var p1 = parseInt(parts[0]).toString(16); - var p2 = parseInt(parts[1]).toString(16); - var p3 = parseInt(parts[2]).toString(16); - var p4 = parseInt(parts[3]).toString(16); - var pp = parseInt(pubport).toString(16); - domain = p1 + p2 + p3 + p4 + pp + ".nodesvr.doriantech.com"; - } else { - domain = pubip.replace(/[^0-9a-zA-Z]/gi, "").toLowerCase() + ".nodesvrip6.doriantech.com"; + if (domain) { + ipRequestCompleted = true; + process.emit("ipRequestCompleted"); + } else { + var callbackDone = false; + + var dnsTimeout = setTimeout(function() { + callbackDone = true; + ipRequestCompleted = true; + process.emit("ipRequestCompleted"); + }, 3000); + + try { + dns.reverse(pubip, function(err, hostnames) { + if(callbackDone) return; + clearTimeout(dnsTimeout); + if(!err && hostnames.length > 0) domain = hostnames[0]; + ipRequestCompleted = true; + process.emit("ipRequestCompleted"); + }); + } catch(err) { + clearTimeout(dnsTimeout); + callbackDone = true; + ipRequestCompleted = true; + process.emit("ipRequestCompleted"); } } - ipRequestCompleted = true; - process.emit("ipRequestCompleted"); }); }); ipRequest2.on("error", function () {