mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
* wip: enable tls on lens-k8s-proxy Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * cleanup Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * type -> interface Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * more dependencies Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * refactor Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * run di.runSetups() after app is ready Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * tls fixes & refactor Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * cleanup Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * cleanup Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * refactor Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * refactor Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
66 lines
1.8 KiB
TypeScript
66 lines
1.8 KiB
TypeScript
/**
|
|
* Copyright (c) OpenLens Authors. All rights reserved.
|
|
* Licensed under MIT License. See LICENSE in root directory for more information.
|
|
*/
|
|
|
|
import { chunk } from "lodash";
|
|
import tls from "tls";
|
|
import url from "url";
|
|
import { apiKubePrefix } from "../../common/vars";
|
|
import type { ProxyApiRequestArgs } from "./types";
|
|
|
|
const skipRawHeaders = new Set(["Host", "Authorization"]);
|
|
|
|
export async function kubeApiUpgradeRequest({ req, socket, head, cluster }: ProxyApiRequestArgs) {
|
|
const proxyUrl = await cluster.contextHandler.resolveAuthProxyUrl() + req.url.replace(apiKubePrefix, "");
|
|
const proxyCa = await cluster.contextHandler.resolveAuthProxyCa();
|
|
const apiUrl = url.parse(cluster.apiUrl);
|
|
const pUrl = url.parse(proxyUrl);
|
|
const connectOpts = {
|
|
port: parseInt(pUrl.port),
|
|
host: pUrl.hostname,
|
|
ca: proxyCa,
|
|
};
|
|
|
|
const proxySocket = tls.connect(connectOpts, () => {
|
|
proxySocket.write(`${req.method} ${pUrl.path} HTTP/1.1\r\n`);
|
|
proxySocket.write(`Host: ${apiUrl.host}\r\n`);
|
|
|
|
for (const [key, value] of chunk(req.rawHeaders, 2)) {
|
|
if (skipRawHeaders.has(key)) {
|
|
continue;
|
|
}
|
|
|
|
proxySocket.write(`${key}: ${value}\r\n`);
|
|
}
|
|
|
|
proxySocket.write("\r\n");
|
|
proxySocket.write(head);
|
|
});
|
|
|
|
proxySocket.setKeepAlive(true);
|
|
socket.setKeepAlive(true);
|
|
proxySocket.setTimeout(0);
|
|
socket.setTimeout(0);
|
|
|
|
proxySocket.on("data", function (chunk) {
|
|
socket.write(chunk);
|
|
});
|
|
proxySocket.on("end", function () {
|
|
socket.end();
|
|
});
|
|
proxySocket.on("error", function () {
|
|
socket.write(`HTTP/${req.httpVersion} 500 Connection error\r\n\r\n`);
|
|
socket.end();
|
|
});
|
|
socket.on("data", function (chunk) {
|
|
proxySocket.write(chunk);
|
|
});
|
|
socket.on("end", function () {
|
|
proxySocket.end();
|
|
});
|
|
socket.on("error", function () {
|
|
proxySocket.end();
|
|
});
|
|
}
|