@@ -4,6 +4,7 @@ import * as contentDisposition from 'content-disposition';
44import * as express from 'express' ;
55import * as fs from 'fs' ;
66import * as http from 'http' ;
7+ import * as https from 'https' ;
78import * as morgan from 'morgan' ;
89import * as multer from 'multer' ;
910import * as net from 'net' ;
@@ -18,6 +19,7 @@ import { HTTPHeaders, ImageRenderOptions, RenderOptions } from '../types';
1819import { Sanitizer } from '../sanitizer/Sanitizer' ;
1920import { isSanitizeRequest } from '../sanitizer/types' ;
2021import { asyncMiddleware , trustedUrlMiddleware , authTokenMiddleware } from './middlewares' ;
22+ import { SecureVersion } from 'tls' ;
2123
2224const 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