Skip to content

feat: respect system proxy settings on Windows platform #704

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

Merged
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions assets/jsons/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,18 @@
"error-notification": {
"message": "An error occured, unable to change symlinks settings."
}
},
"use-system-proxy": {
"title": "Use system proxy",
"description": "BSMansger will make network requests through system proxy.",
"modal": {
"title": "Restart Needed",
"body": "Turning off system proxy will quit and re-launch BSManager. Are you sure you want to do this?",
"confirm-btn": "Yes I'm sure"
},
"error-notification": {
"message": "An error occured, unable to use system proxy."
}
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions assets/jsons/translations/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,18 @@
"error-notification": {
"message": "发生错误,无法更改符号链接设置。"
}
},
"use-system-proxy": {
"title": "使用系统代理",
"description": "BSMansger将通过系统代理发起网络请求。",
"modal": {
"title": "需要重启",
"body": "关闭系统代理需要退出并重新启动BSManager。您确定要这样做吗?",
"confirm-btn": "是的,我确定"
},
"error-notification": {
"message": "发生错误,无法启用系统代理。"
}
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"@types/color": "^3.0.3",
"@types/crypto-js": "^4.2.1",
"@types/dateformat": "^5.0.0",
"@types/global-agent": "^2.1.3",
"@types/got": "^9.6.12",
"@types/jest": "^29.5.11",
"@types/node": "22.8.6",
Expand Down Expand Up @@ -170,6 +171,7 @@
"format-duration": "^3.0.2",
"framer-motion": "^11.2.6",
"fs-extra": "^11.2.0",
"global-agent": "^3.0.0",
"got": "^14.4.4",
"history": "^5.3.0",
"is-elevated": "^4.0.0",
Expand Down
75 changes: 75 additions & 0 deletions src/main/helpers/proxy.helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import log from 'electron-log';
import { RegDwordValue, RegSzValue } from "regedit-rs"
import { execOnOs } from "../helpers/env.helpers";
import { bootstrap } from 'global-agent';
import { StaticConfigurationService } from "../services/static-configuration.service";

const staticConfig = StaticConfigurationService.getInstance();

const { list } = (execOnOs({ win32: () => require("regedit-rs") }, true) ?? {}) as typeof import("regedit-rs");

async function isProxyEnabled(): Promise<boolean>{
const res = await list("HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings");
const key = res["HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"];
if(!key.exists){ throw new Error("Key \"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\" not exist"); }
const registryValue = key.values.ProxyEnable as RegDwordValue;
if(!registryValue){ throw new Error("Value \"ProxyEnable\" not exist"); }
return (1 === registryValue.value);
}

async function getProxyServer(): Promise<string>{
const res = await list("HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings");
const key = res["HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"];
if(!key.exists){ throw new Error("Key \"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\" not exist"); }
const registryValue = key.values.ProxyServer as RegSzValue;
if(!registryValue){ throw new Error("Value \"ProxyServer\" not exist"); }
return registryValue.value;
}

async function getProxyOverride(): Promise<string>{
const res = await list("HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings");
const key = res["HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"];
if(!key.exists){ throw new Error("Key \"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\" not exist"); }
const registryValue = key.values.ProxyOverride as RegSzValue;
if(!registryValue){ throw new Error("Value \"ProxyOverride\" not exist"); }
return registryValue.value;
}

async function configureWindowsProxy() : Promise<void> {
if (await isProxyEnabled().catch(err => log.error(err))) {
const httpProxyUrl = `http://${await getProxyServer().catch(err => log.error(err))}`
process.env.GLOBAL_AGENT_HTTP_PROXY = httpProxyUrl;
process.env.GLOBAL_AGENT_HTTPS_PROXY = httpProxyUrl;

process.env.GLOBAL_AGENT_NO_PROXY = `${await getProxyOverride().catch(err => log.error(err))}`;

log.info(`configureWindowsProxy: Using system proxy: ${process.env.GLOBAL_AGENT_HTTP_PROXY}`);

bootstrap();
}
else {
log.info(`configureWindowsProxy: System proxy not detected`);
}
}

export function configureProxy() {
if (process.platform === "win32") {
const useSystemProxy = staticConfig.get("use-system-proxy");

if (useSystemProxy === true) {
configureWindowsProxy();
}

log.info(`configureProxy: UseSystemProxy is set to ${useSystemProxy}`);

staticConfig.$watch("use-system-proxy").subscribe((useSystemProxy) => {
if (useSystemProxy === true) {
configureWindowsProxy();
}

log.info(`configureProxy: UseSystemProxy is set to ${useSystemProxy}`);
});
} else {
log.info('configureProxy: Unsupported platform');
}
}
5 changes: 4 additions & 1 deletion src/main/services/request.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import path from 'path';
import { pipeline } from 'stream/promises';
import sanitize from 'sanitize-filename';
import internal from 'stream';
import { configureProxy } from 'main/helpers/proxy.helpers';

export class RequestService {
private static instance: RequestService;
Expand All @@ -23,7 +24,9 @@ export class RequestService {
return RequestService.instance;
}

private constructor() {}
private constructor() {
configureProxy();
}

public async getJSON<T = unknown>(url: string): Promise<{ data: T; headers: IncomingHttpHeaders }> {

Expand Down
1 change: 1 addition & 0 deletions src/main/services/static-configuration.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export interface StaticConfigKeyValues {
"song-details-cache-etag": string;
"disable-hadware-acceleration": boolean;
"use-symlinks": boolean;
"use-system-proxy": boolean;

// Linux Specific static configs
"proton-folder": string;
Expand Down
47 changes: 47 additions & 0 deletions src/renderer/pages/settings-page.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -541,10 +541,12 @@ function AdvancedSettings() {

const [hardwareAccelerationEnabled, setHardwareAccelerationEnabled] = useState(true);
const [useSymlink, setUseSymlink] = useState(false);
const [useSystemProxy, setUseSystemProxy] = useState(false);

useEffect(() => {
staticConfig.get("disable-hadware-acceleration").then(disabled =>setHardwareAccelerationEnabled(() => disabled !== true));
staticConfig.get("use-symlinks").then(useSymlinks => setUseSymlink(() => useSymlinks));
staticConfig.get("use-system-proxy").then(useSystemProxy => setUseSystemProxy(() => useSystemProxy));
}, []);

const onChangeHardwareAcceleration = async (newHardwareAccelerationEnabled: boolean) => {
Expand Down Expand Up @@ -609,6 +611,45 @@ function AdvancedSettings() {
setUseSymlink(() => newUseSymlink);
}

const onChangeUseSystemProxy = async (newUseSystemProxy: boolean) => {

if (window.electron.platform !== "win32" || newUseSystemProxy === useSystemProxy) {
return;
}

if(!newUseSystemProxy){
const res = await modal.openModal(BasicModal, { data: {
title: "pages.settings.advanced.use-system-proxy.modal.title",
body: "pages.settings.advanced.use-system-proxy.modal.body",
image: BeatConflict,
buttons: [
{ id: "cancel", text: "misc.cancel", type: "cancel" },
{ id: "confirm", text: "pages.settings.advanced.use-system-proxy.modal.confirm-btn", type: "error", onClick: () => true }
]
}});

if(res.exitCode !== ModalExitCode.COMPLETED || res.data !== "confirm"){ return; }
}

const { error } = await tryit(() => staticConfig.set("use-system-proxy", newUseSystemProxy));

if(error){
notification.notifyError({ title: "notifications.types.error", desc: "pages.settings.advanced.use-system-proxy.error-notification.message" });
return;
}

setUseSystemProxy(() => newUseSystemProxy);

if(!progressBar.require()){
return;
}

if (!newUseSystemProxy) {
await lastValueFrom(ipc.sendV2("restart-app"));
}

}

const advancedItems: Item[] = [{
checked: hardwareAccelerationEnabled,
text: t.text("pages.settings.advanced.hardware-acceleration.title"),
Expand All @@ -622,6 +663,12 @@ function AdvancedSettings() {
desc: t.text("pages.settings.advanced.use-symlinks.description"),
onChange: onChangeUseSymlinks
});
advancedItems.push({
checked: useSystemProxy,
text: t.text("pages.settings.advanced.use-system-proxy.title"),
desc: t.text("pages.settings.advanced.use-system-proxy.description"),
onChange: onChangeUseSystemProxy
});
}

return <SettingContainer title="pages.settings.advanced.title" description="pages.settings.advanced.description">
Expand Down