2023-04-13 15:00:34 +00:00
|
|
|
import type { RegisterServerOptions } from '@peertube/peertube-types'
|
|
|
|
import type { ProsodyConfig } from './config'
|
2023-05-23 10:39:05 +00:00
|
|
|
import { debugNumericParameter } from '../debug'
|
2023-04-13 15:00:34 +00:00
|
|
|
import { prosodyCtl, reloadProsody } from './ctl'
|
|
|
|
import * as path from 'path'
|
|
|
|
import * as fs from 'fs'
|
|
|
|
|
|
|
|
interface Renew {
|
|
|
|
timer: NodeJS.Timer
|
2023-04-13 16:22:03 +00:00
|
|
|
lastFromDirMtime?: number
|
2023-04-13 15:00:34 +00:00
|
|
|
}
|
|
|
|
let renew: Renew | undefined
|
|
|
|
|
2023-04-13 16:22:03 +00:00
|
|
|
function startProsodyCertificatesRenewCheck (options: RegisterServerOptions, config: ProsodyConfig): void {
|
|
|
|
const logger = options.peertubeHelpers.logger
|
|
|
|
|
|
|
|
if (!config.certificates) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// check every day (or every minutes in debug mode)
|
2023-05-23 10:39:05 +00:00
|
|
|
const checkInterval = debugNumericParameter(options, 'renewCertCheckInterval', 60000, 3600000 * 24)
|
2023-04-13 16:22:03 +00:00
|
|
|
|
|
|
|
if (renew) {
|
|
|
|
stopProsodyCertificatesRenewCheck(options)
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.info('Starting Prosody Certificates Renew Check')
|
|
|
|
renewCheck(options, config).then(() => {}, () => {})
|
|
|
|
|
|
|
|
const timer = setInterval(() => {
|
|
|
|
logger.debug('Checking if Prosody certificates need to be renewed')
|
|
|
|
renewCheck(options, config).then(() => {}, () => {})
|
|
|
|
}, checkInterval)
|
|
|
|
|
|
|
|
renew = {
|
|
|
|
timer: timer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function stopProsodyCertificatesRenewCheck (options: RegisterServerOptions): void {
|
|
|
|
const logger = options.peertubeHelpers.logger
|
|
|
|
if (renew === undefined) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
logger.info('Stoping Prosody Certificates Renew Check')
|
|
|
|
clearInterval(renew.timer)
|
2023-04-13 15:00:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function ensureProsodyCertificates (options: RegisterServerOptions, config: ProsodyConfig): Promise<void> {
|
|
|
|
if (config.certificates !== 'generate-self-signed') { return }
|
|
|
|
const logger = options.peertubeHelpers.logger
|
2023-04-13 16:22:03 +00:00
|
|
|
logger.info('Prosody needs certificates, checking if certificates are okay...')
|
2023-04-13 15:00:34 +00:00
|
|
|
|
|
|
|
const prosodyDomain = config.host
|
|
|
|
const filepath = _filePathToTest(options, config)
|
2023-04-13 16:22:03 +00:00
|
|
|
if (!filepath) { return }
|
2023-04-13 15:00:34 +00:00
|
|
|
if (fs.existsSync(filepath)) {
|
|
|
|
logger.info(`The certificate ${filepath} exists, no need to generate it`)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Using: prososyctl --config /.../prosody.cfg.lua cert generate prosodyDomain.tld
|
|
|
|
await prosodyCtl(options, 'cert', {
|
|
|
|
additionalArgs: ['generate', prosodyDomain],
|
|
|
|
yesMode: true,
|
|
|
|
stdErrFilter: (data) => {
|
|
|
|
// For an unknow reason, `prosodyctl cert generate` outputs openssl data on stderr...
|
|
|
|
// So we filter these logs.
|
|
|
|
if (data.match(/Generating \w+ private key/)) { return false }
|
|
|
|
if (data.match(/^[.+o*\n]*$/m)) { return false }
|
|
|
|
if (data.match(/e is \d+/)) { return false }
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-04-13 16:22:03 +00:00
|
|
|
async function renewCheck (options: RegisterServerOptions, config: ProsodyConfig): Promise<void> {
|
|
|
|
if (config.certificates === 'generate-self-signed') {
|
|
|
|
return renewCheckSelfSigned(options, config)
|
|
|
|
}
|
|
|
|
if (config.certificates === 'use-from-dir') {
|
|
|
|
return renewCheckFromDir(options, config)
|
|
|
|
}
|
|
|
|
throw new Error('Unknown value for config.certificates')
|
|
|
|
}
|
|
|
|
|
2023-04-13 15:00:34 +00:00
|
|
|
async function renewCheckSelfSigned (options: RegisterServerOptions, config: ProsodyConfig): Promise<void> {
|
|
|
|
const logger = options.peertubeHelpers.logger
|
|
|
|
// We have to check if the self signed certificate is still valid.
|
|
|
|
// Prosodyctl generated certificates are valid 365 days.
|
|
|
|
// We will renew it every 10 months (and every X minutes in debug mode)
|
2023-05-23 10:39:05 +00:00
|
|
|
const renewEvery = debugNumericParameter(options, 'renewSelfSignedCertInterval', 5 * 60000, 3600000 * 24 * 30 * 10)
|
2023-04-13 15:00:34 +00:00
|
|
|
|
|
|
|
// getting the file date...
|
|
|
|
const filepath = _filePathToTest(options, config)
|
2023-04-13 16:22:03 +00:00
|
|
|
if (!filepath) { return }
|
2023-04-13 15:00:34 +00:00
|
|
|
if (!fs.existsSync(filepath)) {
|
|
|
|
logger.error('Missing certificate file: ' + filepath)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const stat = fs.statSync(filepath)
|
|
|
|
const age = (new Date()).getTime() - stat.mtimeMs
|
|
|
|
if (age <= renewEvery) {
|
|
|
|
logger.debug(`The age of the certificate ${filepath} is ${age}ms, which is less than the period ${renewEvery}ms`)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
logger.info(`The age of the certificate ${filepath} is ${age}ms, which is more than the period ${renewEvery}ms`)
|
|
|
|
await ensureProsodyCertificates(options, config)
|
|
|
|
await reloadProsody(options)
|
|
|
|
}
|
|
|
|
|
2023-04-13 16:22:03 +00:00
|
|
|
async function renewCheckFromDir (options: RegisterServerOptions, config: ProsodyConfig): Promise<void> {
|
|
|
|
// We will browse all dir files, get the more recent file update time, and compare it to the previous call.
|
2023-04-13 15:00:34 +00:00
|
|
|
const logger = options.peertubeHelpers.logger
|
2023-04-13 16:22:03 +00:00
|
|
|
if (!renew) { return }
|
|
|
|
let mtimeMs: number | undefined
|
|
|
|
const dir = config.paths.certs
|
|
|
|
if (!dir) { return }
|
|
|
|
const files = fs.readdirSync(dir, { withFileTypes: true })
|
|
|
|
files.forEach(file => {
|
|
|
|
if (!file.isFile()) { return }
|
|
|
|
const stat = fs.statSync(path.resolve(dir, file.name))
|
|
|
|
if (stat.mtimeMs > (mtimeMs ?? 0)) {
|
|
|
|
mtimeMs = stat.mtimeMs
|
|
|
|
}
|
|
|
|
})
|
|
|
|
logger.debug('renewCheckFromDir: the most recent file in the certs dir has mtimeMs=' + (mtimeMs ?? '').toString())
|
|
|
|
if (!mtimeMs) {
|
2023-04-13 15:00:34 +00:00
|
|
|
return
|
|
|
|
}
|
2023-04-13 16:22:03 +00:00
|
|
|
if (!renew.lastFromDirMtime) {
|
|
|
|
renew.lastFromDirMtime = mtimeMs
|
|
|
|
return
|
2023-04-13 15:00:34 +00:00
|
|
|
}
|
2023-04-13 16:22:03 +00:00
|
|
|
if (renew.lastFromDirMtime === mtimeMs) {
|
|
|
|
logger.debug('No change in certs modification dates.')
|
|
|
|
return
|
2023-04-13 15:00:34 +00:00
|
|
|
}
|
2023-04-13 16:22:03 +00:00
|
|
|
logger.info('There is a file that was modified in the certs dir, reloading prosody...')
|
|
|
|
await reloadProsody(options)
|
2023-04-13 15:00:34 +00:00
|
|
|
}
|
|
|
|
|
2023-04-13 16:22:03 +00:00
|
|
|
function _filePathToTest (options: RegisterServerOptions, config: ProsodyConfig): string | null {
|
|
|
|
if (!config.paths.certs) { return null }
|
|
|
|
return path.resolve(config.paths.certs, config.host + '.crt')
|
2023-04-13 15:00:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
ensureProsodyCertificates,
|
|
|
|
startProsodyCertificatesRenewCheck,
|
|
|
|
stopProsodyCertificatesRenewCheck
|
|
|
|
}
|