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();
相关推荐
止水编程 water_proof10 分钟前
Java--HTTP(上)
网络·网络协议·http
止水编程 water_proof8 小时前
Java-HTTP响应以及HTTPS(下)
网络·网络协议·http
好望角雾眠8 小时前
第四阶段C#通讯开发-9:网络协议Modbus下的TCP与UDP
网络·笔记·网络协议·tcp/ip·c#·modbus
C2H5OH6669 小时前
WebSocket-练习1
网络·websocket·网络协议
狂奔的sherry9 小时前
Socket vs WebSocket
网络·websocket·网络协议
2501_9151063211 小时前
App HTTPS 抓包 工程化排查与工具组合实战
网络协议·ios·小程序·https·uni-app·php·iphone
0和1的舞者13 小时前
网络通信的奥秘:HTTP详解 (七)
服务器·网络·网络协议·http·okhttp·软件工程·1024程序员节
节点小宝13 小时前
节点小宝免费版流量机制解析:点对点直连技术与备用流量设计
网络·网络协议·p2p
huangdengji15 小时前
基于openresty反向代理、dns劫持、实现对http请求、响应内容抓包
网络协议·http·openresty
00后程序员张16 小时前
混淆 iOS 类名与变量名的实战指南,多工具组合把混淆做成工程能力(混淆 iOS 类名变量名/IPA 成品混淆Ipa/Guard CLI 实操)
android·ios·小程序·https·uni-app·iphone·webview