Node.js Advanced Tunnel Proxy: undici / superagent / https
Node.js undici, superagent, and native https module integrating 16Yun Crawler Proxy.
16Yun Engineering TeamApr 23, 20261 min read
undici
undici 是 Node.js 官方的新一代 HTTP 客户端,通过 ProxyAgent 支持代理:
import { request, ProxyAgent } from 'undici';
const proxyUrl = `http://${user}:${pass}@${host}:${port}`;
// 场景 A:强制切换 IP
for (let i = 1; i <= 3; i++) {
const proxyAgent = new ProxyAgent(proxyUrl);
const resp = await request('https://httpbin.org/ip', {
dispatcher: proxyAgent,
headers: { 'Connection': 'close' },
});
const data = await resp.body.json();
console.log(`请求 ${i}: ${data.origin}`);
proxyAgent.close();
}
// 场景 B:保持 IP
const proxyAgent = new ProxyAgent(proxyUrl);
for (let i = 1; i <= 3; i++) {
const resp = await request('https://httpbin.org/ip', {
dispatcher: proxyAgent,
headers: { 'Connection': 'keep-alive' },
});
const data = await resp.body.json();
console.log(`请求 ${i}: ${data.origin}`);
}
proxyAgent.close();
undici 的 ProxyAgent 支持 headers 选项在 CONNECT 阶段注入自定义头(HTTPS Tunnel 需验证):
const proxyAgent = new ProxyAgent({
uri: proxyUrl,
headers: { 'Proxy-Tunnel': 'undici-demo' },
});
superagent
superagent 通过 .agent() 方法挂载 https-proxy-agent:
import superagent from 'superagent';
import { HttpsProxyAgent } from 'https-proxy-agent';
const proxyUrl = `http://${user}:${pass}@${host}:${port}`;
// HTTPS Tunnel 通过 proxyOpts.headers
const agent = new HttpsProxyAgent(proxyUrl, {
headers: { 'Proxy-Tunnel': 'superagent-demo' },
});
const resp = await superagent
.get('https://httpbin.org/ip')
.agent(agent)
.timeout({ response: 15000 });
console.log(resp.text);
https 原生模块(HTTPS CONNECT 头注入)
Node.js 原生 https 模块配合 https-proxy-agent 可以精细控制 CONNECT 阶段:
import { HttpsProxyAgent } from 'https-proxy-agent';
const agent = new HttpsProxyAgent(proxyUrl, {
headers: { 'Proxy-Tunnel': 'https-demo' },
});
const req = https.request({
hostname: 'httpbin.org',
path: '/ip',
agent,
method: 'GET',
}, (res) => {
let data = '';
res.on('data', chunk => data += chunk);
res.on('end', () => console.log(data));
});
req.end();
三种方案对比
| 特性 | undici | superagent | https + agent |
|---|---|---|---|
| 性能 | 最高 | 中 | 中 |
| API 风格 | 标准回调/async | 链式 .then() | 事件驱动 |
| HTTPS Tunnel | ⚠️ 需验证 | ✅ | ✅ |
| HTTP/2 | ✅ | ❌ | ❌ |
| 适用场景 | 高性能后端 | 通用请求 | 底层控制 |
Need an enterprise proxy plan?
We can tailor architecture to your target domains, concurrency, and reliability goals.