TooTallNate / proxy-agents

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

Unable to proxy DNS requests when using a socks proxy agent #324

Closed ruochenjia closed 2 months ago

ruochenjia commented 2 months ago

Socks5 protocol supports DNS requests as well as regular HTTP requests, but the SocksProxyAgent implementation does not handle these requests properly.

I've tried to create a TOR proxy agent using SocksProxyAgent:

import https from "https";
import { SocksProxyAgent } from "socks-proxy-agent";

function sendHttpRequest(url, body, method, headers, signal) {
    const outgoing = https.request(url, {
        protocol: "https:",
        host: url.host,
        port: url.port,
        path: url.href.slice(url.origin.length),
        agent: agent,
        signal: signal,
        method: method,
        headers: headers,
        setHost: true,
        maxHeaderSize: 32768
    });

    if (body != null)
        body.pipe(outgoing, { end: true });
    else
        outgoing.end();

    return new Promise((resolve, reject) => {
        outgoing.on("response", (res) => resolve(res));
        outgoing.on("error", (err) => reject(err));
    });
}

const agent = new SocksProxyAgent("socks5://127.0.0.1:9050");

try {
    const res = await sendHttpRequest(
        new URL("http://s4k4ceiapwwgcm3mkb6e4diqecpo7kvdnfr5gg7sph7jjppqkvwwqtyd.onion/"),
        null, "GET", {}, void 0 );

    console.log(res.statusCode);
} catch (err) {
    console.error(err);
}

and I've got the following error message:

Error: getaddrinfo ENOTFOUND s4k4ceiapwwgcm3mkb6e4diqecpo7kvdnfr5gg7sph7jjppqkvwwqtyd.onion
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:107:26) {
  errno: -3008,
  code: 'ENOTFOUND',
  syscall: 'getaddrinfo',
  hostname: 's4k4ceiapwwgcm3mkb6e4diqecpo7kvdnfr5gg7sph7jjppqkvwwqtyd.onion'
}

Note: I've tested the same socks5 address using Firefox's built-in proxy features, and everything works fine.

TooTallNate commented 2 months ago

Try with socks5h protocol