bug fixes #2

Closed
Ghost wants to merge 3 commits from fixes into master
3 changed files with 298 additions and 263 deletions
Showing only changes of commit 0b387a150f - Show all commits

View File

@ -5,222 +5,263 @@ 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: SANDBOX_URL baseUrl: SANDBOX_URL
}; };
function extend(obj) { function extend(obj) {
var newObj = {}; var newObj = {};
for (var i in obj) { for (var i in obj) {
if (obj.hasOwnProperty(i)) { if (obj.hasOwnProperty(i)) {
newObj[i] = obj[i]; newObj[i] = obj[i];
} }
} }
return newObj; return newObj;
} }
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 (
// console.debug(queryString); encodeURIComponent(key) + '=' + encodeURIComponent(params[key])
return baseUrl + '?' + queryString; );
})
.join('&');
// console.debug(queryString);
return baseUrl + '?' + queryString;
} }
module.exports.create = function (config) { module.exports.create = function(config) {
// config = { baseUrl, token } // config = { baseUrl, token }
var baseUrl = config.baseUrl || defaults.baseUrl; var baseUrl = config.baseUrl || defaults.baseUrl;
var globalParams = { var globalParams = {
apiUser: config.apiUser, apiUser: config.apiUser,
apiKey: config.apiKey, apiKey: config.apiKey,
username: config.username, username: config.username,
ClientIp: config.clientIp ClientIp: config.clientIp
}; };
function api(command, params) { function api(command, params) {
var requestParams = extend(globalParams); var requestParams = extend(globalParams);
requestParams['Command'] = command; requestParams['Command'] = command;
assign(requestParams,params); assign(requestParams, params);
var url = requestUrl(baseUrl, requestParams); var url = requestUrl(baseUrl, requestParams);
console.log('DEBUG >>> url: ' + url); console.log('DEBUG >>> url: ' + url);
console.log('DEBUG >>> requestParams: ' + JSON.stringify(requestParams, null, 2)); 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('DEBUG >>> error: ' + JSON.stringify(result['ApiResponse']['Errors'][i]['Error'][0], null, 2)); let i = 0;
} i < result['ApiResponse']['Errors'].length;
throw new Error('API Error'); i++
} else { // Status="OK" ) {
return result['ApiResponse']['CommandResponse'][0] console.log(
} 'DEBUG >>> error: ' +
}); JSON.stringify(
}); result['ApiResponse']['Errors'][i][
} 'Error'
][0],
null,
2
)
);
}
throw new Error('API Error');
} else {
// Status="OK"
return result['ApiResponse']['CommandResponse'][0];
}
});
});
}
return { return {
init: function (deps) { init: function(deps) {
request = deps.request; request = deps.request;
return null; return null;
}, },
zones: function(data) { zones: function(data) {
return api('namecheap.domains.getList',{}).then(function (zonesResponse) { return api('namecheap.domains.getList', {}).then(function(
// console.log('zones'); zonesResponse
// console.log(zonesResponse); ) {
return zonesResponse['DomainGetListResult'].map(function (x) { // console.log('zones');
return x['Domain'][0]['$']['Name']; // console.log(zonesResponse);
}); return zonesResponse['DomainGetListResult'].map(function(x) {
return x['Domain'][0]['$']['Name'];
});
});
},
}); set: function(data) {
}, console.log(`DEBUG >>> data: ${JSON.stringify(data, null, 2)}`);
var ch = data.challenge;
var txt = ch.dnsAuthorization;
set: function (data) { var params = {};
console.log(`DEBUG >>> data: ${JSON.stringify(data, null, 2)}`); // var zone = ch.dnsZone;
var ch = data.challenge; var zone = ch.identifier.value;
var txt = ch.dnsAuthorization; console.log(`DEBUG >>> zone: ${zone}`);
var params = {}; // the domain is the first part
// var zone = ch.dnsZone; // params['SLD'] = zone.split('.')[0];
var zone = ch.identifier.value; // the rest of the components are the TLD
console.log(`DEBUG >>> zone: ${zone}`); // params['TLD'] = zone.split('.').splice(1).join('.');
// the domain is the first part var domains = zone.split('.');
// params['SLD'] = zone.split('.')[0]; console.log('DEBUG >>> ' + domains);
// the rest of the components are the TLD
// params['TLD'] = zone.split('.').splice(1).join('.');
var domains = zone.split('.'); // if you have subdomain foo.blah.com, SLD = blah and TLD = com
console.log('DEBUG >>> ' + domains); params['TLD'] = domains[domains.length - 1];
params['SLD'] = domains[domains.length - 2];
// if you have subdomain foo.blah.com, SLD = blah and TLD = com console.log(`DEBUG >>> SLD: ${params['SLD']}`);
params['TLD'] = domains[domains.length - 1]; console.log(`DEBUG >>> TLD: ${params['TLD']}`);
params['SLD'] = domains[domains.length - 2];
console.log(`DEBUG >>> SLD: ${params['SLD']}`); // setting a host record overwrites all existing,
console.log(`DEBUG >>> TLD: ${params['TLD']}`); // adding a new records means you've have to send back all previous records too
// setting a host record overwrites all existing, return api('namecheap.domains.dns.getHosts', params).then(function(
// adding a new records means you've have to send back all previous records too hostsResponse
) {
var currentHostRecordsCount =
hostsResponse['DomainDNSGetHostsResult'][0]['host'].length;
return api('namecheap.domains.dns.getHosts',params).then(function (hostsResponse) { for (var i = 0; i < currentHostRecordsCount; i++) {
var currentHostRecordsCount = hostsResponse['DomainDNSGetHostsResult'][0]['host'].length; // console.log(hostsResponse['DomainDNSGetHostsResult'][i]['host'][0]);
var currentEntry =
hostsResponse['DomainDNSGetHostsResult'][0]['host'][i][
'$'
];
for (var i = 0; i < currentHostRecordsCount; i++) { params['HostName' + (i + 1)] = currentEntry['Name'];
// console.log(hostsResponse['DomainDNSGetHostsResult'][i]['host'][0]); params['RecordType' + (i + 1)] = currentEntry['Type'];
var currentEntry = hostsResponse['DomainDNSGetHostsResult'][0]['host'][i]['$']; params['Address' + (i + 1)] = currentEntry['Address'];
params['TTL' + (i + 1)] = currentEntry['TTL'];
}
params['HostName'+(i+1)] = currentEntry['Name']; params['HostName' + (currentHostRecordsCount + 1)] =
params['RecordType'+(i+1)] = currentEntry['Type']; ch.dnsPrefix;
params['Address'+(i+1)] = currentEntry['Address']; params['RecordType' + (currentHostRecordsCount + 1)] = 'TXT';
params['TTL'+(i+1)] = currentEntry['TTL']; params['Address' + (currentHostRecordsCount + 1)] = txt;
} params['TTL' + (currentHostRecordsCount + 1)] = 100; // in minutes
params['HostName'+(currentHostRecordsCount+1)] = ch.dnsPrefix; // console.log(params);
params['RecordType'+(currentHostRecordsCount+1)] = 'TXT';
params['Address'+(currentHostRecordsCount+1)] = txt;
params['TTL'+(currentHostRecordsCount+1)] = 100; // in minutes
// console.log(params); return api('namecheap.domains.dns.setHosts', params)
.then(function(setHostResponse) {
// console.log('setHost');
// console.log(setHostResponse);
return true;
})
.catch(function(err) {
throw new Error(
'record did not set. check subdomain, api key, etc'
);
});
});
},
remove: function(data) {
var ch = data.challenge;
return api('namecheap.domains.dns.setHosts',params).then(function (setHostResponse) { var params = {};
// console.log('setHost'); var zone = ch.identifier.value;
// console.log(setHostResponse); var domains = zone.split('.');
return true
}).catch(function (err) {
throw new Error('record did not set. check subdomain, api key, etc');
});
});
}, params['TLD'] = domains[domains.length - 1];
remove: function (data) { params['SLD'] = domains[domains.length - 2];
var ch = data.challenge;
var params = {}; // setting a host record overwrites all existing,
var zone = ch.identifier.value; // removing a new records means you've have to send back all previous records without removed
var domains = zone.split('.');
params['TLD'] = domains[domains.length - 1]; return api('namecheap.domains.dns.getHosts', params).then(function(
params['SLD'] = domains[domains.length - 2]; hostsResponse
) {
var currentHostRecordsCount =
hostsResponse['DomainDNSGetHostsResult'][0]['host'].length;
// setting a host record overwrites all existing, for (var i = 0; i < currentHostRecordsCount; i++) {
// removing a new records means you've have to send back all previous records without removed // console.log(hostsResponse['DomainDNSGetHostsResult'][i]['host'][0]);
var currentEntry =
hostsResponse['DomainDNSGetHostsResult'][0]['host'][i][
'$'
];
if (currentEntry['Address'] != ch.dnsAuthorization) {
params['HostName' + (i + 1)] = currentEntry['Name'];
params['RecordType' + (i + 1)] = currentEntry['Type'];
params['Address' + (i + 1)] = currentEntry['Address'];
params['TTL' + (i + 1)] = currentEntry['TTL'];
}
}
return api('namecheap.domains.dns.getHosts',params).then(function (hostsResponse) { return api('namecheap.domains.dns.setHosts', params)
var currentHostRecordsCount = hostsResponse['DomainDNSGetHostsResult'][0]['host'].length; .then(function(setHostResponse) {
// console.log('setHost');
// console.log(setHostResponse);
return true;
})
.catch(function(err) {
throw new Error(
'record did not remove. check subdomain, api key, etc'
);
});
});
},
get: function(data) {
var ch = data.challenge;
for (var i = 0; i < currentHostRecordsCount; i++) { var params = {};
// console.log(hostsResponse['DomainDNSGetHostsResult'][i]['host'][0]); var zone = ch.identifier.value;
var currentEntry = hostsResponse['DomainDNSGetHostsResult'][0]['host'][i]['$']; var domains = zone.split('.');
if(currentEntry['Address'] != ch.dnsAuthorization){
params['HostName'+(i+1)] = currentEntry['Name'];
params['RecordType'+(i+1)] = currentEntry['Type'];
params['Address'+(i+1)] = currentEntry['Address'];
params['TTL'+(i+1)] = currentEntry['TTL'];
}
}
return api('namecheap.domains.dns.setHosts',params).then(function (setHostResponse) { params['TLD'] = domains[domains.length - 1];
// console.log('setHost'); params['SLD'] = domains[domains.length - 2];
// console.log(setHostResponse);
return true
}).catch(function (err) {
throw new Error('record did not remove. check subdomain, api key, etc');
});
});
}, return api('namecheap.domains.dns.getHosts', params).then(function(
get: function (data) { hostsResponse
var ch = data.challenge; ) {
// console.log('hosts');
// console.log(hostsResponse);
var currentHostRecords =
hostsResponse['DomainDNSGetHostsResult'][0]['host'];
var params = {}; var entries = currentHostRecords.filter(function(x) {
var zone = ch.identifier.value; return x['$']['Type'] === 'TXT';
var domains = zone.split('.'); });
params['TLD'] = domains[domains.length - 1]; var entry = entries.filter(function(x) {
params['SLD'] = domains[domains.length - 2]; // console.log('data', x.data);
// console.log('dnsAuth', ch.dnsAuthorization, ch);
return x['$']['Address'] === ch.dnsAuthorization;
})[0];
return api('namecheap.domains.dns.getHosts',params).then(function (hostsResponse) { if (entry) {
// console.log('hosts'); return { dnsAuthorization: entry['$']['Address'] };
// console.log(hostsResponse); } else {
var currentHostRecords = hostsResponse['DomainDNSGetHostsResult'][0]['host']; return null;
}
var entries = currentHostRecords.filter(function (x) { });
return x['$']['Type'] === 'TXT'; }
}); };
var entry = entries.filter(function (x) {
// console.log('data', x.data);
// console.log('dnsAuth', ch.dnsAuthorization, ch);
return x['$']['Address'] === ch.dnsAuthorization;
})[0];
if (entry) {
return {dnsAuthorization: entry['$']['Address']};
} else {
return null;
}
});
}
};
}; };

