Skip to content

Commit

Permalink
read windows system proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
Last-Order committed Feb 16, 2024
1 parent fbd091a commit 1d23dac
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
11 changes: 11 additions & 0 deletions npm-shrinkwrap.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"erii": "^2.0.6",
"https-proxy-agent": "^7.0.2",
"socks-proxy-agent": "^8.0.2",
"winreg": "^1.2.5",
"ws": "^7.4.6"
},
"devDependencies": {
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ Erii.bind(
if (options.verbose) {
logger.enableDebugMode();
}
if (process.platform === "win32") {
await ProxyAgent.readWindowsSystemProxy();
}
ProxyAgent.readProxyConfigurationFromEnv();
const fileOptions = readConfigFile();
if (Object.keys(fileOptions).length > 0) {
Expand Down
48 changes: 47 additions & 1 deletion src/utils/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,23 @@ import { Agent } from "agent-base";
import { HttpsProxyAgent } from "https-proxy-agent";
import { SocksProxyAgent } from "socks-proxy-agent";
import logger from "./log";
import { default as Registry } from "winreg";

interface RegistryKeyItem {
name: string;
value: string;
}

const readRegistryKey = (key: any): Promise<RegistryKeyItem[]> => {
return new Promise((resolve, reject) => {
key.values((err, items) => {
if (err) {
reject(err);
}
resolve(items);
});
});
};

class InvalidProxyServerError extends Error {}

Expand Down Expand Up @@ -63,9 +80,38 @@ class ProxyAgentHelper {
/**
* Read proxy configuration from environment variables.
* By default, ALL_PROXY, HTTP_PROXY and HTTPS_PROXY will be used.
* Note: environment variables will override system proxy in Windows.
*/
readProxyConfigurationFromEnv() {
this.setProxy(process.env.ALL_PROXY || process.env.HTTP_PROXY || process.env.HTTPS_PROXY);
const proxySettings = process.env.ALL_PROXY || process.env.HTTP_PROXY || process.env.HTTPS_PROXY;
if (proxySettings) {
this.setProxy(process.env.ALL_PROXY || process.env.HTTP_PROXY || process.env.HTTPS_PROXY);
}
}

/**
* Read Windows system proxy from registry
*/
async readWindowsSystemProxy() {
if (process.platform !== "win32") {
// not a windows environment
return;
}
const key = new Registry({
hive: Registry.HKCU,
key: "\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
});
try {
const items = await readRegistryKey(key);
const proxyEnableItem = items.find((item) => item.name === "ProxyEnable");
const proxyServerItem = items.find((item) => item.name === "ProxyServer");
const isProxyEnable = proxyEnableItem.value === "0x1";
if (isProxyEnable && proxyServerItem && proxyServerItem.value !== "") {
this.setProxy(proxyServerItem.value);
}
} catch {
// ignore
}
}
}

Expand Down

0 comments on commit 1d23dac

Please sign in to comment.