Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Proxy for Electron App

I am using of some of the npm modules which are making get request behind the scenes to pull some data from websites. But there is no option or setting to set proxy for those requests, so I want to know how to set proxy for entire electron app so that all the requests go through that proxy?

like image 470
Johne Doe Avatar asked Apr 12 '18 23:04

Johne Doe


People also ask

Do Electron apps need node?

Prerequisites​To use Electron, you need to install Node. js. We recommend that you use the latest LTS version available. Please install Node.

Can an Electron app run in browser?

As long as your main use of Electron is to create a 'native browser wrapper' for a web-app this is entirely possible. You'll probably have to step through your application and disable Electron specific functionality at multiple places.

Can Electron apps be fast?

Super Fast Turnaround Electron's number one strength is its turnaround speed. No other application development framework can go from 0 to fully functioning app as quickly as Electron can. Recently we were able to turnaround an app for a client in 2 weeks, because it was built on top of an existing React library.


1 Answers

Using request:

Use environment variables :

process.env.HTTP_PROXY = 'http://192.168.0.36:3128'

Using Axios:

Install this package :

npm install https-proxy-agent

Then :

const axios = require('axios');
const HttpsProxyAgent = require('https-proxy-agent');

let config = {}
config.httpsAgent = new HttpsProxyAgent('http://192.168.0.36:3128')
config.url = 'https://example.com'
config.method = 'GET'

axios(config).then(...).catch(...)

Electron app

For the wall app (like IMG SRC in HTML), you can use command line switches supported by Electron :

const { app } = require('electron')
app.commandLine.appendSwitch('proxy-server', '172.17.0.2:3128')
app.on('ready', () => {
  // Your code here
})

See documentation

like image 162
JeffProd Avatar answered Sep 20 '22 20:09

JeffProd