-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathletsencrypt.js
400 lines (327 loc) · 12.4 KB
/
letsencrypt.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
const rq = require('request-promise');
const acme = require('acme-client');
const Core = require('@alicloud/pop-core');
const fs = require('fs-extra');
const path = require('path');
const moment = require('moment');
const nodessh = require('node-ssh');
const { Command } = require('commander');
const program = new Command();
const confList = require('./conf');
let accountPem;
try{
accountPem = fs.readFileSync(path.join(__dirname, './account.pem'));
}catch (e) {
console.warn(`account.pem不存在`)
}
if(accountPem){
accountPem = Buffer.from(accountPem);
}
const ssh = new nodessh();
program.version('0.0.1');
program
.option('-alycdn', 'PUSH 证书至阿里云cdn', aliyCDN)
.option('-s', '开始域名配置', start)
program.parse(process.argv);
function getAliClient(conf,endpoint='https://alidns.aliyuncs.com',apiVersion='2015-01-09'){
const aliClient = new Core({
accessKeyId: conf.ali.accessKeyId,
accessKeySecret: conf.ali.accessKeySecret,
endpoint: endpoint,
apiVersion: apiVersion
});
return aliClient
}
async function run(conf) {
console.log('开始执行配置文件为:', conf.name);
async function getDomainRecordList() {
var params = {
"DomainName": conf.ali.domain,
"PageSize": 500
};
var requestOption = {
method: 'GET'
};
return aliClient.request('DescribeDomainRecords', params, requestOption)
}
async function setAliDns(value) {
let domainRecordListRes = await getDomainRecordList();
domainRecordListRes = domainRecordListRes;
const list = domainRecordListRes.DomainRecords.Record;
// 找到是否有记录为_acme-challenge
const find = list.find((n) => {
return n.RR == '_acme-challenge';
});
if (find) {
// 修改解析记录
if(find.Value == value){
console.log('当前域名DNS解析记录值与验证值一致',value);
return Promise.resolve(true);
}
await changeAcmeDomainRecore(find.RecordId, value)
} else {
// 添加解析记录
await addAcmeDomainRecord(value)
}
}
function timeSleep(time) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(true)
}, time)
})
}
async function addAcmeDomainRecord(value) {
var params = {
"DomainName": conf.ali.domain,
"RR": "_acme-challenge",
"Type": "TXT",
"Value": value
};
var requestOption = {
method: 'POST'
};
return aliClient.request('AddDomainRecord', params, requestOption)
}
async function changeAcmeDomainRecore(recordId, value) {
var params = {
"DomainName": conf.ali.domain,
"RecordId": recordId,
"RR": "_acme-challenge",
"Type": "TXT",
"Value": value
};
var requestOption = {
method: 'POST'
};
return aliClient.request('UpdateDomainRecord', params, requestOption)
}
const aliClient = getAliClient(conf)
let privateKey, client, account,accountUrl;
let clientObj ={
directoryUrl: acme.directory.letsencrypt.production,
accountKey: accountPem,
}
if (!accountPem||!confList.accountUrl) {
privateKey = await acme.forge.createPrivateKey();
accountPem = privateKey;
await fs.writeFile(path.join(__dirname,'./account.pem'),accountPem);
console.log('初始化客户端');
clientObj.accountKey=accountPem;
client = new acme.Client(clientObj);
console.log('创建账号');
account = await client.createAccount({
termsOfServiceAgreed: true,
contact: confList.contact
});
console.log('获取账号url');
accountUrl = await client.getAccountUrl();
// 写入accountUrl
confList.accountUrl = accountUrl;
await fs.writeJson('./conf.json', confList,{spaces:2});
console.log('写入账号url成功')
}else{
console.log('拥有账号,开始初始化客户端');
clientObj.accountUrl=confList.accountUrl;
client = new acme.Client(clientObj);
}
// 拼装identi
const identifiers = conf.identifiers.map((n) => {
return {type: 'dns', value: n}
});
const order = await client.createOrder({
wildcard: true,
identifiers: identifiers,
// identifiers:{ type: 'dns', value: '*.emufan.com' }
});
console.log('order', order);
// 从订单获取授权
const auth = await client.getAuthorizations(order);
console.log('auth', auth);
// 获取授权列表
const challengeList = [];
for (let i of auth) {
// 获取授权challenge
// 找到DNS授权类型的challenge
const dnsChallenge = i.challenges.find((n) => {
return n.type == 'dns-01'
})
const challenge = await client.getChallengeKeyAuthorization(dnsChallenge);
console.log('challenge', challenge);
challengeList.push(challenge)
}
let index = -1;
for (let i of auth) {
// 验证challenge
let userAuth;
const dnsChallenge = i.challenges.find((n) => {
return n.type == 'dns-01';
});
index++;
console.log('开始设置',challengeList[index])
await setAliDns(challengeList[index]);
// 5分钟后进行验证
await timeSleep(confList.sleepTime);
console.log('开始验证!',challengeList[index])
try {
userAuth = await client.verifyChallenge(i, dnsChallenge);
} catch (e) {
// 验证失败
console.error('userAuth Error', e);
return false;
}
// 通知acme授权完成
let authComplete,waitStatus;
try {
authComplete = await client.completeChallenge(dnsChallenge);
waitStatus= await client.waitForValidStatus(dnsChallenge);
} catch (e) {
console.error('通知acme授权完成失败 ---err', e);
return
}
console.log('验证成功:', auth.indexOf(i),dnsChallenge)
}
let certificateKey, certificateCsr;
try {
[certificateKey, certificateCsr] = await acme.forge.createCsr({
commonName: conf.cName,
altNames: conf.identifiers
});
} catch (e) {
console.error('获取certificateKey&certificateCsr失败', e);
return false;
}
// 最终确定订单
let fOrder;
try {
fOrder = await client.finalizeOrder(order, certificateCsr);
} catch (e) {
console.error('获取fOrder失败', e);
// return false;
}
console.log('fOrder', fOrder);
// 获取证书值
let cert;
try {
cert = await client.getCertificate(order);
} catch (e) {
console.error('获取cert失败', e);
return false;
}
console.log('cert', cert);
// 私钥为certificateKey 公钥为cert
// 写入本地
const pemPath = path.join(__dirname, `./cert/${conf.cName}-public.pem`);
await fs.outputFile(pemPath, cert);
const privPath = path.join(__dirname, `./cert/${conf.cName}-private.pem`);
await fs.outputFile(privPath, certificateKey);
// 通过sftp上传线上
if(!conf.sftp){
return false;
}
await ssh.connect(conf.sftp);
const pemRes = await ssh.putFile(pemPath, `${conf.sftp.uploadPath}/${conf.cName}-public.pem`);
console.log('上传sftp成功', pemRes);
const privateRes = await ssh.putFile(privPath, `${conf.sftp.uploadPath}/${conf.cName}-private.pem`);
console.log('上传private成功', privateRes);
// 执行execShell
const execRes = await ssh.execCommand(conf.sftp.execShell, { cwd:'/' });
console.log('执行exec命令', execRes);
conf.time = moment().format('YYYY-MM-DD');
// 记录写入时间
await fs.writeJson('./conf.json', confList,{spaces:2})
console.log('更新conf.json成功');
return Promise.resolve(true);
}
async function start() {
for (let i of confList.confData) {
// 如果请求的证书配置时间没有超过2.5个月则跳过本次请求
if (i.time) {
const confTime = moment(i.time);
const expireMonth = confTime.add(2, 'months').add(5, 'days');
// 是否超过2个月5天
if (!(moment().isAfter(expireMonth))) {
console.log(`当前配置文件:${i.name},没有超过2.5个月,将不更新证书`);
continue;
}
}
await run(i);
}
console.log('全部配置文件更新完毕');
console.log('下面开始检索更新阿里云CDN证书');
await aliyCDN();
process.exit();
}
async function aliyCDN(){
for (let conf of confList.confData) {
// 如果请求的证书需要配置cdn
if (conf.ali && conf.ali.cdn) {
const aliClient = getAliClient(conf,'https://cdn.aliyuncs.com','2018-05-10')
const currentCdn = conf.ali.cdn;
async function getAliCdnList(){
var params = {
"PageSize": 50,
};
var requestOption = {
method: 'POST'
};
return aliClient.request('DescribeUserDomains', params, requestOption)
}
async function setAliCdnHostSSL({name,ServerCertificate,PrivateKey}){
var params = {
"DomainName": name,
"CertName":name,
"ServerCertificateStatus":'on',
"CertType":'upload',
ServerCertificate,
PrivateKey,
"ForceSet":"1"
};
var requestOption = {
method: 'POST'
};
return aliClient.request('SetDomainServerCertificate', params, requestOption)
}
const cdnList = await getAliCdnList()
console.log('cdnList',cdnList)
// 判断当前cdnList是否有存在的cdn
for (let i of cdnList.Domains.PageData){
if(currentCdn.domain.indexOf(i.DomainName)===-1){
console.warn('当前域名未配置在conf文件 跳过',i.DomainName)
continue
}else{
const pemPath = path.join(__dirname, `./cert/${conf.cName}-public.pem`);
const privPath = path.join(__dirname, `./cert/${conf.cName}-private.pem`);
try {
await fs.ensureFile(pemPath)
await fs.ensureFile(privPath)
} catch (err) {
console.error(conf.cName,'私钥公钥文件不存在',err)
return false;
}
const ServerCertificate = await fs.readFile(pemPath)
const PrivateKey = await fs.readFile(privPath)
let setRes;
try{
setRes = await setAliCdnHostSSL({name:i.DomainName,ServerCertificate,PrivateKey})
}catch (e){
console.error(i.DomainName,'设置域名SSL出错',e)
}
console.log('设置cdn域名成功',i.DomainName,setRes)
}
}
}
}
console.log('全部配置文件CDN域名信息更新完毕,请手动操作控制台配置CDN 如已配置请忽略');
process.exit();
}
process.on('unhandledRejection', (p) => {
// console.error('unhandledRejection', p);
console.trace('unhandledRejection', p);
});// 全局监听未处理的promise错误
process.on('uncaughtException', function (err) {
// console.error('uncaughtException', err);
console.trace('uncaughtException', err);
});
// 测试环境
// https://acme-staging-v02.api.letsencrypt.org/directory