TooTallNate / proxy-agents

Node.js HTTP Proxy Agents Monorepo
https://proxy-agents.n8.io
871 stars 229 forks source link

Proxy wont connect #319

Open TutorialMan7727 opened 1 week ago

TutorialMan7727 commented 1 week ago

The following code wont connect to proxy. Proxy address is valid. (written in typescript)

import { Proxy, ProxyList } from "./proxyTypes"; import axios from "axios"; import { SocksProxyAgent } from "socks-proxy-agent";

const getProxys = async (): Promise => { const proxyUrl: string = "https://proxylist.geonode.com/api/proxy-list?limit=500&page=1&sort_by=lastChecked&sort_type=desc"; return (await axios.get(proxyUrl)).data; };

const getRandomProxy = (proxys: ProxyList): Proxy => { return proxys.data[Math.floor(Math.random() * proxys.data.length)]; };

const proxyToUri = (proxy: Proxy): string => { return ${proxy.protocols[0]}://${proxy.ip}:${proxy.port}; };

(async () => { const proxys = await getProxys(); const proxy = getRandomProxy(proxys); const uri = proxyToUri(proxy);

const httpsAgent = new SocksProxyAgent(uri); const client = axios.create({ httpsAgent });

client.get("https://google.com/").then((res) => { console.log(res.data); }); })();