Skip to content
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

feat: respect system proxy settings on Windows platform #704

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
64 changes: 30 additions & 34 deletions package-lock.json

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

4 changes: 4 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 All @@ -95,6 +96,7 @@
"@types/use-double-click": "^1.0.4",
"@types/webpack-bundle-analyzer": "^4.4.2",
"@types/webpack-env": "^1.18.0",
"@types/winreg": "^1.2.36",
"@typescript-eslint/eslint-plugin": "^6.20.0",
"@typescript-eslint/parser": "^6.20.0",
"autoprefixer": "^10.4.17",
Expand Down Expand Up @@ -170,6 +172,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 Expand Up @@ -201,6 +204,7 @@
"to-ico": "^1.1.5",
"use-double-click": "^1.0.5",
"use-fit-text": "^2.4.0",
"winreg": "^1.2.5",
"yauzl": "^3.2.0"
},
"engines": {
Expand Down
68 changes: 68 additions & 0 deletions src/main/helpers/proxy.helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import log from 'electron-log';
GoldJohnKing marked this conversation as resolved.
Show resolved Hide resolved
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);
});
});
}
});
});
}
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