-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotifications.mts
More file actions
74 lines (60 loc) · 1.71 KB
/
Copy pathnotifications.mts
File metadata and controls
74 lines (60 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { spawn } from "node:child_process";
import { createServer } from "node:http";
import { createInterface } from "node:readline";
function notify(title: string, message: string) {
spawn(
"notify-send",
["-a", "Gateway Alert", "--", title.trim(), message.trim()],
{ stdio: "inherit" },
);
}
createServer((req, res) => {
try {
const chunks: Uint8Array[] = [];
req.on("readable", () => {
try {
let chunk: Uint8Array;
while (null !== (chunk = req.read())) {
chunks.push(chunk);
}
} catch (error) {
console.error(error);
}
});
req.on("end", () => {
try {
const data = JSON.parse(Buffer.concat(chunks).toString("utf8"));
data.message = data.message.replaceAll(":3000", ":4000");
console.log(
data.title.trim() + "\n" + data.message.trim() + "\n\n---\n",
);
notify(data.title, data.message);
res.writeHead(204);
res.end();
} catch (error) {
console.error(error);
}
});
} catch (error) {
console.error(error);
}
}).listen(10101);
const filterRe = / 172\.172\.172\.| 172\.19\.0\.| 192\.168\.1\./g;
const ch = spawn(
"docker",
["compose", "logs", "nginx", "--since", new Date().toISOString(), "--follow"],
{ cwd: new URL(".", import.meta.url) },
);
let shouldNotifyOnForeignActivity = true;
for await (const i of createInterface(ch.stdout)) {
filterRe.lastIndex = 0;
if (filterRe.test(i)) continue;
console.log(i);
if (shouldNotifyOnForeignActivity) {
shouldNotifyOnForeignActivity = false;
notify("Foreign Activity Detected", i);
setTimeout(() => {
shouldNotifyOnForeignActivity = true;
}, 30_000);
}
}