ninenines / gun

HTTP/1.1, HTTP/2, Websocket client (and more) for Erlang/OTP.
ISC License
891 stars 232 forks source link

How do I use gun with Socks5 proxy? #272

Closed rdtq closed 2 years ago

rdtq commented 2 years ago

I've been trying to setup this for a few days now 😋 I'm using gun with Elixir project.

Here's my code (roughly translated from the docs here https://ninenines.eu/docs/en/gun/1.3/manual/gun.connect/#_examples ):

{:ok, pid} = :gun.open('socks5server.net', 1080)
:gun.await_up(pid)
stream = :gun.connect(pid, %{host: 'www.bing.com', port: 443, protocols: [:http], transport: :tls})
:gun.await(pid, stream)
stream2 = :gun.get(pid, '/')
:gun.await(pid, stream2)
:gun.stream_info(pid, stream2)
body = :gun.await_body(pid, stream2)

What I get is:

{:error, :timeout}

Am I using gun wrong?

essen commented 2 years ago

You have to configure the socks protocol. gun:connect is for HTTP CONNECT.

https://github.com/ninenines/gun/blob/master/test/socks_SUITE.erl#L215-L224 for example.

It's for 2.0 but it's not super different for 1.3, not sure anything even changed.

rdtq commented 2 years ago

Thanks! What worked for me was

    {:ok, socks_conn} = :gun.open('socks5server.net', 1080, %{transport: :tcp, protocols: [{:socks, %{host: 'bing.com', port: 443, transport: :tls}}]})