2019-07-10 20:03:25 +00:00
|
|
|
'use strict';
|
|
|
|
|
2019-07-11 02:39:15 +00:00
|
|
|
var defaults = {
|
2019-07-12 01:32:43 +00:00
|
|
|
baseUrl: 'https://dns.api.gandi.net/api/v5/'
|
2019-07-11 02:39:15 +00:00
|
|
|
};
|
2019-07-10 20:03:25 +00:00
|
|
|
|
2019-07-12 03:58:22 +00:00
|
|
|
module.exports.create = function(config) {
|
2019-07-12 01:32:43 +00:00
|
|
|
var baseUrl = (config.baseUrl || defaults.baseUrl).replace(/\/$/, '');
|
2019-07-11 02:39:15 +00:00
|
|
|
var authtoken = config.token;
|
|
|
|
var request;
|
2019-07-12 01:32:43 +00:00
|
|
|
|
2019-07-10 20:03:25 +00:00
|
|
|
return {
|
2019-07-12 03:58:22 +00:00
|
|
|
init: function(opts) {
|
2019-07-10 20:03:25 +00:00
|
|
|
request = opts.request;
|
|
|
|
return null;
|
|
|
|
},
|
2019-07-11 02:39:15 +00:00
|
|
|
|
2019-07-12 03:58:22 +00:00
|
|
|
zones: function(opts) {
|
2019-07-11 02:39:15 +00:00
|
|
|
console.log(opts);
|
2019-07-12 01:32:43 +00:00
|
|
|
|
2019-07-11 02:39:15 +00:00
|
|
|
return request({
|
2019-07-12 01:32:43 +00:00
|
|
|
method: 'GET',
|
|
|
|
url: baseUrl + '/zones',
|
|
|
|
headers: {
|
|
|
|
'X-Api-Key': authtoken
|
|
|
|
},
|
|
|
|
json: true
|
2019-07-12 03:58:22 +00:00
|
|
|
}).then(function(resp) {
|
|
|
|
return resp.body.map(function(zone) {
|
2019-07-12 01:32:43 +00:00
|
|
|
return zone.name;
|
2019-07-12 03:58:22 +00:00
|
|
|
});
|
|
|
|
});
|
2019-07-12 01:32:43 +00:00
|
|
|
},
|
2019-07-11 02:39:15 +00:00
|
|
|
|
2019-07-12 03:58:22 +00:00
|
|
|
set: function(opts) {
|
2019-07-11 02:39:15 +00:00
|
|
|
console.log(opts);
|
2019-07-12 01:32:43 +00:00
|
|
|
|
2019-07-11 02:39:15 +00:00
|
|
|
return request({
|
|
|
|
method: 'GET',
|
2019-07-12 03:58:22 +00:00
|
|
|
url:
|
|
|
|
baseUrl +
|
|
|
|
'/domains/' +
|
|
|
|
opts.challenge.dnsZone +
|
|
|
|
'/records/' +
|
|
|
|
opts.challenge.dnsPrefix +
|
|
|
|
'/TXT',
|
2019-07-12 01:32:43 +00:00
|
|
|
headers: {
|
|
|
|
'X-Api-Key': authtoken
|
|
|
|
},
|
2019-07-11 02:39:15 +00:00
|
|
|
json: true
|
2019-07-12 03:58:22 +00:00
|
|
|
}).then(function(resp) {
|
2019-07-12 01:32:43 +00:00
|
|
|
if (resp.body.cause === 'Not Found') {
|
2019-07-11 02:39:15 +00:00
|
|
|
return request({
|
2019-07-12 01:32:43 +00:00
|
|
|
method: 'POST',
|
2019-07-12 03:58:22 +00:00
|
|
|
url:
|
|
|
|
baseUrl +
|
|
|
|
'/domains/' +
|
|
|
|
opts.challenge.dnsZone +
|
|
|
|
'/records',
|
2019-07-12 01:32:43 +00:00
|
|
|
headers: {
|
|
|
|
'X-Api-Key': authtoken
|
|
|
|
},
|
|
|
|
json: {
|
2019-07-12 03:58:22 +00:00
|
|
|
rrset_name: opts.challenge.dnsPrefix,
|
|
|
|
rrset_type: 'TXT',
|
|
|
|
rrset_ttl: 300,
|
|
|
|
rrset_values: [opts.challenge.dnsAuthorization]
|
2019-07-12 01:32:43 +00:00
|
|
|
}
|
2019-07-12 03:58:22 +00:00
|
|
|
});
|
2019-07-12 01:32:43 +00:00
|
|
|
} else {
|
2019-07-12 03:58:22 +00:00
|
|
|
const body = resp.body;
|
|
|
|
let value = body.rrset_values.map(x => JSON.parse(x));
|
2019-07-12 01:32:43 +00:00
|
|
|
|
|
|
|
if (body.rrset_values) {
|
|
|
|
return request({
|
|
|
|
method: 'PUT',
|
2019-07-12 03:58:22 +00:00
|
|
|
url:
|
|
|
|
baseUrl +
|
|
|
|
'/domains/' +
|
|
|
|
opts.challenge.dnsZone +
|
|
|
|
'/records/' +
|
|
|
|
opts.challenge.dnsPrefix +
|
|
|
|
'/TXT',
|
2019-07-12 01:32:43 +00:00
|
|
|
headers: {
|
|
|
|
'X-Api-Key': authtoken
|
|
|
|
},
|
|
|
|
json: {
|
2019-07-12 03:58:22 +00:00
|
|
|
rrset_ttl: 300,
|
|
|
|
rrset_values: value.concat([
|
|
|
|
opts.challenge.dnsAuthorization
|
|
|
|
])
|
2019-07-12 01:32:43 +00:00
|
|
|
}
|
2019-07-12 03:58:22 +00:00
|
|
|
});
|
2019-07-12 01:32:43 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-12 03:58:22 +00:00
|
|
|
});
|
2019-07-10 20:03:25 +00:00
|
|
|
},
|
2019-07-11 02:39:15 +00:00
|
|
|
|
2019-07-12 03:58:22 +00:00
|
|
|
remove: function(opts) {
|
2019-07-11 02:39:15 +00:00
|
|
|
console.log(opts);
|
2019-07-12 01:32:43 +00:00
|
|
|
|
2019-07-11 02:39:15 +00:00
|
|
|
return request({
|
|
|
|
method: 'DELETE',
|
2019-07-12 03:58:22 +00:00
|
|
|
url:
|
|
|
|
baseUrl +
|
|
|
|
'/domains/' +
|
|
|
|
opts.challenge.dnsZone +
|
|
|
|
'/records/' +
|
|
|
|
opts.challenge.dnsPrefix +
|
|
|
|
'/TXT',
|
2019-07-12 01:32:43 +00:00
|
|
|
headers: {
|
|
|
|
'X-Api-Key': authtoken
|
|
|
|
},
|
2019-07-11 02:39:15 +00:00
|
|
|
json: true
|
2019-07-12 03:58:22 +00:00
|
|
|
});
|
2019-07-12 01:32:43 +00:00
|
|
|
},
|
2019-07-11 02:39:15 +00:00
|
|
|
|
2019-07-12 03:58:22 +00:00
|
|
|
get: function(opts) {
|
2019-07-11 02:39:15 +00:00
|
|
|
console.log(opts);
|
2019-07-12 01:32:43 +00:00
|
|
|
|
2019-07-11 02:39:15 +00:00
|
|
|
return request({
|
|
|
|
method: 'GET',
|
2019-07-12 03:58:22 +00:00
|
|
|
url:
|
|
|
|
baseUrl +
|
|
|
|
'/domains/' +
|
|
|
|
opts.challenge.dnsZone +
|
|
|
|
'/records/' +
|
|
|
|
opts.challenge.dnsPrefix,
|
2019-07-12 01:32:43 +00:00
|
|
|
headers: {
|
|
|
|
'X-Api-Key': authtoken
|
|
|
|
},
|
2019-07-11 02:39:15 +00:00
|
|
|
json: true
|
2019-07-12 03:58:22 +00:00
|
|
|
}).then(function(resp) {
|
|
|
|
const body = resp.body;
|
2019-07-11 02:39:15 +00:00
|
|
|
if (body.length > 0) {
|
2019-07-12 03:58:22 +00:00
|
|
|
let value = body[0].rrset_values
|
|
|
|
.map(x => JSON.parse(x))
|
|
|
|
.filter(
|
|
|
|
field => field === opts.challenge.dnsAuthorization
|
|
|
|
);
|
2019-07-11 02:39:15 +00:00
|
|
|
if (value !== []) {
|
2019-07-12 01:32:43 +00:00
|
|
|
return {
|
|
|
|
dnsAuthorization: value[0]
|
2019-07-12 03:58:22 +00:00
|
|
|
};
|
2019-07-12 01:32:43 +00:00
|
|
|
} else {
|
2019-07-12 03:58:22 +00:00
|
|
|
return null;
|
2019-07-11 02:39:15 +00:00
|
|
|
}
|
2019-07-12 01:32:43 +00:00
|
|
|
} else {
|
2019-07-12 03:58:22 +00:00
|
|
|
return null;
|
2019-07-11 02:39:15 +00:00
|
|
|
}
|
2019-07-12 03:58:22 +00:00
|
|
|
});
|
2019-07-10 20:03:25 +00:00
|
|
|
}
|
2019-07-12 03:58:22 +00:00
|
|
|
};
|
|
|
|
};
|