Add support for proxies that require basic username/password authentication #561
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
See issue #545
Electron has a
login
event that allows automatic handling of basic username/password authentication. Because this event includes a callback function, it's difficult to forward it back to Electron.NET.So, this PR provides a workaround by adding optional
Proxy
andProxyCredentials
fields to theBrowserWindowOptions
andBrowserViewConstructorOptions
classes. If these fields are included,browserWindow.ts
andbrowserView.ts
make use of them in their handlers of thecreateBrowserWindow
andcreateBrowserView
events, respectively. These files share access to a globalproxyToCredentialsMap
which maps the proxy (host:port
) to its credentials (username:password
format). If only aProxy
is supplied, they call thewebContents.session.setProxy
method, providing an alternative to explicitly calling that function in Electron.NET. If both aProxy
and itsProxyCredentials
are supplied, they add an entry toproxyToCredentialsMap
. Supplying onlyProxyCredentials
has no effect.Finally,
browserWindow.ts
adds an event listener for theapp
'slogin
event. This listener uses theauthInfo
.isProxy
field to check if the type of basic authorization requested is for a proxy (i.e. the server returned a 407 status code) — there are cases in which basic authorization is used for restricting access to a site (example here) in which case a 401 status code is returned instead.The
authInfo
object also provides information about the host and port of the request (in the case of a 401 status code) or proxy (in the case of a 407 status code). Thislogin
handler checks if the proxy,${authInfo.port}:${authInfo.host}
, exists as a key in the globalproxyToCredentialsMap
. If it does, and its corresponding value contains only 1 semicolon (as it should, since credentials should be inusername:password
format), then it splits on the semicolon to retrieve the username and password, and submits the callback function which provides authentication to the proxy server.Example usage (in
Startup.cs
):In this example, I used separate proxies for the browserWindow and the browserView. Here is a screenshot of the result:
The window on the left is a normal Safari window that contains my actual IP. The window on the right is the Electron window — the top half is the BrowserWindow, and the bottom half contains its embedded BrowserView. Notice that each IP is different because of the proxies supplied to each window.