feat: respect system proxy settings on Windows platform

This commit is contained in:
Gold John King
2024-12-17 19:25:28 +08:00
parent 26699c1867
commit 46db881278
4 changed files with 106 additions and 35 deletions
+68
View File
@@ -0,0 +1,68 @@
import log from 'electron-log';
import Winreg from 'winreg';
import { bootstrap } from 'global-agent';
export function configureProxy() {
if (process.platform === "win32") {
configureWindowsProxy();
} else {
log.info('configureProxy: Unsupported platform');
}
}
async function configureWindowsProxy() {
if (await getWindowsProxy()){
bootstrap();
log.info(`Using system proxy: ${process.env.GLOBAL_AGENT_HTTP_PROXY}`);
}
}
function getWindowsProxy() {
return new Promise((resolve, reject) => {
const regKey = new Winreg({
hive: Winreg.HKCU,
key: '\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings'
});
regKey.get('ProxyEnable', (err, result) => {
if (err) {
log.error('Error accessing registry for ProxyEnable: ', err);
resolve(false);
}
// ProxyEnable = 1 means proxy is enabled, 0 means it's disabled
if (parseInt(result.value) !== 1) {
log.info('Proxy Enabled: false');
resolve(false);
} else {
log.info('Proxy Enabled: true');
regKey.get('ProxyServer', (err, result) => {
if (err) {
log.error('Error accessing registry for ProxyServer: ', err);
resolve(false);
}
const httpProxyUrl = `http://${result.value}`
process.env.GLOBAL_AGENT_HTTP_PROXY = httpProxyUrl;
process.env.GLOBAL_AGENT_HTTPS_PROXY = httpProxyUrl;
log.info('Proxy Server: ', httpProxyUrl);
regKey.get('ProxyOverride', (err, result) => {
if (err) {
log.error('Error accessing registry for ProxyOverride: ', err);
resolve(false);
}
process.env.GLOBAL_AGENT_NO_PROXY = result.value;
log.info('Proxy Override: ', result.value);
resolve(true);
});
});
}
});
});
}
+4 -1
View File
@@ -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;
@@ -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 }> {