Skip to content

Commit d3165a1

Browse files
authored
Server: Support HTTPS configuration (#527)
* Server: Support HTTPS configuration * add support for minTLSVersion
1 parent 2c1f7dd commit d3165a1

File tree

3 files changed

+49
-11
lines changed

3 files changed

+49
-11
lines changed

default.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
"service": {
33
"host": null,
44
"port": 8081,
5+
"protocol": "http",
6+
"certFile": "",
7+
"certKey": "",
58

69
"metrics": {
710
"enabled": false,

src/service/config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ export interface ServiceConfig {
2222
service: {
2323
host?: string;
2424
port: number;
25+
protocol?: string;
26+
certFile?: string;
27+
certKey?: string;
28+
minTLSVersion?: string;
2529
metrics: MetricsConfig;
2630
logging: LoggingConfig;
2731
security: SecurityConfig;
@@ -33,6 +37,7 @@ export const defaultServiceConfig: ServiceConfig = {
3337
service: {
3438
host: undefined,
3539
port: 8081,
40+
protocol: 'http',
3641
metrics: {
3742
enabled: false,
3843
collectDefaultMetrics: true,

src/service/http-server.ts

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as contentDisposition from 'content-disposition';
44
import * as express from 'express';
55
import * as fs from 'fs';
66
import * as http from 'http';
7+
import * as https from 'https';
78
import * as morgan from 'morgan';
89
import * as multer from 'multer';
910
import * as net from 'net';
@@ -18,6 +19,7 @@ import { HTTPHeaders, ImageRenderOptions, RenderOptions } from '../types';
1819
import { Sanitizer } from '../sanitizer/Sanitizer';
1920
import { isSanitizeRequest } from '../sanitizer/types';
2021
import { asyncMiddleware, trustedUrlMiddleware, authTokenMiddleware } from './middlewares';
22+
import { SecureVersion } from 'tls';
2123

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

@@ -100,17 +102,7 @@ export class HttpServer {
100102
return res.status(500).json(err);
101103
});
102104

103-
if (this.config.service.host) {
104-
this.server = this.app.listen(this.config.service.port, this.config.service.host, () => {
105-
const info = this.server.address() as net.AddressInfo;
106-
this.log.info(`HTTP Server started, listening at http://${this.config.service.host}:${info.port}`);
107-
});
108-
} else {
109-
this.server = this.app.listen(this.config.service.port, () => {
110-
const info = this.server.address() as net.AddressInfo;
111-
this.log.info(`HTTP Server started, listening at http://localhost:${info.port}`);
112-
});
113-
}
105+
this.createServer();
114106

115107
const metrics = {
116108
durationHistogram: new promClient.Histogram({
@@ -141,6 +133,44 @@ export class HttpServer {
141133
await this.browser.start();
142134
}
143135

136+
createServer() {
137+
const { protocol, host, port } = this.config.service;
138+
if (protocol === 'https') {
139+
const { certFile, certKey, minTLSVersion } = this.config.service
140+
if (!certFile || !certKey) {
141+
throw new Error('No cert file or cert key provided, cannot start HTTPS server');
142+
}
143+
144+
if (minTLSVersion && minTLSVersion !== 'TLSv1.2' && minTLSVersion !== 'TLSv1.3') {
145+
throw new Error('Only allowed TLS min versions are TLSv1.2 and TLSv1.3');
146+
}
147+
148+
const options = {
149+
cert: fs.readFileSync(certFile),
150+
key: fs.readFileSync(certKey),
151+
152+
maxVersion: 'TLSv1.3' as SecureVersion,
153+
minVersion: (minTLSVersion || 'TLSv1.2') as SecureVersion,
154+
}
155+
156+
this.server = https.createServer(options, this.app)
157+
} else {
158+
this.server = http.createServer(this.app)
159+
}
160+
161+
if (host) {
162+
this.server.listen(port, host, () => {
163+
const info = this.server.address() as net.AddressInfo;
164+
this.log.info(`${protocol?.toUpperCase()} Server started, listening at ${protocol}://${host}:${info.port}`);
165+
});
166+
} else {
167+
this.server = this.app.listen(port, () => {
168+
const info = this.server.address() as net.AddressInfo;
169+
this.log.info(`${protocol?.toUpperCase()} Server started, listening at ${protocol}://localhost:${info.port}`);
170+
});
171+
}
172+
}
173+
144174
close() {
145175
this.server.close();
146176
}

0 commit comments

Comments
 (0)