Compare commits
	
		
			No commits in common. "02d9cc1e76b101c371a4eb015ac2f2096531ce51" and "5d966f422a2977c87c657f88847932b0be943adc" have entirely different histories.
		
	
	
		
			02d9cc1e76
			...
			5d966f422a
		
	
		
							
								
								
									
										12
									
								
								DynDnsBot.js
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								DynDnsBot.js
									
									
									
									
									
								
							@ -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 = random
 | 
			
		||||
      ? (callback) => setTimeout(callback, Math.random()*seconds*1000)
 | 
			
		||||
      : (callback) => callback();
 | 
			
		||||
    this.interval = setInterval(handler(()=>this.update()), seconds*1000);
 | 
			
		||||
    let handler = (callback) => random
 | 
			
		||||
      ?()=>setTimeout(callback, Math.random()*seconds*1000)
 | 
			
		||||
      :callback;
 | 
			
		||||
    this.interval = setInterval(handler(this.update), seconds*1000);
 | 
			
		||||
    this.update();
 | 
			
		||||
  }
 | 
			
		||||
  async getIp(){
 | 
			
		||||
    return await rest.get(this.config.ipApi).then(res => {
 | 
			
		||||
      return res.text;
 | 
			
		||||
      return JSON.parse(res.text);
 | 
			
		||||
    }).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
 | 
			
		||||
    });
 | 
			
		||||
    header['X-Ovh-Consumer'] = this.credentials.consumerKey;
 | 
			
		||||
    header['X-Ovh-Application'] = this.credentials.applicationName;
 | 
			
		||||
    await this.sendRequest({path, body, getMethod, header});
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@ -79,7 +78,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)', (input) => {
 | 
			
		||||
    await this.rl.question('continue? (Y/n)', () => {
 | 
			
		||||
      switch (input) {
 | 
			
		||||
        case 'n': process.exit(); break;
 | 
			
		||||
        default: return res.consumerKey;
 | 
			
		||||
 | 
			
		||||
@ -11,7 +11,6 @@
 | 
			
		||||
  "ovhCredentials": {
 | 
			
		||||
    "applicationKey": "",
 | 
			
		||||
    "applicationSecret": "",
 | 
			
		||||
    "applicationName": "ovh-api-dyndns",
 | 
			
		||||
    "apiUrl": "https://ca.api.ovh.com/1.0"
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										15
									
								
								index.js
									
									
									
									
									
								
							
							
						
						
									
										15
									
								
								index.js
									
									
									
									
									
								
							@ -1,7 +1,16 @@
 | 
			
		||||
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();
 | 
			
		||||
							
								
								
									
										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