feat: proxy rules with `--proxy-rules` flag (#854)

See https://electronjs.org/docs/api/session?q=proxy#sessetproxyconfig-callback
This commit is contained in:
otato.z 2019-10-23 07:38:39 +08:00 committed by Ronan Jouchet
parent 84f06e3e79
commit ef13ff1e1d
5 changed files with 40 additions and 0 deletions

View File

@ -75,6 +75,15 @@ function clearCache(browserWindow, targetUrl = null) {
});
}
function setProxyRules(browserWindow, proxyRules) {
browserWindow.webContents.session.setProxy(
{
proxyRules,
},
() => {},
);
}
/**
*
* @param {{}} inpOptions AppArgs from nativefier.json
@ -284,6 +293,11 @@ function createMainWindow(inpOptions, onAppQuit, setDockBadge) {
if (options.userAgent) {
window.webContents.setUserAgent(options.userAgent);
}
if (options.proxyRules) {
setProxyRules(window, options.proxyRules);
}
maybeInjectCss(window);
sendParamsOnDidFinishLoad(window);
window.webContents.on('new-window', onNewWindow);
@ -318,6 +332,10 @@ function createMainWindow(inpOptions, onAppQuit, setDockBadge) {
mainWindow.webContents.setUserAgent(options.userAgent);
}
if (options.proxyRules) {
setProxyRules(mainWindow, options.proxyRules);
}
maybeInjectCss(mainWindow);
sendParamsOnDidFinishLoad(mainWindow);

View File

@ -43,6 +43,7 @@
- [[enable-es3-apis]](#enable-es3-apis)
- [[insecure]](#insecure)
- [[internal-urls]](#internal-urls)
- [[proxy-rules]](#proxy-rules)
- [[flash]](#flash)
- [[flash-path]](#flash-path)
- [[disk-cache-size]](#disk-cache-size)
@ -378,6 +379,21 @@ Or, if you want to allow all domains for example for external auths,
nativefier https://google.com --internal-urls ".*?"
```
#### [proxy-rules]
```
--proxy-rules <value>
```
Proxy rules. See [proxyRules](https://electronjs.org/docs/api/session?q=proxy#sessetproxyconfig-callback) for more details.
Example:
```bash
nativefier https://google.com --proxy-rules http://127.0.0.1:1080
```
#### [flash]
```

View File

@ -42,6 +42,7 @@ function selectAppArgs(options) {
disableDevTools: options.disableDevTools,
zoom: options.zoom,
internalUrls: options.internalUrls,
proxyRules: options.proxyRules,
crashReporter: options.crashReporter,
singleInstance: options.singleInstance,
clearCache: options.clearCache,

View File

@ -200,6 +200,10 @@ if (require.main === module) {
'--internal-urls <value>',
'regular expression of URLs to consider "internal"; all other URLs will be opened in an external browser. (default: URLs on same second-level domain as app)',
)
.option(
'--proxy-rules <value>',
'proxy rules. See https://electronjs.org/docs/api/session?q=proxy#sessetproxyconfig-callback',
)
.option(
'--crash-reporter <value>',
'remote server URL to send crash reports',

View File

@ -58,6 +58,7 @@ export default function(inpOptions) {
tmpdir: false,
zoom: inpOptions.zoom || 1.0,
internalUrls: inpOptions.internalUrls || null,
proxyRules: inpOptions.proxyRules || null,
singleInstance: inpOptions.singleInstance || false,
clearCache: inpOptions.clearCache || false,
appVersion: inpOptions.appVersion,