Compare commits

...

3 Commits

Author SHA1 Message Date
adb
02d9cc1e76 fix api X-Ovh-Application header 2021-04-01 23:23:55 +02:00
adb
581fadba8f fix DynDnsBot timout 2021-04-01 23:23:04 +02:00
adb
914f6bafce create class JsonDataStore / fix proxy 2021-04-01 23:22:09 +02:00
6 changed files with 55 additions and 44 deletions

View File

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

43
JsonDataStore.js Normal file
View File

@ -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;
}
}

View File

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

View File

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

View File

@ -1,16 +1,7 @@
import {DynDnsBot} from './DynDnsBot.js'; import {DynDnsBot} from './DynDnsBot.js';
import {loadData, storeData} from './jsonDataStore.js'; import {JsonDataStore} from './JsonDataStore.js';
let configPath = 'config.json'; let store = new JsonDataStore('config.json');
let config = loadData(configPath);
let configProxy = new Proxy(config, { let bot = new DynDnsBot({config: store.getSyncedData()});
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});
bot.setInterval(); bot.setInterval();

View File

@ -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;
}
}