From 9524902b1732909f74c0487dd18afbb17a6bcc2a Mon Sep 17 00:00:00 2001 From: Roy Razon Date: Wed, 13 Mar 2024 17:13:13 +0200 Subject: [PATCH] tunnel server: fix keepAlive --- tunnel-server/package.json | 1 + tunnel-server/src/proxy/index.ts | 10 +++++++++- tunnel-server/yarn.lock | 16 +++++++++++++++- 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/tunnel-server/package.json b/tunnel-server/package.json index 1dd831db..885249cc 100644 --- a/tunnel-server/package.json +++ b/tunnel-server/package.json @@ -11,6 +11,7 @@ "@fastify/cors": "^8.3.0", "@fastify/request-context": "^5.0.0", "@sindresorhus/fnv1a": "^3.0.0", + "agentkeepalive": "^4.5.0", "content-type": "^1.0.5", "cookies": "^0.8.0", "fastify": "^4.22.2", diff --git a/tunnel-server/src/proxy/index.ts b/tunnel-server/src/proxy/index.ts index bbe206de..046c98e1 100644 --- a/tunnel-server/src/proxy/index.ts +++ b/tunnel-server/src/proxy/index.ts @@ -1,5 +1,6 @@ import httpProxy from 'http-proxy' import { IncomingMessage } from 'http' +import Agent from 'agentkeepalive' import net from 'net' import type { Logger } from 'pino' import { inspect } from 'util' @@ -43,7 +44,14 @@ export const proxy = ({ authFactory: (client: { publicKey: KeyObject; publicKeyThumbprint: string }) => Authenticator loginUrl: ({ env, returnPath }: { env: string; returnPath?: string }) => string }) => { - const theProxy = httpProxy.createProxyServer({ xfwd: true }) + const agent = new Agent({ + maxSockets: 100, + keepAlive: true, + maxFreeSockets: 10, + timeout: 60000, + }) + + const theProxy = httpProxy.createProxyServer({ xfwd: true, agent }) const injectionHandlers = proxyInjectionHandlers({ log }) theProxy.on('proxyRes', injectionHandlers.proxyResHandler) theProxy.on('proxyReq', injectionHandlers.proxyReqHandler) diff --git a/tunnel-server/yarn.lock b/tunnel-server/yarn.lock index ee2f13d8..d3c785e7 100644 --- a/tunnel-server/yarn.lock +++ b/tunnel-server/yarn.lock @@ -1216,6 +1216,13 @@ acorn@^8.9.0: resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.10.0.tgz#8be5b3907a67221a81ab23c7889c4c5526b62ec5" integrity sha512-F0SAmZ8iUtS//m8DmCTA0jlh6TDKkHQyK6xc6V4KDTyZKA9dnvX9/3sRTVQrWm79glUAZbnmmNcdYwUIHWVybw== +agentkeepalive@^4.5.0: + version "4.5.0" + resolved "https://registry.npmjs.org/agentkeepalive/-/agentkeepalive-4.5.0.tgz#2673ad1389b3c418c5a20c5d7364f93ca04be923" + integrity sha512-5GG/5IbQQpC9FpkRGsSvZI5QYeSCzlJHdpBQntCsuTOxhKD8lqKhrleg2Yi7yvMIf82Ycmmqln9U8V9qwEiJew== + dependencies: + humanize-ms "^1.2.1" + ajv-formats@^2.1.1: version "2.1.1" resolved "https://registry.yarnpkg.com/ajv-formats/-/ajv-formats-2.1.1.tgz#6e669400659eb74973bbf2e33327180a0996b520" @@ -2299,6 +2306,13 @@ human-signals@^2.1.0: resolved "https://registry.yarnpkg.com/human-signals/-/human-signals-2.1.0.tgz#dc91fcba42e4d06e4abaed33b3e7a3c02f514ea0" integrity sha512-B4FFZ6q/T2jhhksgkbEW3HBvWIfDW85snkQgawt07S7J5QXTk6BkNV+0yAeZrM5QpMAdYlocGoljn0sJ/WQkFw== +humanize-ms@^1.2.1: + version "1.2.1" + resolved "https://registry.npmjs.org/humanize-ms/-/humanize-ms-1.2.1.tgz#c46e3159a293f6b896da29316d8b6fe8bb79bbed" + integrity sha512-Fl70vYtsAFb/C06PTS9dZBo7ihau+Tu/DNCk/OyHhea07S+aeMWpFFkUaXRa8fI+ScZbEI8dfSxwY7gxZ9SAVQ== + dependencies: + ms "^2.0.0" + iconv-lite@^0.6.3: version "0.6.3" resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.6.3.tgz#a52f80bf38da1952eb5c681790719871a1a72501" @@ -3052,7 +3066,7 @@ ms@2.1.2: resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.2.tgz#d09d1f357b443f493382a8eb3ccd183872ae6009" integrity sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w== -ms@^2.1.1: +ms@^2.0.0, ms@^2.1.1: version "2.1.3" resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.3.tgz#574c8138ce1d2b5861f0b44579dbadd60c6615b2" integrity sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==