37 lines
1.1 KiB
JavaScript
37 lines
1.1 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().replace(/\s/g, "")
|
|
const cachedOldIp = fs.readFileSync("./ip.cache").toString()
|
|
let oldIp = cachedOldIp ? cachedOldIp : currentIp
|
|
|
|
async function checkIp(sendMessage) {
|
|
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(sendMessage)
|
|
setInterval(
|
|
() => checkIp(sendMessage),
|
|
(settings.checkers["ip-changed"].interval ?? 3600) * 1000
|
|
)
|
|
|
|
return
|
|
} |