Compare commits
	
		
			3 Commits
		
	
	
		
			02d9cc1e76
			...
			24ff180ea6
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 24ff180ea6 | |||
| 6e0d4164c1 | |||
| 231805f373 | 
							
								
								
									
										14
									
								
								DynDnsBot.js
									
									
									
									
									
								
							
							
						
						
									
										14
									
								
								DynDnsBot.js
									
									
									
									
									
								
							@ -12,16 +12,18 @@ export class DynDnsBot{
 | 
				
			|||||||
    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;
 | 
				
			||||||
    this.config.records.forEach(record => {
 | 
					    Object.keys(this.config.records).forEach(domain => {
 | 
				
			||||||
      record.target = newIp;
 | 
					      this.config.records[domain].forEach(record => {
 | 
				
			||||||
      this.dns.updateRecord(record);
 | 
					        record.target = newIp;
 | 
				
			||||||
 | 
					        this.dns.updateRecord(record, domain);
 | 
				
			||||||
 | 
					      })
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  setInterval(seconds = this.config.updateInterval, random = this.config.randomInterval){
 | 
					  setInterval(seconds = this.config.updateInterval, random = this.config.randomInterval){
 | 
				
			||||||
    let handler = random
 | 
					    let handler = random
 | 
				
			||||||
      ? (callback) => setTimeout(callback, Math.random()*seconds*1000)
 | 
					      ? () => setTimeout(()=>this.update(), Math.random()*seconds*1000)
 | 
				
			||||||
      : (callback) => callback();
 | 
					      : () => this.update();
 | 
				
			||||||
    this.interval = setInterval(handler(()=>this.update()), seconds*1000);
 | 
					    this.interval = setInterval(handler, seconds*1000);
 | 
				
			||||||
    this.update();
 | 
					    this.update();
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  async getIp(){
 | 
					  async getIp(){
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										44
									
								
								OvhApi.js
									
									
									
									
									
								
							
							
						
						
									
										44
									
								
								OvhApi.js
									
									
									
									
									
								
							@ -3,8 +3,9 @@ import sha1 from 'sha1';
 | 
				
			|||||||
import readline from 'readline';
 | 
					import readline from 'readline';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export class OvhApi{
 | 
					export class OvhApi{
 | 
				
			||||||
  constructor({credentials}){
 | 
					  constructor({credentials, logs = true}){
 | 
				
			||||||
    this.credentials = credentials;
 | 
					    this.credentials = credentials;
 | 
				
			||||||
 | 
					    this.log = logs ? console.log : ()=>{};
 | 
				
			||||||
    this.baseUrl = this.credentials.apiUrl;
 | 
					    this.baseUrl = this.credentials.apiUrl;
 | 
				
			||||||
    this.methods = {get: 'GET', post: 'POST', put: 'PUT', delete: 'DELETE', path: 'PATCH'};
 | 
					    this.methods = {get: 'GET', post: 'POST', put: 'PUT', delete: 'DELETE', path: 'PATCH'};
 | 
				
			||||||
    this.rl = readline.createInterface({
 | 
					    this.rl = readline.createInterface({
 | 
				
			||||||
@ -18,17 +19,17 @@ export class OvhApi{
 | 
				
			|||||||
    getMethod=rest=>rest.get,
 | 
					    getMethod=rest=>rest.get,
 | 
				
			||||||
    header={accept: 'json'}
 | 
					    header={accept: 'json'}
 | 
				
			||||||
  }){
 | 
					  }){
 | 
				
			||||||
    console.log(`api request at ${path} =>`);
 | 
					    this.log(`api request at ${path} =>`);
 | 
				
			||||||
    console.log(header);
 | 
					    this.log(header);
 | 
				
			||||||
    console.log(body);
 | 
					    this.log(body);
 | 
				
			||||||
    let request = getMethod(rest)(`${this.baseUrl}${path}`);
 | 
					    let request = getMethod(rest)(`${this.baseUrl}${path}`);
 | 
				
			||||||
    await Object.keys(header).forEach(key => request.set(key, header[key]));
 | 
					    await Object.keys(header).forEach(key => request.set(key, header[key]));
 | 
				
			||||||
    return await request.send(body).then(res => {
 | 
					    return await request.send(body).then(res => {
 | 
				
			||||||
      console.log('api response =>');
 | 
					      this.log('api response =>');
 | 
				
			||||||
      console.log(JSON.parse(res.text));
 | 
					      this.log(JSON.parse(res.text));
 | 
				
			||||||
      return JSON.parse(res.text);
 | 
					      return JSON.parse(res.text);
 | 
				
			||||||
    }).catch(err => {
 | 
					    }).catch(err => {
 | 
				
			||||||
      console.error(err);
 | 
					      console.error(err.response.text || err);
 | 
				
			||||||
      return false;
 | 
					      return false;
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
@ -45,11 +46,12 @@ export class OvhApi{
 | 
				
			|||||||
    header['X-Ovh-Signature'] = await this.getSignature({
 | 
					    header['X-Ovh-Signature'] = await this.getSignature({
 | 
				
			||||||
      method: getMethod(this.methods),
 | 
					      method: getMethod(this.methods),
 | 
				
			||||||
      query: this.baseUrl+path,
 | 
					      query: this.baseUrl+path,
 | 
				
			||||||
      body, timestamp
 | 
					      body: JSON.stringify(body),
 | 
				
			||||||
 | 
					      timestamp
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
    header['X-Ovh-Consumer'] = this.credentials.consumerKey;
 | 
					    header['X-Ovh-Consumer'] = this.credentials.consumerKey;
 | 
				
			||||||
    header['X-Ovh-Application'] = this.credentials.applicationName;
 | 
					    header['X-Ovh-Application'] = this.credentials.applicationKey;
 | 
				
			||||||
    await this.sendRequest({path, body, getMethod, header});
 | 
					    return await this.sendRequest({path, body, getMethod, header});
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  async getSignature({method = 'GET', query, body='', timestamp}){
 | 
					  async getSignature({method = 'GET', query, body='', timestamp}){
 | 
				
			||||||
@ -87,11 +89,11 @@ export class OvhApi{
 | 
				
			|||||||
    });
 | 
					    });
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  async updateRecord({domain, subDomain, recordId, target, ttl = 3600}){
 | 
					  async updateRecord({zone, subDomain, id, target, ttl = 3600, fieldType = 'A'}, domain = zone){
 | 
				
			||||||
    return await this.sendSignedRequest({
 | 
					    return await this.sendSignedRequest({
 | 
				
			||||||
      path: `/domain/zone/${domain}/record/${recordId}`,
 | 
					      path: `/domain/zone/${domain}/record/${id}`,
 | 
				
			||||||
      getMethod: rest=>rest.put,
 | 
					      getMethod: rest=>rest.put,
 | 
				
			||||||
      body: {subDomain, target, ttl}
 | 
					      body: {subDomain, target, ttl, fieldType}
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -100,4 +102,20 @@ export class OvhApi{
 | 
				
			|||||||
      path: '/auth/time'
 | 
					      path: '/auth/time'
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  async getRecords({domain, fieldType = undefined, subDomain = undefined}){
 | 
				
			||||||
 | 
					    return await this.sendSignedRequest({
 | 
				
			||||||
 | 
					      path: `/domain/zone/${domain}/record`,
 | 
				
			||||||
 | 
					      body: {
 | 
				
			||||||
 | 
					        fieldType,
 | 
				
			||||||
 | 
					        subDomain
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  async getRecord({domain, id}){
 | 
				
			||||||
 | 
					    return await this.sendSignedRequest({
 | 
				
			||||||
 | 
					      path: `/domain/zone/${domain}/record/${id}`
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@ -2,12 +2,18 @@
 | 
				
			|||||||
  "ipApi": "https://api.ipify.org/",
 | 
					  "ipApi": "https://api.ipify.org/",
 | 
				
			||||||
  "updateInterval": 300,
 | 
					  "updateInterval": 300,
 | 
				
			||||||
  "randomInterval": true,
 | 
					  "randomInterval": true,
 | 
				
			||||||
  "records": [{
 | 
					  "records": {
 | 
				
			||||||
    "domain": "foo.bar",
 | 
					    "foo.bar": [
 | 
				
			||||||
    "subDomain": "foo.foo.bar",
 | 
					      {
 | 
				
			||||||
    "recordId": 0,
 | 
					        "fieldType": "A",
 | 
				
			||||||
    "ttl": 300
 | 
					        "subDomain": "sub",
 | 
				
			||||||
  }],
 | 
					        "id": 0,
 | 
				
			||||||
 | 
					        "target": "127.0.0.1",
 | 
				
			||||||
 | 
					        "ttl": 300,
 | 
				
			||||||
 | 
					        "zone": "foo.bar"
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    ]
 | 
				
			||||||
 | 
					  },
 | 
				
			||||||
  "ovhCredentials": {
 | 
					  "ovhCredentials": {
 | 
				
			||||||
    "applicationKey": "",
 | 
					    "applicationKey": "",
 | 
				
			||||||
    "applicationSecret": "",
 | 
					    "applicationSecret": "",
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										24
									
								
								setup.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								setup.js
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,24 @@
 | 
				
			|||||||
 | 
					import {JsonDataStore} from './JsonDataStore.js';
 | 
				
			||||||
 | 
					import {OvhApi} from "./OvhApi.js";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					let store = new JsonDataStore('config.json').getSyncedData();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					if (!store.ovhCredentials){
 | 
				
			||||||
 | 
					  console.log('ovhCredentials are undefined!');
 | 
				
			||||||
 | 
					  process.exit();
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					if (!store.ovhCredentials.applicationKey || !store.ovhCredentials.applicationSecret){
 | 
				
			||||||
 | 
					  console.log('applicationKey and/or applicationSecret are missing');
 | 
				
			||||||
 | 
					  process.exit();
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					let dns = new OvhApi({credentials: store.ovhCredentials, logs: false});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					if (!store.ovhCredentials.consumerKey) await dns.getConsumerKey();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					console.log('list all available records for defined domains =>');
 | 
				
			||||||
 | 
					Object.keys(store.records).forEach(domain => {
 | 
				
			||||||
 | 
					  dns.getRecords({domain}).then(ids => ids.forEach(id => {
 | 
				
			||||||
 | 
					    dns.getRecord({domain, id}).then(console.log);
 | 
				
			||||||
 | 
					  }));
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user