I am making a node application and already know how i can implement a Proxy if required, but i'm not sure how i actually check the current system proxy settings.
From what i read its supposed to be in process.env.http_proxy
but that is undefined after setting a proxy in my windows proxy settings.
How does one get the current Proxy settings in NodeJS?
You can use get-proxy-settings package from NPM.
It is capable of:
Retrieving the settings from the internet settings on Windows in the registry
I just tested it on Windows 10 and it was able to get my proxy settings.
Alternatively, you can take a look at their source and do this on your own. Here are some key functions:
async function getProxyWindows(): Promise<ProxySettings> {
// HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings
const values = await openKey(Hive.HKCU, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings");
const proxy = values["ProxyServer"];
const enable = values["ProxyEnable"];
const enableValue = Number(enable && enable.value);
if (enableValue > 0 && proxy) {
return parseWindowsProxySetting(proxy.value);
} else {
return null;
}
}
function parseWindowsProxySetting(proxySetting: string): ProxySettings {
if (!proxySetting) { return null; }
if (isValidUrl(proxySetting)) {
const setting = new ProxySetting(proxySetting);
return {
http: setting,
https: setting,
};
}
const settings = proxySetting.split(";").map(x => x.split("=", 2));
const result = {};
for (const [key, value] of settings) {
if (value) {
result[key] = new ProxySetting(value);
}
}
return processResults(result);
}
async function openKey(hive: string, key: string): Promise<RegKeyValues> {
const keyPath = `${hive}\\${key}`;
const { stdout } = await execAsync(`${getRegPath()} query "${keyPath}"`);
const values = parseOutput(stdout);
return values;
}
function getRegPath() {
if (process.platform === "win32" && process.env.windir) {
return path.join(process.env.windir as string, "system32", "reg.exe");
} else {
return "REG";
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With