From 1a218730e9a0c16fd71f4606adfde5862be2e7b7 Mon Sep 17 00:00:00 2001 From: Kyren223 Date: Mon, 6 Jan 2025 17:34:47 +0200 Subject: Server now disconnects connection when a new connection matches the same user ID, note that there seems to be a weird thing where if 2 clients are from the same IP, the disconnected client will just reconnect to the new client's socket, so they will both use the same connection for communication --- internal/server/session/session.go | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) (limited to 'internal/server/session') diff --git a/internal/server/session/session.go b/internal/server/session/session.go index 2b7121a..88a19e7 100644 --- a/internal/server/session/session.go +++ b/internal/server/session/session.go @@ -25,6 +25,7 @@ type SessionManager interface { type Session struct { manager SessionManager addr *net.TCPAddr + cancel context.CancelFunc writeQueue chan packet.Packet issuedTime time.Time @@ -36,14 +37,21 @@ type Session struct { mu sync.Mutex } -func NewSession(manager SessionManager, addr *net.TCPAddr, id snowflake.ID, pubKey ed25519.PublicKey) *Session { +func NewSession( + manager SessionManager, + addr *net.TCPAddr, cancel context.CancelFunc, + id snowflake.ID, pubKey ed25519.PublicKey, +) *Session { session := &Session{ - writeQueue: make(chan packet.Packet, 10), - PubKey: pubKey, manager: manager, addr: addr, - id: id, + cancel: cancel, + writeQueue: make(chan packet.Packet, 10), + issuedTime: time.Time{}, challenge: make([]byte, 32), + PubKey: pubKey, + id: id, + mu: sync.Mutex{}, } session.Challenge() // Make sure an initial nonce is generated return session @@ -89,3 +97,17 @@ func (s *Session) Read(ctx context.Context) (packet.Packet, bool) { return packet.Packet{}, false } } + +func (s *Session) Close() { + timeout := 1 * time.Second + ctx, cancel := context.WithTimeout(context.Background(), timeout) + payload := &packet.Error{ + Error: "new connection from another location, closing this one", + PktType: packet.PacketError, + } + pkt := packet.NewPacket(packet.NewMsgPackEncoder(payload)) + s.Write(ctx, pkt) + cancel() + + s.cancel() +} -- cgit v1.3.1