2025-02-04 00:38:39 +01:00
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as path from 'path';
|
|
|
|
import { fileURLToPath } from 'url';
|
2025-05-09 17:00:27 +00:00
|
|
|
import type { Certificates } from '../models/certificate-types.js';
|
2025-02-04 00:38:39 +01:00
|
|
|
|
|
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
|
|
|
2025-05-09 17:00:27 +00:00
|
|
|
/**
|
|
|
|
* Loads the default SSL certificates from the assets directory
|
|
|
|
* @returns The certificate key pair
|
|
|
|
*/
|
|
|
|
export function loadDefaultCertificates(): Certificates {
|
2025-02-04 00:38:39 +01:00
|
|
|
try {
|
2025-05-09 17:00:27 +00:00
|
|
|
// Need to adjust path from /ts/certificate/utils to /assets/certs
|
|
|
|
const certPath = path.join(__dirname, '..', '..', '..', 'assets', 'certs');
|
2025-02-04 00:38:39 +01:00
|
|
|
const privateKey = fs.readFileSync(path.join(certPath, 'key.pem'), 'utf8');
|
|
|
|
const publicKey = fs.readFileSync(path.join(certPath, 'cert.pem'), 'utf8');
|
|
|
|
|
|
|
|
if (!privateKey || !publicKey) {
|
|
|
|
throw new Error('Failed to load default certificates');
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
privateKey,
|
|
|
|
publicKey
|
|
|
|
};
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error loading default certificates:', error);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
2025-05-09 17:00:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if a certificate file exists at the specified path
|
|
|
|
* @param certPath Path to check for certificate
|
|
|
|
* @returns True if the certificate exists, false otherwise
|
|
|
|
*/
|
|
|
|
export function certificateExists(certPath: string): boolean {
|
|
|
|
return fs.existsSync(certPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensures the certificate directory exists
|
|
|
|
* @param dirPath Path to the certificate directory
|
|
|
|
*/
|
|
|
|
export function ensureCertificateDirectory(dirPath: string): void {
|
|
|
|
if (!fs.existsSync(dirPath)) {
|
|
|
|
fs.mkdirSync(dirPath, { recursive: true });
|
|
|
|
}
|
|
|
|
}
|