Compare commits
	
		
			3 Commits
		
	
	
		
			5d966f422a
			...
			02d9cc1e76
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 02d9cc1e76 | |||
| 581fadba8f | |||
| 914f6bafce | 
							
								
								
									
										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 = (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
									
								
							
							
						
						
									
										43
									
								
								JsonDataStore.js
									
									
									
									
									
										Normal 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;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -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;
 | 
				
			||||||
 | 
				
			|||||||
@ -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"
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
							
								
								
									
										15
									
								
								index.js
									
									
									
									
									
								
							
							
						
						
									
										15
									
								
								index.js
									
									
									
									
									
								
							@ -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();
 | 
				
			||||||
@ -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…
	
		Reference in New Issue
	
	Block a user