103
package-lock.json generated
View File

@ -1,58 +1,49 @@
{ {
"name": "acme-dns-01-namecheap", "name": "acme-dns-01-namecheap",
"version": "3.0.0", "version": "3.0.0",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {
"@root/request": { "@root/request": {
"version": "1.3.11", "version": "1.3.11",
"resolved": "https://registry.npmjs.org/@root/request/-/request-1.3.11.tgz", "resolved": "https://registry.npmjs.org/@root/request/-/request-1.3.11.tgz",
"integrity": "sha512-3a4Eeghcjsfe6zh7EJ+ni1l8OK9Fz2wL1OjP4UCa0YdvtH39kdXB9RGWuzyNv7dZi0+Ffkc83KfH0WbPMiuJFw==" "integrity": "sha512-3a4Eeghcjsfe6zh7EJ+ni1l8OK9Fz2wL1OjP4UCa0YdvtH39kdXB9RGWuzyNv7dZi0+Ffkc83KfH0WbPMiuJFw=="
}, },
"acme-challenge-test": { "acme-challenge-test": {
"version": "3.3.2", "version": "3.3.2",
"resolved": "https://registry.npmjs.org/acme-challenge-test/-/acme-challenge-test-3.3.2.tgz", "resolved": "https://registry.npmjs.org/acme-challenge-test/-/acme-challenge-test-3.3.2.tgz",
"integrity": "sha512-0AbMcaON20wpI5vzFDAqwcv2VerY4xIlNCqX0w1xEJUIu/EQtQNmkje+rKNuy2TUl2KBMdIaR6YBbJUdaEiC4w==", "integrity": "sha512-0AbMcaON20wpI5vzFDAqwcv2VerY4xIlNCqX0w1xEJUIu/EQtQNmkje+rKNuy2TUl2KBMdIaR6YBbJUdaEiC4w==",
"dev": true, "dev": true,
"requires": { "requires": {
"@root/request": "^1.3.11" "@root/request": "^1.3.11"
}, },
"dependencies": { "dependencies": {
"@root/request": { "@root/request": {
"version": "1.3.11", "version": "1.3.11",
"resolved": "https://registry.npmjs.org/@root/request/-/request-1.3.11.tgz", "resolved": "https://registry.npmjs.org/@root/request/-/request-1.3.11.tgz",
"integrity": "sha512-3a4Eeghcjsfe6zh7EJ+ni1l8OK9Fz2wL1OjP4UCa0YdvtH39kdXB9RGWuzyNv7dZi0+Ffkc83KfH0WbPMiuJFw==", "integrity": "sha512-3a4Eeghcjsfe6zh7EJ+ni1l8OK9Fz2wL1OjP4UCa0YdvtH39kdXB9RGWuzyNv7dZi0+Ffkc83KfH0WbPMiuJFw==",
"dev": true "dev": true
} }
} }
}, },
"acme-dns-01-test": { "sax": {
"version": "3.3.1", "version": "1.2.4",
"resolved": "https://registry.npmjs.org/acme-dns-01-test/-/acme-dns-01-test-3.3.1.tgz", "resolved": "https://registry.npmjs.org/sax/-/sax-1.2.4.tgz",
"integrity": "sha512-di2/n19FDLc/pe4CDxd/FpxuuCZG7CHEQVjWr96vvtxe5XNNgdHi2eJqVP0z9WBf9s61zxslyRPrAWzTN8ZVWw==", "integrity": "sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw=="
"dev": true, },
"requires": { "xml2js": {
"acme-challenge-test": "^3.3.1" "version": "0.4.19",
} "resolved": "https://registry.npmjs.org/xml2js/-/xml2js-0.4.19.tgz",
}, "integrity": "sha512-esZnJZJOiJR9wWKMyuvSE1y6Dq5LCuJanqhxslH2bxM6duahNZ+HMpCLhBQGZkbX6xRf8x1Y2eJlgt2q3qo49Q==",
"sax": { "requires": {
"version": "1.2.4", "sax": ">=0.6.0",
"resolved": "https://registry.npmjs.org/sax/-/sax-1.2.4.tgz", "xmlbuilder": "~9.0.1"
"integrity": "sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw==" }
}, },
"xml2js": { "xmlbuilder": {
"version": "0.4.19", "version": "9.0.7",
"resolved": "https://registry.npmjs.org/xml2js/-/xml2js-0.4.19.tgz", "resolved": "https://registry.npmjs.org/xmlbuilder/-/xmlbuilder-9.0.7.tgz",
"integrity": "sha512-esZnJZJOiJR9wWKMyuvSE1y6Dq5LCuJanqhxslH2bxM6duahNZ+HMpCLhBQGZkbX6xRf8x1Y2eJlgt2q3qo49Q==", "integrity": "sha1-Ey7mPS7FVlxVfiD0wi35rKaGsQ0="
"requires": { }
"sax": ">=0.6.0", }
"xmlbuilder": "~9.0.1"
}
},
"xmlbuilder": {
"version": "9.0.7",
"resolved": "https://registry.npmjs.org/xmlbuilder/-/xmlbuilder-9.0.7.tgz",
"integrity": "sha1-Ey7mPS7FVlxVfiD0wi35rKaGsQ0="
}
}
} }

