add get zones and fix bugs

This commit is contained in:
nyaundi brian 2019-07-16 18:04:55 +03:00
parent b55b6998ba
commit b4ccf2a10f
1 changed files with 77 additions and 76 deletions

View File

@ -11,7 +11,7 @@ const PRODUCTION_URL = 'https://api.namecheap.com/xml.response';
var defaults = { var defaults = {
baseUrl: SANDBOX_URL baseUrl: PRODUCTION_URL
}; };
function extend(obj) { function extend(obj) {
@ -24,11 +24,15 @@ function extend(obj) {
return newObj; return newObj;
} }
function assign(obj1,obj2) {
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).map(function (key) {
return encodeURIComponent(key) + '=' + encodeURIComponent(params[key]); return encodeURIComponent(key) + '=' + encodeURIComponent(params[key]);
}).join('&'); }).join('&');
console.debug(queryString); // console.debug(queryString);
return baseUrl + '?' + queryString; return baseUrl + '?' + queryString;
} }
@ -40,63 +44,79 @@ module.exports.create = function (config) {
apiUser: config.apiUser, apiUser: config.apiUser,
apiKey: config.apiKey, apiKey: config.apiKey,
username: config.username, username: config.username,
ClientIp: '122.178.155.204' ClientIp: config.clientIp
}; };
function api(command, params) {
var requestParams = extend(globalParams);
requestParams['Command'] = command;
assign(requestParams,params);
var url = requestUrl(baseUrl, requestParams);
// console.log(url);
return request({
method: 'POST',
url: url,
}).then(function (response) {
var responseBody = response.body;
console.log(responseBody);
return parseString(responseBody).then(function (result) {
// check response status
if (result['ApiResponse']['$']['Status'] === 'ERROR') {
for (let i = 0; i < result['ApiResponse']['Errors'].length; i++) {
console.log(result['ApiResponse']['Errors'][i])
}
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) {
throw new Error("not implemented yet"); return api('namecheap.domains.getList',{}).then(function (zonesResponse) {
// console.log('zones');
// console.log(zonesResponse);
return zonesResponse['DomainGetListResult'].map(function (x) {
return x['Domain'][0]['$']['Name'];
});
});
}, },
set: function (data) { set: function (data) {
var ch = data.challenge; var ch = data.challenge;
var domainname = ch.identifier.value;
var zone = domainname;
var dnsPrefix = ch.dnsHost.replace(new RegExp('.' + zone + '$'), '');
var txt = ch.dnsAuthorization; var txt = ch.dnsAuthorization;
var params = extend(globalParams); var params = {};
params['Command'] = 'namecheap.domains.dns.setHosts'; var zone = ch.dnsZone;
// 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('.');
params['HostName1'] = dnsPrefix; params['HostName1'] = ch.dnsPrefix;
params['RecordType1'] = 'TXT'; params['RecordType1'] = 'TXT';
params['Address1'] = txt; params['Address1'] = txt;
params['TTL1'] = 100; params['TTL1'] = 1; // in minutes
var url = requestUrl(baseUrl, params); return api('namecheap.domains.dns.setHosts',params).then(function (setHostResponse) {
console.debug(url); // console.log('setHost');
// console.log(setHostResponse);
console.log('adding txt', data); return true
return request({
method: 'POST',
url: url,
}).then(function (resp) {
resp = resp.body;
console.log(resp);
return parseString(resp, function (err, result) {
console.dir(result);
if (result['ApiResponse']['$']['Status'] === 'ERROR') {
for (let i = 0; i < result['ApiResponse']['Errors'].length; i++) {
console.log(result['ApiResponse']['Errors'][i])
}
throw new Error('record did not set. check subdomain, api key, etc');
} else {
return true
}
});
}).catch(function (err) {
throw new Error('record did not set. check subdomain, api key, etc');
}); });
}, },
remove: function (data) { remove: function (data) {
var domainname = data.challenge.altname; var domainname = data.challenge.altname;
@ -107,57 +127,38 @@ module.exports.create = function (config) {
}, },
get: function (data) { get: function (data) {
var ch = data.challenge; var ch = data.challenge;
var domainname = data.challenge.altname;
var zone = domainname;
var params = {};
var zone = ch.dnsZone;
var params = extend(globalParams);
params['Command'] = 'namecheap.domains.dns.getHosts';
// 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('.');
return api('namecheap.domains.dns.getHosts',params).then(function (hostsResponse) {
console.log('hosts');
console.log(hostsResponse);
var url = requestUrl(baseUrl, params); var entries = hostsResponse['DomainDNSGetHostsResult'].filter(function (x) {
console.debug(url);
console.log('getting txt', data); return x['$']['Type'] === 'TXT';
return request({
method: 'POST',
url: url,
}).then(function (resp) {
resp = resp.body;
return parseString(resp, function (err, result) {
console.dir(result);
if (result['ApiResponse']['$']['Status'] === 'ERROR') {
for (let i = 0; i < result['ApiResponse']['Errors'].length; i++) {
console.log(result['ApiResponse']['Errors'][i])
}
throw new Error('record did not set. check subdomain, api key, etc');
} else { // Status="OK"
var entries = result['ApiResponse']['CommandResponse']['DomainDNSGetHostsResult'].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;
}
}
}); });
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;
}
}); });
} }
}; };
}; };