-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
63 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`) | ||
} |