UNPKG

ddnsman

Version:
49 lines (48 loc) 1.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const events_1 = require("events"); const util_1 = require("./util"); class IpMonitor extends events_1.EventEmitter { async getIp() { if (this.ip) { return this.ip; } else { try { const ip = await util_1.getPublicIP(); this.ip = ip; return ip; } catch (err) { // tslint:disable-next-line:no-console console.error(err); } } } async checkIp() { try { const ip = await util_1.getPublicIP(); if (this.ip !== ip) { this.ip = ip; this.emit('change', ip); } } catch (err) { util_1.logger.error('Failed to get external IP: %o' + err); // tslint:disable-next-line:no-console console.error(err); } } // default interval 10s start(interval = 10000) { this.timer = setInterval(() => { this.checkIp(); }, interval); } stop() { if (this.timer) { clearInterval(this.timer); } } } exports.IpMonitor = IpMonitor;