Skip to content

Commit

Permalink
Fix ssl again
Browse files Browse the repository at this point in the history
  • Loading branch information
willnode committed Oct 19, 2024
1 parent 25d06de commit ec75780
Showing 1 changed file with 27 additions and 34 deletions.
61 changes: 27 additions & 34 deletions sudocleanssl.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
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 ';
const test = process.env.NODE_ENV == "test";
Expand All @@ -26,42 +23,38 @@ function cmd(str) {
}).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}`);
const listCertsRenewals = cmd(cmdListCertsRenewals).trim().split('\n');

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];
const deltaHour = Math.trunc(Date.now() - lastIssuedDate / (3600000));
if (deltaHour > 24) {
if (test) {
console.log(`TEST: Will check ${domain} due to last issue ${deltaHour} hours`);
continue;
}
console.log(`Disabling renewal for ${domain}`);
var c = cat(domainFile).replace(/\nletsencrypt_renew=1/, '');
new ShellString(c).to(domainFile);
count++;
} else {
if (test) {
console.log(`TEST: Skipping ${domain} due to last issue ${deltaHour} hours`);
}
const domainDetail = cmd(askDomainDetailPrefix + domain);
const lastIssuedDateStr = domainDetail.match(/Lets Encrypt cert issued: (.+)/);
const expiryDateStr = domainDetail.match(/SSL cert expiry: (.+)/);
const domainFileStr = domainDetail.match(/File: (.+)/);
if (lastIssuedDateStr && expiryDateStr && domainFileStr) {
const lastIssuedDate = Date.parse(lastIssuedDateStr[1]);
const expiryDate = Date.parse(expiryDateStr[1]);
const domainFile = domainFileStr[1];
const deltaExp = Math.trunc((expiryDate - Date.now()) / (3600000 * 24));
const deltaHour = Math.trunc((Date.now() - lastIssuedDate) / (3600000));
if (deltaExp > 30) {
if (test) {
console.log(`TEST: Skipping ${domain} due to expiry ${deltaHour} days`);
}
} else if (deltaHour < 24) {
if (test) {
console.log(`TEST: Skipping ${domain} due to last issue ${deltaHour} hours`);
}
} else {
if (test) {
console.log(`TEST: Will check ${domain} due to last issue ${deltaHour} hours`);
continue;
}
console.log(`Disabling renewal for ${domain}`);
var c = cat(domainFile).replace(/\nletsencrypt_renew=1/, '');
new ShellString(c).to(domainFile);
count++;
}
}
}
Expand Down

0 comments on commit ec75780

Please sign in to comment.