View File

@ -1,36 +1,39 @@
{ {
"name": "acme-dns-01-namecheap", "name": "acme-dns-01-namecheap",
"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",
"scripts": { "files": [
"test": "node ./test.js" "lib",
}, "test.js"
"repository": { ],
"type": "git", "scripts": {
"url": "https://git.coolaj86.com/coolaj86/acme-dns-01-namecheap.js.git" "test": "node ./test.js"
}, },
"keywords": [ "repository": {
"namecheap", "type": "git",
"name-cheap", "url": "https://git.coolaj86.com/coolaj86/acme-dns-01-namecheap.js.git"
"dns", },
"dns-01", "keywords": [
"letsencrypt", "namecheap",
"acme", "name-cheap",
"greenlock" "dns",
], "dns-01",
"author": "AJ ONeal <coolaj86@gmail.com> (https://coolaj86.com/)", "letsencrypt",
"contributors": [ "acme",
"Nyaundi Brian <danleyb2@gmail.com> (https://git.coolaj86.com/danleyb2/)", "greenlock"
"Archie Baer <archie@abaer.dev> (https://abaer.dev)" ],
], "author": "AJ ONeal <coolaj86@gmail.com> (https://coolaj86.com/)",
"license": "MPL-2.0", "contributors": [
"dependencies": { "Nyaundi Brian <danleyb2@gmail.com> (https://git.coolaj86.com/danleyb2/)",
"@root/request": "^1.3.11", "Archie Baer <archie@abaer.dev> (https://abaer.dev)"
"xml2js": "^0.4.19" ],
}, "license": "MPL-2.0",
"devDependencies": { "dependencies": {
"acme-challenge-test": "^3.3.2", "@root/request": "^1.3.11",
"acme-dns-01-test": "^3.2.1" "xml2js": "^0.4.19"
} },
"devDependencies": {
"acme-challenge-test": "^3.3.2"
}
} }