2019-06-08 16:15:58 +00:00
|
|
|
'use strict';
|
|
|
|
var util = require('util');
|
|
|
|
|
2019-06-15 18:54:30 +00:00
|
|
|
var request; // = require('@root/request');
|
2019-06-08 16:15:58 +00:00
|
|
|
var parseString = require('xml2js').parseString;
|
|
|
|
parseString = util.promisify(parseString);
|
|
|
|
|
|
|
|
|
|
|
|
const SANDBOX_URL = 'https://api.sandbox.namecheap.com/xml.response';
|
|
|
|
const PRODUCTION_URL = 'https://api.namecheap.com/xml.response';
|
|
|
|
|
|
|
|
|
|
|
|
var defaults = {
|
2019-07-16 15:04:55 +00:00
|
|
|
baseUrl: PRODUCTION_URL
|
2019-06-08 16:15:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
function extend(obj) {
|
|
|
|
var newObj = {};
|
|
|
|
for (var i in obj) {
|
|
|
|
if (obj.hasOwnProperty(i)) {
|
|
|
|
newObj[i] = obj[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newObj;
|
|
|
|
}
|
|
|
|
|
2019-07-16 15:04:55 +00:00
|
|
|
function assign(obj1,obj2) {
|
|
|
|
for (var attrname in obj2) { obj1[attrname] = obj2[attrname]; }
|
|
|
|
}
|
|
|
|
|
2019-06-08 16:15:58 +00:00
|
|
|
function requestUrl(baseUrl, params) {
|
|
|
|
var queryString = Object.keys(params).map(function (key) {
|
|
|
|
return encodeURIComponent(key) + '=' + encodeURIComponent(params[key]);
|
|
|
|
}).join('&');
|
2019-07-16 15:04:55 +00:00
|
|
|
// console.debug(queryString);
|
2019-06-08 16:15:58 +00:00
|
|
|
return baseUrl + '?' + queryString;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.create = function (config) {
|
|
|
|
// config = { baseUrl, token }
|
|
|
|
var baseUrl = config.baseUrl || defaults.baseUrl;
|
|
|
|
|
|
|
|
var globalParams = {
|
|
|
|
apiUser: config.apiUser,
|
|
|
|
apiKey: config.apiKey,
|
|
|
|
username: config.username,
|
2019-07-16 15:04:55 +00:00
|
|
|
ClientIp: config.clientIp
|
2019-06-08 16:15:58 +00:00
|
|
|
};
|
|
|
|
|
2019-07-16 15:04:55 +00:00
|
|
|
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;
|
2019-07-18 06:19:18 +00:00
|
|
|
// console.log(responseBody);
|
2019-07-16 15:04:55 +00:00
|
|
|
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]
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-06-08 16:15:58 +00:00
|
|
|
return {
|
2019-06-15 18:54:30 +00:00
|
|
|
init: function (deps) {
|
|
|
|
request = deps.request;
|
|
|
|
return null;
|
|
|
|
},
|
2019-07-16 15:04:55 +00:00
|
|
|
|
2019-06-15 18:54:30 +00:00
|
|
|
zones: function(data) {
|
2019-07-16 15:04:55 +00:00
|
|
|
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'];
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
2019-06-15 18:54:30 +00:00
|
|
|
},
|
2019-07-16 15:04:55 +00:00
|
|
|
|
2019-06-08 16:15:58 +00:00
|
|
|
set: function (data) {
|
|
|
|
var ch = data.challenge;
|
|
|
|
var txt = ch.dnsAuthorization;
|
|
|
|
|
2019-07-16 15:04:55 +00:00
|
|
|
var params = {};
|
|
|
|
var zone = ch.dnsZone;
|
2019-06-08 16:15:58 +00:00
|
|
|
// the domain is the first part
|
|
|
|
params['SLD'] = zone.split('.')[0];
|
|
|
|
// the rest of the components are the TLD
|
|
|
|
params['TLD'] = zone.split('.').splice(1).join('.');
|
|
|
|
|
2019-07-18 06:19:18 +00:00
|
|
|
// setting a host record overwrites all existing,
|
|
|
|
// 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) {
|
|
|
|
var currentHostRecordsCount = hostsResponse['DomainDNSGetHostsResult'][0]['host'].length;
|
2019-06-08 16:15:58 +00:00
|
|
|
|
2019-07-18 06:19:18 +00:00
|
|
|
for (var i = 0; i < currentHostRecordsCount; i++) {
|
|
|
|
// console.log(hostsResponse['DomainDNSGetHostsResult'][i]['host'][0]);
|
|
|
|
var currentEntry = hostsResponse['DomainDNSGetHostsResult'][0]['host'][i]['$'];
|
|
|
|
|
|
|
|
params['HostName'+(i+1)] = currentEntry['Name'];
|
|
|
|
params['RecordType'+(i+1)] = currentEntry['Type'];
|
|
|
|
params['Address'+(i+1)] = currentEntry['Address'];
|
|
|
|
params['TTL'+(i+1)] = currentEntry['TTL'];
|
|
|
|
}
|
2019-07-16 15:04:55 +00:00
|
|
|
|
2019-07-18 06:19:18 +00:00
|
|
|
params['HostName'+(currentHostRecordsCount+1)] = ch.dnsPrefix;
|
|
|
|
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');
|
|
|
|
});
|
2019-06-08 16:15:58 +00:00
|
|
|
});
|
2019-07-16 15:04:55 +00:00
|
|
|
|
2019-06-08 16:15:58 +00:00
|
|
|
},
|
|
|
|
remove: function (data) {
|
2019-07-18 08:13:31 +00:00
|
|
|
var ch = data.challenge;
|
|
|
|
|
|
|
|
var params = {};
|
|
|
|
var zone = ch.dnsZone;
|
|
|
|
// the domain is the first part
|
|
|
|
params['SLD'] = zone.split('.')[0];
|
|
|
|
// the rest of the components are the TLD
|
|
|
|
params['TLD'] = zone.split('.').splice(1).join('.');
|
|
|
|
|
|
|
|
// setting a host record overwrites all existing,
|
|
|
|
// 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) {
|
|
|
|
var currentHostRecordsCount = hostsResponse['DomainDNSGetHostsResult'][0]['host'].length;
|
2019-06-08 16:15:58 +00:00
|
|
|
|
2019-07-18 08:13:31 +00:00
|
|
|
for (var i = 0; i < currentHostRecordsCount; i++) {
|
|
|
|
// 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.setHosts',params).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');
|
|
|
|
});
|
|
|
|
});
|
2019-06-08 16:15:58 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
get: function (data) {
|
|
|
|
var ch = data.challenge;
|
|
|
|
|
2019-07-16 15:04:55 +00:00
|
|
|
var params = {};
|
|
|
|
var zone = ch.dnsZone;
|
2019-06-08 16:15:58 +00:00
|
|
|
|
|
|
|
// the domain is the first part
|
|
|
|
params['SLD'] = zone.split('.')[0];
|
|
|
|
// the rest of the components are the TLD
|
|
|
|
params['TLD'] = zone.split('.').splice(1).join('.');
|
|
|
|
|
2019-07-16 15:04:55 +00:00
|
|
|
return api('namecheap.domains.dns.getHosts',params).then(function (hostsResponse) {
|
2019-07-18 06:19:18 +00:00
|
|
|
// console.log('hosts');
|
|
|
|
// console.log(hostsResponse);
|
|
|
|
var currentHostRecords = hostsResponse['DomainDNSGetHostsResult'][0]['host'];
|
2019-06-08 16:15:58 +00:00
|
|
|
|
2019-07-18 06:19:18 +00:00
|
|
|
var entries = currentHostRecords.filter(function (x) {
|
2019-07-16 15:04:55 +00:00
|
|
|
return x['$']['Type'] === 'TXT';
|
|
|
|
});
|
2019-06-08 16:15:58 +00:00
|
|
|
|
2019-07-16 15:04:55 +00:00
|
|
|
var entry = entries.filter(function (x) {
|
|
|
|
// console.log('data', x.data);
|
|
|
|
// console.log('dnsAuth', ch.dnsAuthorization, ch);
|
|
|
|
return x['$']['Address'] === ch.dnsAuthorization;
|
|
|
|
})[0];
|
2019-06-08 16:15:58 +00:00
|
|
|
|
2019-07-16 15:04:55 +00:00
|
|
|
if (entry) {
|
|
|
|
return {dnsAuthorization: entry['$']['Address']};
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2019-06-08 16:15:58 +00:00
|
|
|
|
|
|
|
});
|
2019-07-16 15:04:55 +00:00
|
|
|
|
2019-06-08 16:15:58 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|