mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Move httpProxy server to injectables
Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
parent
180e5b9449
commit
b6050dea4e
@ -3,7 +3,7 @@
|
|||||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { isLongRunningRequest } from "../lens-proxy/is-long-running-request";
|
import { isLongRunningRequest } from "../lens-proxy/helpers";
|
||||||
|
|
||||||
describe("isLongRunningRequest", () => {
|
describe("isLongRunningRequest", () => {
|
||||||
it("returns true on watches", () => {
|
it("returns true on watches", () => {
|
||||||
|
|||||||
@ -9,9 +9,9 @@ import contentSecurityPolicyInjectable from "../../common/vars/content-security-
|
|||||||
import kubeAuthProxyServerInjectable from "../cluster/kube-auth-proxy-server.injectable";
|
import kubeAuthProxyServerInjectable from "../cluster/kube-auth-proxy-server.injectable";
|
||||||
import routeRequestInjectable from "../router/route-request.injectable";
|
import routeRequestInjectable from "../router/route-request.injectable";
|
||||||
import getClusterForRequestInjectable from "./get-cluster-for-request.injectable";
|
import getClusterForRequestInjectable from "./get-cluster-for-request.injectable";
|
||||||
import { isLongRunningRequest } from "./is-long-running-request";
|
import { isLongRunningRequest } from "./helpers";
|
||||||
import type { ProxyIncomingMessage } from "./messages";
|
import type { ProxyIncomingMessage } from "./messages";
|
||||||
import proxyInjectable from "./proxy.injectable";
|
import rawHttpProxyInjectable from "./proxy/raw-proxy.injectable";
|
||||||
|
|
||||||
export type HandleRouteRequest = (req: ProxyIncomingMessage, res: ServerResponse) => Promise<void>;
|
export type HandleRouteRequest = (req: ProxyIncomingMessage, res: ServerResponse) => Promise<void>;
|
||||||
|
|
||||||
@ -20,7 +20,7 @@ const handleRouteRequestInjectable = getInjectable({
|
|||||||
instantiate: (di): HandleRouteRequest => {
|
instantiate: (di): HandleRouteRequest => {
|
||||||
const getClusterForRequest = di.inject(getClusterForRequestInjectable);
|
const getClusterForRequest = di.inject(getClusterForRequestInjectable);
|
||||||
const routeRequest = di.inject(routeRequestInjectable);
|
const routeRequest = di.inject(routeRequestInjectable);
|
||||||
const proxy = di.inject(proxyInjectable);
|
const proxy = di.inject(rawHttpProxyInjectable);
|
||||||
const contentSecurityPolicy = di.inject(contentSecurityPolicyInjectable);
|
const contentSecurityPolicy = di.inject(contentSecurityPolicyInjectable);
|
||||||
|
|
||||||
return async (req, res) => {
|
return async (req, res) => {
|
||||||
|
|||||||
@ -2,6 +2,7 @@
|
|||||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||||
*/
|
*/
|
||||||
|
import type { IncomingMessage } from "http";
|
||||||
import { getBoolean } from "../utils/parse-query";
|
import { getBoolean } from "../utils/parse-query";
|
||||||
|
|
||||||
const watchParam = "watch";
|
const watchParam = "watch";
|
||||||
@ -12,3 +13,7 @@ export function isLongRunningRequest(reqUrl: string) {
|
|||||||
|
|
||||||
return getBoolean(url.searchParams, watchParam) || getBoolean(url.searchParams, followParam);
|
return getBoolean(url.searchParams, watchParam) || getBoolean(url.searchParams, followParam);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getRequestId(req: IncomingMessage) {
|
||||||
|
return (req.headers.host ?? "") + req.url;
|
||||||
|
}
|
||||||
@ -7,20 +7,18 @@ import { LensProxy } from "./lens-proxy";
|
|||||||
import lensProxyPortInjectable from "./lens-proxy-port.injectable";
|
import lensProxyPortInjectable from "./lens-proxy-port.injectable";
|
||||||
import emitAppEventInjectable from "../../common/app-event-bus/emit-event.injectable";
|
import emitAppEventInjectable from "../../common/app-event-bus/emit-event.injectable";
|
||||||
import loggerInjectable from "../../common/logger.injectable";
|
import loggerInjectable from "../../common/logger.injectable";
|
||||||
import proxyInjectable from "./proxy.injectable";
|
|
||||||
import handleRouteRequestInjectable from "./handle-route-request.injectable";
|
|
||||||
import lensProxyHttpsServerInjectable from "./https-proxy/server.injectable";
|
import lensProxyHttpsServerInjectable from "./https-proxy/server.injectable";
|
||||||
|
import proxyRetryInjectable from "./proxy/retry.injectable";
|
||||||
|
|
||||||
const lensProxyInjectable = getInjectable({
|
const lensProxyInjectable = getInjectable({
|
||||||
id: "lens-proxy",
|
id: "lens-proxy",
|
||||||
|
|
||||||
instantiate: (di) => new LensProxy({
|
instantiate: (di) => new LensProxy({
|
||||||
proxy: di.inject(proxyInjectable),
|
|
||||||
proxyServer: di.inject(lensProxyHttpsServerInjectable),
|
proxyServer: di.inject(lensProxyHttpsServerInjectable),
|
||||||
handleRouteRequest: di.inject(handleRouteRequestInjectable),
|
|
||||||
lensProxyPort: di.inject(lensProxyPortInjectable),
|
lensProxyPort: di.inject(lensProxyPortInjectable),
|
||||||
emitAppEvent: di.inject(emitAppEventInjectable),
|
emitAppEvent: di.inject(emitAppEventInjectable),
|
||||||
logger: di.inject(loggerInjectable),
|
logger: di.inject(loggerInjectable),
|
||||||
|
proxyRetry: di.inject(proxyRetryInjectable),
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -3,18 +3,16 @@
|
|||||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import net from "net";
|
import type net from "net";
|
||||||
import type https from "https";
|
import type https from "https";
|
||||||
import type http from "http";
|
import type http from "http";
|
||||||
import type httpProxy from "http-proxy";
|
|
||||||
import type { Cluster } from "../../common/cluster/cluster";
|
import type { Cluster } from "../../common/cluster/cluster";
|
||||||
import type { ProxyApiRequestArgs } from "./proxy-functions";
|
import type { ProxyApiRequestArgs } from "./proxy-functions";
|
||||||
import assert from "assert";
|
|
||||||
import type { SetRequired } from "type-fest";
|
import type { SetRequired } from "type-fest";
|
||||||
import type { EmitAppEvent } from "../../common/app-event-bus/emit-event.injectable";
|
import type { EmitAppEvent } from "../../common/app-event-bus/emit-event.injectable";
|
||||||
import type { Logger } from "../../common/logger";
|
import type { Logger } from "../../common/logger";
|
||||||
import { disallowedPorts } from "./disallowed-ports";
|
import { disallowedPorts } from "./disallowed-ports";
|
||||||
import type { HandleRouteRequest } from "./handle-route-request.injectable";
|
import type { ProxyRetry } from "./proxy/retry.injectable";
|
||||||
|
|
||||||
export type GetClusterForRequest = (req: http.IncomingMessage) => Cluster | undefined;
|
export type GetClusterForRequest = (req: http.IncomingMessage) => Cluster | undefined;
|
||||||
export type ServerIncomingMessage = SetRequired<http.IncomingMessage, "url" | "method">;
|
export type ServerIncomingMessage = SetRequired<http.IncomingMessage, "url" | "method">;
|
||||||
@ -22,20 +20,14 @@ export type LensProxyApiRequest = (args: ProxyApiRequestArgs) => void | Promise<
|
|||||||
|
|
||||||
interface Dependencies {
|
interface Dependencies {
|
||||||
emitAppEvent: EmitAppEvent;
|
emitAppEvent: EmitAppEvent;
|
||||||
handleRouteRequest: HandleRouteRequest;
|
|
||||||
readonly proxy: httpProxy;
|
|
||||||
readonly lensProxyPort: { set: (portNumber: number) => void };
|
readonly lensProxyPort: { set: (portNumber: number) => void };
|
||||||
readonly logger: Logger;
|
readonly logger: Logger;
|
||||||
readonly proxyServer: https.Server;
|
readonly proxyServer: https.Server;
|
||||||
|
readonly proxyRetry: ProxyRetry;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class LensProxy {
|
export class LensProxy {
|
||||||
protected closed = false;
|
constructor(private readonly dependencies: Dependencies) {}
|
||||||
protected retryCounters = new Map<string, number>();
|
|
||||||
|
|
||||||
constructor(private readonly dependencies: Dependencies) {
|
|
||||||
this.configureProxy(dependencies.proxy);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Starts to listen on an OS provided port. Will reject if the server throws
|
* Starts to listen on an OS provided port. Will reject if the server throws
|
||||||
@ -107,61 +99,6 @@ export class LensProxy {
|
|||||||
this.dependencies.logger.info("[LENS-PROXY]: Closing server");
|
this.dependencies.logger.info("[LENS-PROXY]: Closing server");
|
||||||
|
|
||||||
this.dependencies.proxyServer.close();
|
this.dependencies.proxyServer.close();
|
||||||
this.closed = true;
|
this.dependencies.proxyRetry.close();
|
||||||
}
|
|
||||||
|
|
||||||
protected configureProxy(proxy: httpProxy): httpProxy {
|
|
||||||
proxy.on("proxyRes", (proxyRes, req, res) => {
|
|
||||||
const retryCounterId = this.getRequestId(req);
|
|
||||||
|
|
||||||
if (this.retryCounters.has(retryCounterId)) {
|
|
||||||
this.retryCounters.delete(retryCounterId);
|
|
||||||
}
|
|
||||||
|
|
||||||
proxyRes.on("aborted", () => { // happens when proxy target aborts connection
|
|
||||||
res.end();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
proxy.on("error", (error, req, res, target) => {
|
|
||||||
if (this.closed || res instanceof net.Socket) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.dependencies.logger.error(`[LENS-PROXY]: http proxy errored for cluster: ${error}`, { url: req.url });
|
|
||||||
|
|
||||||
if (target) {
|
|
||||||
this.dependencies.logger.debug(`Failed proxy to target: ${JSON.stringify(target, null, 2)}`);
|
|
||||||
|
|
||||||
if (req.method === "GET" && (!res.statusCode || res.statusCode >= 500)) {
|
|
||||||
const reqId = this.getRequestId(req);
|
|
||||||
const retryCount = this.retryCounters.get(reqId) || 0;
|
|
||||||
const timeoutMs = retryCount * 250;
|
|
||||||
|
|
||||||
if (retryCount < 20) {
|
|
||||||
this.dependencies.logger.debug(`Retrying proxy request to url: ${reqId}`);
|
|
||||||
setTimeout(() => {
|
|
||||||
this.retryCounters.set(reqId, retryCount + 1);
|
|
||||||
this.dependencies.handleRouteRequest(req as any, res)
|
|
||||||
.catch(error => this.dependencies.logger.error(`[LENS-PROXY]: failed to handle request on proxy error: ${error}`));
|
|
||||||
}, timeoutMs);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
res.writeHead(500).end(`Oops, something went wrong.\n${error}`);
|
|
||||||
} catch (e) {
|
|
||||||
this.dependencies.logger.error(`[LENS-PROXY]: Failed to write headers: `, e);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return proxy;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected getRequestId(req: http.IncomingMessage): string {
|
|
||||||
assert(req.headers.host);
|
|
||||||
|
|
||||||
return req.headers.host + req.url;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,13 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
|
||||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
|
||||||
*/
|
|
||||||
import { getInjectable } from "@ogre-tools/injectable";
|
|
||||||
import { createProxy } from "http-proxy";
|
|
||||||
|
|
||||||
const proxyInjectable = getInjectable({
|
|
||||||
id: "proxy",
|
|
||||||
instantiate: () => createProxy(),
|
|
||||||
});
|
|
||||||
|
|
||||||
export default proxyInjectable;
|
|
||||||
@ -0,0 +1,22 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||||
|
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||||
|
*/
|
||||||
|
import { getInjectable } from "@ogre-tools/injectable";
|
||||||
|
import onErrorInjectable from "./on-error.injectable";
|
||||||
|
import onProxyResInjectable from "./on-proxy-res.injectable";
|
||||||
|
import rawHttpProxyInjectable from "./raw-proxy.injectable";
|
||||||
|
|
||||||
|
const httpProxyInjectable = getInjectable({
|
||||||
|
id: "http-proxy",
|
||||||
|
instantiate: (di) => {
|
||||||
|
const proxy = di.inject(rawHttpProxyInjectable);
|
||||||
|
|
||||||
|
proxy.on("proxyRes", di.inject(onProxyResInjectable));
|
||||||
|
proxy.on("error", di.inject(onErrorInjectable));
|
||||||
|
|
||||||
|
return proxy;
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
export default httpProxyInjectable;
|
||||||
@ -0,0 +1,57 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||||
|
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||||
|
*/
|
||||||
|
import { getInjectable } from "@ogre-tools/injectable";
|
||||||
|
import type { ServerResponse } from "http";
|
||||||
|
import type { ProxyTargetUrl } from "http-proxy";
|
||||||
|
import { Socket } from "net";
|
||||||
|
import loggerInjectable from "../../../common/logger.injectable";
|
||||||
|
import handleRouteRequestInjectable from "../handle-route-request.injectable";
|
||||||
|
import { getRequestId } from "../helpers";
|
||||||
|
import type { ProxyIncomingMessage } from "../messages";
|
||||||
|
import proxyRetryInjectable from "./retry.injectable";
|
||||||
|
|
||||||
|
const onErrorInjectable = getInjectable({
|
||||||
|
id: "on-error",
|
||||||
|
instantiate: (di) => {
|
||||||
|
const proxyRetry = di.inject(proxyRetryInjectable);
|
||||||
|
const logger = di.inject(loggerInjectable);
|
||||||
|
const handleRouteRequest = di.inject(handleRouteRequestInjectable);
|
||||||
|
|
||||||
|
return (error: Error, req: ProxyIncomingMessage, res: ServerResponse | Socket, target?: ProxyTargetUrl) => {
|
||||||
|
if (proxyRetry.isClosed() || res instanceof Socket) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.error(`[LENS-PROXY]: http proxy errored for cluster: ${error}`, { url: req.url });
|
||||||
|
|
||||||
|
if (target) {
|
||||||
|
logger.debug(`Failed proxy to target: ${JSON.stringify(target, null, 2)}`);
|
||||||
|
|
||||||
|
if (req.method === "GET" && (!res.statusCode || res.statusCode >= 500)) {
|
||||||
|
const reqId = getRequestId(req);
|
||||||
|
const retryCount = proxyRetry.getCount(reqId);
|
||||||
|
const timeoutMs = retryCount * 250;
|
||||||
|
|
||||||
|
if (retryCount < 20) {
|
||||||
|
logger.debug(`Retrying proxy request to url: ${reqId}`);
|
||||||
|
setTimeout(() => {
|
||||||
|
proxyRetry.incrementCount(reqId);
|
||||||
|
handleRouteRequest(req, res)
|
||||||
|
.catch(error => logger.error(`[LENS-PROXY]: failed to handle request on proxy error: ${error}`));
|
||||||
|
}, timeoutMs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
res.writeHead(500).end(`Oops, something went wrong.\n${error}`);
|
||||||
|
} catch (e) {
|
||||||
|
logger.error(`[LENS-PROXY]: Failed to write headers: `, e);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
export default onErrorInjectable;
|
||||||
@ -0,0 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||||
|
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||||
|
*/
|
||||||
|
import { getInjectable } from "@ogre-tools/injectable";
|
||||||
|
import type { IncomingMessage, ServerResponse } from "http";
|
||||||
|
import { getRequestId } from "../helpers";
|
||||||
|
import proxyRetryInjectable from "./retry.injectable";
|
||||||
|
|
||||||
|
const onProxyResInjectable = getInjectable({
|
||||||
|
id: "on-proxy-res",
|
||||||
|
instantiate: (di) => {
|
||||||
|
const proxyRetry = di.inject(proxyRetryInjectable);
|
||||||
|
|
||||||
|
return (proxyRes: IncomingMessage, req: IncomingMessage, res: ServerResponse) => {
|
||||||
|
proxyRetry.clearCount(getRequestId(req));
|
||||||
|
|
||||||
|
proxyRes.on("aborted", () => { // happens when proxy target aborts connection
|
||||||
|
res.end();
|
||||||
|
});
|
||||||
|
};
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
export default onProxyResInjectable;
|
||||||
@ -0,0 +1,15 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||||
|
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||||
|
*/
|
||||||
|
import { getInjectable } from "@ogre-tools/injectable";
|
||||||
|
import { createProxy } from "http-proxy";
|
||||||
|
import type { ProxyIncomingMessage } from "../messages";
|
||||||
|
|
||||||
|
// NOTE: this is separate from httpProxyInjectable to fix circular dependency issues
|
||||||
|
const rawHttpProxyInjectable = getInjectable({
|
||||||
|
id: "raw-http-proxy",
|
||||||
|
instantiate: () => createProxy<ProxyIncomingMessage>(),
|
||||||
|
});
|
||||||
|
|
||||||
|
export default rawHttpProxyInjectable;
|
||||||
37
packages/core/src/main/lens-proxy/proxy/retry.injectable.ts
Normal file
37
packages/core/src/main/lens-proxy/proxy/retry.injectable.ts
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||||
|
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||||
|
*/
|
||||||
|
import { getInjectable } from "@ogre-tools/injectable";
|
||||||
|
|
||||||
|
export interface ProxyRetry {
|
||||||
|
clearCount: (id: string) => void;
|
||||||
|
getCount: (id: string) => number;
|
||||||
|
incrementCount: (id: string) => void;
|
||||||
|
isClosed: () => boolean;
|
||||||
|
close: () => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
const proxyRetryInjectable = getInjectable({
|
||||||
|
id: "proxy-retry",
|
||||||
|
instantiate: (): ProxyRetry => {
|
||||||
|
const counters = new Map<string, number>();
|
||||||
|
let closed = false;
|
||||||
|
|
||||||
|
return {
|
||||||
|
clearCount: (id: string) => {
|
||||||
|
counters.delete(id);
|
||||||
|
},
|
||||||
|
getCount: (id: string) => counters.get(id) ?? 0,
|
||||||
|
incrementCount: (id: string) => {
|
||||||
|
counters.set(id, (counters.get(id) ?? 0) + 1);
|
||||||
|
},
|
||||||
|
isClosed: () => closed,
|
||||||
|
close: () => {
|
||||||
|
closed = true;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
export default proxyRetryInjectable;
|
||||||
Loading…
Reference in New Issue
Block a user