Compare commits
No commits in common. "02d9cc1e76b101c371a4eb015ac2f2096531ce51" and "5d966f422a2977c87c657f88847932b0be943adc" have entirely different histories.
02d9cc1e76
...
5d966f422a
10
DynDnsBot.js
10
DynDnsBot.js
@ -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 = random
|
let handler = (callback) => random
|
||||||
? (callback) => setTimeout(callback, Math.random()*seconds*1000)
|
?()=>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 res.text;
|
return JSON.parse(res.text);
|
||||||
}).catch(console.error);
|
}).catch(console.error);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,43 +0,0 @@
|
|||||||
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,7 +48,6 @@ 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});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -79,7 +78,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)', (input) => {
|
await this.rl.question('continue? (Y/n)', () => {
|
||||||
switch (input) {
|
switch (input) {
|
||||||
case 'n': process.exit(); break;
|
case 'n': process.exit(); break;
|
||||||
default: return res.consumerKey;
|
default: return res.consumerKey;
|
||||||
|
@ -11,7 +11,6 @@
|
|||||||
"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"
|
||||||
}
|
}
|
||||||
}
|
}
|
15
index.js
15
index.js
@ -1,7 +1,16 @@
|
|||||||
import {DynDnsBot} from './DynDnsBot.js';
|
import {DynDnsBot} from './DynDnsBot.js';
|
||||||
import {JsonDataStore} from './JsonDataStore.js';
|
import {loadData, storeData} from './jsonDataStore.js';
|
||||||
|
|
||||||
let store = new JsonDataStore('config.json');
|
let configPath = 'config.json';
|
||||||
|
let config = loadData(configPath);
|
||||||
|
|
||||||
let bot = new DynDnsBot({config: store.getSyncedData()});
|
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});
|
||||||
bot.setInterval();
|
bot.setInterval();
|
25
jsonDataStore.js
Normal file
25
jsonDataStore.js
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
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…
Reference in New Issue
Block a user