2022-05-28 15:03:43 +02:00
|
|
|
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 })
|
|
|
|
|
2022-06-01 12:41:42 +02:00
|
|
|
const currentIp = await getIp().replace(/\s/g, "")
|
2022-05-28 15:03:43 +02:00
|
|
|
const cachedOldIp = fs.readFileSync("./ip.cache").toString()
|
2022-06-01 12:39:51 +02:00
|
|
|
let oldIp = cachedOldIp ? cachedOldIp : currentIp
|
2022-05-28 15:03:43 +02:00
|
|
|
|
2022-05-28 16:10:37 +02:00
|
|
|
async function checkIp(sendMessage) {
|
2022-05-28 15:03:43 +02:00
|
|
|
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...")
|
2022-05-28 16:10:37 +02:00
|
|
|
checkIp(sendMessage)
|
|
|
|
setInterval(
|
|
|
|
() => checkIp(sendMessage),
|
|
|
|
(settings.checkers["ip-changed"].interval ?? 3600) * 1000
|
|
|
|
)
|
|
|
|
|
2022-05-28 15:03:43 +02:00
|
|
|
return
|
|
|
|
}
|