1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
This commit is contained in:
Jari Kolehmainen 2021-09-23 07:34:50 +03:00
parent f2528968e6
commit 683aed20a7
3 changed files with 18 additions and 5 deletions

View File

@ -48,7 +48,7 @@ if (typeof window === "undefined") {
});
} else {
apiBase = new JsonApi({
serverAddress: `http://localhost:${window.location.port}`,
serverAddress: `http://${window.location.host}`,
apiBase: apiPrefix,
debug: isDevelopment || isDebugging,
});

View File

@ -21,6 +21,7 @@
// Base http-service / json-api class
import { randomBytes } from "crypto";
import { merge } from "lodash";
import nodeFetch, { Response as NodeResponse, RequestInit as NodeRequestInit } from "node-fetch";
import { stringify } from "querystring";
@ -83,8 +84,10 @@ export class JsonApi<D = JsonApiData, P extends JsonApiParams = JsonApiParams> {
protected getRequestUrl(path: string, useSubdomain = false) {
if (window) {
const subdomain = useSubdomain ? `${randomBytes(2).toString("hex")}.` : "";
const url = new URL(this.config.serverAddress);
return `${this.config.apiBase}${path}`;
return `${url.protocol}//${subdomain}${url.host}${this.config.apiBase}${path}`;
} else {
return `${this.config.serverAddress}${this.config.apiBase}${path}`;
}

View File

@ -185,15 +185,25 @@ export class LensProxy extends Singleton {
}
protected async handleRequest(req: http.IncomingMessage, res: http.ServerResponse) {
// allow to fetch apis in "clusterId.localhost:port" from "localhost:port"
// this should be safe because we have already validated cluster uuid
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, PATCH, OPTIONS");
res.setHeader("Access-Control-Allow-Headers", "Content-Type, X-Cluster-Id");
if (req.method === "OPTIONS") {
res.end();
return;
}
const cluster = this.getClusterForRequest(req);
if (cluster) {
const proxyTarget = await this.getProxyTarget(req, cluster.contextHandler);
if (proxyTarget) {
// allow to fetch apis in "clusterId.localhost:port" from "localhost:port"
// this should be safe because we have already validated cluster uuid
res.setHeader("Access-Control-Allow-Origin", "*");
return this.proxy.web(req, res, proxyTarget);
}