summaryrefslogtreecommitdiff
path: root/internal/server/session/session.go
diff options
context:
space:
mode:
authorKyren223 <Kyren223@proton.me>2025-01-06 17:34:47 +0200
committerKyren223 <Kyren223@proton.me>2025-01-06 17:34:47 +0200
commit1a218730e9a0c16fd71f4606adfde5862be2e7b7 (patch)
treebebf7d03f96f95174afbc0268af58dc7ca09c958 /internal/server/session/session.go
parentdb57a31b08c1a3b00c2e253f02e67119e3cfa108 (diff)
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
Diffstat (limited to 'internal/server/session/session.go')
-rw-r--r--internal/server/session/session.go30
1 files changed, 26 insertions, 4 deletions
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()
+}