gpt4 book ai didi

node.js - 如何在 Node JS 中获取当前的系统代理?

转载 作者:行者123 更新时间:2023-12-03 12:12:54 31 4
gpt4 key购买 nike

我正在制作一个 Node 应用程序,并且已经知道如何在需要时实现代理,我不确定我实际上如何检查当前系统代理设置。

从我读到它应该在 process.env.http_proxy 中,但在我的 Windows 代理设置中设置代理后未定义。

如何在 NodeJS 中获取当前的代理设置?

最佳答案

您可以使用 get-proxy-settings来自 NPM 的包。
它能够:

Retrieving the settings from the internet settings on Windows in the registry


我刚刚在 Windows 10 上对其进行了测试,它能够获取我的代理设置。
或者,您可以查看他们的 source并自行完成。以下是一些关键功能:
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";
}
}

关于node.js - 如何在 Node JS 中获取当前的系统代理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55109704/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com