Skip to content

Instantly share code, notes, and snippets.

@autioch
Created July 21, 2024 16:03
Show Gist options
  • Save autioch/97785c1792bfc70796b1bf82362f9239 to your computer and use it in GitHub Desktop.
Save autioch/97785c1792bfc70796b1bf82362f9239 to your computer and use it in GitHub Desktop.
Scan your local network for active services
const http = require('http');
const net = require('net');
const PORTS_TO_SCAN = [80, 443, 22, 21, 8080];
const scanService = (i, port) => new Promise((resolve) => {
const ip = `192.168.1.${i}`;
const socket = new net.Socket();
socket.setTimeout(4000);
socket.on('connect', () => {
socket.destroy();
resolve({ ip, port });
}).on('timeout', () => {
socket.destroy();
resolve(false);
}).on('error', () => {
socket.destroy();
resolve(false);
});
socket.connect(port, ip);
});
http
.createServer(async (req, res) => {
const scanTasks = [];
for (let i = 1; i <= 254; i++) {
scanTasks.push(...PORTS_TO_SCAN.map((port) => scanService(i, port)));
}
const services = await Promise.all(scanTasks);
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(services.filter(Boolean).map(s => `<br/><a href="//${s.ip}:${s.port}" target="_blank">${s.ip}:${s.port}</a>`).join(''));
})
.listen(3000, () => console.log(`http://localhost:3000`));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment