lexiforest / curl_cffi

Python binding for curl-impersonate fork via cffi. A http client that can impersonate browser tls/ja3/http2 fingerprints.
https://curl-cffi.readthedocs.io/
MIT License
2.51k stars 266 forks source link

Passing proxies to a websocket #415

Open johnraysimpson opened 1 month ago

johnraysimpson commented 1 month ago

The question

I just wanted clarity on using proxies with a websocket specifically, I'm assuming that if I set up a session with certain proxies, then use the ws_connect method then the proxies will be utilised for that request? E.g.

from curl_cffi import requests

session = requests.Session(proxies=some_proxy_dict)
ws = session.ws_connect(some_ws_url)  # the proxies are passed from the session to the websocket connection
johnraysimpson commented 1 month ago

I've had a dig around and got it to work, I'm not sure if it's a bug or not but someone might be able to figure that out, but using Session(proxies={"http": "http://some_proxy", "https": "https://some_proxy"}) doesn't appear to make websocket connections through that proxy. My work around at the moment is: Session(proxy=some_proxy_str)

(was able to test as I have access to a few proxy providers and know of a websocket url that only works with some - using the first method made connections no matter the proxies whereas the second method failed on the proxies I expected to fail)

perklet commented 1 month ago

Thanks for posting your findings back.

I think we should add something like proxies={"ws": "http://some_proxy", "wss": "https://some_proxy"} for consistency. Let's keep this open until it's resolved.

johnraysimpson commented 1 month ago

Ah right, makes more sense now, would it be nice to have a routine that converts the {"http": "http://some_proxy", "https": "https://some_proxy"} in a session to proxies={"ws": "http://some_proxy", "wss": "https://some_proxy"} when connecting to a websocket through Session? Only use case is when needing to make normal requests and websocket connections with the same IP, I think that would be quite elegant