discord / loqui

RPC Transport Layer - with minimal bullshit.
MIT License
220 stars 31 forks source link

EX client TLS #25

Open cwertyar opened 6 years ago

cwertyar commented 6 years ago

Greetings!

I was wondering if it is somehow possible to use the Elixir client to connect to a server over TLS? I've seen that it is possible to run the Elixir server with :ranch_ssl as its transport and also the go client seems to intentionally enable the possibility of running over a TLS connection. Maybe this is possible and I just missed it, I'm still quite new to elixir and the erlang documentation about the options for :gen_tcp seemed quite daunting.

Thanks!

scohen commented 6 years ago

From my cursory check, in order for the client to support tls, I believe the gen_tcp module in the client would need to be paramaterized so it could be swapped out with the ssl module.

So, the sort answer is that the elixir client presently doesn't support tls right now.

cwertyar commented 6 years ago

Thank you for your answer @scohen !

I had a go at this today but came across more problems. The parameterization of the transport was quite smooth actually and it worked fine. The thing I'm faced with now is that the gen_servers all have lines like this one where a match is done on :tcp while on an SSL connection it's actually :ssl:

{:tcp, ^sock, data} ->

I've handled it like in the following example. Would this be acceptable for you or is there a better alternative?

{type, ^sock, data} when type in [:tcp, :ssl]
scohen commented 6 years ago

I think what you can do is something like this. Say you've added a transport key to the Client GenServer's state:

def handle_info({transport, socket, data}, %State{sock: socket, transport: transport}=state) do
cwertyar commented 6 years ago

I think that is what I have done for the most part. I pushed the changes into my fork here so you can see what exactly I needed to change.

scohen commented 6 years ago

What I suggested above is slightly different, it requires the transport in your state match the message coming into the genserver, the alternative allows either :tcp or :ssl messages to be handled. I'm not sure why that would happen, exactly, but it's a little too permissive.

Why don't you submit that as a PR?