From b9d3b7ea9df409794eefe48ae1003e8444f519bb Mon Sep 17 00:00:00 2001 From: bt Date: Wed, 8 Apr 2026 13:37:56 +0200 Subject: [daemon] Server to server communication --- notes.txt | 3 +++ server/server.go | 23 ++++++++++++++++++++++- tools/run-many.sh | 13 +++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 notes.txt create mode 100755 tools/run-many.sh diff --git a/notes.txt b/notes.txt new file mode 100644 index 0000000..64817ad --- /dev/null +++ b/notes.txt @@ -0,0 +1,3 @@ +# personal notes and snippets + +ifconfig lo0 alias 127.0.0.2 diff --git a/server/server.go b/server/server.go index 3876bac..712f654 100644 --- a/server/server.go +++ b/server/server.go @@ -168,7 +168,7 @@ func (s *Server) handleMessage(conn net.Conn, user User, msg core.Message) error return s.handleLocalMessage(channel, msg) } - return core.ErrNotSupported + return s.handleOutboundMessage(channel, host, msg) } func (s *Server) handleLocalMessage(channel string, msg core.Message) error { @@ -179,3 +179,24 @@ func (s *Server) handleLocalMessage(channel string, msg core.Message) error { return user.Send(msg) } + +func (s *Server) handleOutboundMessage(channel, host string, msg core.Message) error { + conn, err := net.Dial("tcp", host+":9999") + if err != nil { + return err + } + defer conn.Close() + + hs := core.Handshake{0, 1} + if err := core.Send(conn, hs); err != nil { + return err + } + + // TODO, servers should not use this type of auth + auth := core.Auth{"server", "valid"} + if err := core.Send(conn, auth); err != nil { + return err + } + + return core.Send(conn, msg) +} diff --git a/tools/run-many.sh b/tools/run-many.sh new file mode 100755 index 0000000..9c84e0d --- /dev/null +++ b/tools/run-many.sh @@ -0,0 +1,13 @@ +#!/bin/sh + +# to use this script add 127.0.0.2 address to loopback interface and add +# following lines to hosts file +# 127.0.0.1 localhost s1 +# 127.0.0.2 s2 + +tmux \ + new-session "go run cmd/daemon/main.go -a 127.0.0.1:9999 -n s1; read" \; \ + split-window "go run cmd/daemon/main.go -a 127.0.0.2:9999 -n s2; read" \; \ + split-window "sleep 0.5; go run cmd/client/main.go -u u1 -a s1:9999; read" \; \ + split-window "sleep 0.5; go run cmd/client/main.go -u u2 -a s2:9999; read" \; \ + select-layout tiled; \ No newline at end of file -- cgit v1.2.3