Skip to content

Instantly share code, notes, and snippets.

@hamptonmoore
Last active February 27, 2021 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hamptonmoore/96f47b83ac54bcb4a5c735a9a07f47ed to your computer and use it in GitHub Desktop.
Save hamptonmoore/96f47b83ac54bcb4a5c735a9a07f47ed to your computer and use it in GitHub Desktop.
const express = require("express");
const execSync = require("child_process").execSync;
const fs = require("fs");
const app = express();
const port = 6969;
function padIP(ip) {
let inflate = [];
ip.split(":").forEach((x) => (x != "" ? inflate.push("0".repeat(4 - x.length) + x) : inflate.push("")));
let zeroes = 0;
inflate.forEach((x) => (zeroes += x.length));
let final = "";
inflate.forEach((x) => (x == "" ? (final += "0".repeat(32 - zeroes)) : (final += x)));
return final.match(/.{1,4}/g).join(":");
}
app.get("/", (req, res) => {
let userIP = padIP(req.connection.remoteAddress);
// let connections = fs.readFileSync("/proc/net/sockstat6", {encoding:'utf8', flag:'r'}).split("\n");
let connections = execSync("conntrack -L").toString().split("\n");
connections = connections.map((l) => l.split(/\s+/g));
connections = connections.filter((conn) => {
if (conn[4] == undefined) {
return false;
}
if (conn[4].includes(".")) {
return false;
}
return padIP(conn[4].split("src=")[1] || "0:0:0:0:0:0:0:0") == userIP && conn[6] == `sport=${req.connection._peername.port}`;
});
let color;
if (connections.length > 0) {
color =
"#" +
padIP(connections[0][5].split("dst=")[1])
.split(":")
.slice(-3)
.map((i) => i.slice(-2))
.join("");
} else {
res.send("An error has occured, I am very sorry\n");
return;
}
res.send(`Setting to color ${color}\n`);
fs.writeFileSync("/var/www/rgb-background/currentColour.txt", color);
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment