Sending logs successfully
This commit is contained in:
66
index.js
66
index.js
@@ -6,16 +6,13 @@ const tls = require("tls");
|
|||||||
const httpProxy = require("http-proxy");
|
const httpProxy = require("http-proxy");
|
||||||
const { WebSocketServer } = require('ws')
|
const { WebSocketServer } = require('ws')
|
||||||
const path = require("path");
|
const path = require("path");
|
||||||
|
const {default: forms} = require("forms")
|
||||||
|
const z = require("zod")
|
||||||
|
|
||||||
// ---------------------------
|
// ---------------------------
|
||||||
// Request Log Store
|
// Request Log Store
|
||||||
// ---------------------------
|
// ---------------------------
|
||||||
let requests = [];
|
let store;
|
||||||
|
|
||||||
// Mutex not required in Node (single-threaded)
|
|
||||||
function recordRequest(info) {
|
|
||||||
requests.push(info);
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---------------------------
|
// ---------------------------
|
||||||
// IP Helpers
|
// IP Helpers
|
||||||
@@ -25,10 +22,27 @@ function isLocalIP(ip) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
function cleanIP(remoteAddr) {
|
function getIP(req) {
|
||||||
if (!remoteAddr) return "";
|
const headers = req.headers;
|
||||||
let ip = remoteAddr.split(":")[0].replace("[", "").replace("]", "");
|
|
||||||
if (ip.startsWith("::ffff:")) ip = ip.replace("::ffff:", "");
|
let ip =
|
||||||
|
headers["cf-connecting-ip"] ||
|
||||||
|
headers["x-real-ip"] ||
|
||||||
|
headers["x-forwarded-for"]?.split(",")[0]?.trim() ||
|
||||||
|
req.socket?.remoteAddress ||
|
||||||
|
req.connection?.remoteAddress ||
|
||||||
|
"";
|
||||||
|
|
||||||
|
// Normalize IPv6-mapped IPv4
|
||||||
|
if (ip.startsWith("::ffff:")) {
|
||||||
|
ip = ip.slice(7);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Normalize localhost IPv6
|
||||||
|
if (ip === "::1") {
|
||||||
|
ip = "127.0.0.1";
|
||||||
|
}
|
||||||
|
|
||||||
return ip;
|
return ip;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -37,8 +51,7 @@ function cleanIP(remoteAddr) {
|
|||||||
// ---------------------------
|
// ---------------------------
|
||||||
function loggingMiddleware(next) {
|
function loggingMiddleware(next) {
|
||||||
return (req, res) => {
|
return (req, res) => {
|
||||||
const rawIP = req.socket.remoteAddress;
|
const ip = getIP(req);
|
||||||
const ip = cleanIP(rawIP);
|
|
||||||
|
|
||||||
let path = req.url.startsWith("/") ? req.url.slice(1) : req.url;
|
let path = req.url.startsWith("/") ? req.url.slice(1) : req.url;
|
||||||
if (path === "") path = "/";
|
if (path === "") path = "/";
|
||||||
@@ -57,13 +70,13 @@ function loggingMiddleware(next) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
recordRequest({
|
store.add("log", {
|
||||||
Timestamp: new Date().toISOString(),
|
time: new Date().toISOString(),
|
||||||
Host: req.headers.host || "",
|
host: req.headers.host || "",
|
||||||
IP: ip,
|
ip: ip,
|
||||||
Path: path,
|
path: path,
|
||||||
Method: req.method
|
method: req.method
|
||||||
});
|
})
|
||||||
|
|
||||||
next(req, res);
|
next(req, res);
|
||||||
};
|
};
|
||||||
@@ -192,6 +205,21 @@ function startServer() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
async function connectToForms() {
|
||||||
|
await store.connect()
|
||||||
|
store.register("log", z.toJSONSchema(z.object({
|
||||||
|
time: z.string(),
|
||||||
|
host: z.string(),
|
||||||
|
ip: z.string(),
|
||||||
|
path: z.string(),
|
||||||
|
method: z.string()
|
||||||
|
})))
|
||||||
|
}
|
||||||
|
|
||||||
|
store = new forms.client()
|
||||||
|
connectToForms()
|
||||||
|
|
||||||
server.listen(3000, () => {
|
server.listen(3000, () => {
|
||||||
console.log("🔒 HTTPS server running on port 3000");
|
console.log("🔒 HTTPS server running on port 3000");
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"http-proxy": "^1.18.1",
|
"http-proxy": "^1.18.1",
|
||||||
"ws": "^8.18.3"
|
"ws": "^8.18.3",
|
||||||
|
"zod": "^4.2.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user