Skip to content

Commit 0ee7097

Browse files
committed
Add support for proxies that require basic authentication
1 parent 30941df commit 0ee7097

File tree

9 files changed

+87
-5
lines changed

9 files changed

+87
-5
lines changed

ElectronNET.API/Entities/BrowserViewConstructorOptions.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,17 @@ public class BrowserViewConstructorOptions
99
/// See BrowserWindow.
1010
/// </summary>
1111
public WebPreferences WebPreferences { get; set; }
12+
13+
/// <summary>
14+
/// A proxy to set on creation in the format host:port.
15+
/// The proxy can be alternatively set using the BrowserView.WebContents.SetProxyAsync function.
16+
/// </summary>
17+
public string Proxy { get; set; }
18+
19+
/// <summary>
20+
/// The credentials of the Proxy in the format username:password.
21+
/// These will only be used if the Proxy field is also set.
22+
/// </summary>
23+
public string ProxyCredentials { get; set; }
1224
}
1325
}

ElectronNET.API/Entities/BrowserWindowOptions.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,5 +258,17 @@ public class BrowserWindowOptions
258258
/// Settings of web page's features.
259259
/// </summary>
260260
public WebPreferences WebPreferences { get; set; }
261+
262+
/// <summary>
263+
/// A proxy to set on creation in the format host:port.
264+
/// The proxy can be alternatively set using the BrowserWindow.WebContents.SetProxyAsync function.
265+
/// </summary>
266+
public string Proxy { get; set; }
267+
268+
/// <summary>
269+
/// The credentials of the Proxy in the format username:password.
270+
/// These will only be used if the Proxy field is also set.
271+
/// </summary>
272+
public string ProxyCredentials { get; set; }
261273
}
262274
}

ElectronNET.Host/api/browserView.js

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ElectronNET.Host/api/browserView.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ElectronNET.Host/api/browserView.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { BrowserView } from 'electron';
22
const browserViews: BrowserView[] = (global['browserViews'] = global['browserViews'] || []) as BrowserView[];
33
let browserView: BrowserView, electronSocket;
4+
const proxyToCredentialsMap: { [proxy: string]: string } = (global['proxyToCredentialsMap'] = global['proxyToCredentialsMap'] || []) as { [proxy: string]: string };
45

5-
const browserViewApi = (socket: SocketIO.Socket) => {
6+
const browserViewApi = (socket: SocketIO.Socket, app: Electron.App) => {
67
electronSocket = socket;
78

89
socket.on('createBrowserView', (options) => {
@@ -12,6 +13,15 @@ const browserViewApi = (socket: SocketIO.Socket) => {
1213

1314
browserView = new BrowserView(options);
1415
browserView['id'] = browserViews.length + 1;
16+
17+
if (options.proxy) {
18+
browserView.webContents.session.setProxy({proxyRules: options.proxy});
19+
}
20+
21+
if (options.proxy && options.proxyCredentials) {
22+
proxyToCredentialsMap[options.proxy] = options.proxyCredentials;
23+
}
24+
1525
browserViews.push(browserView);
1626

1727
electronSocket.emit('BrowserViewCreated', browserView['id']);

ElectronNET.Host/api/browserWindows.js

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ElectronNET.Host/api/browserWindows.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ElectronNET.Host/api/browserWindows.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,23 @@ const windows: Electron.BrowserWindow[] = (global['browserWindows'] = global['br
55
let readyToShowWindowsIds: number[] = [];
66
let window, lastOptions, electronSocket;
77
let mainWindowURL;
8+
const proxyToCredentialsMap: { [proxy: string]: string } = (global['proxyToCredentialsMap'] = global['proxyToCredentialsMap'] || []) as { [proxy: string]: string };
9+
810
export = (socket: SocketIO.Socket, app: Electron.App) => {
911
electronSocket = socket;
12+
13+
app.on('login', (event, webContents, request, authInfo, callback) => {
14+
if (authInfo.isProxy) {
15+
let proxy = `${authInfo.host}:${authInfo.port}`
16+
if (proxy in proxyToCredentialsMap && proxyToCredentialsMap[proxy].split(':').length === 2) {
17+
event.preventDefault()
18+
let user = proxyToCredentialsMap[proxy].split(':')[0]
19+
let pass = proxyToCredentialsMap[proxy].split(':')[1]
20+
callback(user, pass)
21+
}
22+
}
23+
})
24+
1025
socket.on('register-browserWindow-ready-to-show', (id) => {
1126
if (readyToShowWindowsIds.includes(id)) {
1227
readyToShowWindowsIds = readyToShowWindowsIds.filter(value => value !== id);
@@ -213,6 +228,14 @@ export = (socket: SocketIO.Socket, app: Electron.App) => {
213228
window = new BrowserWindow(options);
214229
}
215230

231+
if (options.proxy) {
232+
window.webContents.session.setProxy({proxyRules: options.proxy});
233+
}
234+
235+
if (options.proxy && options.proxyCredentials) {
236+
proxyToCredentialsMap[options.proxy] = options.proxyCredentials;
237+
}
238+
216239
window.on('ready-to-show', () => {
217240
if (readyToShowWindowsIds.includes(window.id)) {
218241
readyToShowWindowsIds = readyToShowWindowsIds.filter(value => value !== window.id);

ElectronNET.Host/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ function startSocketApiBridge(port) {
201201
if (shellApi === undefined) shellApi = require('./api/shell')(socket);
202202
if (screen === undefined) screen = require('./api/screen')(socket);
203203
if (clipboard === undefined) clipboard = require('./api/clipboard')(socket);
204-
if (browserView === undefined) browserView = require('./api/browserView').browserViewApi(socket);
204+
if (browserView === undefined) browserView = require('./api/browserView').browserViewApi(socket, app);
205205
if (powerMonitor === undefined) powerMonitor = require('./api/powerMonitor')(socket);
206206
if (nativeTheme === undefined) nativeTheme = require('./api/nativeTheme')(socket);
207207
if (dock === undefined) dock = require('./api/dock')(socket);

0 commit comments

Comments
 (0)