Skip to content

Commit

Permalink
Add clean ssl helper func
Browse files Browse the repository at this point in the history
  • Loading branch information
willnode committed Dec 27, 2023
1 parent 993a6fd commit 6aa19c0
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "domcloud-bridge",
"version": "0.35.1",
"version": "0.36.0",
"description": "Deployment runner for DOM Cloud",
"main": "app.js",
"engines": {
Expand Down
60 changes: 60 additions & 0 deletions sudocleanssl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/usr/bin/env node

// stop renewing failed SSL certs if found

import shelljs, { ShellString, cat } from 'shelljs';
const { exec } = shelljs;

// | DOMAIN NAME | PATH TO CERTIFICATE FILE | VALID UNTIL | EXPIRES IN | STATUS |
const certsExpiryRegexp = /^\| (\S+)\s+\| (\/.+?)\s+\| (.+?)\s+\| (.*?)\s+\| (\S+)\s+\|$/gm;
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 => x.match(certsExpiryRegexp)).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}`)
}

0 comments on commit 6aa19c0

Please sign in to comment.