Compare commits

..

No commits in common. "24ff180ea69375eac49a9a634f5870fd563190d1" and "02d9cc1e76b101c371a4eb015ac2f2096531ce51" have entirely different histories.

4 changed files with 25 additions and 75 deletions

View File

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

View File

@ -3,9 +3,8 @@ import sha1 from 'sha1';
import readline from 'readline'; import readline from 'readline';
export class OvhApi{ export class OvhApi{
constructor({credentials, logs = true}){ constructor({credentials}){
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({
@ -19,17 +18,17 @@ export class OvhApi{
getMethod=rest=>rest.get, getMethod=rest=>rest.get,
header={accept: 'json'} header={accept: 'json'}
}){ }){
this.log(`api request at ${path} =>`); console.log(`api request at ${path} =>`);
this.log(header); console.log(header);
this.log(body); console.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 => {
this.log('api response =>'); console.log('api response =>');
this.log(JSON.parse(res.text)); console.log(JSON.parse(res.text));
return JSON.parse(res.text); return JSON.parse(res.text);
}).catch(err => { }).catch(err => {
console.error(err.response.text || err); console.error(err);
return false; return false;
}); });
} }
@ -46,12 +45,11 @@ 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: JSON.stringify(body), body, timestamp
timestamp
}); });
header['X-Ovh-Consumer'] = this.credentials.consumerKey; header['X-Ovh-Consumer'] = this.credentials.consumerKey;
header['X-Ovh-Application'] = this.credentials.applicationKey; header['X-Ovh-Application'] = this.credentials.applicationName;
return await this.sendRequest({path, body, getMethod, header}); await this.sendRequest({path, body, getMethod, header});
} }
async getSignature({method = 'GET', query, body='', timestamp}){ async getSignature({method = 'GET', query, body='', timestamp}){
@ -89,11 +87,11 @@ export class OvhApi{
}); });
} }
async updateRecord({zone, subDomain, id, target, ttl = 3600, fieldType = 'A'}, domain = zone){ async updateRecord({domain, subDomain, recordId, target, ttl = 3600}){
return await this.sendSignedRequest({ return await this.sendSignedRequest({
path: `/domain/zone/${domain}/record/${id}`, path: `/domain/zone/${domain}/record/${recordId}`,
getMethod: rest=>rest.put, getMethod: rest=>rest.put,
body: {subDomain, target, ttl, fieldType} body: {subDomain, target, ttl}
}); });
} }
@ -102,20 +100,4 @@ 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}`
});
}
} }

View File

@ -2,18 +2,12 @@
"ipApi": "https://api.ipify.org/", "ipApi": "https://api.ipify.org/",
"updateInterval": 300, "updateInterval": 300,
"randomInterval": true, "randomInterval": true,
"records": { "records": [{
"foo.bar": [ "domain": "foo.bar",
{ "subDomain": "foo.foo.bar",
"fieldType": "A", "recordId": 0,
"subDomain": "sub", "ttl": 300
"id": 0, }],
"target": "127.0.0.1",
"ttl": 300,
"zone": "foo.bar"
}
]
},
"ovhCredentials": { "ovhCredentials": {
"applicationKey": "", "applicationKey": "",
"applicationSecret": "", "applicationSecret": "",

View File

@ -1,24 +0,0 @@
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);
}));
});