Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] How to set network proxy? #163

Open
inix opened this issue Nov 19, 2021 · 8 comments
Open

[Question] How to set network proxy? #163

inix opened this issue Nov 19, 2021 · 8 comments
Labels

Comments

@inix
Copy link

inix commented Nov 19, 2021

Hi:
The Binance domains are blocked in some areas, how to set network proxy if using this lib?
Thanks

@tiagosiebler
Copy link
Owner

Hi there,

The second parameter for both REST clients lets you pass parameters to the HTTP client used in my connectors (Axios):
https://github.com/tiagosiebler/binance/blob/master/src/main-client.ts#L78
https://github.com/tiagosiebler/binance/blob/master/src/usdm-client.ts#L80

One of the axios configuration parameters is for proxies:
https://axios-http.com/docs/req_config

This example passes the example from axios into the main REST client:

import { MainClient } from 'binance';

const axiosOptions = {
    proxy: {
    protocol: 'https',
    host: '127.0.0.1',
    port: 9000,
    auth: {
      username: 'mikeymike',
      password: 'rapunz3l'
    }
  }
}
const client = new MainClient({ }, axiosOptions);

@inix
Copy link
Author

inix commented Nov 20, 2021

Hi there,

The second parameter for both REST clients lets you pass parameters to the HTTP client used in my connectors (Axios): https://github.com/tiagosiebler/binance/blob/master/src/main-client.ts#L78 https://github.com/tiagosiebler/binance/blob/master/src/usdm-client.ts#L80

One of the axios configuration parameters is for proxies: https://axios-http.com/docs/req_config

This example passes the example from axios into the main REST client:

import { MainClient } from 'binance';

const axiosOptions = {
    proxy: {
    protocol: 'https',
    host: '127.0.0.1',
    port: 9000,
    auth: {
      username: 'mikeymike',
      password: 'rapunz3l'
    }
  }
}
const client = new MainClient({ }, axiosOptions);

Does websocket client support proxy?

@tiagosiebler
Copy link
Owner

tiagosiebler commented Nov 20, 2021

Does websocket client support proxy?

Not at the moment - it simply opens a connection to the ws urls directly:
https://github.com/tiagosiebler/binance/blob/master/src/websocket-client.ts#L159

If you're willing to research how to add proxy support to the ws module when it opens a connection, and implement it (ideally avoiding new dependencies as much as possible), I'll gladly review a pull request.

If you're worried about doing it alone or just want to generally discuss this, I also recommend joining the node traders engineering community on telegram: https://t.me/nodetraders

@inix
Copy link
Author

inix commented Nov 20, 2021

Does websocket client support proxy?

Not at the moment - it simply opens a connection to the ws urls directly: https://github.com/tiagosiebler/binance/blob/master/src/websocket-client.ts#L159

If you're willing to research how to add proxy support to the ws module when it opens a connection, and implement it (ideally avoiding new dependencies as much as possible), I'll gladly review a pull request.

If you're worried about doing it alone or just want to generally discuss this, I also recommend joining the node traders engineering community on telegram: https://t.me/nodetraders

@tiagosiebler
The websocket traffics can go through proxy by using a custom http.Agent implementation like https-proxy-agent or socks-proxy-agent.
More details: https://github.com/websockets/ws#how-to-connect-via-a-proxy
I have tested successfully using https-proxy-agent.

@tiagosiebler
Copy link
Owner

Sounds good! If you're willing on making the change (in a way that doesn't break existing functionality), I'd gladly review a pull request.

It would also be ideal if these modules could be injected somehow, instead of adding them as a dependency (as they do bring other dependencies as well, which increase module size for others using this connector in frontend projects).

Perhaps a parameter for the websocket client constructor (or a setter method like wsClient.setHttpAgent(httpsproxyagent)), as well as documentation in the readme for using this connector with proxies.

Looks like that would work, based on the https proxy agent docs: https://github.com/TooTallNate/node-https-proxy-agent#ws-websocket-connection-example

@inix
Copy link
Author

inix commented Nov 24, 2021

Sounds good! If you're willing on making the change (in a way that doesn't break existing functionality), I'd gladly review a pull request.

It would also be ideal if these modules could be injected somehow, instead of adding them as a dependency (as they do bring other dependencies as well, which increase module size for others using this connector in frontend projects).

Perhaps a parameter for the websocket client constructor (or a setter method like wsClient.setHttpAgent(httpsproxyagent)), as well as documentation in the readme for using this connector with proxies.

Looks like that would work, based on the https proxy agent docs: https://github.com/TooTallNate/node-https-proxy-agent#ws-websocket-connection-example

Both http proxy and socks proxy repo works. If using socks5 proxy, the proxy addr should be something like 'socks://127.0.0.1' instead of 'socks5://127.0.0.1'.

@genru
Copy link

genru commented Jul 10, 2022

Sounds good! If you're willing on making the change (in a way that doesn't break existing functionality), I'd gladly review a pull request.

It would also be ideal if these modules could be injected somehow, instead of adding them as a dependency (as they do bring other dependencies as well, which increase module size for others using this connector in frontend projects).

Perhaps a parameter for the websocket client constructor (or a setter method like wsClient.setHttpAgent(httpsproxyagent)), as well as documentation in the readme for using this connector with proxies.

Looks like that would work, based on the https proxy agent docs: https://github.com/TooTallNate/node-https-proxy-agent#ws-websocket-connection-example

I noticed the websocket component is using isomorphic-ws which does not support option when create. So i am not sure if it's possible to use agent as proxy solution.

@genru
Copy link

genru commented Jul 10, 2022

Sounds good! If you're willing on making the change (in a way that doesn't break existing functionality), I'd gladly review a pull request.
It would also be ideal if these modules could be injected somehow, instead of adding them as a dependency (as they do bring other dependencies as well, which increase module size for others using this connector in frontend projects).
Perhaps a parameter for the websocket client constructor (or a setter method like wsClient.setHttpAgent(httpsproxyagent)), as well as documentation in the readme for using this connector with proxies.
Looks like that would work, based on the https proxy agent docs: https://github.com/TooTallNate/node-https-proxy-agent#ws-websocket-connection-example

Both http proxy and socks proxy repo works. If using socks5 proxy, the proxy addr should be something like 'socks://127.0.0.1' instead of 'socks5://127.0.0.1'.

@inix could you kindly give some details to instruct how websocket using proxy?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants