From 00f7a3663b10f44a1b5d5ef15d8ae06064df0b9d Mon Sep 17 00:00:00 2001 From: azw Date: Mon, 24 Apr 2023 07:21:39 +0000 Subject: [PATCH] 1 --- server/wsproxy/kcpsession.h | 6 ++++++ server/wsproxy/longsessionmgr.cc | 13 ++++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/server/wsproxy/kcpsession.h b/server/wsproxy/kcpsession.h index 6f72d3e..c1ee0e9 100644 --- a/server/wsproxy/kcpsession.h +++ b/server/wsproxy/kcpsession.h @@ -22,6 +22,12 @@ public: void SendClientMsg(char* buf, int buf_len); virtual void OnRecvPacket(a8::UdpPacket* pkt) override; + static int GetSecretKeyLen() { return sizeof(long long) / 4; } + static long long ReadSecretKey(const char* buf, int buf_len) + { + return buf_len < GetSecretKeyLen() ? 0 : *((long long*)buf); + } + protected: virtual void DecodeUserPacket(char* buf, int& offset, unsigned int buflen) override; diff --git a/server/wsproxy/longsessionmgr.cc b/server/wsproxy/longsessionmgr.cc index d0779a5..0a1bbda 100644 --- a/server/wsproxy/longsessionmgr.cc +++ b/server/wsproxy/longsessionmgr.cc @@ -81,11 +81,14 @@ std::shared_ptr LongSessionMgr::GetSession(int socket_handle) void LongSessionMgr::ProcUdpPacket(a8::UdpPacket* pkt) { - int socket_handle = 0; - long long secret_key = 0; - auto session = GetSession(socket_handle); - if (session && secret_key == session->GetKcpSession()->GetSecretKey()) { - session->GetKcpSession()->OnRecvPacket(pkt); + const int IKCP_OVERHEAD = 24; + if (pkt->buf_len > IKCP_OVERHEAD + KcpSession::GetSecretKeyLen()) { + int socket_handle = ikcp_getconv(pkt->buf); + long long secret_key = KcpSession::ReadSecretKey(pkt->buf, pkt->buf_len); + auto session = GetSession(socket_handle); + if (session && secret_key == session->GetKcpSession()->GetSecretKey()) { + session->GetKcpSession()->OnRecvPacket(pkt); + } } }