-
Notifications
You must be signed in to change notification settings - Fork 266
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added the ability to specify a proxy in the config.json file #58
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e50281f
Added the ability to specify a proxy for electron to connect through …
TheraNinjaCat 4626697
Added proxy support from the electron-config.json file.
27c801f
Updated proxy support to apply to multiple sessions, and changed upda…
4836afe
Merge new proxy_config, and fast-foward branch
f9a37e6
Fixed linting, and correctly imported electron-updater in the updater…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
Copyright 2021 New Vector Ltd | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if you intended to assign copyright to us or not, but feel free to use your name instead. |
||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import { Session } from 'electron'; | ||
import fs from 'fs'; | ||
import type Store from 'electron-store'; | ||
|
||
export interface ProxyConfig { | ||
mode?: "direct" | "auto_detect" | "pac_script" | "fixed_servers" | "system"; | ||
pacScript?: string; | ||
proxyRules?: string; | ||
proxyBypassRules?: string; | ||
} | ||
|
||
type TypedStore = Store<{ proxy?: ProxyConfig }>; | ||
|
||
export class Proxy { | ||
public ready: Promise<any>; | ||
private static readonly STORE_KEY = "proxy"; | ||
|
||
private readonly store: TypedStore; | ||
private readonly sessions: Array<Session>; | ||
private proxy: ProxyConfig; | ||
private pacWatcher: fs.FSWatcher; | ||
|
||
constructor( { store, sessions = [] }: { store: TypedStore, sessions: Session[] }) { | ||
this.store = store; | ||
this.sessions = sessions; | ||
|
||
if (this.store.has(Proxy.STORE_KEY)) { | ||
console.log("Setting up proxy."); | ||
this.setProxy(this.store.get(Proxy.STORE_KEY)); | ||
} | ||
} | ||
|
||
public setProxy(proxy: ProxyConfig): void { | ||
this.proxy = proxy; | ||
this.store.set(Proxy.STORE_KEY, this.proxy); | ||
if (this.proxy.pacScript) { | ||
// Add custom handling for the file: URI handler as chromium does not support it | ||
// https://bugs.chromium.org/p/chromium/issues/detail?id=839566#c40 | ||
const pacURL = new URL(this.proxy.pacScript); | ||
if (pacURL.protocol === 'file:') { | ||
this.setProxyFromPACFile(pacURL.pathname); | ||
this.watchProxyPACFile(pacURL.pathname); | ||
} | ||
} | ||
} | ||
|
||
public async applyProxy(): Promise<any> { | ||
// Apply the proxy config to the sessions | ||
if (!this.proxy) return; | ||
|
||
return Promise.allSettled( | ||
this.sessions.map((session) => | ||
session.closeAllConnections() // Ensure all in-progress connections are closed | ||
.then(() => session.setProxy(this.proxy)) // Set the proxy settings | ||
.then(() => session.forceReloadProxyConfig()), // Ensure the updated config has been reloaded | ||
)); | ||
} | ||
|
||
private setProxyFromPACFile(pacFile: fs.PathLike): void { | ||
// Convert PAC file path into a base64 data: URI | ||
if (!this.proxy) return; | ||
|
||
const pacBuf = fs.readFileSync(pacFile); | ||
this.proxy.pacScript = `data:application/x-javascript-config;base64,${pacBuf.toString('base64')}`; | ||
} | ||
|
||
private watchProxyPACFile(pacFile: fs.PathLike): void { | ||
// Watch the PAC file for changes and reapply config if a change is detected | ||
if (this.pacWatcher) return; | ||
|
||
this.pacWatcher = fs.watch(pacFile, async (event) => { | ||
console.log("Started watching PAC file."); | ||
}); | ||
|
||
this.pacWatcher.on('change', (eventType: string) => { | ||
console.log("PAC file changed, updating proxy settings."); | ||
this.setProxyFromPACFile(pacFile); | ||
this.applyProxy(); | ||
}); | ||
|
||
this.pacWatcher.on('close', () => { | ||
console.log("Stopped watching PAC file."); | ||
}); | ||
} | ||
|
||
public close(): void { | ||
// Cleanup the fs watcher | ||
if (!this.pacWatcher) return; | ||
|
||
this.pacWatcher.close(); | ||
this.pacWatcher = null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we need to document somewhere that
electron-updater
is for proxy config, but I don't have any good ideas for where that could be recorded.