Compare commits

..

No commits in common. "5d966f422a2977c87c657f88847932b0be943adc" and "1b193248ed097fac370d01858856d061d92e6b9d" have entirely different histories.

6 changed files with 28 additions and 102 deletions

View File

@ -18,15 +18,14 @@ 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 this.interval = setInterval(random
?()=>setTimeout(callback, Math.random()*seconds*1000) ?setTimeout(this.update, Math.random()*seconds*1000)
:callback; :this.update,
this.interval = setInterval(handler(this.update), seconds*1000); seconds*1000);
this.update();
} }
async getIp(){ async getIp(){
return await rest.get(this.config.ipApi).then(res => { return await rest.get(this.config.ipApi).then(newIp => {
return JSON.parse(res.text); return newIp;
}).catch(console.error); }).catch(console.error);
} }
} }

View File

@ -1,73 +1,37 @@
import rest from 'superagent'; import rest from 'superagent';
import sha1 from 'sha1';
import readline from 'readline';
export class OvhApi{ export class OvhApi{
constructor({credentials}){ async constructor({credentials}){
this.credentials = credentials; this.credentials = credentials;
this.baseUrl = this.credentials.apiUrl; this.baseUrl = credentials.apiUrl;
this.methods = {get: 'GET', post: 'POST', put: 'PUT', delete: 'DELETE', path: 'PATCH'}; this.apiToken = await this.getToken({applicationKey: credentials.applicationKey});
this.rl = readline.createInterface({
input: process.stdin
});
} }
async sendRequest({ async sendRequest({
path, path,
body={}, obj={},
getMethod=rest=>rest.get, method=rest=>rest.get,
header={accept: 'json'} header={accept: 'json'}
}){ }){
console.log(`api request at ${path} =>`); let request = method(rest)(`${this.baseUrl}${path}`);
console.log(header);
console.log(body);
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(obj).then(res => {
console.log('api response =>');
console.log(JSON.parse(res.text));
return JSON.parse(res.text); return JSON.parse(res.text);
}).catch(err => { }).catch(err => {
console.error(err); console.log(err);
return false; return false;
}); });
} }
async sendSignedRequest({ async getToken({applicationKey}){
path, return await this.sendRequest({
body={},
getMethod=rest=>rest.get,
header={accept: 'json'}
}){
if (!this.credentials.consumerKey) await this.getConsumerKey();
let timestamp = await this.getApiTime();
header['X-Ovh-Timestamp'] = timestamp;
header['X-Ovh-Signature'] = await this.getSignature({
method: getMethod(this.methods),
query: this.baseUrl+path,
body, timestamp
});
header['X-Ovh-Consumer'] = this.credentials.consumerKey;
await this.sendRequest({path, body, getMethod, header});
}
async getSignature({method = 'GET', query, body='', timestamp}){
return '$1$' + sha1(
this.credentials.applicationSecret+'+'+
this.credentials.consumerKey+'+'+
method+'+'+query+'+'+body+'+'+timestamp
);
}
async getConsumerKey(){
let res = await this.sendRequest({
path: '/auth/credential', path: '/auth/credential',
getMethod: rest=>rest.post, method: rest=>rest.post,
header: { header: {
'X-Ovh-Application': this.credentials.applicationKey, 'X-Ovh-Application': applicationKey,
'Content-type': 'application/json' 'Content-type': 'application/json'
}, },
body: { obj: {
accessRules: [ accessRules: [
{method: 'GET', path: '/domain/zone/*'}, {method: 'GET', path: '/domain/zone/*'},
{method: 'POST', path: '/domain/zone/*'}, {method: 'POST', path: '/domain/zone/*'},
@ -75,28 +39,17 @@ export class OvhApi{
] ]
} }
}); });
this.credentials.consumerKey = res.consumerKey;
console.log('please validate on ovh site:');
console.log(res.validationUrl);
await this.rl.question('continue? (Y/n)', () => {
switch (input) {
case 'n': process.exit(); break;
default: return res.consumerKey;
}
});
} }
async updateRecord({domain, subDomain, recordId, target, ttl = 3600}){ async updateRecord({domain, subDomain, recordId, target, ttl = 3600}){
return await this.sendSignedRequest({
path: `/domain/zone/${domain}/record/${recordId}`,
getMethod: rest=>rest.put,
body: {subDomain, target, ttl}
});
}
async getApiTime(){
return await this.sendRequest({ return await this.sendRequest({
path: '/auth/time' path: `/domain/zone/${domain}/record/${recordId}`,
method: rest=>rest.put,
obj: {
subDomain,
target,
ttl
}
}); });
} }
} }

View File

@ -1,13 +0,0 @@
version: '3'
services:
node:
image: node:alpine
container_name: ovh-api-dyndns
restart: always
volumes:
- ./:/home/node/app/
- ./node_logs/:/var/log/
working_dir: /home/node/app/
environment:
- NODE_ENV=production
command: sh -c 'npm i && nodejs index.js'

View File

@ -10,7 +10,6 @@
}], }],
"ovhCredentials": { "ovhCredentials": {
"applicationKey": "", "applicationKey": "",
"applicationSecret": "",
"apiUrl": "https://ca.api.ovh.com/1.0" "apiUrl": "https://ca.api.ovh.com/1.0"
} }
} }

View File

@ -1,16 +1,5 @@
import {DynDnsBot} from './DynDnsBot.js'; import {DynDnsBot} from './DynDnsBot.js';
import {loadData, storeData} from './jsonDataStore.js'; import {loadData} from './jsonDataStore.js';
let configPath = 'config.json'; let bot = new DynDnsBot({config: loadData('config.json')});
let config = loadData(configPath);
let configProxy = new Proxy(config, {
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();

View File

@ -11,7 +11,6 @@
"author": "adb", "author": "adb",
"license": "", "license": "",
"dependencies": { "dependencies": {
"sha1": "^1.1.1",
"superagent": "^6.1.0" "superagent": "^6.1.0"
}, },
"devDependencies": { "devDependencies": {