enoise more or less just works

This commit is contained in:
Peter Harpending
2026-09-02 02:45:21 -07:00
parent e5436c2e3e
commit 17cca5ecfe
5 changed files with 50 additions and 101 deletions
-1
View File
@@ -244,6 +244,5 @@ init_ho_names() ->
"Delight", "Capri", "Trixie", "Cinnamon"]. "Delight", "Capri", "Trixie", "Cinnamon"].
recycle([N | Ns]) -> recycle([N | Ns]) ->
{N, Ns ++ [N]}. {N, Ns ++ [N]}.
+36
View File
@@ -0,0 +1,36 @@
#!/usr/bin/env escript
-mode(compile).
% john state
-record(js,
{tsock = none :: gen_tcp:socket(),
nsock = none :: enoise:noise_socket()}).
main([]) ->
TcpOpts = [binary, {packet, 0}, {active, once}],
{ok, TSock} = gen_tcp:connect("localhost", 6969, TcpOpts),
ok = inet:setopts(TSock, [{active, once}]),
{ok, NSock, _HandshakeState} = enoise:connect(TSock, noise_options()),
j_loop(#js{tsock = TSock, nsock = NSock}).
noise_options() ->
% totally safe crypto
KP = enoise_keypair:new(dh25519),
[{noise, "Noise_NN_25519_ChaChaPoly_BLAKE2b"},
{e, KP}].
j_loop(JS = #js{nsock = NSock}) ->
receive
{noise, NSock, Bytes} ->
ok = io:format("< ~tp~n", [Bytes]),
j_loop(JS);
Unknown ->
ok = io:format("~tp unknown message, killing self: ~tp~n",
[self(), Unknown]),
exit(normal)
end.
-30
View File
@@ -1,30 +0,0 @@
% @doc echo server client
-module(nechoc).
-export([main/0]).
main() ->
{ok, TcpSocket} = gen_tcp:connect("localhost", 6969, [binary, {packet, 0}]),
ok = inet:setopts(Socket, [{active, once}]),
{ok, NoiseSock, _HandshakeState} = enoise:connect(TcpSocket, noise_options()),
loop(Socket).
noise_options() ->
% totally safe crypto
KP = enoise_keypair:new(dh25519),
[{noise, "Noise_NN_25519_ChaChaPoly_BLAKE2b"},
{e, KP}].
j_loop(NSock) ->
ok = inet:setopts(NSock, [{active, once}]),
receive
{tcp, Socket, Bytes} ->
ok = io:format("< ~tp~n", [Bytes]),
loop(Socket);
{tcp_closed, Socket} ->
ok = io:format("connection terminated~n", []),
exit(normal)
end.
+14 -70
View File
@@ -1,65 +1,7 @@
%% @doc #!/usr/bin/env escript
%% super simple telnet echo server daemon
%%
%% no noise yet
%%
%% pimps and hos model (from trash talk template):
%%
%% ```
%% t_sup
%% |
%% t_clients
%% |
%% +--- t_client_man pimp
%% |
%% +--- t_client_sup bottom bitch
%% |
%% +--- <0.1.0> (t_client) ho
%% +--- <0.2.0> (t_client) ho
%% +--- <0.3.0> (t_client) ho
%% '''
%%
%% each ho:
%%
%% <ul>
%% <li>Takes listen socket as arg</li>
%% <li>spawns next ho</li>
%% </ul>
%%
%% ```
%% start(ListenSocket) ->
%% t_client_sup:start_acceptor(ListenSocket).
%%
%% start_link(ListenSocket) ->
%% proc_lib:start_link(?MODULE, init, [self(), ListenSocket]).
%%
%% init(Parent, ListenSocket) ->
%% ok = io:format("~p Listening.~n", [self()]),
%% Debug = sys:debug_options([]),
%% ok = proc_lib:init_ack(Parent, {ok, self()}),
%% listen(Parent, Debug, ListenSocket).
%%
%% listen(Parent, Debug, ListenSocket) ->
%% case gen_tcp:accept(ListenSocket) of
%% {ok, Socket} ->
%% {ok, _} = start(ListenSocket),
%% {ok, Peer} = inet:peername(Socket),
%% ok = io:format("~p Connection accepted from: ~p~n", [self(), Peer]),
%% ok = t_client_man:enroll(),
%% State = #s{socket = Socket},
%% loop(Parent, Debug, State);
%% {error, closed} ->
%% ok = io:format("~p Retiring: Listen socket closed.~n", [self()]),
%% exit(normal)
%% end.
%% '''
%%
%% @end
-module(echod).
-mode(compile).
-include("./ansi.hrl"). -include("./ansi.hrl").
-export([main/0]).
% ho from pimp pov % ho from pimp pov
-record(pho, -record(pho,
@@ -78,10 +20,11 @@
-record(hs, -record(hs,
{name = none :: string(), {name = none :: string(),
daddy = none :: pid(), daddy = none :: pid(),
asock = none :: gen_tcp:socket(),
nsock = none :: enoise:noise_socket()}). nsock = none :: enoise:noise_socket()}).
main() -> main([]) ->
io:format("RBX example 4 telnet echo server daemon~n" io:format("RBX example 4 telnet echo server daemon~n"
"`one' part of the many-to-one relationship~n", "`one' part of the many-to-one relationship~n",
[]), []),
@@ -174,37 +117,38 @@ p_echo_except(_, _, _, []) ->
ok. ok.
%% runs in ho context %% runs in ho context
h_init(LSock, HS = #hs{name = Name, daddy = Daddy, nsock = none}) -> h_init(LSock, HS = #hs{name = Name, daddy = Daddy, asock = none, nsock = none}) ->
h_logln(Name, "h_init"), h_logln(Name, "h_init"),
% this is key: accept needs to run in ho context, otherwise she % this is key: accept needs to run in ho context, otherwise she
% doesn't own the acceptor socket % doesn't own the acceptor socket
% %
% blocks until client connects % blocks until client connects
case enoise:accept(LSock, noise_options()) of case gen_tcp:accept(LSock) of
{ok, NSock, _} -> {ok, ASock} ->
h_logln(Name, "taken"), h_logln(Name, "taken"),
% tell daddy to spawn the next ho % tell daddy to spawn the next ho
Daddy ! {ho, self(), taken}, Daddy ! {ho, self(), taken},
{ok, NSock, _} = enoise:accept(ASock, noise_options()),
Welcome = io_lib:format("HI! I'm ~ts. What's your name?\r\n", Welcome = io_lib:format("HI! I'm ~ts. What's your name?\r\n",
[Name]), [Name]),
ok = h_send(Name, NSock, Welcome), ok = h_send(Name, NSock, Welcome),
HS_II = HS#hs{nsock = ASock}, HS_II = HS#hs{asock = ASock, nsock = NSock},
h_loop(HS_II); h_loop(HS_II);
{error, closed} -> {error, closed} ->
h_logln(Name, "didn't get taken") h_logln(Name, "didn't get taken")
end. end.
h_loop(HS = #hs{name = Name, daddy = Daddy, nsock = ASock}) -> h_loop(HS = #hs{name = Name, daddy = Daddy, asock = ASock, nsock = NSock}) ->
ok = inet:setopts(ASock, [{active, once}]), ok = inet:setopts(ASock, [{active, once}]),
receive receive
{tcp, ASock, Data} -> {noise, NSock, Data} ->
Data_II = string:chomp(Data), Data_II = string:chomp(Data),
ok = h_rcvd(Name, Data_II), ok = h_rcvd(Name, Data_II),
Daddy ! {ho, self(), sent, Data_II}, Daddy ! {ho, self(), sent, Data_II},
h_loop(HS); h_loop(HS);
{pimp, send, Data} -> {pimp, send, Data} ->
ok = h_send(Name, ASock, Data), ok = h_send(Name, NSock, Data),
h_loop(HS); h_loop(HS);
{tcp_closed, ASock} -> {tcp_closed, ASock} ->
exit(its_fine); exit(its_fine);
@@ -238,9 +182,9 @@ h_rcvd(Name, MsgStr) ->
Msg = io_lib:format("<< ~ts :: ~ts~n", [Name, MsgStr]), Msg = io_lib:format("<< ~ts :: ~ts~n", [Name, MsgStr]),
io:put_chars(Msg). io:put_chars(Msg).
h_send(Name, Socket, Msg) -> h_send(Name, NSock, Msg) ->
ok = io:format(">> ~ts :: ~ts~n", [Name, Msg]), ok = io:format(">> ~ts :: ~ts~n", [Name, Msg]),
enoise:send(Socket, Msg). enoise:send(NSock, Msg).
init_ho_names() -> init_ho_names() ->