Created
May 9, 2024 12:57
-
-
Save diegovgsilva95/15ef37207cc36de5a911bb8ab77da599 to your computer and use it in GitHub Desktop.
Monitoring of IP Address renewing (leasing) using Node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { spawn } from "child_process" | |
import { log } from "console" | |
import { appendFile } from "fs/promises" | |
import {stdout} from "process" | |
var sleep = ms => new Promise(r => setTimeout(r, ms)) | |
const getMyIP = async function(){ | |
return new Promise((res, rej)=>{ | |
let timeout = 500 | |
let timeoutHandler = () => rej(Error("Timeout")) | |
let timeoutCode = setTimeout(timeoutHandler, timeout) | |
let resOrRej = fn => { | |
clearTimeout(timeoutCode) | |
fn() | |
} | |
let data = "" | |
let ch = spawn("node", ["./net-info.mjs"]) | |
ch.stdout.on("data", datum => {data += datum.toString("utf8")}) | |
ch.on("error", err => resOrRej(rej.bind(this, err))) | |
ch.on("exit", (code) => resOrRej(res.bind(this, data))) | |
}) | |
} | |
let currentIPs = [] | |
let lastTS = 0 | |
let allVanishedIPs = [] | |
while(1){ | |
let newIPs = [] | |
let vanishedIPs = [] | |
while((Date.now() - lastTS) < 60000){ | |
await sleep(1000) | |
} | |
lastTS = Date.now() | |
stdout.write("\x9BH\x9B2J\x9B3J") | |
let ipRaw = await getMyIP() | |
if(ipRaw.length > 0){ | |
let sessionIPs = [] | |
let ipObj = JSON.parse(ipRaw) | |
for(let [ifaceName, ifaceAddresses] of Object.entries(ipObj)){ | |
for(let ifaceAddress of ifaceAddresses){ | |
if(ifaceAddress.family != "IPv6" || ifaceName == "lo" ) continue | |
sessionIPs.push(ifaceAddress.address) | |
} | |
} | |
for(let ip of currentIPs){ | |
if(!sessionIPs.includes(ip)){ // It vanished | |
vanishedIPs.push(ip) | |
if(!allVanishedIPs.includes(ip)) | |
allVanishedIPs.push(ip) | |
} | |
} | |
for(let ip of sessionIPs){ | |
if(!currentIPs.includes(ip)){ // It's new! | |
newIPs.push(ip) | |
} | |
} | |
for(let ip of newIPs){ | |
currentIPs.push(ip) | |
} | |
for(let ip of vanishedIPs){ | |
currentIPs.splice(currentIPs.indexOf(ip), 1) | |
} | |
log(`Current IPs:\n` + Array.from(Array(8)).fill("").map((_,i)=>sessionIPs[i]||_).join("\n")) | |
log(`New IPs:\n` + Array.from(Array(8)).fill("").map((_,i)=>newIPs[i]||_).join("\n")) | |
log(`Vanished IPs:\n` + Array.from(Array(8)).fill("").map((_,i)=>vanishedIPs[i]||_).join("\n")) | |
log(`History of all IPs:\n` + Array.from(Array(8)).fill("").map((_,i)=>currentIPs[i]||_).join("\n")) | |
log(`History of vanished IPs:\n` + Array.from(Array(8)).fill("").map((_,i)=>allVanishedIPs[i]||_).join("\n")) | |
} | |
await sleep(50) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { networkInterfaces } from "os" | |
// The invocation of networkInterfaces is externalized in order to avoid possible caching from Node.js. | |
// Hence, as net-info.mjs is called, it'll always return fresh info about IP Adressses. | |
let data = networkInterfaces() | |
process.stdout.write(JSON.stringify(data)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment