Compare commits

...

3 Commits

@ -8,7 +8,7 @@ export class DynDnsBot{
this.lastIp = null;
}
async update() {
async update(){
let newIp = await this.getIp();
if (newIp === this.lastIp) return;
this.lastIp = newIp;
@ -18,15 +18,15 @@ export class DynDnsBot{
});
}
setInterval(seconds = this.config.updateInterval, random = this.config.randomInterval){
let handler = (callback) => random
?()=>setTimeout(callback, Math.random()*seconds*1000)
:callback;
this.interval = setInterval(handler(this.update), seconds*1000);
let handler = random
? (callback) => setTimeout(callback, Math.random()*seconds*1000)
: (callback) => callback();
this.interval = setInterval(handler(()=>this.update()), seconds*1000);
this.update();
}
async getIp(){
return await rest.get(this.config.ipApi).then(res => {
return JSON.parse(res.text);
return res.text;
}).catch(console.error);
}
}

@ -0,0 +1,43 @@
import fs from "fs";
export class JsonDataStore {
constructor(path) {
this.path = path;
this.data = this.loadData(path);
this.proxyHandler = {
set: (target, key, value) => {
console.log(`config changed: ${key} set from ${target[key]} to ${value}`);
target[key] = value;
this.storeData(this.data);
return true;
},
get: (target, key) => {
if (typeof target[key] === 'object') return new Proxy(target[key], this.proxyHandler);
return target[key];
}
};
this.proxy = new Proxy(this.data, this.proxyHandler);
}
storeData(data, path = this.path) {
try {
fs.writeFileSync(path, JSON.stringify(data));
} catch (err) {
console.error(err);
}
}
loadData(path = this.path) {
try {
return JSON.parse(fs.readFileSync(path, 'utf8'));
} catch (err) {
console.error(err);
return false;
}
}
getData(){
return this.data;
}
getSyncedData(){
return this.proxy;
}
}

@ -48,6 +48,7 @@ export class OvhApi{
body, timestamp
});
header['X-Ovh-Consumer'] = this.credentials.consumerKey;
header['X-Ovh-Application'] = this.credentials.applicationName;
await this.sendRequest({path, body, getMethod, header});
}
@ -78,7 +79,7 @@ export class OvhApi{
this.credentials.consumerKey = res.consumerKey;
console.log('please validate on ovh site:');
console.log(res.validationUrl);
await this.rl.question('continue? (Y/n)', () => {
await this.rl.question('continue? (Y/n)', (input) => {
switch (input) {
case 'n': process.exit(); break;
default: return res.consumerKey;

@ -11,6 +11,7 @@
"ovhCredentials": {
"applicationKey": "",
"applicationSecret": "",
"applicationName": "ovh-api-dyndns",
"apiUrl": "https://ca.api.ovh.com/1.0"
}
}

@ -1,16 +1,7 @@
import {DynDnsBot} from './DynDnsBot.js';
import {loadData, storeData} from './jsonDataStore.js';
import {JsonDataStore} from './JsonDataStore.js';
let configPath = 'config.json';
let config = loadData(configPath);
let store = new JsonDataStore('config.json');
let configProxy = new Proxy(config, {
set: (target, key, value) => {
console.log(`config changed: ${key} set from ${target[key]} to ${value}`);
target[key] = value;
storeData(config, configPath);
return true;
}
});
let bot = new DynDnsBot({config: configProxy});
let bot = new DynDnsBot({config: store.getSyncedData()});
bot.setInterval();

@ -1,25 +0,0 @@
import fs from "fs";
export function storeData(data, path) {
try {
fs.writeFileSync(path, JSON.stringify(data));
} catch (err) {
console.error(err);
}
}
export function loadData(path) {
try {
return JSON.parse(fs.readFileSync(path, 'utf8'))
} catch (err) {
console.error(err)
return false
}
}
export function getStat(path) {
try {
return fs.existsSync(path) === true;
} catch(err) {
console.error(err)
return false;
}
}
Loading…
Cancel
Save