Skip to content

Instantly share code, notes, and snippets.

@dszoboszlay
Created August 8, 2023 14:56
Show Gist options
  • Save dszoboszlay/eba84b2e69022d1301416db03d452377 to your computer and use it in GitHub Desktop.
Save dszoboszlay/eba84b2e69022d1301416db03d452377 to your computer and use it in GitHub Desktop.
#!/usr/bin/env escript
-mode(compile).
-define(PORT, 2345).
-define(SERVER_TIMEOUT, 1000).
-define(CLIENT_TIMEOUT, 1500).
main(Msg) ->
application:ensure_all_started(ssl),
spawn(fun server/0),
timer:sleep(100),
client(Msg).
server() ->
Options = [{certs_keys, [#{certfile => "cert.pem", keyfile => "key.pem"}]},
{versions, ['tlsv1.2']},
{verify, verify_none},
{ciphers, ssl:cipher_suites(all, 'tlsv1.2')}
],
{ok, LS} = ssl:listen(?PORT, Options),
{ok, TS} = ssl:transport_accept(LS),
{ok, S} = ssl:handshake(TS),
receive
{ssl_closed, S} ->
io:format("server closed~n");
{ssl_error, S, Reason} ->
io:format("server error: ~p~n", [Reason])
after
?SERVER_TIMEOUT ->
io:format("server timeout~n"),
ssl:close(S)
end.
client(Msg) ->
Options = [{versions, ['tlsv1.2']},
{verify, verify_none},
{renegotiate_at, 5}
],
{ok, S} = ssl:connect("localhost", ?PORT, Options, infinity),
spawn(fun () -> client(S, Msg) end),
receive
{ssl_closed, S} ->
io:format("client closed~n"),
halt(0);
{ssl_error, S, Reason} ->
io:format("client error: ~p~n", [Reason]),
halt(0)
after
?CLIENT_TIMEOUT ->
io:format("client timeout~n"),
ssl:close(S),
halt(1)
end.
client(_S, []) ->
io:format("client is not sending messages!~n");
client(S, Msg) ->
timer:sleep(10),
case ssl:send(S, Msg) of
ok ->
client(S, Msg);
{error, Reason} ->
io:format("client send error: ~p~n", [Reason])
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment