socks5-http-client icon indicating copy to clipboard operation
socks5-http-client copied to clipboard

Using different hostnames with request.js seems to be broken

Open philiiiiiipp opened this issue 9 years ago • 2 comments

Hey, I tried to use the Agent in combination with request and changing tor proxy hostnames. One Tor is running on localhost, the other on 'tor_proxy'.

request(
        {
          uri: 'http://ipv4bot.whatismyipaddress.com',
          agentClass: Agent,
          agentOptions: {
            socksHost: 'localhost',
            socksPort: Tor.PORT
          }
        }, function (error, response, body) {
        console.log(body);
});

works as expected, the request is running over the localhost tor. The second request however

request(
        {
          uri: 'http://ipv4bot.whatismyipaddress.com',
          agentClass: Agent,
          agentOptions: {
            socksHost: 'tor_proxy',
            socksPort: Tor.PORT
          }
        }, function (error, response, body) {
        console.log(body);
});

Is also running over localhost. I investigated a bit further, and looking at which options are coming into socks5-http-client/lib/Agent, the first one is

Url {
  protocol: 'http:',
  slashes: true,
  auth: null,
  host: 'ipv4bot.whatismyipaddress.com',
  port: 80,
  hostname: 'ipv4bot.whatismyipaddress.com',
  hash: null,
  search: null,
  query: null,
  pathname: '/',
  path: '/',
  href: 'http://ipv4bot.whatismyipaddress.com/',
  socksPort: 9050,
  socksHost: 'localhost' }

and the second one

Url {
  protocol: 'http:',
  slashes: true,
  auth: null,
  host: 'ipv4bot.whatismyipaddress.com',
  port: 80,
  hostname: 'ipv4bot.whatismyipaddress.com',
  hash: null,
  search: null,
  query: null,
  pathname: '/',
  path: '/',
  href: 'http://ipv4bot.whatismyipaddress.com/',
  socksPort: 9050,
  socksHost: 'tor_proxy',
  agent: 
   Agent {
     domain: null,
     _events: { free: [Function] },
     _eventsCount: 1,
     _maxListeners: undefined,
     defaultPort: 80,
     protocol: 'http:',
     options: 
      { socksHost: 'localhost',
        socksPort: 9050,
        href: 'http://ipv4bot.whatismyipaddress.com/',
        path: null,
        pathname: '/',
        query: null,
        search: null,
        hash: null,
        hostname: 'ipv4bot.whatismyipaddress.com',
        port: 80,
        host: 'ipv4bot.whatismyipaddress.com',
        auth: null,
        slashes: true,
        protocol: 'http:' },
     requests: {},
     sockets: {},
     freeSockets: {},
     keepAliveMsecs: 1000,
     keepAlive: false,
     maxSockets: Infinity,
     maxFreeSockets: 256,
     socksHost: 'localhost',
     socksPort: 9050,
     createConnection: [Function] } `}

Seems like request is caching the agent if it used one before and so not changing the proxy hostname.

philiiiiiipp avatar Apr 15 '16 10:04 philiiiiiipp

I had a similar problem. I was having trouble changing the ports in consecutive requests. I solved it by passing a new instance of Agent class to the agent property instead of passing agentClass and agentOptions separately.

const Agent = require('socks5-http-client/lib/Agent');
const request = require('request');

request({
    url: 'http://whatismyipaddress.com',
    agent: new Agent({
         socksHost: 'localhost',
         socksPort: 9050
    })
}, (err, res, body) => {
    // do something with the response
})

Hope this helps!

abdulqadir93 avatar Jun 12 '16 21:06 abdulqadir93

got the same problem

shiny avatar Jun 30 '16 20:06 shiny