distubejs / ytdl-core

YouTube video downloader in javascript.
MIT License
293 stars 50 forks source link

Generated IP address not being used in outgoing undici request #12

Closed mariusbegby closed 1 year ago

mariusbegby commented 1 year ago

Describe the bug

IP address from ipv6 rotation is not actually used in outgoing network request.

undici does not have localAddress on request() but instead on Client. You need to create a new Client and set localAddress there and then send request through Client.request(). Something like this:

exports.request = async(url, options = {}) => {
  const { requestOptions } = options;

  const client = new Client('https://www.youtube.com', {
    localAddress: requestOptions.localAddress,
    autoSelectFamily: true
  });

  const req = await client.request({
    path: '/watch?v=rUxyKA_-grg', // example path
    method: 'GET',
  });

  //const req = await request(url, requestOptions);
  const code = req.statusCode.toString();
  if (code.startsWith('2')) {
    if (req.headers['content-type'].includes('application/json')) return req.body.json();
    return req.body.text();
  }
  if (code.startsWith('3')) return exports.request(req.headers.location, options);
  const e = new Error(`Status code: ${code}`);
  e.statusCode = req.statusCode;
  throw e;
};

The above snippet does not work, it throws some errors, so treat as pseudo code. But maybe it helps to understand that you need to initialize a new Client where localAddress is specified before sending the request. I unfortunately don't have any experience with @distube/ytdl-core codebase or undici, so don't know how to implement the fix.

Currently, @distube/ytdl-core versions using undici is not actually using the generated IP address, and thus the "IP rotation" has no effect.

Environment

skick1234 commented 1 year ago

Available on 4.13.0. Guide: IP Rotation