From 35e27546b71b9913de2d913d4730fb7739df7dbc Mon Sep 17 00:00:00 2001 From: BurnyLlama Date: Sat, 28 May 2022 15:03:43 +0200 Subject: [PATCH] Started working on checkers... --- .gitignore | 3 ++- index.js | 33 ++++++++++++++++++++++++------ libs/getChecker.js | 5 +++++ modules/checkers/health-checker.js | 9 ++++++++ modules/checkers/ip-changed.js | 33 ++++++++++++++++++++++++++++++ modules/notifiers/xmpp.js | 4 ++-- package.json | 1 + 7 files changed, 79 insertions(+), 9 deletions(-) create mode 100644 libs/getChecker.js create mode 100644 modules/checkers/health-checker.js create mode 100644 modules/checkers/ip-changed.js diff --git a/.gitignore b/.gitignore index 84f3d69..95806cd 100644 --- a/.gitignore +++ b/.gitignore @@ -132,4 +132,5 @@ dist # Hihi config.yml -pnpm-lock.yaml \ No newline at end of file +pnpm-lock.yaml +ip.cache \ No newline at end of file diff --git a/index.js b/index.js index b764d06..a40995d 100644 --- a/index.js +++ b/index.js @@ -1,18 +1,39 @@ import settings from "./libs/settings.js" import getNotifier from "./libs/getNotifier.js" import getLogger from "./libs/logger.js" +import getChecker from "./libs/getChecker.js" const log = getLogger("MAIN", "blue") log("Starting up maintenance-bot...") -log("Starting up notifiers...") -const enabledNotifiers = Object.keys(settings.notifiers).filter(notifier => settings.notifiers[notifier].enabled) -const notifiers = await Promise.all(enabledNotifiers.map(notifier => getNotifier(notifier))) -log("Starting up checkers...") +log("Finding enabled notifiers and checkers...") +const enabledNotifiers = Object.keys(settings.notifiers).filter(notifier => settings.notifiers[notifier].enabled) +const enabledCheckers = Object.keys(settings.checkers).filter(checker => settings.checkers[checker].enabled) +log("Enabled notifiers:", enabledNotifiers) +log("Enabled checkers: ", enabledCheckers) + + +log("Starting up notifiers and checkers...") +const notifiers = await Promise.all(enabledNotifiers.map(notifier => getNotifier(notifier))) +const sendMessage = msg => notifiers.forEach(notifier => notifier(msg)) +await Promise.all(enabledCheckers.map(checker => getChecker(checker, sendMessage))) +log("Started all checkers!") + log("Sending out start-up messages...") -notifiers.forEach(notifier => notifier("Started up 'maintenance-bot'...")) +sendMessage("Started up 'maintenance-bot'...") +log("Start up complete!") -log("Start up complete!") \ No newline at end of file + +function onExitWarn() { + sendMessage("OH NO I AM DYING!") + setTimeout(() => process.exit(0), 200) +} +process.on("beforeExit", onExitWarn) +process.on("exit", onExitWarn) +process.on("SIGINT", onExitWarn) +process.on("SIGTERM", onExitWarn) +process.on("uncaughtException", onExitWarn) +process.on("unhandledRejection", onExitWarn) \ No newline at end of file diff --git a/libs/getChecker.js b/libs/getChecker.js new file mode 100644 index 0000000..1d26912 --- /dev/null +++ b/libs/getChecker.js @@ -0,0 +1,5 @@ +export default function getChecker(checker, sendMessage) { + return new Promise( + resolve => import(`../modules/checkers/${checker}.js`).then( + module => resolve(module.start(sendMessage)))) +} diff --git a/modules/checkers/health-checker.js b/modules/checkers/health-checker.js new file mode 100644 index 0000000..189a2f6 --- /dev/null +++ b/modules/checkers/health-checker.js @@ -0,0 +1,9 @@ + +import getLogger from "../../libs/logger.js" +import settings from "../../libs/settings.js" + +const log = getLogger("Health Checker", settings.checkers["health-checker"].color) + +export function start() { + log("Starting up the health checker...") +} \ No newline at end of file diff --git a/modules/checkers/ip-changed.js b/modules/checkers/ip-changed.js new file mode 100644 index 0000000..686045c --- /dev/null +++ b/modules/checkers/ip-changed.js @@ -0,0 +1,33 @@ +import getLogger from "../../libs/logger.js" +import settings from "../../libs/settings.js" +import fs from "fs" +import publicIp from "public-ip" + +if (!fs.existsSync("./ip.cache")) fs.writeFileSync("./ip.cache", "") + +const log = getLogger("IP Watcher", settings.checkers["ip-changed"].color) +const getIp = async () => await publicIp.v4({ onlyHttps: true }) + +const currentIp = await getIp() +const cachedOldIp = fs.readFileSync("./ip.cache").toString() +let oldIp = cachedOldIp ? oldIp : currentIp + +async function checkIp() { + log("Checking for IP changes...") + const nowIp = await getIp() + if (nowIp !== oldIp) { + sendMessage(`IP change detected! Old IP was '${oldIp}', new IP is '${nowIp}'.`) + oldIp = nowIp + fs.writeFileSync("./ip.cache", nowIp) + return + } + + log("No IP change detected!") +} + +export async function start(sendMessage) { + log("Starting up the IP watcher...") + checkIp() + setInterval(checkIp, (settings.checkers["ip-changed"].interval ?? 3600) * 1000) + return +} \ No newline at end of file diff --git a/modules/notifiers/xmpp.js b/modules/notifiers/xmpp.js index 4b9d8a3..7368f0a 100644 --- a/modules/notifiers/xmpp.js +++ b/modules/notifiers/xmpp.js @@ -1,6 +1,5 @@ import settings from "../../libs/settings.js" -import { client, jid, xml } from "@xmpp/client" -import debug from "@xmpp/debug" +import { client, xml } from "@xmpp/client" import getLogger from "../../libs/logger.js" const log = getLogger("XMPP", settings.notifiers.xmpp.color) @@ -36,6 +35,7 @@ export async function start() { } xmpp.on("online", address => { + xmpp.send(xml("presence", { type: "available" })) log(`Logged in as ${address.toString()}`) }) diff --git a/package.json b/package.json index 29fe70f..9a5208a 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "@xmpp/debug": "^0.13.0", "colors": "^1.4.0", "discord.js": "^13.7.0", + "public-ip": "^5.0.0", "yaml": "^2.1.0" } }