From 62849e8081a114d424bc3419e7986ac29d74a733 Mon Sep 17 00:00:00 2001 From: Zaid Abdulla Date: Sun, 18 Oct 2020 12:52:01 -0700 Subject: [PATCH] Fix connection timeout being set incorrectly --- bin/genieacs-cwmp.ts | 22 +++++++++++----------- bin/genieacs-fs.ts | 12 ++++++++---- bin/genieacs-nbi.ts | 12 ++++++++---- bin/genieacs-ui.ts | 12 ++++++++---- lib/cwmp.ts | 16 +++++++++++----- lib/server.ts | 38 +++++++++++++++++++++++--------------- 6 files changed, 69 insertions(+), 43 deletions(-) diff --git a/bin/genieacs-cwmp.ts b/bin/genieacs-cwmp.ts index 6e557e3..a271d77 100755 --- a/bin/genieacs-cwmp.ts +++ b/bin/genieacs-cwmp.ts @@ -77,9 +77,16 @@ if (!cluster.worker) { cluster.stop(); }); } else { - const ssl = { - key: config.get("CWMP_SSL_KEY") as string, - cert: config.get("CWMP_SSL_CERT") as string, + const key = config.get("CWMP_SSL_KEY") as string; + const cert = config.get("CWMP_SSL_CERT") as string; + + const options = { + port: SERVICE_PORT, + host: SERVICE_ADDRESS, + ssl: key && cert ? { key, cert } : null, + onConnection: cwmp.onConnection, + timeout: 30000, + keepAliveTimeout: 0, }; process.on("uncaughtException", (err) => { @@ -94,14 +101,7 @@ if (!cluster.worker) { const initPromise = Promise.all([db.connect(), cache.connect()]) .then(() => { - server.start( - SERVICE_PORT, - SERVICE_ADDRESS, - ssl, - cwmp.listener, - cwmp.onConnection, - 0 - ); + server.start(options, cwmp.listener); }) .catch((err) => { setTimeout(() => { diff --git a/bin/genieacs-fs.ts b/bin/genieacs-fs.ts index a743523..b6d3146 100755 --- a/bin/genieacs-fs.ts +++ b/bin/genieacs-fs.ts @@ -73,9 +73,13 @@ if (!cluster.worker) { cluster.stop(); }); } else { - const ssl = { - key: config.get("FS_SSL_KEY") as string, - cert: config.get("FS_SSL_CERT") as string, + const key = config.get("FS_SSL_KEY") as string; + const cert = config.get("FS_SSL_CERT") as string; + const options = { + port: SERVICE_PORT, + host: SERVICE_ADDRESS, + ssl: key && cert ? { key, cert } : null, + timeout: 30000, }; let stopping = false; @@ -98,7 +102,7 @@ if (!cluster.worker) { const initPromise = Promise.all([db.connect(), cache.connect()]) .then(() => { - server.start(SERVICE_PORT, SERVICE_ADDRESS, ssl, _listener); + server.start(options, _listener); }) .catch((err) => { setTimeout(() => { diff --git a/bin/genieacs-nbi.ts b/bin/genieacs-nbi.ts index 4111884..4b695bf 100755 --- a/bin/genieacs-nbi.ts +++ b/bin/genieacs-nbi.ts @@ -77,9 +77,13 @@ if (!cluster.worker) { cluster.stop(); }); } else { - const ssl = { - key: config.get("NBI_SSL_KEY") as string, - cert: config.get("NBI_SSL_CERT") as string, + const key = config.get("NBI_SSL_KEY") as string; + const cert = config.get("NBI_SSL_CERT") as string; + const options = { + port: SERVICE_PORT, + host: SERVICE_ADDRESS, + ssl: key && cert ? { key, cert } : null, + timeout: 30000, }; let stopping = false; @@ -102,7 +106,7 @@ if (!cluster.worker) { const initPromise = Promise.all([db.connect(), cache.connect()]) .then(() => { - server.start(SERVICE_PORT, SERVICE_ADDRESS, ssl, _listener); + server.start(options, _listener); }) .catch((err) => { setTimeout(() => { diff --git a/bin/genieacs-ui.ts b/bin/genieacs-ui.ts index faae2bf..83c40ea 100755 --- a/bin/genieacs-ui.ts +++ b/bin/genieacs-ui.ts @@ -79,9 +79,13 @@ if (!cluster.worker) { cluster.stop(); }); } else { - const ssl = { - key: config.get("UI_SSL_KEY") as string, - cert: config.get("UI_SSL_CERT") as string, + const key = config.get("UI_SSL_KEY") as string; + const cert = config.get("UI_SSL_CERT") as string; + const options = { + port: SERVICE_PORT, + host: SERVICE_ADDRESS, + ssl: key && cert ? { key, cert } : null, + timeout: 30000, }; let stopping = false; @@ -106,7 +110,7 @@ if (!cluster.worker) { const initPromise = Promise.all([db2.connect(), cache.connect()]) .then(() => { - server.start(SERVICE_PORT, SERVICE_ADDRESS, ssl, _listener); + server.start(options, _listener); }) .catch((err) => { setTimeout(() => { diff --git a/lib/cwmp.ts b/lib/cwmp.ts index 60de0b3..9d56815 100644 --- a/lib/cwmp.ts +++ b/lib/cwmp.ts @@ -840,8 +840,11 @@ async function sendAcsRequest( return writeResponse(sessionContext, res); } -async function getSession(connection, sessionId): Promise { - const sessionContext = currentSessions.get(connection); +async function getSession( + connection: Socket, + sessionId: string +): Promise { + let sessionContext = currentSessions.get(connection); if (sessionContext) { currentSessions.delete(connection); return sessionContext; @@ -853,7 +856,9 @@ async function getSession(connection, sessionId): Promise { const sessionContextString = await cache.pop(`session_${sessionId}`); if (!sessionContextString) return null; - return session.deserialize(sessionContextString); + sessionContext = await session.deserialize(sessionContextString); + connection.setTimeout(sessionContext.timeout); + return sessionContext; } // Only needed to prevent tree shaking from removing the remoteAddress @@ -1068,6 +1073,8 @@ async function processRequest( ); } + sessionContext.httpRequest.socket.setTimeout(sessionContext.timeout * 1000); + if (sessionContext.debug) { debug.incomingHttpRequest( sessionContext.httpRequest, @@ -1412,7 +1419,7 @@ async function listenerAsync( } const parseWarnings = []; - let rpc; + let rpc: SoapMessage; try { rpc = soap.request( bodyStr, @@ -1517,7 +1524,6 @@ async function listenerAsync( _sessionContext.httpRequest = httpRequest; _sessionContext.httpResponse = httpResponse; _sessionContext.sessionId = crypto.randomBytes(8).toString("hex"); - httpRequest.socket.setTimeout(_sessionContext.timeout * 1000); const { tasks: dueTasks, diff --git a/lib/server.ts b/lib/server.ts index f8a662e..21b8fb9 100644 --- a/lib/server.ts +++ b/lib/server.ts @@ -52,35 +52,43 @@ function closeServer(timeout, callback): void { }); } +interface ServerOptions { + port?: number; + host?: string; + ssl?: { key: string; cert: string }; + timeout?: number; + keepAliveTimeout?: number; + onConnection?: (socket: Socket) => void; +} + export function start( - port: number, - networkInterface: string, - ssl: { key: string; cert: string }, - _listener: http.RequestListener, - onConnection?: (socket: Socket) => void, - keepAliveTimeout = -1 + options: ServerOptions, + _listener: http.RequestListener ): void { listener = _listener; - if (ssl && ssl.key && ssl.cert) { - const options = { - key: ssl.key + if (options.ssl) { + const opts = { + key: options.ssl.key .split(":") .map((f) => fs.readFileSync(path.resolve(ROOT_DIR, f.trim()))), - cert: ssl.cert + cert: options.ssl.cert .split(":") .map((f) => fs.readFileSync(path.resolve(ROOT_DIR, f.trim()))), }; - server = https.createServer(options, listener); - if (onConnection != null) server.on("secureConnection", onConnection); + server = https.createServer(opts, listener); + if (options.onConnection) + server.on("secureConnection", options.onConnection); } else { server = http.createServer(listener); - if (onConnection != null) server.on("connection", onConnection); + if (options.onConnection) server.on("connection", options.onConnection); } - if (keepAliveTimeout >= 0) server.keepAliveTimeout = keepAliveTimeout; - server.listen(port, networkInterface); + server.timeout = options.timeout || 0; + if (options.keepAliveTimeout != null) + server.keepAliveTimeout = options.keepAliveTimeout; + server.listen({ port: options.port, host: options.host }); } export function stop(): Promise {