NodeJS:利用 Axios 实现 HTTP、HTTPS 和 SOCKS5 代理请求

在日常开发中,网络请求是不可避免的。通过使用代理服务器,可以更好地控制请求的来源、隐藏 IP 地址,或者绕过网络限制。在本篇文章中,我将分享如何使用 axios 库结合 HTTP、HTTPS 和 SOCKS5 代理来发送网络请求,并详细介绍实现过程。

准备工作

首先,我们需要确保项目中安装了必要的依赖,包括 axioshttp-proxy-agenthttps-proxy-agentsocks-proxy-agent。可以使用以下命令进行安装:

bash 复制代码
npm install axios http-proxy-agent https-proxy-agent socks-proxy-agent

安装完成后,就可以开始构建代码了。

代码实现

1. 导入必要的模块

我们首先导入 axios 库及相应的代理模块:

js 复制代码
import axios from "axios";
import { HttpProxyAgent } from "http-proxy-agent";
import { HttpsProxyAgent } from "https-proxy-agent";
import { SocksProxyAgent } from "socks-proxy-agent";

这里我们使用 axios 作为 HTTP 客户端,用它来发送网络请求;而 http-proxy-agenthttps-proxy-agentsocks-proxy-agent 用于处理不同类型的代理协议。

2. 配置 HTTP 代理

我们通过 HttpProxyAgent 配置 HTTP 代理:

js 复制代码
const http = () => {
  const httpAgent = new HttpProxyAgent("http://127.0.0.1:7899");
  httpAxios.defaults.httpAgent = httpAgent;
  httpAxios.defaults.proxy = true;

  httpAxios.get("http://ipinfo.io", {}).then((res) => {
    console.log(res.data);
  });
};
  • HttpProxyAgent("<http://127.0.0.1:7899>"):定义 HTTP 代理的地址。这里我们指定代理服务器位于本地的 7899 端口。
  • httpAxios.defaults.httpAgent:设置 axios 请求的 httpAgent,用于处理代理请求。
  • httpAxios.defaults.proxy = true:启用代理模式。
  • httpAxios.get("<http://ipinfo.io>"):发送 HTTP 请求,并输出响应内容。

3. 配置 HTTPS 代理

对于 HTTPS 请求,我们可以使用 HttpsProxyAgent

js 复制代码
const https = () => {
  const httpsAgent = new HttpsProxyAgent("http://127.0.0.1:7899");
  httpsAxios.defaults.httpsAgent = httpsAgent;
  httpsAxios.defaults.proxy = false;

  httpsAxios.get("https://ipinfo.io", {}).then((res) => {
    console.log(res.data);
  });
};
  • HttpsProxyAgent("<http://127.0.0.1:7899>"):这里我们依然使用同一个代理地址。
  • httpsAxios.defaults.httpsAgent:为 HTTPS 请求设置 httpsAgent
  • httpsAxios.defaults.proxy = false:禁用内置的 axios 代理,因为我们手动定义了代理。

4. 配置 SOCKS5 代理

为了支持 SOCKS5 代理,我们需要使用 SocksProxyAgent

js 复制代码
const socks5 = () => {
  const socks5Agent = new SocksProxyAgent("socks5://127.0.0.1:7898");
  socks5Axios.defaults.httpsAgent = socks5Agent;
  socks5Axios.defaults.proxy = false;

  socks5Axios.get("https://ipinfo.io").then((res) => {
    console.log(res.data);
  }).catch((err) => {
    console.log(err.message);
  });
};
  • SocksProxyAgent("socks5://127.0.0.1:7898"):定义 SOCKS5 代理地址。我们使用本地的 7898 端口。
  • socks5Axios.defaults.httpsAgent:将 socks5Agent 作为 axioshttpsAgent
  • socks5Axios.defaults.proxy = false:禁用 axios 自带的代理功能。

5. 执行代码

最后,我们将所有代理请求依次执行:

js 复制代码
http();
https();
socks5();

完整代码

以下是完整的代码实现:

js 复制代码
import axios from "axios";
import { HttpProxyAgent } from "http-proxy-agent";
import { HttpsProxyAgent } from "https-proxy-agent";
import { SocksProxyAgent } from "socks-proxy-agent";

let httpAxios = axios;
let httpsAxios = axios;
let socks5Axios = axios;

const http = () => {
  const httpAgent = new HttpProxyAgent("http://127.0.0.1:7899");
  httpAxios.defaults.httpAgent = httpAgent;
  httpAxios.defaults.proxy = true;

  httpAxios.get("http://ipinfo.io", {}).then((res) => {
    console.log(res.data);
  });
};

const https = () => {
  const httpsAgent = new HttpsProxyAgent("http://127.0.0.1:7899");
  httpsAxios.defaults.httpsAgent = httpsAgent;
  httpsAxios.defaults.proxy = false;

  httpsAxios.get("https://ipinfo.io", {}).then((res) => {
    console.log(res.data);
  });
};

const socks5 = () => {
  const socks5Agent = new SocksProxyAgent("socks5://127.0.0.1:7898");
  socks5Axios.defaults.httpsAgent = socks5Agent;
  socks5Axios.defaults.proxy = false;

  socks5Axios.get("https://ipinfo.io").then((res) => {
    console.log(res.data);
  }).catch((err) => {
    console.log(err.message);
  });
};

http();
https();
socks5();
相关推荐
猫头虎16 分钟前
极简远程革命:节点小宝 — 无公网IP的极速内网穿透远程解决方案
网络·网络协议·tcp/ip·ai编程·远程工作·内网·穿透
Waitccy1 小时前
HTTP 与 HTTPS 的深度剖析:差异、原理与应用场景
网络协议·http·https·php
ん贤2 小时前
GoWeb开发
开发语言·后端·tcp/ip·http·https·go·goweb
GOATLong2 小时前
Socket 编程 TCP
linux·服务器·开发语言·网络·c++·网络协议·tcp/ip
你好我是小美2 小时前
深入理解 HTTPS:从 TCP 到 HTTP 再到加密传输的完整流程
tcp/ip·http·https
Mr-Apple14 小时前
Decode rpc invocation failed: null -> DecodeableRpcInvocation
网络·网络协议·rpc
IpdataCloud14 小时前
IP 风险画像如何实现对恶意 IP 的有效拦截?
网络·网络协议·tcp/ip
JAVA学习通15 小时前
【JavaEE】 HTTP协议(1.0)
网络·网络协议·http
2501_9160137416 小时前
日常开发中,iOS 性能调优我们怎么做?
websocket·网络协议·tcp/ip·http·网络安全·https·udp
努力也学不会java16 小时前
【HTTP】《HTTP 全原理解析:从请求到响应的奇妙之旅》
java·网络·网络协议·http