-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
87 lines (81 loc) · 2.92 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
const { getDomainRoot } = require('./lib/utils');
const { generateCert, check } = require('./lib/letsencrypt');
const {
describeDomainCertificateInfo,
uploadDomainServerCertificate,
setDomainServerCertificate,
} = require('./lib/cdn');
const { domains, beforeDays, useWildcardCert } = require('./lib/config');
async function main() {
const domainsNeedToBeRenewed = {};
const currentTime = new Date().getTime();
const expireThreshold = currentTime + beforeDays * 24 * 60 * 60 * 1000;
let i = 0;
// get info one by one to avoid request throttle.
for (let i = 0; i < domains.length; i++) {
const currentDomainName = domains[i];
let renewInfo;
try {
const { CertInfos: { CertInfo } } = await describeDomainCertificateInfo(currentDomainName);
const currentCert = CertInfo.find(o => o.ServerCertificateStatus === 'on');
if (currentCert) {
const expireTime = new Date(currentCert.CertExpireTime).getTime();
if (expireTime <= expireThreshold) {
renewInfo = currentCert;
}
} else {
renewInfo = {
DomainName: currentDomainName,
};
}
} catch(e) {
console.error(e);
}
if (renewInfo) {
const mainDomain = getDomainRoot(currentDomainName);
domainsNeedToBeRenewed[mainDomain] = domainsNeedToBeRenewed[mainDomain] || [];
domainsNeedToBeRenewed[mainDomain].push(renewInfo);
}
}
// check if there's any domain needs to be renewed.
const domainGroupsNeedToBeRenewed = Object.keys(domainsNeedToBeRenewed);
if (domainGroupsNeedToBeRenewed.length === 0) {
console.log(`No need to renew domain.`);
return;
}
// group domains by its root.
const groupForAllDomains = {};
domains.forEach((domainName) => {
const mainDomain = getDomainRoot(domainName);
groupForAllDomains[mainDomain] = groupForAllDomains[mainDomain] || {
status: 'unknown',
domains: [],
};
groupForAllDomains[mainDomain].domains.push(domainName);
});
// renew domains by group
for (let i = 0; i < domainGroupsNeedToBeRenewed.length; i++) {
const domainRoot = domainGroupsNeedToBeRenewed[i];
const allDomainsInThisGroup = groupForAllDomains[domainRoot].domains;
const { ServerCertificate, PrivateKey } = await generateCert(
useWildcardCert ? [`*.${domainRoot}`] : allDomainsInThisGroup,
);
const domainsInThisRenewGroup = domainsNeedToBeRenewed[domainRoot];
const firstDomainInRenewGroup = domainsInThisRenewGroup[0];
const { CertName } = await uploadDomainServerCertificate(
firstDomainInRenewGroup.DomainName,
ServerCertificate,
PrivateKey,
);
for (let j = 1; j < domainsInThisRenewGroup.length; j++) {
const currentDomainName = domainsInThisRenewGroup[j];
await setDomainServerCertificate(currentDomainName.DomainName, CertName);
}
}
}
check()
.then(main)
.then(() => {
console.log('Congratulations. All done.');
process.exit(0);
});