You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
921 B
JavaScript

import {OvhApi} from './OvhApi.js';
import rest from 'superagent';
export class DynDnsBot{
constructor({config}){
this.config = config;
this.dns = new OvhApi({credentials: config.ovhCredentials});
this.lastIp = null;
}
async update() {
let newIp = await this.getIp();
if (newIp === this.lastIp) return;
this.lastIp = newIp;
this.config.records.forEach(record => {
record.target = newIp;
this.dns.updateRecord(record);
});
}
setInterval(seconds = this.config.updateInterval, random = this.config.randomInterval){
3 years ago
let handler = (callback) => random
?()=>setTimeout(callback, Math.random()*seconds*1000)
:callback;
this.interval = setInterval(handler(this.update), seconds*1000);
this.update();
}
async getIp(){
3 years ago
return await rest.get(this.config.ipApi).then(res => {
return JSON.parse(res.text);
}).catch(console.error);
}
}