1515// limitations under the License.
1616
1717import express from 'express' ;
18- import https from 'https' ;
18+ import https , { Server } from 'https' ;
1919import http from 'http' ;
2020import WebSocket from 'ws' ;
2121import { init as initConfig , config } from './lib/config' ;
22- import { init as initCert , key , cert , ca } from './lib/cert' ;
22+ import { init as initCert , genTLSContext , loadCAs } from './lib/cert' ;
2323import { createLogger , LogLevelString } from 'bunyan' ;
2424import * as utils from './lib/utils' ;
25- import { router as apiRouter } from './routers/api' ;
25+ import { router as apiRouter , setAddTLSContext } from './routers/api' ;
2626import { router as p2pRouter , eventEmitter as p2pEventEmitter } from './routers/p2p' ;
2727import RequestError , { errorHandler } from './lib/request-error' ;
2828import * as eventsHandler from './handlers/events'
@@ -36,8 +36,17 @@ const log = createLogger({ name: 'app.ts', level: utils.constants.LOG_LEVEL as L
3636
3737const swaggerDocument = YAML . load ( path . join ( __dirname , './swagger.yaml' ) ) ;
3838
39+ let p2pServer : Server
40+
3941let delegatedWebSocket : WebSocket | undefined = undefined ;
4042
43+ export const addTLSContext = async ( hostname : string ) => {
44+ await loadCAs ( )
45+ // The most recent context wins (per the Node.js spec), so to get a reload we just add a wildcard context
46+ p2pServer . addContext ( hostname , genTLSContext ( ) )
47+ } ;
48+ setAddTLSContext ( addTLSContext )
49+
4150export const start = async ( ) => {
4251 await initConfig ( ) ;
4352 await initCert ( ) ;
@@ -46,13 +55,7 @@ export const start = async () => {
4655 const apiServer = http . createServer ( apiApp ) ;
4756
4857 const p2pApp = express ( ) ;
49- const p2pServer = https . createServer ( {
50- key,
51- cert,
52- ca,
53- rejectUnauthorized : true ,
54- requestCert : true ,
55- } , p2pApp ) ;
58+ p2pServer = https . createServer ( genTLSContext ( ) , p2pApp ) ;
5659
5760 const wss = new WebSocket . Server ( {
5861 server : apiServer , verifyClient : ( info , cb ) => {
0 commit comments