Compare commits
3 Commits
7b13d4ec73
...
fe53d69e90
Author | SHA1 | Date | |
---|---|---|---|
fe53d69e90 | |||
fdd3dfb1d2 | |||
beaca3e61c |
2
.gitignore
vendored
2
.gitignore
vendored
@ -1 +1 @@
|
|||||||
config.json
|
config.json*
|
@ -13,11 +13,11 @@ export class DynDnsBot{
|
|||||||
if (newIp === this.lastIp) return;
|
if (newIp === this.lastIp) return;
|
||||||
this.lastIp = newIp;
|
this.lastIp = newIp;
|
||||||
Object.keys(this.config.records).forEach(domain => {
|
Object.keys(this.config.records).forEach(domain => {
|
||||||
this.config.records[domain].forEach(record => {
|
Promise.all(this.config.records[domain].map(async record => {
|
||||||
record.target = newIp;
|
record.target = newIp;
|
||||||
this.dns.updateRecord(record, domain).then(()=>this.dns.refreshZone(domain));
|
await this.dns.updateRecord(record, domain);
|
||||||
})
|
})).then(() => this.dns.refreshZone(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
|
||||||
|
25
OvhApi.js
25
OvhApi.js
@ -5,7 +5,7 @@ import readline from 'readline';
|
|||||||
export class OvhApi{
|
export class OvhApi{
|
||||||
constructor({credentials, logs = true}){
|
constructor({credentials, logs = true}){
|
||||||
this.credentials = credentials;
|
this.credentials = credentials;
|
||||||
this.log = logs ? console.log : ()=>{};
|
this.log = logs ? this.requestLogs : ()=>{};
|
||||||
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,29 +19,36 @@ export class OvhApi{
|
|||||||
getMethod=rest=>rest.get,
|
getMethod=rest=>rest.get,
|
||||||
header={accept: 'json'}
|
header={accept: 'json'}
|
||||||
}){
|
}){
|
||||||
this.log(`api request at ${path} =>`);
|
|
||||||
this.log(header);
|
|
||||||
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 => {
|
||||||
this.log('api response =>');
|
this.log({path, header, body, res: 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.response.text || err);
|
this.log({path, header, body, err: err?.response?.text});
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
requestLogs({path, header, body, res, err}){
|
||||||
|
console.log(`api request at ${path} =>`);
|
||||||
|
console.log('header:');
|
||||||
|
console.log(header);
|
||||||
|
console.log('body:')
|
||||||
|
console.log(body);
|
||||||
|
console.log('api response =>');
|
||||||
|
if (res) console.log(res);
|
||||||
|
if (err) console.error(err)
|
||||||
|
}
|
||||||
|
|
||||||
async sendSignedRequest({
|
async sendSignedRequest({
|
||||||
path,
|
path,
|
||||||
body={},
|
body={},
|
||||||
getMethod=rest=>rest.get,
|
getMethod=rest=>rest.get,
|
||||||
header={accept: 'json'}
|
header={accept: 'json'},
|
||||||
|
timestamp = (Date.now()/1000).toFixed()
|
||||||
}){
|
}){
|
||||||
if (!this.credentials.consumerKey) await this.getConsumerKey();
|
if (!this.credentials.consumerKey) await this.getConsumerKey();
|
||||||
let timestamp = await this.getApiTime();
|
|
||||||
header['X-Ovh-Timestamp'] = timestamp;
|
header['X-Ovh-Timestamp'] = timestamp;
|
||||||
header['X-Ovh-Signature'] = await this.getSignature({
|
header['X-Ovh-Signature'] = await this.getSignature({
|
||||||
method: getMethod(this.methods),
|
method: getMethod(this.methods),
|
||||||
|
7
setup.js
7
setup.js
@ -14,6 +14,13 @@ if (!store.ovhCredentials.applicationKey || !store.ovhCredentials.applicationSec
|
|||||||
|
|
||||||
let dns = new OvhApi({credentials: store.ovhCredentials, logs: false});
|
let dns = new OvhApi({credentials: store.ovhCredentials, logs: false});
|
||||||
|
|
||||||
|
let apiTime = await dns.getApiTime();
|
||||||
|
let serverTime = (Date.now()/1000).toFixed();
|
||||||
|
if (apiTime > serverTime+1 || apiTime < serverTime-1){
|
||||||
|
console.error(`serverTime (${serverTime}) and apiTIme (${apiTime}) are different`);
|
||||||
|
process.exit();
|
||||||
|
}
|
||||||
|
|
||||||
if (!store.ovhCredentials.consumerKey) await dns.getConsumerKey();
|
if (!store.ovhCredentials.consumerKey) await dns.getConsumerKey();
|
||||||
|
|
||||||
console.log('list all available records for defined domains =>');
|
console.log('list all available records for defined domains =>');
|
||||||
|
Loading…
Reference in New Issue
Block a user