acme-dns-01-vultr.js/lib/index.js

151 lines
3.8 KiB
JavaScript

'use strict';
var request = require('@root/request');
request = require('util').promisify(request);
var defaults = {
baseUrl: 'https://api.vultr.com/'
};
module.exports.create = function(config) {
var baseUrl = config.baseUrl || defaults.baseUrl;
var apiKey = config.apiKey;
return {
set: function(data) {
var ch = data.challenge;
var domainname = ch.identifier.value;
var zone = domainname;
var dnsPrefix = ch.dnsHost.replace(new RegExp('.' + zone + '$'), '');
var txt = ch.dnsAuthorization;
// If the domain to be verified is
var url = baseUrl + 'v1/dns/create_record';
console.log('adding txt', data);
return request({
method: 'POST',
url: url,
headers: {
'API-Key': apiKey
},
form: {
type: 'TXT',
name: dnsPrefix,
data: '"'+txt+'"', // vultr requires the TXT record wraped in quotes
domain: config.domain,
ttl: 300
}
}).then(function(resp) {
if(resp.statusCode==200){
return true;
}else{
console.log(resp.statusCode);
console.log(resp.body);
throw new Error('record did not set. check subdomain, api key, etc');
}
});
},
remove: function(data) {
var ch = data.challenge;
var domainname = data.challenge.altname;
var zone = domainname;
var url = baseUrl + 'v1/dns/records';
return request({
method: 'GET',
url: url+'?domain='+config.domain,
// PROBLEM (fixed): Remember to set json: true (not need to JSON.parse)
json: true,
headers: {
'API-Key': apiKey
}
}).then(function(resp) {
if(resp.statusCode==200){
resp = resp.body;
console.log(resp);
var entries =
resp.filter(function(x) {
return x.type === 'TXT';
});
var entry = entries.filter(function(x) {
// vultr wraps the TXT record in double quotes
return x.data.substring(1, x.data.length - 1) === ch.dnsAuthorization;
})[0];
if (entry) {
return entry['RECORDID'];
} else {
throw new Error(
"Couldn't remove record. check subdomain, api key, etc"
);
}
}else{
throw new Error("record did not set. check subdomain, api key, etc");
}
})
.then(function(recordId) {
var domainname = data.challenge.altname;
var zone = domainname;
var url = baseUrl + 'v1/dns/delete_record';
return request({
method: 'POST',
url: url,
headers: {
'API-Key': apiKey
},
form: {
domain: config.domain,
'RECORDID': recordId
}
}).then(function(resp) {
if(resp.statusCode==200){
return true;
}else{
console.log(resp.statusCode);
console.log(resp.body);
throw new Error("record did not remove. check subdomain, api key, etc");
}
});
});
},
get: function(data) {
var ch = data.challenge;
var domainname = data.challenge.altname;
var url = baseUrl + 'v1/dns/records';
console.log('getting txt', data);
// Digital ocean provides the api to fetch records by ID. Since we do not have id, we fetch all the records,
// filter the required TXT record
return request({
method: 'GET',
url: url+'?domain='+config.domain,
json: true,
headers: {
'API-Key': apiKey
}
}).then(function(resp) {
resp = resp.body;
var entries = resp.filter(function(x) {
return x.type === 'TXT';
});
var entry = entries.filter(function(x) {
// vultr wraps the TXT record in double quotes
return x.data.substring(1, x.data.length - 1) === ch.dnsAuthorization;
})[0];
if (entry) {
return { dnsAuthorization: entry.data.substring(1, entry.data.length - 1)};
} else {
return null;
}
});
}
};
};