diff --git a/tdrive/backend/node/package.json b/tdrive/backend/node/package.json index f7c73f79..a67376e7 100644 --- a/tdrive/backend/node/package.json +++ b/tdrive/backend/node/package.json @@ -79,7 +79,7 @@ "@types/lodash": "^4.14.165", "@types/minio": "^7.0.7", "@types/mongodb": "^4.0.7", - "@types/node": "^14.11.2", + "@types/node": "20", "@types/node-cron": "^3.0.0", "@types/node-fetch": "^2.5.12", "@types/node-uuid": "^0.0.28", @@ -124,6 +124,7 @@ "@sentry/node": "^6.19.7", "@sentry/tracing": "^6.19.7", "@socket.io/redis-adapter": "^7.1.0", + "@types/http-proxy": "^1.17.11", "@types/pdf-parse": "^1.1.1", "@types/redis": "^4.0.11", "@types/sharp": "^0.29.5", @@ -141,7 +142,6 @@ "deep-object-diff": "^1.1.0", "emoji-name-map": "^1.2.9", "eta": "^1.12.3", - "fast-proxy": "^2.1.0", "fastify": "^3.29.4", "fastify-cors": "^4.1.0", "fastify-formbody": "^5.0.0", @@ -161,6 +161,7 @@ "get-website-favicon": "^0.0.7", "html-metadata-parser": "^2.0.4", "html-to-text": "^8.2.1", + "http-proxy": "^1.18.1", "idtoken-verifier": "^2.2.3", "jsonwebtoken": "^8.5.1", "jwks-rsa": "^3.0.1", @@ -172,6 +173,7 @@ "mongodb": "^4.1.0", "multistream": "^4.1.0", "njwt": "^2.0.0", + "node-abort-controller": "^3.1.1", "node-cache": "^5.1.2", "node-cron": "^3.0.0", "node-fetch": "^2.6.7", diff --git a/tdrive/backend/node/src/services/applications-api/index.ts b/tdrive/backend/node/src/services/applications-api/index.ts index 8fa658e7..4b56ca73 100644 --- a/tdrive/backend/node/src/services/applications-api/index.ts +++ b/tdrive/backend/node/src/services/applications-api/index.ts @@ -1,10 +1,21 @@ import config from "config"; -import FastProxy from "fast-proxy"; +import httpProxy from "http-proxy"; import { Prefix, TdriveService } from "../../core/platform/framework"; import WebServerAPI from "../../core/platform/services/webserver/provider"; import Application from "../applications/entities/application"; import web from "./web/index"; +const proxy = httpProxy.createProxyServer({}); +// https://github.com/http-party/node-http-proxy/issues/1471 +proxy.on("proxyReq", (proxyReq, req: any) => { + if (req.body && ["POST", "PATCH", "PUT"].includes(req.method)) { + const bodyData = JSON.stringify(req.body); + proxyReq.setHeader("content-type", "application/json"); + proxyReq.setHeader("content-length", Buffer.byteLength(bodyData)); + proxyReq.write(bodyData); + } +}); + @Prefix("/api") export default class ApplicationsApiService extends TdriveService { version = "1"; @@ -20,17 +31,20 @@ export default class ApplicationsApiService extends TdriveService { //Redirect requests from /plugins/* to the plugin server (if installed) const apps = config.get("applications.plugins") || []; for (const app of apps) { - const domain = app.internal_domain; - const prefix = app.external_prefix; + const domain = app.internal_domain.replace(/(\/$|^\/)/gm, ""); + const prefix = app.external_prefix.replace(/(\/$|^\/)/gm, ""); if (domain && prefix) { - const { proxy, close } = FastProxy({ - base: domain, - }); - console.log("Listening at ", "/" + prefix.replace(/(\/$|^\/)/gm, "") + "/*"); - fastify.addHook("onClose", close); - fastify.all("/" + prefix.replace(/(\/$|^\/)/gm, "") + "/*", (req, rep) => { - proxy(req.raw, rep.raw, req.url, {}); - }); + try { + fastify.all("/" + prefix + "/*", (req, rep) => { + proxy.web(req.raw, rep.raw, { + target: domain, + }); + }); + console.log("Listening at ", "/" + prefix + "/*"); + } catch (e) { + console.log(e); + console.log("Can't listen to ", "/" + prefix + "/*"); + } } } diff --git a/tdrive/backend/node/yarn.lock b/tdrive/backend/node/yarn.lock index bc3a4165..dbc3d173 100644 --- a/tdrive/backend/node/yarn.lock +++ b/tdrive/backend/node/yarn.lock @@ -1848,6 +1848,13 @@ resolved "https://registry.yarnpkg.com/@types/http-cache-semantics/-/http-cache-semantics-4.0.1.tgz#0ea7b61496902b95890dc4c3a116b60cb8dae812" integrity sha512-SZs7ekbP8CN0txVG2xVRH6EgKmEm31BOxA07vkFaETzZz1xh+cbt8BcI0slpymvwhx5dlFnQG2rTlPVQn+iRPQ== +"@types/http-proxy@^1.17.11": + version "1.17.11" + resolved "https://registry.yarnpkg.com/@types/http-proxy/-/http-proxy-1.17.11.tgz#0ca21949a5588d55ac2b659b69035c84bd5da293" + integrity sha512-HC8G7c1WmaF2ekqpnFq626xd3Zz0uvaqFmBJNRZCGEZCXkvSdJoNFn/8Ygbd9fKNQj8UzLdCETaI0UWPAjK7IA== + dependencies: + "@types/node" "*" + "@types/istanbul-lib-coverage@*", "@types/istanbul-lib-coverage@^2.0.0", "@types/istanbul-lib-coverage@^2.0.1": version "2.0.4" resolved "https://registry.npmjs.org/@types/istanbul-lib-coverage/-/istanbul-lib-coverage-2.0.4.tgz" @@ -1967,11 +1974,16 @@ dependencies: "@types/node" "*" -"@types/node@*", "@types/node@>=10.0.0", "@types/node@>=8", "@types/node@^14.0.1", "@types/node@^14.11.2": +"@types/node@*", "@types/node@>=10.0.0", "@types/node@>=8", "@types/node@^14.0.1": version "14.18.21" resolved "https://registry.npmjs.org/@types/node/-/node-14.18.21.tgz" integrity sha512-x5W9s+8P4XteaxT/jKF0PSb7XEvo5VmqEWgsMlyeY4ZlLK8I6aH6g5TPPyDlLAep+GYf4kefb7HFyc7PAO3m+Q== +"@types/node@20": + version "20.1.2" + resolved "https://registry.yarnpkg.com/@types/node/-/node-20.1.2.tgz#8fd63447e3f99aba6c3168fd2ec4580d5b97886f" + integrity sha512-CTO/wa8x+rZU626cL2BlbCDzydgnFNgc19h4YvizpTO88MFQxab8wqisxaofQJ/9bLGugRdWIuX/TbIs6VVF6g== + "@types/node@^15.0.1": version "15.14.9" resolved "https://registry.yarnpkg.com/@types/node/-/node-15.14.9.tgz#bc43c990c3c9be7281868bbc7b8fdd6e2b57adfa" @@ -4519,6 +4531,11 @@ event-target-shim@^5.0.0: resolved "https://registry.yarnpkg.com/event-target-shim/-/event-target-shim-5.0.1.tgz#5d4d3ebdf9583d63a5333ce2deb7480ab2b05789" integrity sha512-i/2XbnSz/uxRCU6+NdVJgKWDTM427+MqYbkQzD321DuCQJUqOuJKIA0IM2+W2xtYHdKOmZ4dR6fExsd4SXL+WQ== +eventemitter3@^4.0.0: + version "4.0.7" + resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-4.0.7.tgz#2de9b68f6528d5644ef5c59526a1b4a07306169f" + integrity sha512-8guHBZCwKnFhYdHr2ysuRWErTwhoN2X8XELRlrRwpmfeY2jjuUN4taQMsULKUVo1K4DvZl+0pgfyoysHxvmvEw== + events@^3.3.0: version "3.3.0" resolved "https://registry.yarnpkg.com/events/-/events-3.3.0.tgz#31a95ad0a924e2d2c419a813aeb2c4e878ea7400" @@ -4696,17 +4713,6 @@ fast-levenshtein@^2.0.6, fast-levenshtein@~2.0.6: resolved "https://registry.npmjs.org/fast-levenshtein/-/fast-levenshtein-2.0.6.tgz" integrity sha512-DCXu6Ifhqcks7TZKY3Hxp3y6qphY5SJZmrWMDrKcERSOXWQdMhU9Ig/PYrzyw/ul9jOIyh0N4M0tbC5hodg8dw== -fast-proxy@^2.1.0: - version "2.1.0" - resolved "https://registry.yarnpkg.com/fast-proxy/-/fast-proxy-2.1.0.tgz#f4544e1db5fa72cb584a6a28497717cc88c561a6" - integrity sha512-ihCPWH8j4pfSIULx6evT39RzZjCkGScCVn2BRyJElDu3v96NCWjPJPX8Olnzxv30mMQUEEijonTKroqCVpXjUQ== - dependencies: - end-of-stream "^1.4.4" - pump "^3.0.0" - semver "^7.3.5" - tiny-lru "^7.0.6" - undici "^3.3.6" - fast-redact@^3.0.0: version "3.1.1" resolved "https://registry.npmjs.org/fast-redact/-/fast-redact-3.1.1.tgz" @@ -5028,6 +5034,11 @@ follow-redirects@1.5.10: dependencies: debug "=3.1.0" +follow-redirects@^1.0.0: + version "1.15.2" + resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.2.tgz#b460864144ba63f2681096f274c4e57026da2c13" + integrity sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA== + follow-redirects@^1.14.0, follow-redirects@^1.14.4: version "1.15.1" resolved "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.1.tgz" @@ -5623,6 +5634,15 @@ http-proxy-agent@^4.0.1: agent-base "6" debug "4" +http-proxy@^1.18.1: + version "1.18.1" + resolved "https://registry.yarnpkg.com/http-proxy/-/http-proxy-1.18.1.tgz#401541f0534884bbf95260334e72f88ee3976549" + integrity sha512-7mz/721AbnJwIVbnaSv1Cz3Am0ZLT/UBwkC92VlxhXv/k/BBQfM2fXElQNC27BVGr0uwUpplYPQM9LnaBMR5NQ== + dependencies: + eventemitter3 "^4.0.0" + follow-redirects "^1.0.0" + requires-port "^1.0.0" + http2-wrapper@^1.0.0-beta.5.2: version "1.0.3" resolved "https://registry.yarnpkg.com/http2-wrapper/-/http2-wrapper-1.0.3.tgz#b8f55e0c1f25d4ebd08b3b0c2c079f9590800b3d" @@ -7484,6 +7504,11 @@ node-abi@^3.3.0: dependencies: semver "^7.3.5" +node-abort-controller@^3.1.1: + version "3.1.1" + resolved "https://registry.yarnpkg.com/node-abort-controller/-/node-abort-controller-3.1.1.tgz#a94377e964a9a37ac3976d848cb5c765833b8548" + integrity sha512-AGK2yQKIjRuqnc6VkX2Xj5d+QW8xZ87pa1UK6yA6ouUyuxfHuMP6umE5QK7UmTeOAymo+Zx1Fxiuw9rVx8taHQ== + node-addon-api@^3.1.0: version "3.2.1" resolved "https://registry.yarnpkg.com/node-addon-api/-/node-addon-api-3.2.1.tgz#81325e0a2117789c0128dab65e7e38f07ceba161" @@ -9576,11 +9601,6 @@ through@2, through@~2.3, through@~2.3.1: resolved "https://registry.npmjs.org/through/-/through-2.3.8.tgz" integrity sha512-w89qg7PI8wAdvX60bMDP+bFoD5Dvhm9oLheFp5O4a2QF0cSBGsBX4qZmadPMvVqlLJBBci+WqGGOAPvcDeNSVg== -tiny-lru@^7.0.6: - version "7.0.6" - resolved "https://registry.yarnpkg.com/tiny-lru/-/tiny-lru-7.0.6.tgz#b0c3cdede1e5882aa2d1ae21cb2ceccf2a331f24" - integrity sha512-zNYO0Kvgn5rXzWpL0y3RS09sMK67eGaQj9805jlK9G6pSadfriTczzLHFXa/xcW4mIRfmlB9HyQ/+SgL0V1uow== - tiny-lru@^8.0.1: version "8.0.2" resolved "https://registry.npmjs.org/tiny-lru/-/tiny-lru-8.0.2.tgz" @@ -9858,11 +9878,6 @@ undefsafe@^2.0.2: resolved "https://registry.npmjs.org/undefsafe/-/undefsafe-2.0.5.tgz" integrity sha512-WxONCrssBM8TSPRqN5EmsjVrsv4A8X12J4ArBiiayv3DyyG3ZlIg6yysuuSYdZsVz3TKcTg2fd//Ujd4CHV1iA== -undici@^3.3.6: - version "3.3.6" - resolved "https://registry.yarnpkg.com/undici/-/undici-3.3.6.tgz#06d3b97b7eeff46bce6f8a71079c09f64dd59dc1" - integrity sha512-/j3YTZ5AobMB4ZrTY72mzM54uFUX32v0R/JRW9G2vOyF1uSKYAx+WT8dMsAcRS13TOFISv094TxIyWYk+WEPsA== - unfetch@^4.2.0: version "4.2.0" resolved "https://registry.yarnpkg.com/unfetch/-/unfetch-4.2.0.tgz#7e21b0ef7d363d8d9af0fb929a5555f6ef97a3be"