我需要在 NodeJS 服务器的几个部分使用 VPN,但仅限于从一个 API 获取数据时。我的服务器中的其他一切都应该正常工作。我正在使用 WireGuard,我可以使用 OpenVPN,但我更喜欢 WireGuard,因为使用 OpenVPN 时速度要慢得多。我不确定这是否可行以及如何做到。
我做了一些研究,最终在 NodeJS 中编写了下面的内容,但它不起作用。我对网络不太了解,所以我不明白一切。我注意到的是,当我在 PowerShell 中运行命令“Get-NetIPInterface”并且 WireGuard 与我的 VPN 正常连接时,我可以在那里看到它。但是当我断开与 WireGuard 的连接时,它就消失了,我不确定这是否与它有关,但这就是我能想到的所有可能有帮助的信息,因为我有点无能为力。
import http from "http";
import axios from "axios";
const interfaceWg = "---";
const agent = new http.Agent({
localAddress: interfaceWg,
});
const axiosWg = axios.create({
httpAgent: agent,
});
const url = "---";
// Make an HTTP request using the axios instance with the WireGuard agent
axiosWg
.get(url)
.then((response) => {
console.log("Response:", response.data);
})
.catch((error) => {
console.error("Error:", error);
});
据我所知,它最终只是执行一个正常的获取请求,当我在显示我的 IP 的网站上执行获取请求时,我得到的是我的 IP,而不是我想要的来自 VPN 的 IP。
使用wireproxy将wireguard公开为socks5/http客户端,然后使用axios(我更喜欢axios)和httpproxyagent通过wireguard获取