JoshGlazebrook / socks

Fully featured SOCKS proxy client supporting SOCKSv4, SOCKSv4a, and SOCKSv5. Includes Bind and Associate functionality.
https://www.npmjs.com/package/socks
MIT License
304 stars 35 forks source link

What happened with Agent property? #23

Closed xNEL99 closed 6 years ago

xNEL99 commented 6 years ago

Why does the Agent property got removed on the new update

JoshGlazebrook commented 6 years ago

The agent implementation was removed in v2 as it makes more sense for socks to be fully focused on being just a socks client.

I recommend: https://www.npmjs.com/package/socks-proxy-agent as a replacement, which uses the socks library behind the scenes.

Please see the migration guide for moving from v1 -> v2: https://github.com/JoshGlazebrook/socks/blob/master/docs/migratingFromV1.md

T-vK commented 3 years ago

@JoshGlazebrook I appreciate the link to socks-proxy-agent, however I wanted to use your socks library because of the proxy chaining feature which doesn't appear to be available in socks-proxy-agent.

Is there any chance you can help me setting up an agent that supports a proxy chain?

JoshGlazebrook commented 3 years ago

@T-vK I think it's something that could be added to socks-proxy-agent. They're using .createConnection() already, so it would just be a matter of accepting a list of proxies, and then calling .createConnectionChain() instead.

T-vK commented 3 years ago

Thank you @JoshGlazebrook ! So I have tried to do that here, but I'm getting errors and I haven't been able to get a response from the socks-proxy-agent devs.

I basically just changed SocksProxyAgent to support an array of proxies and turned opts from SocksProxyAgentOptions into SocksProxyAgentOptions[]. But I can't get it to run. When super(opts); is called, I get the following error:

Error Message ``` src/agent.ts:143:9 - error TS2345: Argument of type 'SocksProxyAgentOptions[]' is not assignable to parameter of type 'AgentOptions | { (req: ClientRequest, opts: Re questOptions, fn: AgentCallbackCallback): void; (req: ClientRequest, opts: RequestOptions): Duplex | ... 2 more ... | Promise<...>; } | undefined'. Type 'SocksProxyAgentOptions[]' is not assignable to type '{ (req: ClientRequest, opts: RequestOptions, fn: AgentCallbackCallback): void; (req: ClientRequest, opts : RequestOptions): Duplex | ... 2 more ... | Promise<...>; }'. Type 'SocksProxyAgentOptions[]' provides no match for the signature '(req: ClientRequest, opts: RequestOptions, fn: AgentCallbackCallback): void'. 143 super(opts); ~~~~ Found 1 error. npm ERR! code ELIFECYCLE npm ERR! errno 2 npm ERR! socks-proxy-agent@5.0.0 build: `tsc` npm ERR! Exit status 2 npm ERR! npm ERR! Failed at the socks-proxy-agent@5.0.0 build script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above. npm ERR! A complete log of this run can be found in: npm ERR! /home/fedora/.npm/_logs/2021-01-30T21_01_44_169Z-debug.log ```

This probably happens because opts is now an array of opts, but I don't understand why it's even passed to super and if I need to change the parent class or the parameter I'm passing.

Is there any chance you can help me out?

JoshGlazebrook commented 3 years ago

I can possibly look into it a bit this weekend

T-vK commented 3 years ago

Thank you. :)

korniychuk commented 3 years ago

Maybe it'll be helpful for someone

export class SocksProxyAgent extends https.Agent {
  private readonly proxy: SocksProxy;

  public constructor(options: https.AgentOptions & { proxy: SocksProxy }) {
    const { proxy, ...opts } = options;
    super(opts);
    this.proxy = proxy;
  }

  protected createConnection(
    options: http.ClientRequestArgs,
    callback: (err: Error | null, socket: Socket | null) => void,
  ): void {
    const socksOpts: SocksClientOptions = {
      proxy: this.proxy,
      command: 'connect',
      timeout: options.timeout,
      destination: {
        host: options.host!,
        port: +options.port!,
      },
    };

    void SocksClient.createConnection(socksOpts, (err: Error | null, info?: { socket: Socket }) => {
      if (err) {
        callback(err, null);
      } else {
        const secureSocket = super.createConnection({ ...options, socket: info?.socket });
        callback(null, secureSocket);
      }
    });
  }
}
JoshGlazebrook commented 3 years ago

Previous discussion on why agent was removed from here: https://github.com/JoshGlazebrook/socks/issues/23#issuecomment-354938712

korniychuk commented 3 years ago

Thank @JoshGlazebrook for your comment and quick answer. I read and agree with that answer. That package has an issue with TLS. I've just offered a working solution rewritten from scratch without any dependencies. Maybe someone will be looking for such a solution. I don't offer to add SocksProxyAgent to socks library :) Anyway thanks for your comment 🤝 ✌️

JoshGlazebrook commented 3 years ago

@korniychuk Ah, well feel free to make a PR to add this to the examples folder. I'm sure a lot of people specifically use this package for that use!

ofosukin commented 2 years ago

@T-vK would you kindly share your solution to this problem with me, please. I want to achieve a similar thing. Https request -> local Tor socks5 proxy -> my external socks5 proxy -> webserver.