Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
7eb242f5b8 | ||
![]() |
571a29eebb | ||
![]() |
70e076631c | ||
![]() |
39b01b2794 | ||
![]() |
36f7099354 |
@ -24,6 +24,7 @@ message SS_CMKcpHandshake
|
||||
optional string account_id = 2; //账号id
|
||||
optional string session_id = 3; //session id
|
||||
optional string team_uuid = 4; //保留
|
||||
optional int32 secret_key_place = 5; //私钥存放位置 0:存在用户协议前(老) 1:存在kcp底层协议头之后(新)
|
||||
}
|
||||
|
||||
message SS_SMKcpHandshake
|
||||
|
@ -1,6 +1,7 @@
|
||||
#include "precompile.h"
|
||||
|
||||
#include <memory.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <f8/netmsghandler.h>
|
||||
#include <f8/udplog.h>
|
||||
@ -33,23 +34,22 @@ KcpSession::~KcpSession()
|
||||
|
||||
}
|
||||
|
||||
void KcpSession::Init(int socket_handle)
|
||||
void KcpSession::Init(int socket_handle, int secret_key_place)
|
||||
{
|
||||
socket_handle_ = socket_handle;
|
||||
secret_key_ = App::Instance()->NewUuid();
|
||||
kcp_ = ikcp_create(socket_handle_, (void*)this);
|
||||
#if 1
|
||||
const KcpConf& kcp_conf = JsonDataMgr::Instance()->GetKcpConf();
|
||||
ikcp_wndsize(kcp_, kcp_conf.sndwnd, kcp_conf.rcvwnd);
|
||||
ikcp_nodelay(kcp_, kcp_conf.nodelay, kcp_conf.interval, kcp_conf.resend, kcp_conf.nc);
|
||||
kcp_->rx_minrto = kcp_conf.rx_minrto;
|
||||
kcp_->fastresend = kcp_conf.fastresend;
|
||||
#else
|
||||
ikcp_wndsize(kcp_, 128, 128);
|
||||
ikcp_nodelay(kcp_, 2, 10, 2, 1);
|
||||
kcp_->rx_minrto = 10;
|
||||
kcp_->fastresend = 1;
|
||||
#endif
|
||||
secret_key_place_ = secret_key_place;
|
||||
if (secret_key_place_ > 0) {
|
||||
secret_key_place_ = 1;
|
||||
} else if (secret_key_place_ < 0) {
|
||||
secret_key_place_ = 0;
|
||||
}
|
||||
kcp_->output = UdpOutput;
|
||||
init_tick_ = a8::XGetTickCount();
|
||||
}
|
||||
@ -75,8 +75,21 @@ void KcpSession::SendClientMsg(char* buf, int buf_len)
|
||||
|
||||
void KcpSession::OnRecvPacket(a8::UdpPacket* pkt)
|
||||
{
|
||||
const int IKCP_OVERHEAD = 24;
|
||||
|
||||
remote_addr_ = pkt->remote_addr;
|
||||
ikcp_input(kcp_, pkt->buf, pkt->buf_len);
|
||||
if (GetSecretKeyPlace()) {
|
||||
char* buf = (char*)malloc(pkt->buf_len - GetSecretKeyLen());
|
||||
int buflen = pkt->buf_len - GetSecretKeyLen();
|
||||
memmove(buf, pkt->buf, IKCP_OVERHEAD);
|
||||
if (pkt->buf_len > IKCP_OVERHEAD + GetSecretKeyLen()) {
|
||||
memmove(buf + IKCP_OVERHEAD, pkt->buf + IKCP_OVERHEAD + GetSecretKeyLen(), buflen - IKCP_OVERHEAD);
|
||||
}
|
||||
ikcp_input(kcp_, buf, buflen);
|
||||
free(buf);
|
||||
} else {
|
||||
ikcp_input(kcp_, pkt->buf, pkt->buf_len);
|
||||
}
|
||||
}
|
||||
|
||||
void KcpSession::UpdateInput()
|
||||
@ -91,8 +104,20 @@ void KcpSession::UpdateInput()
|
||||
|
||||
void KcpSession::DecodeUserPacket(char* buf, int& offset, unsigned int buflen)
|
||||
{
|
||||
//packagelen + msgid + magiccode + msgbody
|
||||
//2 + 2 + 4+ xx + \0 + xx
|
||||
if (GetSecretKeyPlace()) {
|
||||
DecodeUserPacketNew(buf, offset, buflen);
|
||||
} else {
|
||||
DecodeUserPacketOld(buf, offset, buflen);
|
||||
}
|
||||
}
|
||||
|
||||
int KcpSession::GetSecretKeyPlace()
|
||||
{
|
||||
return secret_key_place_;
|
||||
}
|
||||
|
||||
void KcpSession::DecodeUserPacketOld(char* buf, int& offset, unsigned int buflen)
|
||||
{
|
||||
bool warning = false;
|
||||
while (buflen - offset >= sizeof(f8::PackHead) + GetSecretKeyLen()) {
|
||||
long long secret_key = KcpSession::ReadSecretKey(&buf[offset], buflen);
|
||||
@ -124,6 +149,37 @@ void KcpSession::DecodeUserPacket(char* buf, int& offset, unsigned int buflen)
|
||||
}
|
||||
|
||||
if (warning) {
|
||||
f8::UdpLog::Instance()->Warning("收到kcp client非法数据包", {});
|
||||
f8::UdpLog::Instance()->Warning("收到kcp client非法数据包1", {});
|
||||
}
|
||||
}
|
||||
|
||||
void KcpSession::DecodeUserPacketNew(char* buf, int& offset, unsigned int buflen)
|
||||
{
|
||||
bool warning = false;
|
||||
while (buflen - offset >= sizeof(f8::PackHead)) {
|
||||
f8::PackHead* p = (f8::PackHead*)&buf[offset];
|
||||
if (p->magic_code == f8::MAGIC_CODE) {
|
||||
if (buflen - offset < sizeof(f8::PackHead) + p->packlen) {
|
||||
break;
|
||||
}
|
||||
App::Instance()->AddSocketMsg(SF_Client,
|
||||
socket_handle_,
|
||||
0,
|
||||
//saddr,
|
||||
p->msgid,
|
||||
p->seqid,
|
||||
&buf[offset + sizeof(f8::PackHead)],
|
||||
p->packlen,
|
||||
ST_Udp);
|
||||
offset += sizeof(f8::PackHead) + p->packlen;
|
||||
} else {
|
||||
warning = true;
|
||||
offset++;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (warning) {
|
||||
f8::UdpLog::Instance()->Warning("收到kcp client非法数据包2", {});
|
||||
}
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ public:
|
||||
KcpSession();
|
||||
~KcpSession();
|
||||
|
||||
void Init(int socket_handle);
|
||||
void Init(int socket_handle, int secret_key_place);
|
||||
void UnInit();
|
||||
void Update(long long tick);
|
||||
|
||||
@ -19,6 +19,7 @@ public:
|
||||
int GetSocketHandle() { return socket_handle_; }
|
||||
long long GetSecretKey() { return secret_key_; }
|
||||
void* GetSecretKeyDataPtr() { return &secret_key_; }
|
||||
int GetSecretKeyPlace();
|
||||
|
||||
void SendClientMsg(char* buf, int buf_len);
|
||||
virtual void OnRecvPacket(a8::UdpPacket* pkt) override;
|
||||
@ -31,11 +32,14 @@ public:
|
||||
|
||||
protected:
|
||||
virtual void DecodeUserPacket(char* buf, int& offset, unsigned int buflen) override;
|
||||
void DecodeUserPacketOld(char* buf, int& offset, unsigned int buflen);
|
||||
void DecodeUserPacketNew(char* buf, int& offset, unsigned int buflen);
|
||||
|
||||
private:
|
||||
void UpdateInput();
|
||||
|
||||
private:
|
||||
int secret_key_place_ = 0;
|
||||
int socket_handle_ = 0;
|
||||
long long secret_key_ = 0;
|
||||
ikcpcb* kcp_ = nullptr;
|
||||
|
@ -5,10 +5,13 @@
|
||||
#include "longsession.h"
|
||||
#include "kcpsession.h"
|
||||
|
||||
#include "ss_msgid.pb.h"
|
||||
#include "ss_proto.pb.h"
|
||||
|
||||
void LongSession::Init(f8::MsgHdr& hdr, const ss::SS_CMKcpHandshake& msg)
|
||||
{
|
||||
kcp_session_ = std::make_shared<KcpSession>();
|
||||
kcp_session_->Init(hdr.socket_handle);
|
||||
kcp_session_->Init(hdr.socket_handle, msg.secret_key_place());
|
||||
}
|
||||
|
||||
void LongSession::UnInit()
|
||||
@ -25,3 +28,8 @@ void LongSession::UpdatePing()
|
||||
{
|
||||
last_ping_tick_ = a8::XGetTickCount();
|
||||
}
|
||||
|
||||
int LongSession::GetSecretKeyPlace()
|
||||
{
|
||||
return kcp_session_->GetSecretKeyPlace();
|
||||
}
|
||||
|
@ -17,6 +17,7 @@ class LongSession
|
||||
|
||||
std::shared_ptr<KcpSession> GetKcpSession() { return kcp_session_; }
|
||||
void UpdatePing();
|
||||
int GetSecretKeyPlace();
|
||||
|
||||
private:
|
||||
long long last_ping_tick_ = 0;
|
||||
|
@ -133,30 +133,31 @@ void LongSessionMgr::ProcUdpPacket(a8::UdpPacket* pkt)
|
||||
});
|
||||
return;
|
||||
}
|
||||
session->GetKcpSession()->OnRecvPacket(pkt);
|
||||
#if 0
|
||||
if (pkt->buf_len > IKCP_OVERHEAD + KcpSession::GetSecretKeyLen()) {
|
||||
long long secret_key = KcpSession::ReadSecretKey(pkt->buf + IKCP_OVERHEAD, pkt->buf_len);
|
||||
if (secret_key == session->GetKcpSession()->GetSecretKey()) {
|
||||
session->GetKcpSession()->OnRecvPacket(pkt);
|
||||
if (session->GetSecretKeyPlace()) {
|
||||
if (pkt->buf_len >= IKCP_OVERHEAD + KcpSession::GetSecretKeyLen()) {
|
||||
long long secret_key = KcpSession::ReadSecretKey(pkt->buf + IKCP_OVERHEAD, pkt->buf_len);
|
||||
if (secret_key == session->GetKcpSession()->GetSecretKey()) {
|
||||
session->GetKcpSession()->OnRecvPacket(pkt);
|
||||
} else {
|
||||
f8::UdpLog::Instance()->Warning("ProcUdpPacket host:%s port:%d socket_handle%d secret_key:%d secret_error",
|
||||
{
|
||||
inet_ntoa(pkt->remote_addr.sin_addr),
|
||||
pkt->remote_addr.sin_port,
|
||||
socket_handle,
|
||||
secret_key
|
||||
});
|
||||
}
|
||||
} else {
|
||||
f8::UdpLog::Instance()->Warning("ProcUdpPacket host:%s port:%d socket_handle%d secret_key:%d secret_error",
|
||||
f8::UdpLog::Instance()->Warning("ProcUdpPacket host:%s port:%d buflen:%d bufflen_error",
|
||||
{
|
||||
inet_ntoa(pkt->remote_addr.sin_addr),
|
||||
pkt->remote_addr.sin_port,
|
||||
socket_handle,
|
||||
secret_key
|
||||
pkt->buf_len
|
||||
});
|
||||
}
|
||||
} else {
|
||||
f8::UdpLog::Instance()->Warning("ProcUdpPacket host:%s port:%d buflen:%d bufflen_error",
|
||||
{
|
||||
inet_ntoa(pkt->remote_addr.sin_addr),
|
||||
pkt->remote_addr.sin_port,
|
||||
pkt->buf_len
|
||||
});
|
||||
session->GetKcpSession()->OnRecvPacket(pkt);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void LongSessionMgr::DelSession(int socket_handle)
|
||||
|
Loading…
x
Reference in New Issue
Block a user