33 lines
1.0 KiB
JavaScript
33 lines
1.0 KiB
JavaScript
|
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
|
||
|
}
|