2019-07-24 04:33:32 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var request;
|
2019-07-27 04:47:54 +00:00
|
|
|
var defaults = {
|
|
|
|
baseUrl: 'https://api.dnsimple.com/v2/'
|
|
|
|
};
|
2019-07-24 04:33:32 +00:00
|
|
|
|
|
|
|
module.exports.create = function(config) {
|
2019-07-27 04:47:54 +00:00
|
|
|
var baseUrl = (config.baseUrl || defaults.baseUrl).replace(/\/$/, '');
|
|
|
|
var token = config.token;
|
|
|
|
var account = config.account;
|
|
|
|
|
|
|
|
function api(method, path, form) {
|
|
|
|
var req = {
|
|
|
|
method: method,
|
|
|
|
url: baseUrl + path,
|
|
|
|
headers: {
|
|
|
|
Authorization: 'Bearer ' + token,
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
json: true,
|
|
|
|
form: form
|
|
|
|
};
|
|
|
|
return request(req).then(function(resp) {
|
|
|
|
if (2 !== Math.floor(resp.statusCode / 100)) {
|
|
|
|
console.error(resp.statusCode, req.url);
|
|
|
|
console.error();
|
|
|
|
console.error('Request:');
|
|
|
|
console.error(req);
|
|
|
|
console.error();
|
|
|
|
console.error('Response:');
|
|
|
|
console.error(resp.body);
|
|
|
|
console.error();
|
|
|
|
throw new Error(
|
|
|
|
'Error response. Check token, baseUrl, domains, etc.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return resp;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-07-24 04:33:32 +00:00
|
|
|
return {
|
|
|
|
init: function(opts) {
|
|
|
|
request = opts.request;
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
zones: function(data) {
|
2019-07-25 15:56:01 +00:00
|
|
|
// console.info('List Zones', data);
|
2019-07-27 04:47:54 +00:00
|
|
|
return api('GET', '/' + account + '/zones').then(function(resp) {
|
|
|
|
return resp['body']['data'].map(function(elem) {
|
|
|
|
//console.log('DEBUG >>> elem.name: ' + elem.name);
|
|
|
|
return elem.name;
|
2019-07-25 15:56:01 +00:00
|
|
|
});
|
|
|
|
});
|
2019-07-24 04:33:32 +00:00
|
|
|
},
|
|
|
|
set: function(data) {
|
2019-07-24 04:41:01 +00:00
|
|
|
var ch = data.challenge;
|
|
|
|
if (!ch.dnsZone) {
|
|
|
|
throw new Error('No matching zone for ' + ch.dnsHost);
|
|
|
|
}
|
2019-07-24 04:33:32 +00:00
|
|
|
// console.info('Add TXT', data);
|
2019-07-27 04:47:54 +00:00
|
|
|
var ch = data.challenge;
|
|
|
|
var txt = ch.dnsAuthorization;
|
|
|
|
|
|
|
|
return api(
|
|
|
|
'POST',
|
|
|
|
'/' + account + '/zones/' + ch.dnsZone + '/records',
|
|
|
|
{
|
|
|
|
name: '',
|
|
|
|
type: 'TXT',
|
|
|
|
content: txt
|
|
|
|
}
|
|
|
|
).then(function(resp) {
|
|
|
|
if (resp.statusCode === 201) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
throw new Error(
|
|
|
|
'record did not set. check subdomain, api key, etc'
|
|
|
|
);
|
|
|
|
});
|
2019-07-24 04:33:32 +00:00
|
|
|
},
|
|
|
|
get: function(data) {
|
|
|
|
// console.info('List TXT', data);
|
2019-07-25 15:56:01 +00:00
|
|
|
|
2019-07-27 04:47:54 +00:00
|
|
|
return api(
|
|
|
|
'GET',
|
|
|
|
'/' + account + '/zones/' + data.challenge.dnsZone + '/records'
|
|
|
|
).then(function(resp) {
|
|
|
|
var record = resp.body.data.filter(function(record) {
|
|
|
|
return data.challenge.dnsAuthorization === record.content;
|
|
|
|
})[0];
|
2019-07-25 15:56:01 +00:00
|
|
|
|
2019-07-27 04:47:54 +00:00
|
|
|
if (record) return { dnsAuthorization: record.content };
|
|
|
|
else return null;
|
|
|
|
});
|
2019-07-25 15:56:01 +00:00
|
|
|
},
|
|
|
|
remove: function(data) {
|
2019-07-27 04:47:54 +00:00
|
|
|
return api(
|
|
|
|
'GET',
|
|
|
|
'/' + account + '/zones/' + data.challenge.dnsZone + '/records'
|
|
|
|
).then(function(resp) {
|
|
|
|
var record = resp.body.data.filter(function(record) {
|
|
|
|
return data.challenge.dnsAuthorization === record.content;
|
|
|
|
})[0];
|
2019-07-25 15:56:01 +00:00
|
|
|
|
2019-07-27 04:47:54 +00:00
|
|
|
if (record) {
|
|
|
|
return api(
|
|
|
|
'DELETE',
|
|
|
|
'/' +
|
|
|
|
account +
|
|
|
|
'/zones/' +
|
|
|
|
data.challenge.dnsZone +
|
|
|
|
'/records/' +
|
|
|
|
record.id
|
|
|
|
).then(function(resp) {
|
|
|
|
// console.info('DEBUG >>> resp: ', JSON.stringify(resp, null, 2));
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
throw new Error('Txt Record not found for removal');
|
|
|
|
}
|
|
|
|
});
|
2019-07-24 04:33:32 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|