Search code examples
node.jsaxiossocks

How to download file using axios and socks proxy agent


So I'm going to download a file using axios and socks proxy agent in nodejs. Of course, downloading is successfully carried out when only using axios. But if the socks proxy agent is added into axios initialization, it doesn't.

Below is the code of download function.

async function downloadFile(fileUrl, outputLocationPath, proxy_agent) {
  const writer = fs.createWriteStream(outputLocationPath);

  const client = axios.create({
    baseURL: fileUrl,
    responseType: 'stream',
    httpsAgent: proxy_agent,
    httpAgent: proxy_agent
  });

  const response = await client.get();

  response.data.pipe(writer);

  return new Promise((resolve, reject) => {
    writer.on('finish', resolve);
    writer.on('error', reject);
  });
}

When I use fileURL just as json object link such as "https://myip.wtf/json", it fetches proxy server's ip address really and saves it into a file. But when I specify other url, socket hang error displays. I can't understand why this happens. JSON request workes well, but not in case of ordinary file request. Please help me. Thanks.


Solution

  • I can't see why this fixes the issue, but anyway I have fixed the problem by replacing "socks5" with "socks" in initializing proxy options.

    So what i used first is below;

    /* initialize proxy configuration */
    const proxy_host = "127.0.0.1", proxy_port = 1080;
    const proxy_options = `socks5://${proxy_host}:${proxy_port}`;
    const proxy_agent = new SocksProxyAgent(proxy_options);
    

    This used to cause connection timeout error, but when i change socks5 to socks, it worked.

    There is distinct difference between original socks(socks4) and modern socks5, it must be declared obviously, mustn't I? I don't know why developers of npm socks-proxy-agent didn't announce about this issue, so weird. We still have to write correct protocol while using socks proxy with curl command, google-chrome, etc.

    Hope this would help you guys when using this npm, be caureful in this :)