forked from domcloud/bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sudocleanssl.js
executable file
·60 lines (51 loc) · 2.23 KB
/
sudocleanssl.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
#!/usr/bin/env node
// stop renewing failed SSL certs if found
import shelljs from 'shelljs';
const { exec, ShellString, cat } = shelljs;
// | DOMAIN NAME | PATH TO CERTIFICATE FILE | VALID UNTIL | EXPIRES IN | STATUS |
const certsExpiryRegexp = /^\| (\S+)\s+\| (\/.+?)\s+\| (.+?)\s+\| (.*?)\s+\| (\S+)\s+\|$/m;
const cmdListCertsExpiry = 'virtualmin list-certs-expiry --all-domains';
const cmdListCertsRenewals = 'virtualmin list-domains --name-only --with-feature letsencrypt_renew';
const askDomainDetailPrefix = 'virtualmin list-domains --simple-multiline --domain ';
/**
* @param {string} str
*/
function cmd(str) {
return exec(str, {
silent: true,
fatal: true,
}).stdout.trim();
}
const listCertsExpiry = cmd(cmdListCertsExpiry).split('\n')
.slice(5).map(x => certsExpiryRegexp.exec(x)).filter(x => x);
const listCertsRenewals = cmd(cmdListCertsRenewals).split('\n');
console.log(`Certs currently active: ${listCertsExpiry.length}, domains in active renewal: ${listCertsRenewals.length}`);
let count = 0;
for (const domain of listCertsRenewals) {
const expData = listCertsExpiry.find(x => x[1] == domain);
if (!expData) {
console.error(`Cert info not found for ${domain}`)
continue;
}
if (expData[5] == 'EXPIRED' || (expData[4].includes(' day') && parseInt(expData[4]) < 30)) {
const domainDetail = cmd(askDomainDetailPrefix + domain);
const lastIssuedDateExp = domainDetail.match(/Lets Encrypt cert issued: (.+)/);
const domainFileExp = domainDetail.match(/File: (.+)/);
if (lastIssuedDateExp && domainFileExp) {
const lastIssuedDate = Date.parse(lastIssuedDateExp[1]);
const domainFile = domainFileExp[1];
if (Date.now() - lastIssuedDate < 86400000) {
console.log(`Disabling renewal for ${domain}`);
var c = cat(domainFile).replace(/\nletsencrypt_renew=1/, '');
new ShellString(c).to(domainFile);
count++;
}
}
}
}
if (count == 0) {
console.log('Done and nothing changed');
} else {
console.log(`Change applied for ${count} domains`);
console.log(`Total domains in active renewal: ${cmd(cmdListCertsRenewals).split('\n').length}`)
}