Wednesday, 16 June 2021

Electron Proxy Setup for app level intercepts

I am trying to set up a proxy server to proxy every request in my electron app but I cant seem to get the requests to go through the proxy. I followed the docs and I think this is right but I cant tell if its working or not (I dont see anything in logs)

(async () => {
    const getPort = require('get-port');
    const { setupProxyServer, stopProxyServer } = require('./proxy');
    const instance = 'http://localhost:8080';

    const PROXY_PORT = await getPort();
    await setupProxyServer(instance, PROXY_PORT);

    // Modules to control application life and create native browser window
    const { app, BrowserWindow, session } = require('electron')
    
    app.commandLine.appendSwitch('proxy-server', PROXY_PORT);

    const createWindow = async () => {
        // Create the browser window.
        const mainWindow = new BrowserWindow({
            width: 1800,
            height: 1200
        })
        
        // Open the DevTools.
        if (process.env.DEVTOOLS) mainWindow.webContents.openDevTools();

        // and load the index.html of the app.
        mainWindow.loadURL(instance);
    };

    // This method will be called when Electron has finished
    // initialization and is ready to create browser windows.
    // Some APIs can only be used after this event occurs.
    app.whenReady().then(async () => {
        // session.fromPartition('persist:glide');

        createWindow();

        app.on('activate', () => {
            // On macOS it's common to re-create a window in the app when the
            // dock icon is clicked and there are no other windows open.
            if (BrowserWindow.getAllWindows().length === 0) createWindow()
        });
    });

    // Quit when all windows are closed, except on macOS. There, it's common
    // for applications and their menu bar to stay active until the user quits
    // explicitly with Cmd + Q.
    app.on('window-all-closed', async () => {
        if (process.platform !== 'darwin') app.quit()

        await stopProxyServer();
    });
})();

the middleware set up is fine I can see in the logs that the middleware server is started and awaiting requests.



from Electron Proxy Setup for app level intercepts

No comments:

Post a Comment