Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
4ad4cf29f4 | |||
d9b9b0fcd6 | |||
705902ff32 | |||
91a8c8b237 | |||
4b8a7d2e00 | |||
ff8c41b994 |
12
README.md
12
README.md
@ -1,4 +1,4 @@
|
|||||||
# [acme-dns-01-namecheap](https://git.rootprojects.org/root/acme-dns-01-namecheap) | a [Root](https://rootrpojects.org) project
|
# [acme-dns-01-namecheap](https://git.rootprojects.org/root/acme-dns-01-namecheap.js) | a [Root](https://rootrpojects.org) project
|
||||||
|
|
||||||
NameCheap DNS + Let's Encrypt
|
NameCheap DNS + Let's Encrypt
|
||||||
|
|
||||||
@ -17,11 +17,11 @@ First you create an instance with your credentials:
|
|||||||
|
|
||||||
```js
|
```js
|
||||||
var dns01 = require('acme-dns-01-namecheap').create({
|
var dns01 = require('acme-dns-01-namecheap').create({
|
||||||
apiUser: 'username',
|
apiUser: 'jdoe',
|
||||||
apiKey: 'xxxx',
|
apiKey: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
|
||||||
clientIp: 'public ip',
|
clientIp: '121.22.123.22',
|
||||||
username: 'api user',
|
username: 'jdoe',
|
||||||
baseUrl: 'sandbox or production' // default production
|
baseUrl: 'https://api.namecheap.com/xml.response' // default
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
ZONE=example.co.uk
|
ZONE=example.co.uk
|
||||||
|
|
||||||
API_USER=exampleuser
|
API_USER=exampleuser
|
||||||
API_KEY=xxxxxxxxxxxxxxx
|
API_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
||||||
USERNAME=exampleuser
|
USERNAME=exampleuser
|
||||||
CLIENT_IP=121.22.123.22
|
CLIENT_IP=121.22.123.22
|
||||||
|
130
lib/index.js
130
lib/index.js
@ -2,13 +2,12 @@
|
|||||||
var util = require('util');
|
var util = require('util');
|
||||||
|
|
||||||
var request; // = require('@root/request');
|
var request; // = require('@root/request');
|
||||||
|
//var querystring = require('querystring');
|
||||||
var parseString = require('xml2js').parseString;
|
var parseString = require('xml2js').parseString;
|
||||||
parseString = util.promisify(parseString);
|
parseString = util.promisify(parseString);
|
||||||
|
|
||||||
|
var SANDBOX_URL = 'https://api.sandbox.namecheap.com/xml.response';
|
||||||
const SANDBOX_URL = 'https://api.sandbox.namecheap.com/xml.response';
|
var 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: PRODUCTION_URL
|
||||||
@ -25,13 +24,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;
|
||||||
}
|
}
|
||||||
@ -44,7 +49,7 @@ module.exports.create = function (config) {
|
|||||||
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) {
|
||||||
@ -56,20 +61,23 @@ module.exports.create = function (config) {
|
|||||||
// console.log(url);
|
// console.log(url);
|
||||||
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'] === 'OK') {
|
||||||
for (let i = 0; i < result['ApiResponse']['Errors'].length; i++) {
|
return result['ApiResponse']['CommandResponse'][0];
|
||||||
console.log(result['ApiResponse']['Errors'][i])
|
}
|
||||||
|
|
||||||
|
// Status="ERROR"
|
||||||
|
var i;
|
||||||
|
var len = result['ApiResponse']['Errors'].length;
|
||||||
|
for (i = 0; i < len; i += 1) {
|
||||||
|
console.error(result['ApiResponse']['Errors'][i]);
|
||||||
}
|
}
|
||||||
throw new Error('API Error');
|
throw new Error('API Error');
|
||||||
} else { // Status="OK"
|
|
||||||
return result['ApiResponse']['CommandResponse'][0]
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -81,13 +89,18 @@ 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) {
|
var zones = zonesResponse['DomainGetListResult'][0][
|
||||||
return x['Domain'][0]['$']['Name'];
|
'Domain'
|
||||||
|
].map(function(el) {
|
||||||
|
return el['$']['Name'];
|
||||||
});
|
});
|
||||||
|
//console.log(zones);
|
||||||
|
return zones;
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -100,17 +113,26 @@ module.exports.create = function (config) {
|
|||||||
// 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('.');
|
||||||
|
|
||||||
// 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,22 +140,26 @@ 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;
|
||||||
@ -143,17 +169,26 @@ module.exports.create = function (config) {
|
|||||||
// 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('.');
|
||||||
|
|
||||||
// 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,15 +197,18 @@ 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;
|
||||||
@ -181,12 +219,18 @@ module.exports.create = function (config) {
|
|||||||
// 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) {
|
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 +247,7 @@ module.exports.create = function (config) {
|
|||||||
} else {
|
} else {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
6
package-lock.json
generated
6
package-lock.json
generated
@ -35,6 +35,12 @@
|
|||||||
"acme-challenge-test": "^3.3.1"
|
"acme-challenge-test": "^3.3.1"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"dotenv": {
|
||||||
|
"version": "8.0.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-8.0.0.tgz",
|
||||||
|
"integrity": "sha512-30xVGqjLjiUOArT4+M5q9sYdvuR4riM6yK9wMcas9Vbp6zZa+ocC9dp6QoftuhTPhFAiLK/0C5Ni2nou/Bk8lg==",
|
||||||
|
"dev": true
|
||||||
|
},
|
||||||
"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",
|
||||||
|
@ -1,8 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "acme-dns-01-namecheap",
|
"name": "acme-dns-01-namecheap",
|
||||||
"version": "3.0.0",
|
"version": "3.0.1",
|
||||||
"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"
|
||||||
},
|
},
|
||||||
@ -31,6 +35,7 @@
|
|||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"acme-challenge-test": "^3.3.2",
|
"acme-challenge-test": "^3.3.2",
|
||||||
"acme-dns-01-test": "^3.2.1"
|
"acme-dns-01-test": "^3.2.1",
|
||||||
|
"dotenv": "^8.0.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user