如何使用ssl加密构建非阻塞TCP服务器

发布于 2024-08-05 05:51:57 字数 278 浏览 6 评论 0原文

trapexit.org 上的非阻塞 TCP 服务器 解释了如何基于 tcp_gen 构建服务器,但我想要修改这个示例并使其与 ssl 一起使用。现在我完全不知道如何替换

{ok, Ref} = prim_inet:async_accept(Listen_socket, -1)

Non-blocking TCP server on trapexit.org explains how to build server based on tcp_gen, but i want to modify this example and make it work with ssl. For now i have completely no idea how to replace

{ok, Ref} = prim_inet:async_accept(Listen_socket, -1)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

握住我的手 2024-08-12 05:51:57

具有多线程的简单 echo_ssl

-module(echo_ssl).
-compile([export_all]).

main() ->
    application:start(crypto),
    application:start(ssl),
    ssl:seed("TODO random here"),
    {ok, ListenSocket} = ssl:listen(2840, [
        {ssl_imp, new},
        {active, false},
        {verify, 0},
        {mode,binary},
        {cacertfile, "certs/etc/server/cacerts.pem"},
        {certfile, "certs/etc/server/cert.pem"},
        {keyfile, "certs/etc/server/key.pem"}
    ]),
    io:format("ready to accept connections at port 2840 ~p\n", [ListenSocket]),
    server_loop(ListenSocket).

server_loop(ListenSocket) ->
    {ok, Socket} = ssl:transport_accept(ListenSocket),
    io:format("accepted connection from ~p\n", [ssl:peername(Socket)]),
    ssl:ssl_accept(Socket),
    spawn(fun() -> loop(Socket) end),
    server_loop(ListenSocket).

loop(Socket) ->
    io:format("waiting for packet~n"),
    case ssl:recv(Socket, 1000, 2000) of
        {ok, Data} ->
            io:format("received data: ~s~n", [binary_to_list(Data)]),
            Return = ssl:send(Socket, Data),
            io:format("sending ~p~n", [Return]),
            loop(Socket);
        {error, timeout} ->
           loop(Socket);
        Else ->
            io:format("crap ~p~n",[Else])
    end.

simple echo_ssl with multi threading

-module(echo_ssl).
-compile([export_all]).

main() ->
    application:start(crypto),
    application:start(ssl),
    ssl:seed("TODO random here"),
    {ok, ListenSocket} = ssl:listen(2840, [
        {ssl_imp, new},
        {active, false},
        {verify, 0},
        {mode,binary},
        {cacertfile, "certs/etc/server/cacerts.pem"},
        {certfile, "certs/etc/server/cert.pem"},
        {keyfile, "certs/etc/server/key.pem"}
    ]),
    io:format("ready to accept connections at port 2840 ~p\n", [ListenSocket]),
    server_loop(ListenSocket).

server_loop(ListenSocket) ->
    {ok, Socket} = ssl:transport_accept(ListenSocket),
    io:format("accepted connection from ~p\n", [ssl:peername(Socket)]),
    ssl:ssl_accept(Socket),
    spawn(fun() -> loop(Socket) end),
    server_loop(ListenSocket).

loop(Socket) ->
    io:format("waiting for packet~n"),
    case ssl:recv(Socket, 1000, 2000) of
        {ok, Data} ->
            io:format("received data: ~s~n", [binary_to_list(Data)]),
            Return = ssl:send(Socket, Data),
            io:format("sending ~p~n", [Return]),
            loop(Socket);
        {error, timeout} ->
           loop(Socket);
        Else ->
            io:format("crap ~p~n",[Else])
    end.
迟到的我 2024-08-12 05:51:57

不要用任何东西代替它。而是将您的输入和输出连接到 SSL 处理程序。

花费者是对的,SSL 使用 TCP 进行传输。

这里有人似乎已经实现了基于异步TCP的SSL

Do not replace this with anything. Instead connect your input and output to the SSL handler.

spender is right, SSL uses TCP for transport.

Here someone seems to have implemented SSL over async TCP.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文