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();
相关推荐
骷大人1 小时前
Thinkphp6实现websocket
网络·websocket·网络协议
中云时代-防御可测试-小余1 小时前
怎么选择合适的高防IP
服务器·网络·网络协议·tcp/ip·阿里云·udp·ddos
lichuangcsdn1 小时前
springboot集成websocket给前端推送消息
前端·websocket·网络协议
ladymorgana3 小时前
【OSS】 前端如何直接上传到OSS 上返回https链接,如果做到OSS图片资源加密访问
前端·网络协议·https
白棂4 小时前
面试题——计算机网络:HTTP和HTTPS的区别?
计算机网络·http·https
CodeWithMe4 小时前
【Net】TCP粘包与半包
网络·网络协议·tcp/ip
m0_555762905 小时前
使用 Let‘s Encrypt 和 Certbot 为 Cloudflare 托管的域名申请 SSL 证书
网络·网络协议·ssl
半青年5 小时前
IEC61850规约客户端软件开发实战(第二章)
java·c++·qt·网络协议·c#·信息与通信·iec61850
TA远方5 小时前
【C#】一个简单的http服务器项目开发过程详解
服务器·http·c#·wpf·web·winform·console
巴巴_羊6 小时前
前端面经 websocket
http