Compare commits

...

3 Commits

@ -12,16 +12,18 @@ export class DynDnsBot{
let newIp = await this.getIp();
if (newIp === this.lastIp) return;
this.lastIp = newIp;
this.config.records.forEach(record => {
record.target = newIp;
this.dns.updateRecord(record);
Object.keys(this.config.records).forEach(domain => {
this.config.records[domain].forEach(record => {
record.target = newIp;
this.dns.updateRecord(record, domain);
})
});
}
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);
? () => setTimeout(()=>this.update(), Math.random()*seconds*1000)
: () => this.update();
this.interval = setInterval(handler, seconds*1000);
this.update();
}
async getIp(){

@ -3,8 +3,9 @@ import sha1 from 'sha1';
import readline from 'readline';
export class OvhApi{
constructor({credentials}){
constructor({credentials, logs = true}){
this.credentials = credentials;
this.log = logs ? console.log : ()=>{};
this.baseUrl = this.credentials.apiUrl;
this.methods = {get: 'GET', post: 'POST', put: 'PUT', delete: 'DELETE', path: 'PATCH'};
this.rl = readline.createInterface({
@ -18,17 +19,17 @@ export class OvhApi{
getMethod=rest=>rest.get,
header={accept: 'json'}
}){
console.log(`api request at ${path} =>`);
console.log(header);
console.log(body);
this.log(`api request at ${path} =>`);
this.log(header);
this.log(body);
let request = getMethod(rest)(`${this.baseUrl}${path}`);
await Object.keys(header).forEach(key => request.set(key, header[key]));
return await request.send(body).then(res => {
console.log('api response =>');
console.log(JSON.parse(res.text));
this.log('api response =>');
this.log(JSON.parse(res.text));
return JSON.parse(res.text);
}).catch(err => {
console.error(err);
console.error(err.response.text || err);
return false;
});
}
@ -45,11 +46,12 @@ export class OvhApi{
header['X-Ovh-Signature'] = await this.getSignature({
method: getMethod(this.methods),
query: this.baseUrl+path,
body, timestamp
body: JSON.stringify(body),
timestamp
});
header['X-Ovh-Consumer'] = this.credentials.consumerKey;
header['X-Ovh-Application'] = this.credentials.applicationName;
await this.sendRequest({path, body, getMethod, header});
header['X-Ovh-Application'] = this.credentials.applicationKey;
return await this.sendRequest({path, body, getMethod, header});
}
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({
path: `/domain/zone/${domain}/record/${recordId}`,
path: `/domain/zone/${domain}/record/${id}`,
getMethod: rest=>rest.put,
body: {subDomain, target, ttl}
body: {subDomain, target, ttl, fieldType}
});
}
@ -100,4 +102,20 @@ export class OvhApi{
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/",
"updateInterval": 300,
"randomInterval": true,
"records": [{
"domain": "foo.bar",
"subDomain": "foo.foo.bar",
"recordId": 0,
"ttl": 300
}],
"records": {
"foo.bar": [
{
"fieldType": "A",
"subDomain": "sub",
"id": 0,
"target": "127.0.0.1",
"ttl": 300,
"zone": "foo.bar"
}
]
},
"ovhCredentials": {
"applicationKey": "",
"applicationSecret": "",

@ -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…
Cancel
Save