Eingebaute Fetch und Undici#
Node's global fetch Undici ist. Geben Sie es a ProxyAgent per Anfrage mit dem dispatcher Option, oder legen Sie es global einmal mit setGlobalDispatcher.
import { fetch, ProxyAgent, setGlobalDispatcher } from "undici";
const proxy = new ProxyAgent("http://USER-cc-us:PASS@res.proxy-cyber.com:9000");
// Per request
const res = await fetch("https://api.ipify.org", { dispatcher: proxy });
console.log(await res.text());
// Or globally, for every fetch in the process
setGlobalDispatcher(proxy);A ProxyAgent bündelt Verbindungen, sodass aufeinanderfolgende Anfragen möglicherweise eine Verbindung und damit einen Ausgang wiederverwenden. Für einen neuen Exit pro Anfrage erstellen Sie den Agenten mit pipelining: 0 und connections: 1oder einfach eine andere -sid- pro logischer Sitzung.
Axios#
axios-eigenes proxy Option tunnelt HTTPS nicht zuverlässig; Der Standardansatz ist ein Agent von https-proxy-agent und proxy: false.
import axios from "axios";
import { HttpsProxyAgent } from "https-proxy-agent";
const agent = new HttpsProxyAgent("http://USER-cc-de-city-berlin-sid-cart42-ttl-15m:PASS@res.proxy-cyber.com:9000");
const client = axios.create({ httpsAgent: agent, httpAgent: agent, proxy: false, timeout: 30000 });
const { data } = await client.get("https://api.ipify.org");
console.log(data);:#
import got from "got";
import { HttpsProxyAgent } from "https-proxy-agent";
const agent = new HttpsProxyAgent("http://USER-cc-us:PASS@res.proxy-cyber.com:9000");
const body = await got("https://api.ipify.org", { agent: { https: agent, http: agent }, timeout: { request: 30000 } }).text();
console.log(body);SOCKS5#
import fetch from "node-fetch";
import { SocksProxyAgent } from "socks-proxy-agent";
const agent = new SocksProxyAgent(process.env.PROXY_URL);
const response = await fetch("https://api.ipify.org", { agent });
console.log(await response.text());Umweltvariablen#
Undici liest nicht HTTPS_PROXY Standardmäßig. Neuere Versionen von Undici EnvHttpProxyAgent, was tut; Legen Sie es als globalen Dispatcher fest und der gesamte Prozess, einschließlich der auf Fetch basierenden Bibliotheken von Drittanbietern, läuft über den Proxy.
import { EnvHttpProxyAgent, setGlobalDispatcher } from "undici";
// HTTPS_PROXY=http://USER-cc-us:PASS@res.proxy-cyber.com:9000 NO_PROXY=localhost node app.js
setGlobalDispatcher(new EnvHttpProxyAgent());Wechselkurs und Rotation#
- 1
ProxyAgentpro Sticky Session (anders)-sid-), geteilt durch alle Anfragen dieser Sitzung. - Behalten Sie für eine breite Rotation einen Agenten bei, deaktivieren Sie jedoch die Wiederverwendung von Verbindungen, oder führen Sie N Agenten mit N Sitzungs-IDs aus und führen Sie diese im Round-Robin-Verfahren aus.
- Uhrzeit
UND_ERR_SOCKETundECONNRESET: ein Wohnausgang ging weg; Versuchen Sie es einmal erneut, die nächste Verbindung wählt eine andere.
Fragen, die Menschen stellen#
Warum ignoriert axios meinen Proxy für HTTPS?
Die integrierte Proxy-Unterstützung ist für CONNECT-Tunnel begrenzt. Verwenden Sie https-proxy-agent als httpsAgent und legen Sie Proxy: false fest, damit Axios nicht versucht, es selbst zu handhaben.
Kann ich nur einige Anfragen proxyn?
Ja. Leiten Sie die Anfragen, die sie benötigen, an den Disponenten oder Agenten weiter und lassen Sie die anderen unberührt; Nichts ist global, es sei denn, Sie rufen setGlobalDispatcher auf.