Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

Commit

Permalink
Merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
z720 committed Jul 24, 2020
2 parents 3ea094e + b7485e2 commit c8fea28
Show file tree
Hide file tree
Showing 10 changed files with 698 additions and 303 deletions.
6 changes: 6 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"esversion": 9,
"strict": "global",
"mocha": true,
"node": true
}
1 change: 1 addition & 0 deletions config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"use strict";
const defaultConfig = {
api_key: undefined,
endpoint: "https://dns.api.gandi.net/api/v5",
Expand Down
105 changes: 54 additions & 51 deletions gandi.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,61 @@
"use strict";

const request = require('request');

const messages = {
"e401": "Gandi Authorization declined, check GANDI_API_KEY",
"e404": "Record doesn't exists, Please create record in zone first (update only)",
"e4xx": "Unknown Error: ",
"error_domain": "Configuration error missing domain or record to manage",
"e401": "Gandi Authorization declined, check GANDI_API_KEY",
"e404": "Record doesn't exists, Please create record in zone first (update only)",
"e4xx": "Unknown Error: ",
"error_domain": "Configuration error missing domain or record to manage",
};

module.exports = function(config) {
let callAPI = function(data, func) {
/*
curl -H"X-Api-Key: $APIKEY" \
https://dns.api.gandi.net/api/v5/domains/example.com/records
*/

let method = (func == undefined) ? 'GET': 'PUT';
let callback = (func == undefined) ? data: func;
if ( typeof callback !== 'function' ) {
throw("Unexpected null callback");
}
if (config.domain == undefined || config.record == undefined) {
callback(messages.error_domain);
}
let options = {
method: method,
url: config.endpoint + "/domains/" + config.domain + "/records/" + config.record + "/A",
headers: {
'X-Api-Key': config.api_key
},
json: true
};
if (method == 'PUT') {
options.body = data;
}
if (config.debug) console.debug(method, options.url);
request(options, function(err, res, body) {
if(res.statusCode == 401) {
callback(messages.e401);
} else if (res.statusCode == 404) {
callback(messages.e404);
} else if (err || res.statusCode > 399) {
callback(messages.e4xx + body.message + "(" + err || res.statusCode + ")" , body);
} else {
callback(false, body);
}
});
};
return {
getRecord: function(callback) {
callAPI(callback);
},
updateRecord: function(data, callback) {
callAPI(data, callback);
}
};
let callAPI = function(data, func) {
/*
curl -H"X-Api-Key: $APIKEY" \
https://dns.api.gandi.net/api/v5/domains/example.com/records
*/

}
let method = (func == undefined) ? 'GET': 'PUT';
let callback = (func == undefined) ? data: func;
if ( typeof callback !== 'function' ) {
throw("Unexpected null callback");
}
if (config.domain == undefined || config.record == undefined) {
callback(messages.error_domain);
}
let options = {
method: method,
url: config.endpoint + "/domains/" + config.domain + "/records/" + config.record + "/A",
headers: {
'X-Api-Key': config.api_key
},
json: true
};
if (method == 'PUT') {
options.body = data;
}
if (config.debug) console.debug(method, options.url);
request(options, function(err, res, body) {
if(!res || err) {
callback(messages.e4xx + "(" + err || 'no status' + ")" , body);
} else if (res.statusCode && res.statusCode == 401) {
callback(messages.e401);
} else if (res && res.statusCode && res.statusCode == 404) {
callback(messages.e404);
} else if (res.statusCode > 399) {
callback(messages.e4xx + body.message + "(" + res.statusCode + ")" , body);
} else {
callback(false, body);
}
});
};
return {
getRecord: function(callback) {
callAPI(callback);
},
updateRecord: function(data, callback) {
callAPI(data, callback);
}
};
};
92 changes: 46 additions & 46 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,45 @@ const request = require('request');


const messages = {
"error_init": "Couldn't initiate process: ",
"ok": "Start monitoring IP ",
"dryrun": "Dry Run: current record: "
"error_init": "Couldn't initiate process: ",
"ok": "Start monitoring IP ",
"dryrun": "Dry Run: current record: "
};

let checkIP = function(domain) {
gandi.getRecord(function compareIP(err, record) {
if (config.debug) console.debug('Found ', record['rrset_name'], domain, record['rrset_values'][0]);
if(err) {
//Couldn't get current record at Gandi
console.error(err);
} else {
currentIp(function(err, current) {
let oldip = record['rrset_values'][0];
if (config.debug) console.debug('Current IP:', current.ip);
if (oldip != current.ip) {
record['rrset_values'][0] = current.ip;
gandi.updateRecord(record, function(err) {
if (err) {
console.error(err);
} else{
console.log('Previous IP ', oldip, 'Replaced by', current.ip);
}
});
}
})
}
})
gandi.getRecord(function compareIP(err, record) {
if (config.debug) console.debug('Found ', record.rrset_name, config.domain, record.rrset_values[0]);
if(err) {
//Couldn't get current record at Gandi
console.error(err);
} else {
currentIp(function(err, current) {
let oldip = record.rrset_values[0];
if (config.debug) console.debug('Current IP:', current.ip);
if (oldip != current.ip) {
record.rrset_values[0] = current.ip;
gandi.updateRecord(record, function(err) {
if (err) {
console.error(err);
} else{
console.log('Previous IP ', oldip, 'Replaced by', current.ip);
}
});
}
});
}
});
};

// check existing record
let currentIp = function(callback) {
request.get(config.ipcheck, {json: true}, (err, res, body) => {
if (err) {
callback(err, null);
} else {
callback(null, { ip: body });
}
})
request.get(config.ipcheck, {json: true}, (err, res, body) => {
if (err) {
callback(err, null);
} else {
callback(null, { ip: body });
}
});
};

let args = require('./args.js');
Expand All @@ -60,18 +60,18 @@ let gandi = require('./gandi.js')(config);

// first check: could we get the record
gandi.getRecord(function(err, record) {
if(err) {
console.error(messages.error_init, err);
} else {
if (!args.dryRun) {
console.log(messages.ok, record['rrset_name'], record['domain'], record['rrset_values'][0]);
checkIP(args.domain);
// start a loop every {interval} if interval
if (config.interval > 0) {
setInterval(checkIP, config.interval * 1000 );
}
} else {
console.log(messages.dryrun, record['rrset_name'], config.domain, record['rrset_values'][0]);
}
}
if(err) {
console.error(messages.error_init, err);
} else {
if (!args.dryRun) {
console.log(messages.ok, record.rrset_name, config.domain, record.rrset_values[0]);
checkIP(args.domain);
// start a loop every {interval} if interval
if (config.interval > 0) {
setInterval(checkIP, config.interval * 1000 );
}
} else {
console.log(messages.dryrun, record['rrset_name'], config.domain, record['rrset_values'][0]);
}
}
});
Loading

0 comments on commit c8fea28

Please sign in to comment.