diff options
Diffstat (limited to 'internal/tcpserver.go')
| -rw-r--r-- | internal/tcpserver.go | 16 |
1 files changed, 9 insertions, 7 deletions
diff --git a/internal/tcpserver.go b/internal/tcpserver.go index c65fb0b..a7153f8 100644 --- a/internal/tcpserver.go +++ b/internal/tcpserver.go @@ -9,25 +9,29 @@ import ( "time" ) -func startTcpServer(ctx context.Context, address string, ch chan<- vote) error { - listener, err := net.Listen("tcp", address) +func startTcpServer(ctx context.Context, config config, ch chan<- vote) error { + listener, err := net.Listen("tcp", config.Address) if err != nil { return fmt.Errorf("Error starting TCP server: %s", err.Error()) } defer listener.Close() - log.Printf("TCP server started on %s\n", address) + log.Printf("TCP server started on %s\n", config.Address) for { - // Accept incoming client connections conn, err := listener.Accept() if err != nil { log.Printf("Error accepting connection: %s\n", err.Error()) continue } - log.Printf("Client connected: %s\n", conn.RemoteAddr().String()) + if !config.isParticipant(conn.RemoteAddr().String()) { + log.Printf("Denying connection, peer not a participant: %v\n", conn.RemoteAddr().String()) + conn.Close() + continue + } + log.Printf("Client connected: %s\n", conn.RemoteAddr().String()) go handleConnection(ctx, conn, ch) } } @@ -35,8 +39,6 @@ func startTcpServer(ctx context.Context, address string, ch chan<- vote) error { func handleConnection(ctx context.Context, conn net.Conn, ch chan<- vote) { defer conn.Close() remoteAddr := conn.RemoteAddr().String() - // TODO: Reject remote connection when not in participant list! - log.Printf("Client %s connected\n", remoteAddr) reader := bufio.NewReader(conn) for { |
