1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Move all port handling into LensProxy

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2021-12-13 10:58:24 -05:00
parent 6c9a53f9f4
commit 40fa387b5e
2 changed files with 42 additions and 39 deletions

View File

@ -28,7 +28,7 @@ import * as LensExtensionsCommonApi from "../extensions/common-api";
import * as LensExtensionsMainApi from "../extensions/main-api";
import { app, autoUpdater, dialog, powerMonitor } from "electron";
import { appName, isIntegrationTesting, isMac, isWindows, productName } from "../common/vars";
import { LensProxy, UnsafePortError } from "./lens-proxy";
import { LensProxy } from "./lens-proxy";
import { WindowManager } from "./window-manager";
import { ClusterManager } from "./cluster-manager";
import { shellSync } from "./shell-sync";
@ -184,39 +184,13 @@ app.on("ready", async () => {
initializers.initClusterMetadataDetectors();
logger.info("🔌 Starting LensProxy");
try {
logger.info("🔌 Starting LensProxy");
await lensProxy.listen();
} catch (error) {
dialog.showErrorBox("Lens Error", `Could not start proxy: ${error?.message || "unknown error"}`);
for (let attempt = 0;; attempt += 1) {
try {
await lensProxy.listen();
break;
} catch (error) {
if (error instanceof UnsafePortError) {
lensProxy.close();
if (attempt < 16) {
continue;
}
const result = await dialog.showMessageBox({
title: "Lens Error",
message: "Tried to start several times but only got ports that chromium considers unsafe. Would you like to continue trying?",
buttons: ["No", "Yes"],
cancelId: 0,
defaultId: 1,
type: "error",
});
if (result.response === 1) {
attempt = 0;
continue;
}
}
dialog.showErrorBox("Lens Error", `Could not start proxy: ${error?.message || "unknown error"}`);
return app.exit();
}
return app.exit();
}
// test proxy connection

View File

@ -68,7 +68,9 @@ const disallowedPorts = new Set([
10080,
]);
export class UnsafePortError {}
class UnsafePortError {
constructor(public port: number) {}
}
export class LensProxy extends Singleton {
protected origin: string;
@ -111,11 +113,11 @@ export class LensProxy extends Singleton {
}
/**
* Starts the lens proxy.
* @resolves After the server is listening
* @rejects if there is an error before that happens
* Starts to listen on an OS provided port. Will reject if the server throws
* an error or if the port picked by the OS is one which chrome rejects
* connections to.
*/
listen(): Promise<void> {
private attemptToListen(): Promise<void> {
return new Promise<void>((resolve, reject) => {
this.proxyServer.listen(0, "127.0.0.1");
@ -128,7 +130,7 @@ export class LensProxy extends Singleton {
if (disallowedPorts.has(port)) {
logger.warn(`[LENS-PROXY]: Proxy server has with port known to be considered unsafe to connect to by chrome, restarting...`);
return reject(new UnsafePortError());
return reject(new UnsafePortError(port));
}
logger.info(`[LENS-PROXY]: Proxy server has started at ${address}:${port}`);
@ -148,6 +150,33 @@ export class LensProxy extends Singleton {
});
}
/**
* Starts the lens proxy.
* @resolves After the server is listening on a good port
* @rejects if there is an error before that happens
*/
async listen(): Promise<void> {
const seenPorts = new Set<number>();
for(;;) {
try {
this.proxyServer?.close();
await this.attemptToListen();
break;
} catch (error) {
if (error instanceof UnsafePortError) {
if (seenPorts.has(error.port)) {
throw new Error("Failed to start LensProxy due to seeing too many unsafe ports. Please restart Lens.");
}
continue;
}
throw error;
}
}
}
close() {
logger.info("[LENS-PROXY]: Closing server");
this.proxyServer.close();