From 16e408d0f3a1659f55886ee95a6ea78be7832eb3 Mon Sep 17 00:00:00 2001 From: mkwcat Date: Sat, 9 Dec 2023 08:05:32 -0500 Subject: [PATCH] NATNEG: Delete session after 30 seconds Fixes #3 --- natneg/main.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/natneg/main.go b/natneg/main.go index e644d5c..3184de0 100644 --- a/natneg/main.go +++ b/natneg/main.go @@ -7,6 +7,7 @@ import ( "github.com/logrusorgru/aurora/v3" "net" "sync" + "time" "wwfc/common" "wwfc/logging" ) @@ -120,7 +121,6 @@ func handleConnection(conn net.PacketConn, addr net.Addr, buffer []byte) { mutex.Lock() session, exists := sessions[cookie] if !exists { - // TODO: Figure out removing the session if this request is nonsense or something logging.Notice(moduleName, "Creating session") session = &NATNEGSession{ Cookie: cookie, @@ -128,6 +128,15 @@ func handleConnection(conn net.PacketConn, addr net.Addr, buffer []byte) { Clients: map[byte]*NATNEGClient{}, } sessions[cookie] = session + + // Session has TTL of 30 seconds + time.AfterFunc(30*time.Second, func() { + mutex.Lock() + delete(sessions, cookie) + mutex.Unlock() + + logging.Info(moduleName, "Deleted session") + }) } mutex.Unlock()