Fix connection timeout being set incorrectly

This commit is contained in:
Zaid Abdulla committed 2020-10-18 12:52:01 -07:00
1 parent 9f4b421b59
commit 62849e8081
6 files changed
+69 -43

No files matched your search

+11 -11
View File
@@ -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(() => {
+8 -4
View File
@@ -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(() => {
+8 -4
View File
@@ -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(() => {
+8 -4
View File
@@ -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(() => {
+11 -5
View File
@@ -840,8 +840,11 @@ async function sendAcsRequest(
return writeResponse(sessionContext, res);
}
async function getSession(connection, sessionId): Promise<SessionContext> {
const sessionContext = currentSessions.get(connection);
async function getSession(
connection: Socket,
sessionId: string
): Promise<SessionContext> {
let sessionContext = currentSessions.get(connection);
if (sessionContext) {
currentSessions.delete(connection);
return sessionContext;
@@ -853,7 +856,9 @@ async function getSession(connection, sessionId): Promise<SessionContext> {
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,
+23 -15
View File
@@ -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<void> {