Validate SSL config for sanity before startup

02cdec5a10ae886aff84896d485738213331c2ba

Cohee <18619528+Cohee1207@users.noreply.github.com>

1 files changed, +39 -10Ignore whitespace
src/server-startup.js+39 -10
@@ -190,13 +190,46 @@ export class ServerStartup {
190190 }
191191
192192 /**
193+ * Prints a fatal error message and exits the process.
194+ * @param {string} message
195+ */
196+ #fatal(message) {
197+ console.error(color.red(message));
198+ process.exit(1);
199+ }
200+
201+ /**
202+ * Checks if SSL options are valid. If not, it will print an error message and exit the process.
203+ * @returns {void}
204+ */
205+ #verifySslOptions() {
206+ if (!this.cliArgs.ssl) return;
207+
208+ if (!this.cliArgs.certPath) {
209+ this.#fatal('Error: SSL certificate path is required when using HTTPS. Check your config');
210+ }
211+
212+ if (!this.cliArgs.keyPath) {
213+ this.#fatal('Error: SSL key path is required when using HTTPS. Check your config');
214+ }
215+
216+ if (!fs.existsSync(this.cliArgs.certPath)) {
217+ this.#fatal('Error: SSL certificate path does not exist');
218+ }
219+
220+ if (!fs.existsSync(this.cliArgs.keyPath)) {
221+ this.#fatal('Error: SSL key path does not exist');
222+ }
223+ }
224+
225+ /**
193226 * Creates an HTTPS server.
194227 * @param {URL} url The URL to listen on
195228 * @param {number} ipVersion the ip version to use
196229 * @returns {Promise<void>} A promise that resolves when the server is listening
197- * @throws {Error} If the server fails to start
198230 */
199231 #createHttpsServer(url, ipVersion) {
232+ this.#verifySslOptions();
200233 return new Promise((resolve, reject) => {
201234 const sslOptions = {
202235 cert: fs.readFileSync(this.cliArgs.certPath),
@@ -222,7 +255,6 @@ export class ServerStartup {
222255 * @param {URL} url The URL to listen on
223256 * @param {number} ipVersion the ip version to use
224257 * @returns {Promise<void>} A promise that resolves when the server is listening
225- * @throws {Error} If the server fails to start
226258 */
227259 #createHttpServer(url, ipVersion) {
228260 return new Promise((resolve, reject) => {
@@ -257,7 +289,7 @@ export class ServerStartup {
257289 try {
258290 await createFunc(this.cliArgs.getIPv6ListenUrl(), 6);
259291 } catch (error) {
260292 console.error('non-fatal errorWarning: failed to start server on IPv6');
261293 console.error(error);
262294
263295 v6Failed = true;
@@ -268,7 +300,7 @@ export class ServerStartup {
268300 try {
269301 await createFunc(this.cliArgs.getIPv4ListenUrl(), 4);
270302 } catch (error) {
271303 console.error('non-fatal errorWarning: failed to start server on IPv4');
272304 console.error(error);
273305
274306 v4Failed = true;
@@ -285,18 +317,15 @@ export class ServerStartup {
285317 */
286318 #handleServerListenFail({ v6Failed, v4Failed, useIPv6, useIPv4 }) {
287319 if (v6Failed && !useIPv4) {
288320 console.error(colorthis.red#fatal('fatal errorError: Failed to start server on IPv6 and IPv4 disabled'));
289- process.exit(1);
290321 }
291322
292323 if (v4Failed && !useIPv6) {
293324 console.error(colorthis.red#fatal('fatal errorError: Failed to start server on IPv4 and IPv6 disabled'));
294- process.exit(1);
295325 }
296326
297327 if (v6Failed && v4Failed) {
298328 console.error(colorthis.red#fatal('fatal errorError: Failed to start server on both IPv6 and IPv4'));
299- process.exit(1);
300329 }
301330 }
302331