מובנה להביא ו unici#
הצומת העולמית fetch הוא unici. תן לו ProxyAgent בקשה עם dispatcher אפשרות, או להגדיר אותו בעולם פעם אחת עם 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 בריכות, כך בקשות רצופות עשויות להשתמש בחיבור אחד ולכן יציאה אחת. עבור יציאה טריה לכל בקשה ליצור את הסוכן עם pipelining: 0 ו connections: 1, או פשוט להשתמש שונה -sid- לכל מפגש הגיוני.
אקסיומות#
של אקסיוס proxy אפשרות אינה מנטרת HTTPS באופן אמין; הגישה הסטנדרטית היא סוכן מ https-proxy-agent ו 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);גרביים5#
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());משתנים סביבתיים#
אונדיק לא קורא HTTPS_PROXY כברירת מחדל. גרסת Unici האחרונה ספינה EnvHttpProxyAgent, אשר עושה; להגדיר אותו כשולח הגלובלי ואת כל התהליך, כולל ספריות צד שלישי בנויות על להביא, עובר את הפרוקסי.
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());מטבע ורוטציה#
- אחת
ProxyAgentלכל מפגש דביק (שונה)-sid-), משותף על ידי כל הבקשות של אותו מפגש. - עבור רוטציה רחב, לשמור על סוכן אחד אבל להשבית חיבור חוזר, או להפעיל סוכני N עם זיהויי הפעלה N ו-Robin עגול אותם.
- → צפייה
UND_ERR_SOCKETוECONNRESET: יציאת מגורים נעלמה; נסה פעם אחת, החיבור הבא בוחר אחר.
שאלות שאנשים שואלים#
מדוע אקסיוס מתעלם מה-proxy שלי עבור HTTPS?
תמיכת הפרוקסי המובנית שלה מוגבלת למנהרות חיבור. השתמש ב- https-proxy-agent כ-httpsAgent והגדר פרוקסי: שקר כדי שהאקסיוס לא ינסה להתמודד עם זה בעצמו.
האם אני יכול לשלוח רק כמה בקשות?
כן. להעביר את השולח או הסוכן על הבקשות הזקוקות לו ולהשאיר את האחרים ללא מגע; שום דבר אינו גלובלי אלא אם כן אתה קורא setGlobalDispatcher.