diff --git a/extensions/telemetry/src/tracker.ts b/extensions/telemetry/src/tracker.ts index 4c41c70b19..982ae146c4 100644 --- a/extensions/telemetry/src/tracker.ts +++ b/extensions/telemetry/src/tracker.ts @@ -9,7 +9,7 @@ import { comparer } from "mobx"; export class Tracker extends Util.Singleton { static readonly GA_ID = "UA-159377374-1"; static readonly SEGMENT_KEY = "YENwswyhlOgz8P7EFKUtIZ2MfON7Yxqb"; - protected eventHandlers: Array<(ev: EventBus.AppEvent) => void> = []; + protected eventHandlers: Array<(ev: EventBus.AppEvent ) => void> = []; protected started = false; protected visitor: ua.Visitor; protected analytics: Analytics; @@ -124,28 +124,30 @@ export class Tracker extends Util.Singleton { } protected resolveOS() { + let os = ""; if (App.isMac) { - return "MacOS"; + os = "MacOS"; + } else if(App.isWindows) { + os = "Windows"; + } else if (App.isLinux) { + os = "Linux"; + if (App.isSnap) { + os += "; Snap"; + } else { + os += "; AppImage"; + } + } else { + os = "Unknown"; } - - if (App.isWindows) { - return "Windows"; - } - - if (App.isLinux) { - return `Linux; ${App.isSnap ? "Snap" : "AppImage"}`; - } - - return "Unknown"; + return os; } protected async event(eventCategory: string, eventAction: string, otherParams = {}) { - const allowed = await this.isTelemetryAllowed(); - if (!allowed) { - return; - } - try { + const allowed = await this.isTelemetryAllowed(); + if (!allowed) { + return; + } this.visitor.event({ ec: eventCategory, ea: eventAction, diff --git a/src/main/lens-proxy.ts b/src/main/lens-proxy.ts index ac9e9f1d48..ba1451529a 100644 --- a/src/main/lens-proxy.ts +++ b/src/main/lens-proxy.ts @@ -10,7 +10,6 @@ import { Router } from "./router"; import { ClusterManager } from "./cluster-manager"; import { ContextHandler } from "./context-handler"; import logger from "./logger"; -import _ from "lodash"; export class LensProxy { protected origin: string; @@ -65,55 +64,50 @@ export class LensProxy { protected async handleProxyUpgrade(proxy: httpProxy, req: http.IncomingMessage, socket: net.Socket, head: Buffer) { const cluster = this.clusterManager.getClusterForRequest(req); - if (!cluster) { - return; - } - - const proxyUrl = await cluster.contextHandler.resolveAuthProxyUrl() + req.url.replace(apiKubePrefix, ""); - const apiUrl = url.parse(cluster.apiUrl); - const pUrl = url.parse(proxyUrl); - const connectOpts = { port: parseInt(pUrl.port), host: pUrl.hostname }; - const proxySocket = new net.Socket(); - proxySocket.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 (["Host", "Authorization"].includes(key)) { - continue; + if (cluster) { + const proxyUrl = await cluster.contextHandler.resolveAuthProxyUrl() + req.url.replace(apiKubePrefix, ""); + const apiUrl = url.parse(cluster.apiUrl); + const pUrl = url.parse(proxyUrl); + const connectOpts = { port: parseInt(pUrl.port), host: pUrl.hostname }; + const proxySocket = new net.Socket(); + proxySocket.connect(connectOpts, () => { + proxySocket.write(`${req.method} ${pUrl.path} HTTP/1.1\r\n`); + proxySocket.write(`Host: ${apiUrl.host}\r\n`); + for (let i = 0; i < req.rawHeaders.length; i += 2) { + const key = req.rawHeaders[i]; + if (key !== "Host" && key !== "Authorization") { + proxySocket.write(`${req.rawHeaders[i]}: ${req.rawHeaders[i+1]}\r\n`); + } } + proxySocket.write("\r\n"); + proxySocket.write(head); + }); - proxySocket.write(`${key}: ${value}\r\n`); - } + proxySocket.setKeepAlive(true); + socket.setKeepAlive(true); + proxySocket.setTimeout(0); + socket.setTimeout(0); - 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 (err) { - 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(); - }); + proxySocket.on('data', function (chunk) { + socket.write(chunk); + }); + proxySocket.on('end', function () { + socket.end(); + }); + proxySocket.on('error', function (err) { + 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(); + }); + } } protected createProxy(): httpProxy { diff --git a/src/renderer/api/json-api.ts b/src/renderer/api/json-api.ts index 3477a13036..8d30f08a57 100644 --- a/src/renderer/api/json-api.ts +++ b/src/renderer/api/json-api.ts @@ -2,7 +2,7 @@ import { stringify } from "querystring"; import { EventEmitter } from "../../common/event-emitter"; -import { cancelableFetch, CancelablePromise } from "../utils/cancelableFetch"; +import { cancelableFetch } from "../utils/cancelableFetch"; export interface JsonApiData { } @@ -71,7 +71,7 @@ export class JsonApi { return this.request(path, params, { ...reqInit, method: "delete" }); } - protected request(path: string, params?: P, init: RequestInit = {}): CancelablePromise { + protected request(path: string, params?: P, init: RequestInit = {}) { let reqUrl = this.config.apiBase + path; const reqInit: RequestInit = { ...this.reqInit, ...init }; const { data, query } = params || {} as P; @@ -92,27 +92,28 @@ export class JsonApi { }); } - protected async parseResponse(res: Response, log: JsonApiLog): Promise { + protected parseResponse(res: Response, log: JsonApiLog): Promise { const { status } = res; - const text = await res.text(); - let data; - try { - data = text ? JSON.parse(text) : ""; // DELETE-requests might not have response-body - } catch (e) { - data = text; - } - if (status >= 200 && status < 300) { - this.onData.emit(data, res); - this.writeLog({ ...log, data }); - return data; - } else if (log.method === "GET" && res.status === 403) { - this.writeLog({ ...log, data }); - } else { - const error = new JsonApiErrorParsed(data, this.parseError(data, res)); - this.onError.emit(error, res); - this.writeLog({ ...log, error }); - throw error; - } + return res.text().then(text => { + let data; + try { + data = text ? JSON.parse(text) : ""; // DELETE-requests might not have response-body + } catch (e) { + data = text; + } + if (status >= 200 && status < 300) { + this.onData.emit(data, res); + this.writeLog({ ...log, data }); + return data; + } else if (log.method === "GET" && res.status === 403) { + this.writeLog({ ...log, data }); + } else { + const error = new JsonApiErrorParsed(data, this.parseError(data, res)); + this.onError.emit(error, res); + this.writeLog({ ...log, error }); + throw error; + } + }); } protected parseError(error: JsonApiError | string, res: Response): string[] {