From 6fa3cbf5b0a20e082ddb70e335264187200d0b43 Mon Sep 17 00:00:00 2001 From: Vladyslav Zhukovskyi Date: Mon, 29 Apr 2024 12:58:18 +0300 Subject: [PATCH] chore: use proper proxy configuration Signed-off-by: Vladyslav Zhukovskyi --- .../extensions-catalog.spec.ts | 45 +++++++++++++++++-- .../extensions-catalog/extensions-catalog.ts | 20 ++------- 2 files changed, 44 insertions(+), 21 deletions(-) diff --git a/packages/main/src/plugin/extensions-catalog/extensions-catalog.spec.ts b/packages/main/src/plugin/extensions-catalog/extensions-catalog.spec.ts index 7945d453e03..1fc6606d51d 100644 --- a/packages/main/src/plugin/extensions-catalog/extensions-catalog.spec.ts +++ b/packages/main/src/plugin/extensions-catalog/extensions-catalog.spec.ts @@ -113,6 +113,7 @@ const proxy: Proxy = { onDidStateChange: vi.fn(), onDidUpdateProxy: onDidUpdateProxyEmitter.event, isEnabled: isEnabledProxyMock, + proxy: vi.fn(), } as unknown as Proxy; const configurationRegistry: ConfigurationRegistry = { @@ -181,18 +182,28 @@ test('check getHttpOptions with Proxy', async () => { getAllCertificatesMock.mockReturnValue(['1', '2', '3']); isEnabledProxyMock.mockReturnValue(true); - extensionsCatalog = new ExtensionsCatalog(certificates, proxy, configurationRegistry); - onDidUpdateProxyEmitter.fire({ - httpProxy: 'http://proxy:8080', - httpsProxy: 'http://proxy:8080', + const proxy: Proxy = { + onDidStateChange: vi.fn(), + onDidUpdateProxy: vi.fn(), + isEnabled: isEnabledProxyMock, + proxy: vi.fn(), + } as unknown as Proxy; + vi.spyOn(proxy, 'proxy', 'get').mockReturnValue({ + httpProxy: 'http://localhost', + httpsProxy: 'http://localhost', noProxy: 'localhost', }); + extensionsCatalog = new ExtensionsCatalog(certificates, proxy, configurationRegistry); const options = extensionsCatalog.getHttpOptions(); expect(options).toBeDefined(); // expect the two agents being defined expect(options.agent?.http).toBeDefined(); expect(options.agent?.https).toBeDefined(); + // @ts-expect-error proxy property exists on http object + expect(options.agent?.http?.proxy.href).toBe('http://localhost/'); + // @ts-expect-error proxy property exists on https object + expect(options.agent?.https?.proxy.href).toBe('http://localhost/'); // certificates should be 1, 2, 3 expect(options.https?.certificateAuthority).toBeDefined(); @@ -292,3 +303,29 @@ test('should fetch alternate link', async () => { // no error expect(console.error).not.toBeCalled(); }); + +test('Should use proxy object if proxySettings is undefined', () => { + isEnabledProxyMock.mockReturnValue(true); + const proxy: Proxy = { + onDidStateChange: vi.fn(), + onDidUpdateProxy: vi.fn(), + isEnabled: isEnabledProxyMock, + proxy: vi.fn(), + } as unknown as Proxy; + vi.spyOn(proxy, 'proxy', 'get').mockReturnValue({ + httpProxy: 'http://localhost', + httpsProxy: 'https://localhost', + noProxy: 'localhost', + }); + extensionsCatalog = new ExtensionsCatalog(certificates, proxy, configurationRegistry); + const options = extensionsCatalog.getHttpOptions(); + + expect(options).toBeDefined(); + // expect the two agents being defined + expect(options.agent?.http).toBeDefined(); + expect(options.agent?.https).toBeDefined(); + // @ts-expect-error proxy property exists on http object + expect(options.agent?.http?.proxy.href).toBe('http://localhost/'); + // @ts-expect-error proxy property exists on https object + expect(options.agent?.https?.proxy.href).toBe('https://localhost/'); +}); diff --git a/packages/main/src/plugin/extensions-catalog/extensions-catalog.ts b/packages/main/src/plugin/extensions-catalog/extensions-catalog.ts index b26a54f0e70..d8d1f6f2705 100644 --- a/packages/main/src/plugin/extensions-catalog/extensions-catalog.ts +++ b/packages/main/src/plugin/extensions-catalog/extensions-catalog.ts @@ -16,7 +16,6 @@ * SPDX-License-Identifier: Apache-2.0 ***********************************************************************/ -import type * as podmanDesktopAPI from '@podman-desktop/api'; import type { HttpsOptions, OptionsOfTextResponseBody } from 'got'; import got from 'got'; import { HttpProxyAgent, HttpsProxyAgent } from 'hpagent'; @@ -37,9 +36,6 @@ import { ExtensionsCatalogSettings } from './extensions-catalog-settings.js'; export class ExtensionsCatalog { public static readonly DEFAULT_EXTENSIONS_URL = 'https://registry.podman-desktop.io/api/extensions.json'; - private proxySettings: podmanDesktopAPI.ProxySettings | undefined; - private proxyEnabled: boolean; - private lastFetchTime = 0; private cachedCatalog: InternalCatalogJSON | undefined; static readonly CACHE_TIMEOUT = 1000 * 60 * 60 * 4; // 4 hours @@ -48,17 +44,7 @@ export class ExtensionsCatalog { private certificates: Certificates, private proxy: Proxy, private configurationRegistry: ConfigurationRegistry, - ) { - this.proxy.onDidUpdateProxy(settings => { - this.proxySettings = settings; - }); - - this.proxy.onDidStateChange(state => { - this.proxyEnabled = state; - }); - - this.proxyEnabled = this.proxy.isEnabled(); - } + ) {} init(): void { // register a configuration @@ -190,9 +176,9 @@ export class ExtensionsCatalog { options.https.certificateAuthority = this.certificates.getAllCertificates(); } - if (this.proxyEnabled) { + if (this.proxy.isEnabled()) { // use proxy when performing got request - const proxy = this.proxySettings; + const proxy = this.proxy.proxy; const httpProxyUrl = proxy?.httpProxy; const httpsProxyUrl = proxy?.httpsProxy;