1
| defmodule Owsla.TcpAcceptor do
use GenServer.Behaviour
@ssl_options
[{:certfile, "deviceserver.crt"}, {:keyfile, "deviceserver.key"},
{:ciphers, [
{:dhe_rsa,:aes_256_cbc,:sha256},
{:dhe_dss,:aes_256_cbc,:sha256},
{:rsa,:aes_256_cbc,:sha256},
{:dhe_rsa,:aes_128_cbc,:sha256},
{:dhe_dss,:aes_128_cbc,:sha256},
{:rsa,:aes_128_cbc,:sha256},
{:dhe_rsa,:aes_256_cbc,:sha},
{:dhe_dss,:aes_256_cbc,:sha},
{:rsa,:aes_256_cbc,:sha},
{:dhe_rsa,:'3des_ede_cbc',:sha},
{:dhe_dss,:'3des_ede_cbc',:sha},
{:rsa,:'3des_ede_cbc',:sha},
{:dhe_rsa,:aes_128_cbc,:sha},
{:dhe_dss,:aes_128_cbc,:sha},
{:rsa,:aes_128_cbc,:sha},
{:rsa,:rc4_128,:sha},
{:rsa,:rc4_128,:md5},
{:dhe_rsa,:des_cbc,:sha},
{:rsa,:des_cbc,:sha}
]}]
def start_link(listen_socket) do
:gen_server.start_link(__MODULE__, listen_socket, [])
end
def init(listen_socket) do
:gen_server.cast self, {:listen}
{:ok, listen_socket}
end
def handle_cast( {:listen}, listen_socket) do
do_listen(listen_socket)
end
defp do_listen(listen_socket) do
case :gen_tcp.accept(listen_socket) do
{:ok, socket} ->
case :ssl.ssl_accept(socket, @ssl_options) do
{:ok, ssl_socket} ->
endpoint = Owsla.TcpSupervisor.start_endpoint(ssl_socket)
:ssl.controlling_process(ssl_socket, endpoint)
:gen_server.cast endpoint, {:start}
do_listen(listen_socket)
{:error, :closed} ->
do_listen(listen_socket)
end
{:error, :closed} -> do_listen(listen_socket)
{:error, _} -> { :stop, :error, [] }
end
end
end
|