Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server: Support HTTPS configuration #527

Merged
merged 2 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions default.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"service": {
"host": null,
"port": 8081,
"protocol": "http",
"certFile": "",
"certKey": "",

"metrics": {
"enabled": false,
Expand Down
5 changes: 5 additions & 0 deletions src/service/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export interface ServiceConfig {
service: {
host?: string;
port: number;
protocol?: string;
certFile?: string;
certKey?: string;
minTLSVersion?: string;
metrics: MetricsConfig;
logging: LoggingConfig;
security: SecurityConfig;
Expand All @@ -33,6 +37,7 @@ export const defaultServiceConfig: ServiceConfig = {
service: {
host: undefined,
port: 8081,
protocol: 'http',
metrics: {
enabled: false,
collectDefaultMetrics: true,
Expand Down
52 changes: 41 additions & 11 deletions src/service/http-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as contentDisposition from 'content-disposition';
import * as express from 'express';
import * as fs from 'fs';
import * as http from 'http';
import * as https from 'https';
import * as morgan from 'morgan';
import * as multer from 'multer';
import * as net from 'net';
Expand All @@ -18,6 +19,7 @@ import { HTTPHeaders, ImageRenderOptions, RenderOptions } from '../types';
import { Sanitizer } from '../sanitizer/Sanitizer';
import { isSanitizeRequest } from '../sanitizer/types';
import { asyncMiddleware, trustedUrlMiddleware, authTokenMiddleware } from './middlewares';
import { SecureVersion } from 'tls';

const upload = multer({ storage: multer.memoryStorage() });

Expand Down Expand Up @@ -100,17 +102,7 @@ export class HttpServer {
return res.status(500).json(err);
});

if (this.config.service.host) {
this.server = this.app.listen(this.config.service.port, this.config.service.host, () => {
const info = this.server.address() as net.AddressInfo;
this.log.info(`HTTP Server started, listening at http://${this.config.service.host}:${info.port}`);
});
} else {
this.server = this.app.listen(this.config.service.port, () => {
const info = this.server.address() as net.AddressInfo;
this.log.info(`HTTP Server started, listening at http://localhost:${info.port}`);
});
}
this.createServer();

const metrics = {
durationHistogram: new promClient.Histogram({
Expand Down Expand Up @@ -141,6 +133,44 @@ export class HttpServer {
await this.browser.start();
}

createServer() {
const { protocol, host, port } = this.config.service;
if (protocol === 'https') {
const { certFile, certKey, minTLSVersion } = this.config.service
if (!certFile || !certKey) {
throw new Error('No cert file or cert key provided, cannot start HTTPS server');
}

if (minTLSVersion && minTLSVersion !== 'TLSv1.2' && minTLSVersion !== 'TLSv1.3') {
throw new Error('Only allowed TLS min versions are TLSv1.2 and TLSv1.3');
}

const options = {
cert: fs.readFileSync(certFile),
key: fs.readFileSync(certKey),

maxVersion: 'TLSv1.3' as SecureVersion,
minVersion: (minTLSVersion || 'TLSv1.2') as SecureVersion,
}

this.server = https.createServer(options, this.app)
} else {
this.server = http.createServer(this.app)
}

if (host) {
this.server.listen(port, host, () => {
const info = this.server.address() as net.AddressInfo;
this.log.info(`${protocol?.toUpperCase()} Server started, listening at ${protocol}://${host}:${info.port}`);
});
} else {
this.server = this.app.listen(port, () => {
const info = this.server.address() as net.AddressInfo;
this.log.info(`${protocol?.toUpperCase()} Server started, listening at ${protocol}://localhost:${info.port}`);
});
}
}

close() {
this.server.close();
}
Expand Down
Loading