summaryrefslogtreecommitdiff
path: root/internal/tcpserver.go
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2023-05-19 00:38:36 +0300
committerPaul Buetow <paul@buetow.org>2023-05-19 00:38:36 +0300
commite881e64406bc8f2d0d5d3099a48d3cf4774e7bd8 (patch)
tree587c6e5d91ba73b9a6f56ac8c2dfa1711b9d5b79 /internal/tcpserver.go
parentdde3bed148ea90cd505bf6fb67b08719ebc8933a (diff)
can reject non-participants
Diffstat (limited to 'internal/tcpserver.go')
-rw-r--r--internal/tcpserver.go16
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 {