-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
130 lines (115 loc) · 4.11 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const DigitalOcean = require('do-wrapper').default;
const Promise = require('bluebird');
const dns = Promise.promisifyAll(require('dns'));
const _ = require('lodash');
const acmeRecordPrefix = '_acme-challenge';
const getDomainName = (domain) => domain.split('.').slice(-2).join('.');
const getSubDomainName = (domain) => domain.split('.').slice(0, -2).join('.');
const getAcmeRecordName = (domain) => [acmeRecordPrefix].concat(getSubDomainName(domain)).join('.');
const defaults = {
debug: false,
acmeChallengeDns: '_acme-challenge.',
doApiKey: null
};
const logTag = '[le-challenge-digitalocean]';
function log(debug) {
if (debug) {
let args = Array.prototype.slice.call(arguments);
args.shift();
args.unshift(logTag);
console.info.apply(console, args);
}
};
const Challenge = module.exports;
Challenge.create = function(options) {
const _options = _.merge({}, defaults, options);
return {
getOptions: () => {
return _options;
},
set: Challenge.set,
get: Challenge.get,
remove: Challenge.remove,
loopback: Challenge.loopback,
test: Challenge.test,
_doApi: new DigitalOcean(_options.doApiKey)
};
};
Challenge.set = function(opts, host, challenge, keyAuthorization, cb) {
const keyAuthDigest = require('crypto').createHash('sha256').update(keyAuthorization || '').digest('base64')
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=+$/g, '')
;
const domain = getDomainName(host);
return this._doApi.domainRecordsGetAll(domain)
.then((res) => {
const records = res.body;
const acmeRecordName = getAcmeRecordName(host);
const acmeChallengeRecord = {
type: 'TXT',
name: acmeRecordName,
data: keyAuthDigest,
priority: null,
port: null,
weight: null
};
const existingRecord = _.find(records.domain_records, {name: acmeRecordName});
if (existingRecord) {
log(opts.debug, 'attempting to update record', acmeChallengeRecord);
return this._doApi.domainRecordsUpdate(domain, existingRecord.id, acmeChallengeRecord)
.then(() => {
log(opts.debug, 'updated acme record', acmeChallengeRecord);
return cb(null);
})
.catch((e) => {
console.error(logTag, 'update error', e);
cb(e);
});
} else {
log(opts.debug, 'attempting to create record', acmeChallengeRecord);
return this._doApi.domainRecordsCreate(domain, acmeChallengeRecord)
.then(() => {
log(opts.debug, 'created acme record', acmeChallengeRecord);
return cb(null);
})
.catch((e) => {
console.error(logTag, 'create error', e);
cb(e);
});
}
}).catch((e) => {
console.error(logTag, "couldn't find a domain by", domain);
cb(e);
});
};
Challenge.get = function(opts, domain, token, cb) { /* Not to be implemented */ };
Challenge.remove = function(opts, host, token, cb) {
const domain = getDomainName(host);
return this._doApi.domainRecordsGetAll(domain)
.then((res) => {
const records = res.body;
const acmeRecordName = getAcmeRecordName(host);
const existingRecord = _.find(records.domain_records, {name: acmeRecordName});
if (existingRecord) {
return this._doApi.domainRecordsDelete(domain, existingRecord.id)
.then(() => log(opts.debug, 'DO: deleted acme record', existingRecord))
.then(() => cb(null))
.catch((e) => {
console.error(logTag, 'update error', e);
return cb(e);
});
} else {
log(opts.debug, 'DO: could not find existing record', acmeRecordName);
return cb(new Error('DO: could not find existing record'));
}
}).catch((e) => {
console.error(logTag, "couldn't find a domain by", domain);
cb(e);
});
};
// same as get, but external
Challenge.loopback = function(defaults, domain, challenge, done) {
const challengeDomain = (defaults.test || '') + defaults.acmeChallengeDns + domain;
return dns.resolveTxtAsync(challengeDomain).then((x) => { return done(null, x); }, done);
};