add wait option

This commit is contained in:
AJ ONeal 2019-07-31 16:43:58 -06:00
parent 7efb7b650b
commit de22db43de
2 changed files with 128 additions and 123 deletions

View File

@ -1,137 +1,142 @@
'use strict'; "use strict";
var request; var request;
var defaults = { var defaults = {
baseUrl: 'https://api.name.com/v4/' baseUrl: "https://api.name.com/v4/",
wait: 15 * 1000
}; };
module.exports.create = function(config) { module.exports.create = function(config) {
var baseUrl = (config.baseUrl || defaults.baseUrl).replace(/\/$/, ''); var baseUrl = (config.baseUrl || defaults.baseUrl).replace(/\/$/, "");
var token = config.token; var token = config.token;
var username = config.username; var username = config.username;
var wait = config.wait || defaults.wait;
var plugin = { var plugin = {
init: function(opts) { init: function(opts) {
request = opts.request; request = opts.request;
return null; return null;
}, },
// We must list all zones (domains) in the account // We must list all zones (domains) in the account
zones: function(data) { zones: function(data) {
return api({ return api({
url: baseUrl + '/domains' url: baseUrl + "/domains"
}).then(function(resp) { }).then(function(resp) {
return resp.body.domains.map(function(d) { return resp.body.domains.map(function(d) {
//#console.log("Domain Name:", d.domainName); //#console.log("Domain Name:", d.domainName);
return d.domainName; return d.domainName;
}); });
}); });
}, },
// We must set each record as required // We must set each record as required
set: function(data) { set: function(data) {
// console.log('Add TXT', data); // console.log('Add TXT', data);
var ch = data.challenge; var ch = data.challenge;
if (!ch.dnsZone) { if (!ch.dnsZone) {
throw new Error( throw new Error(
'[Name.com Plugin] Unknown domain: ', "[Name.com Plugin] Unknown domain: ",
data.domain || data.dnsHost data.domain || data.dnsHost
); );
} }
return api({ return api({
method: 'POST', method: "POST",
url: baseUrl + '/domains/' + ch.dnsZone + '/records', url: baseUrl + "/domains/" + ch.dnsZone + "/records",
json: { json: {
host: ch.dnsPrefix, host: ch.dnsPrefix,
type: 'TXT', type: "TXT",
answer: ch.dnsAuthorization, answer: ch.dnsAuthorization,
ttl: 300 // minimum allowed value ttl: 300 // minimum allowed value
} }
}).then(function(resp) { }).then(function(resp) {
if (!resp.body.id) { if (!resp.body.id) {
throw Error('[Name.com API] [set] ' + resp.body); throw Error("[Name.com API] [set] " + resp.body);
} }
return null; // The api returns success long before the record is actually set.
}); // Therefore we wait a bit to make sure the server propagate the response.
}, return new Promise(function(resolve) {
setTimeout(resolve, wait, null);
});
});
},
// We must be able to confirm that the appropriate records were set // We must be able to confirm that the appropriate records were set
get: function(data) { get: function(data) {
// console.log('List TXT', data); // console.log('List TXT', data);
var ch = data.challenge; var ch = data.challenge;
if (!ch.dnsZone) { if (!ch.dnsZone) {
throw new Error( throw new Error(
'[Name.com Plugin] Unknown domain: ', "[Name.com Plugin] Unknown domain: ",
data.domain || data.dnsHost data.domain || data.dnsHost
); );
} }
return api({ return api({
url: baseUrl + '/domains/' + ch.dnsZone + '/records' url: baseUrl + "/domains/" + ch.dnsZone + "/records"
}).then(function(resp) { }).then(function(resp) {
var value = resp.body.records.filter(function(r) { var value = resp.body.records.filter(function(r) {
return ( return (
r.host === ch.dnsPrefix && r.host === ch.dnsPrefix &&
'TXT' === r.type && "TXT" === r.type &&
ch.dnsAuthorization === r.answer ch.dnsAuthorization === r.answer
); );
})[0]; })[0];
if (!value) { if (!value) {
return null; return null;
} }
// adding id to make re-usable for remove // adding id to make re-usable for remove
return { id: value.id, dnsAuthorization: value.answer }; return { id: value.id, dnsAuthorization: value.answer };
}); });
}, },
// We must delete junk records once we're done // We must delete junk records once we're done
remove: function(data) { remove: function(data) {
// console.log('Remove TXT', data); // console.log('Remove TXT', data);
var ch = data.challenge; var ch = data.challenge;
return plugin.get(data).then(function(r) { return plugin.get(data).then(function(r) {
if (!r.id) { if (!r.id) {
throw new Error( throw new Error(
'[Name.com Plugin] [del] Did not find TXT record for ' + "[Name.com Plugin] [del] Did not find TXT record for " + ch.dnsHost
ch.dnsHost );
); }
} return api({
return api({ method: "DELETE",
method: 'DELETE', url: baseUrl + "/domains/" + ch.dnsZone + "/records/" + r.id
url: baseUrl + '/domains/' + ch.dnsZone + '/records/' + r.id }).then(function(resp) {
}).then(function(resp) { return null;
return null; });
}); });
}); }
} };
};
// Authentication and Error handling here // Authentication and Error handling here
function api(opts) { function api(opts) {
opts.auth = { opts.auth = {
user: username, user: username,
pass: token, pass: token,
sendImmediately: true sendImmediately: true
}; };
if (!opts.json) { if (!opts.json) {
opts.json = true; opts.json = true;
} }
return request(opts).then(function(resp) { return request(opts).then(function(resp) {
if (!resp.body.message) { if (!resp.body.message) {
return resp; return resp;
} }
console.error(opts.method + ' ' + opts.url); console.error(opts.method + " " + opts.url);
console.error(resp.headers); console.error(resp.headers);
console.error(resp.body); console.error(resp.body);
throw new Error( throw new Error(
'[Name.com API] ' + "[Name.com API] " +
(opts.method || 'GET') + (opts.method || "GET") +
' ' + " " +
opts.url + opts.url +
' : ' + " : " +
resp.body.message resp.body.message
); );
}); });
} }
return plugin; return plugin;
}; };

View File

@ -1,6 +1,6 @@
{ {
"name": "acme-dns-01-namedotcom", "name": "acme-dns-01-namedotcom",
"version": "3.0.0", "version": "3.0.1",
"description": "Name.com + Let's Encrypt for Node.js - ACME dns-01 challenges w/ ACME.js and Greenlock.js", "description": "Name.com + Let's Encrypt for Node.js - ACME dns-01 challenges w/ ACME.js and Greenlock.js",
"main": "index.js", "main": "index.js",
"files": [ "files": [