Skip to content

Commit

Permalink
Close proxy server on app quit (lensapp#413)
Browse files Browse the repository at this point in the history
Signed-off-by: Lauri Nevala <lauri.nevala@gmail.com>
  • Loading branch information
nevalla authored Jun 10, 2020
1 parent e8fcff0 commit 78eeef2
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 17 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@
"electron-window-state": "^5.0.3",
"filenamify": "^4.1.0",
"handlebars": "4.1.2",
"http-proxy": "^1.17.0",
"http-proxy": "^1.18.0",
"http-proxy-middleware": "^0.19.2",
"https-proxy-agent": "^3.0.1",
"jsonwebtoken": "^8.5.1",
"mac-ca": "^1.0.4",
Expand Down
5 changes: 3 additions & 2 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const promiseIpc = new PromiseIpc({ timeout: 2000 })

let windowManager: WindowManager = null;
let clusterManager: ClusterManager = null;
let proxyServer: proxy.LensProxy = null;
const vmURL = (isDevelopment) ? `http://localhost:${process.env.ELECTRON_WEBPACK_WDS_PORT}` : formatUrl({
pathname: path.join(__dirname, "index.html"),
protocol: "file",
Expand Down Expand Up @@ -57,10 +58,9 @@ async function main() {

// create cluster manager
clusterManager = new ClusterManager(clusterStore.getAllClusterObjects(), port)

// run proxy
try {
proxy.listen(port, clusterManager)
proxyServer = proxy.listen(port, clusterManager)
} catch (error) {
logger.error(`Could not start proxy (127.0.0:${port}): ${error.message}`)
await dialog.showErrorBox("Lens Error", `Could not start proxy (127.0.0:${port}): ${error.message || "unknown error"}`)
Expand Down Expand Up @@ -123,5 +123,6 @@ app.on("activate", () => {
app.on("will-quit", async (event) => {
event.preventDefault(); // To allow mixpanel sending to be executed
if (clusterManager) clusterManager.stop()
if (proxyServer) proxyServer.close()
app.exit(0);
})
12 changes: 12 additions & 0 deletions src/main/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export class LensProxy {
protected clusterManager: ClusterManager
protected retryCounters: Map<string, number> = new Map()
protected router: Router
protected proxyServer: http.Server
protected closed = false

constructor(port: number, clusterManager: ClusterManager) {
this.port = port
Expand All @@ -27,6 +29,13 @@ export class LensProxy {
public run() {
const proxyServer = this.buildProxyServer();
proxyServer.listen(this.port, "127.0.0.1")
this.proxyServer = proxyServer
}

public close() {
logger.info("Closing proxy server")
this.proxyServer.close()
this.closed = true
}

protected buildProxyServer() {
Expand Down Expand Up @@ -70,6 +79,9 @@ export class LensProxy {
}
})
proxy.on("error", (error, req, res, target) => {
if(this.closed) {
return
}
if (target) {
logger.debug("Failed proxy to target: " + JSON.stringify(target))
if (req.method === "GET" && (!res.statusCode || res.statusCode >= 500)) {
Expand Down
28 changes: 14 additions & 14 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4660,10 +4660,10 @@ event-emitter@~0.3.5:
d "1"
es5-ext "~0.10.14"

eventemitter3@^3.0.0:
version "3.1.2"
resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-3.1.2.tgz#2d3d48f9c346698fce83a85d7d664e98535df6e7"
integrity sha512-tvtQIeLVHjDkJYnzf2dgVMxfuSGJeM/7UCG17TT4EumTfNtF+0nebF/4zWOIkCreAbtNqhGEboB6BWrwqNaw4Q==
eventemitter3@^4.0.0:
version "4.0.4"
resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-4.0.4.tgz#b5463ace635a083d018bdc7c917b4c5f10a85384"
integrity sha512-rlaVLnVxtxvoyLsQQFBx53YmXHDxRIzzTLbdfxqi4yocpSjAxXwkU0cScM5JgSKMqEhrZpnvQ2D9gjylR0AimQ==

events@^3.0.0:
version "3.0.0"
Expand Down Expand Up @@ -5852,22 +5852,22 @@ http-errors@~1.7.2:
resolved "https://registry.yarnpkg.com/http-parser-js/-/http-parser-js-0.4.10.tgz#92c9c1374c35085f75db359ec56cc257cbb93fa4"
integrity sha1-ksnBN0w1CF912zWexWzCV8u5P6Q=

http-proxy-middleware@^0.19.1:
version "0.19.1"
resolved "https://registry.yarnpkg.com/http-proxy-middleware/-/http-proxy-middleware-0.19.1.tgz#183c7dc4aa1479150306498c210cdaf96080a43a"
integrity sha512-yHYTgWMQO8VvwNS22eLLloAkvungsKdKTLO8AJlftYIKNfJr3GK3zK0ZCfzDDGUBttdGc8xFy1mCitvNKQtC3Q==
http-proxy-middleware@^0.19.1, http-proxy-middleware@^0.19.2:
version "0.19.2"
resolved "https://registry.yarnpkg.com/http-proxy-middleware/-/http-proxy-middleware-0.19.2.tgz#ee73dcc8348165afefe8de2ff717751d181608ee"
integrity sha512-aYk1rTKqLTus23X3L96LGNCGNgWpG4cG0XoZIT1GUPhhulEHX/QalnO6Vbo+WmKWi4AL2IidjuC0wZtbpg0yhQ==
dependencies:
http-proxy "^1.17.0"
http-proxy "^1.18.1"
is-glob "^4.0.0"
lodash "^4.17.11"
micromatch "^3.1.10"

http-proxy@^1.17.0:
version "1.17.0"
resolved "https://registry.yarnpkg.com/http-proxy/-/http-proxy-1.17.0.tgz#7ad38494658f84605e2f6db4436df410f4e5be9a"
integrity sha512-Taqn+3nNvYRfJ3bGvKfBSRwy1v6eePlm3oc/aWVxZp57DQr5Eq3xhKJi7Z4hZpS8PC3H4qI+Yly5EmFacGuA/g==
http-proxy@^1.18.0, 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 "^3.0.0"
eventemitter3 "^4.0.0"
follow-redirects "^1.0.0"
requires-port "^1.0.0"

Expand Down

0 comments on commit 78eeef2

Please sign in to comment.