bug fixes #2

Closed
Ghost wants to merge 3 commits from fixes into master
3 changed files with 299 additions and 249 deletions

View File

@ -5,13 +5,11 @@ var request; // = require('@root/request');
var parseString = require('xml2js').parseString; var parseString = require('xml2js').parseString;
parseString = util.promisify(parseString); parseString = util.promisify(parseString);
const SANDBOX_URL = 'https://api.sandbox.namecheap.com/xml.response'; const SANDBOX_URL = 'https://api.sandbox.namecheap.com/xml.response';
const PRODUCTION_URL = 'https://api.namecheap.com/xml.response'; const PRODUCTION_URL = 'https://api.namecheap.com/xml.response';
var defaults = { var defaults = {
baseUrl: PRODUCTION_URL baseUrl: SANDBOX_URL
}; };
function extend(obj) { function extend(obj) {
@ -25,13 +23,19 @@ function extend(obj) {
} }
function assign(obj1, obj2) { function assign(obj1, obj2) {
for (var attrname in obj2) { obj1[attrname] = obj2[attrname]; } for (var attrname in obj2) {
obj1[attrname] = obj2[attrname];
}
} }
function requestUrl(baseUrl, params) { function requestUrl(baseUrl, params) {
var queryString = Object.keys(params).map(function (key) { var queryString = Object.keys(params)
return encodeURIComponent(key) + '=' + encodeURIComponent(params[key]); .map(function(key) {
}).join('&'); return (
encodeURIComponent(key) + '=' + encodeURIComponent(params[key])
);
})
.join('&');
// console.debug(queryString); // console.debug(queryString);
return baseUrl + '?' + queryString; return baseUrl + '?' + queryString;
} }
@ -53,22 +57,40 @@ module.exports.create = function (config) {
assign(requestParams, params); assign(requestParams, params);
var url = requestUrl(baseUrl, requestParams); var url = requestUrl(baseUrl, requestParams);
// console.log(url); console.log('DEBUG >>> url: ' + url);
console.log(
'DEBUG >>> requestParams: ' + JSON.stringify(requestParams, null, 2)
);
return request({ return request({
method: 'POST', method: 'POST',
url: url, url: url
}).then(function(response) { }).then(function(response) {
var responseBody = response.body; var responseBody = response.body;
// console.log(responseBody); // console.log(responseBody);
return parseString(responseBody).then(function(result) { return parseString(responseBody).then(function(result) {
// check response status // check response status
if (result['ApiResponse']['$']['Status'] === 'ERROR') { if (result['ApiResponse']['$']['Status'] === 'ERROR') {
for (let i = 0; i < result['ApiResponse']['Errors'].length; i++) { for (
console.log(result['ApiResponse']['Errors'][i]) let i = 0;
i < result['ApiResponse']['Errors'].length;
i++
) {
console.log(
'DEBUG >>> error: ' +
JSON.stringify(
result['ApiResponse']['Errors'][i][
'Error'
][0],
null,
2
)
);
} }
throw new Error('API Error'); throw new Error('API Error');
} else { // Status="OK" } else {
return result['ApiResponse']['CommandResponse'][0] // Status="OK"
return result['ApiResponse']['CommandResponse'][0];
} }
}); });
}); });
@ -81,36 +103,57 @@ module.exports.create = function (config) {
}, },
zones: function(data) { zones: function(data) {
return api('namecheap.domains.getList',{}).then(function (zonesResponse) { return api('namecheap.domains.getList', {}).then(function(
zonesResponse
) {
// console.log('zones'); // console.log('zones');
// console.log(zonesResponse); // console.log(zonesResponse);
return zonesResponse['DomainGetListResult'].map(function(x) { return zonesResponse['DomainGetListResult'].map(function(x) {
return x['Domain'][0]['$']['Name']; return x['Domain'][0]['$']['Name'];
}); });
}); });
}, },
set: function(data) { set: function(data) {
console.log(`DEBUG >>> data: ${JSON.stringify(data, null, 2)}`);
var ch = data.challenge; var ch = data.challenge;
var txt = ch.dnsAuthorization; var txt = ch.dnsAuthorization;
var params = {}; var params = {};
var zone = ch.dnsZone; // var zone = ch.dnsZone;
var zone = ch.identifier.value;
console.log(`DEBUG >>> zone: ${zone}`);
// the domain is the first part // the domain is the first part
params['SLD'] = zone.split('.')[0]; // params['SLD'] = zone.split('.')[0];
// the rest of the components are the TLD // the rest of the components are the TLD
params['TLD'] = zone.split('.').splice(1).join('.'); // params['TLD'] = zone.split('.').splice(1).join('.');
var domains = zone.split('.');
console.log('DEBUG >>> ' + domains);
// if you have subdomain foo.blah.com, SLD = blah and TLD = com
params['TLD'] = domains[domains.length - 1];
params['SLD'] = domains[domains.length - 2];
console.log(`DEBUG >>> SLD: ${params['SLD']}`);
console.log(`DEBUG >>> TLD: ${params['TLD']}`);
// setting a host record overwrites all existing, // setting a host record overwrites all existing,
// adding a new records means you've have to send back all previous records too // adding a new records means you've have to send back all previous records too
return api('namecheap.domains.dns.getHosts',params).then(function (hostsResponse) { return api('namecheap.domains.dns.getHosts', params).then(function(
var currentHostRecordsCount = hostsResponse['DomainDNSGetHostsResult'][0]['host'].length; hostsResponse
) {
var currentHostRecordsCount =
hostsResponse['DomainDNSGetHostsResult'][0]['host'].length;
for (var i = 0; i < currentHostRecordsCount; i++) { for (var i = 0; i < currentHostRecordsCount; i++) {
// console.log(hostsResponse['DomainDNSGetHostsResult'][i]['host'][0]); // console.log(hostsResponse['DomainDNSGetHostsResult'][i]['host'][0]);
var currentEntry = hostsResponse['DomainDNSGetHostsResult'][0]['host'][i]['$']; var currentEntry =
hostsResponse['DomainDNSGetHostsResult'][0]['host'][i][
'$'
];
params['HostName' + (i + 1)] = currentEntry['Name']; params['HostName' + (i + 1)] = currentEntry['Name'];
params['RecordType' + (i + 1)] = currentEntry['Type']; params['RecordType' + (i + 1)] = currentEntry['Type'];
@ -118,42 +161,52 @@ module.exports.create = function (config) {
params['TTL' + (i + 1)] = currentEntry['TTL']; params['TTL' + (i + 1)] = currentEntry['TTL'];
} }
params['HostName'+(currentHostRecordsCount+1)] = ch.dnsPrefix; params['HostName' + (currentHostRecordsCount + 1)] =
ch.dnsPrefix;
params['RecordType' + (currentHostRecordsCount + 1)] = 'TXT'; params['RecordType' + (currentHostRecordsCount + 1)] = 'TXT';
params['Address' + (currentHostRecordsCount + 1)] = txt; params['Address' + (currentHostRecordsCount + 1)] = txt;
params['TTL' + (currentHostRecordsCount + 1)] = 100; // in minutes params['TTL' + (currentHostRecordsCount + 1)] = 100; // in minutes
// console.log(params); // console.log(params);
return api('namecheap.domains.dns.setHosts',params).then(function (setHostResponse) { return api('namecheap.domains.dns.setHosts', params)
.then(function(setHostResponse) {
// console.log('setHost'); // console.log('setHost');
// console.log(setHostResponse); // console.log(setHostResponse);
return true return true;
}).catch(function (err) { })
throw new Error('record did not set. check subdomain, api key, etc'); .catch(function(err) {
throw new Error(
'record did not set. check subdomain, api key, etc'
);
}); });
}); });
}, },
remove: function(data) { remove: function(data) {
var ch = data.challenge; var ch = data.challenge;
var params = {}; var params = {};
var zone = ch.dnsZone; var zone = ch.identifier.value;
// the domain is the first part var domains = zone.split('.');
params['SLD'] = zone.split('.')[0];
// the rest of the components are the TLD params['TLD'] = domains[domains.length - 1];
params['TLD'] = zone.split('.').splice(1).join('.'); params['SLD'] = domains[domains.length - 2];
// setting a host record overwrites all existing, // setting a host record overwrites all existing,
// removing a new records means you've have to send back all previous records without removed // removing a new records means you've have to send back all previous records without removed
return api('namecheap.domains.dns.getHosts',params).then(function (hostsResponse) { return api('namecheap.domains.dns.getHosts', params).then(function(
var currentHostRecordsCount = hostsResponse['DomainDNSGetHostsResult'][0]['host'].length; hostsResponse
) {
var currentHostRecordsCount =
hostsResponse['DomainDNSGetHostsResult'][0]['host'].length;
for (var i = 0; i < currentHostRecordsCount; i++) { for (var i = 0; i < currentHostRecordsCount; i++) {
// console.log(hostsResponse['DomainDNSGetHostsResult'][i]['host'][0]); // console.log(hostsResponse['DomainDNSGetHostsResult'][i]['host'][0]);
var currentEntry = hostsResponse['DomainDNSGetHostsResult'][0]['host'][i]['$']; var currentEntry =
hostsResponse['DomainDNSGetHostsResult'][0]['host'][i][
'$'
];
if (currentEntry['Address'] != ch.dnsAuthorization) { if (currentEntry['Address'] != ch.dnsAuthorization) {
params['HostName' + (i + 1)] = currentEntry['Name']; params['HostName' + (i + 1)] = currentEntry['Name'];
params['RecordType' + (i + 1)] = currentEntry['Type']; params['RecordType' + (i + 1)] = currentEntry['Type'];
@ -162,31 +215,36 @@ module.exports.create = function (config) {
} }
} }
return api('namecheap.domains.dns.setHosts',params).then(function (setHostResponse) { return api('namecheap.domains.dns.setHosts', params)
.then(function(setHostResponse) {
// console.log('setHost'); // console.log('setHost');
// console.log(setHostResponse); // console.log(setHostResponse);
return true return true;
}).catch(function (err) { })
throw new Error('record did not remove. check subdomain, api key, etc'); .catch(function(err) {
throw new Error(
'record did not remove. check subdomain, api key, etc'
);
}); });
}); });
}, },
get: function(data) { get: function(data) {
var ch = data.challenge; var ch = data.challenge;
var params = {}; var params = {};
var zone = ch.dnsZone; var zone = ch.identifier.value;
var domains = zone.split('.');
// the domain is the first part params['TLD'] = domains[domains.length - 1];
params['SLD'] = zone.split('.')[0]; params['SLD'] = domains[domains.length - 2];
// the rest of the components are the TLD
params['TLD'] = zone.split('.').splice(1).join('.');
return api('namecheap.domains.dns.getHosts',params).then(function (hostsResponse) { return api('namecheap.domains.dns.getHosts', params).then(function(
hostsResponse
) {
// console.log('hosts'); // console.log('hosts');
// console.log(hostsResponse); // console.log(hostsResponse);
var currentHostRecords = hostsResponse['DomainDNSGetHostsResult'][0]['host']; var currentHostRecords =
hostsResponse['DomainDNSGetHostsResult'][0]['host'];
var entries = currentHostRecords.filter(function(x) { var entries = currentHostRecords.filter(function(x) {
return x['$']['Type'] === 'TXT'; return x['$']['Type'] === 'TXT';
@ -203,9 +261,7 @@ module.exports.create = function (config) {
} else { } else {
return null; return null;
} }
}); });
} }
}; };
}; };

9
package-lock.json generated
View File

@ -26,15 +26,6 @@
} }
} }
}, },
"acme-dns-01-test": {
"version": "3.3.1",
"resolved": "https://registry.npmjs.org/acme-dns-01-test/-/acme-dns-01-test-3.3.1.tgz",
"integrity": "sha512-di2/n19FDLc/pe4CDxd/FpxuuCZG7CHEQVjWr96vvtxe5XNNgdHi2eJqVP0z9WBf9s61zxslyRPrAWzTN8ZVWw==",
"dev": true,
"requires": {
"acme-challenge-test": "^3.3.1"
}
},
"sax": { "sax": {
"version": "1.2.4", "version": "1.2.4",
"resolved": "https://registry.npmjs.org/sax/-/sax-1.2.4.tgz", "resolved": "https://registry.npmjs.org/sax/-/sax-1.2.4.tgz",

View File

@ -3,6 +3,10 @@
"version": "3.0.0", "version": "3.0.0",
"description": "Namecheap DNS for Let's Encrypt / ACME dns-01 challenges with ACME.js and Greenlock.js", "description": "Namecheap DNS for Let's Encrypt / ACME dns-01 challenges with ACME.js and Greenlock.js",
"main": "index.js", "main": "index.js",
"files": [
"lib",
"test.js"
],
"scripts": { "scripts": {
"test": "node ./test.js" "test": "node ./test.js"
}, },
@ -30,7 +34,6 @@
"xml2js": "^0.4.19" "xml2js": "^0.4.19"
}, },
"devDependencies": { "devDependencies": {
"acme-challenge-test": "^3.3.2", "acme-challenge-test": "^3.3.2"
"acme-dns-01-test": "^3.2.1"
} }
} }