I’m working on a Node.js server that retrieves MAC and IP addresses and sends them to the client. However, I noticed that the server always returns the same MAC and IP addresses, and I have no idea why. It used to work but it randomly stopped working, can anyone help me with this?
here is the code of the server.js file:
const express = require(“express”);
const os = require(“os”);
const fs = require(“fs”);
const moment = require(“moment-timezone”);
const app = express();
const port = process.env.PORT || 3000;
app.use(express.json());
app.use(express.static(“public”));
// Function to get MAC address
function getMACAddress() {
const interfaces = os.networkInterfaces();
console.log(“Network Interfaces:”, interfaces); // Log all network interfaces
for (let interfaceName in interfaces) {
for (let iface of interfaces[interfaceName]) {
if (iface.family === “IPv4” && !iface.internal) {
return iface.mac;
}
}
}
return “00:00:00:00:00:00”;
}
// Function to get local IP address
function getLocalIP() {
const interfaces = os.networkInterfaces();
console.log(“Network Interfaces:”, interfaces); // Log all network interfaces
for (let interfaceName in interfaces) {
for (let iface of interfaces[interfaceName]) {
if (iface.family === “IPv4” && !iface.internal) {
return iface.address;
}
}
}
return “127.0.0.1”;
}
// Convert time to 12-hour format
function convertTo12HourFormat(time) {
return moment(time, “HH:mm:ss”).format(“hh:mm:ss A”);
}
// API routes
app.get(“/api/ip”, (req, res) => {
const mac = getMACAddress();
console.log(“Server fetched MAC Address:”, mac);
res.json({ mac });
});
app.get(“/api/ip2”, (req, res) => {
const ip = getLocalIP();
console.log(“Server fetched IP Address:”, ip);
res.json({ ip });
});
app.get(“/api/mess”, (req, res) => {
const mac = getMACAddress();
let mess;
if (
mac == “” ||
mac == “” ||
mac == “”
) {
mess = “YOU ARE IN SCHOOL!”;
} else if (mac == “”) {
mess = “I am on my computer!”;
} else if (mac == “”) {
mess = “Hi!”;
} else {
mess = “no idea whose computer this is.”;
}
res.json({ mess });
});
app.post(“/api/save-ip”, (req, res) => {
const { mac, ip } = req.body;
console.log(“Received MAC Address:”, mac);
console.log(“Received IP Address:”, ip);
const timestamp = moment()
.tz(“America/New_York”)
.format(“YYYY-MM-DD HH:mm:ss”);
const rt = convertTo12HourFormat(timestamp.split(" ")[1]);
const data = \ntime and date: ${ timestamp.split(" ")[0] } ${rt}\n mac address: ${mac} ip: ${ip}\n
;
fs.appendFile(“public/ip-addresses.txt”, data, (err) => {
if (err) {
console.error("Error writing MAC address to file: ", err);
return res.status(500).send(“Internal Server Error”);
}
res.send(“MAC address and timestamp saved successfully”);
});
});
// Start the server
app.listen(port, () => {
console.log(Server running at http://localhost:${port}
);
});
and the index.html page:
body { font-family: Arial, sans-serif; background-color: #f2f2f2; } #bla { background-color: #333; color: #fff; padding: 10px; text-align: center; position: fixed; top: 0; left: 0; width: 100%; z-index: 999; } #top { margin-top: 200px; } #p { margin-left: 50.1%; margin-bottom: 0; margin-top: 0; width: 295px; transform: translateX(-50%); font-size: 20px; color: #333; } #b { margin-top: 0; margin-left: 50%; transform: translateX(-50%); padding: 10px 20px; font-size: 18px; cursor: pointer; background-color: #333; color: #fff; border: none; border-radius: 5px; } mumbojum2's game pageclick this button to go to the page
hi (hi)- List item