正确的KCP协议sessionId和conv解析
This commit is contained in:
1 parent
4f033f5501
commit
2b140361ca
9 files changed
+125
-33
No files matched your search
Binary file not shown.
@@ -0,0 +1,92 @@
|
||||
-- 原神KCP协议
|
||||
hk4e_kcp_protocol = Proto("HK4E_KCP", "Genshin Impact KCP Protocol")
|
||||
|
||||
-- 协议字段
|
||||
sess = ProtoField.uint32("hk4e_kcp.sess", "sess", base.DEC)
|
||||
conv = ProtoField.uint32("hk4e_kcp.conv", "conv", base.DEC)
|
||||
cmd = ProtoField.uint8("hk4e_kcp.cmd", "cmd", base.DEC)
|
||||
frg = ProtoField.uint8("hk4e_kcp.frg", "frg", base.DEC)
|
||||
wnd = ProtoField.uint16("hk4e_kcp.wnd", "wnd", base.DEC)
|
||||
ts = ProtoField.uint32("hk4e_kcp.ts", "ts", base.DEC)
|
||||
sn = ProtoField.uint32("hk4e_kcp.sn", "sn", base.DEC)
|
||||
una = ProtoField.uint32("hk4e_kcp.una", "una", base.DEC)
|
||||
len = ProtoField.uint32("hk4e_kcp.len", "len", base.DEC)
|
||||
data = ProtoField.bytes("hk4e_kcp.data", "data", base.NONE)
|
||||
|
||||
hk4e_kcp_protocol.fields = { sess, conv, cmd, frg, wnd, ts, sn, una, len, data }
|
||||
|
||||
-- 解析器
|
||||
function hk4e_kcp_protocol.dissector(buffer, pinfo, tree)
|
||||
length = buffer:len()
|
||||
if length == 0 then
|
||||
return
|
||||
end
|
||||
|
||||
pinfo.cols.protocol = hk4e_kcp_protocol.name
|
||||
|
||||
if length == 20 then
|
||||
local enet_sess = buffer(4, 4):uint()
|
||||
local enet_conv = buffer(8, 4):uint()
|
||||
local enet_type = buffer(12, 4):uint()
|
||||
pinfo.cols.info = string.format(" [ENET] Sess=%d Conv=%d Type=%d", enet_sess, enet_conv, enet_type)
|
||||
return
|
||||
end
|
||||
|
||||
local first_sn = buffer(16, 4):le_uint()
|
||||
local first_una = buffer(20, 4):le_uint()
|
||||
local first_len = buffer(24, 4):le_uint()
|
||||
local first_cmd_name = get_cmd_name(buffer(8, 1):le_uint())
|
||||
local info = string.format(" [%s] Sn=%d Una=%d Len=%d", first_cmd_name, first_sn, first_una, first_len)
|
||||
|
||||
pinfo.cols.info = info
|
||||
|
||||
-- 解析多个KCP包
|
||||
local offset = 0
|
||||
while offset < buffer:len() do
|
||||
local sess_buf = buffer(offset + 0, 4)
|
||||
local conv_buf = buffer(offset + 4, 4)
|
||||
local cmd_buf = buffer(offset + 8, 1)
|
||||
local wnd_buf = buffer(offset + 10, 2)
|
||||
local sn_buf = buffer(offset + 16, 4)
|
||||
local len_buf = buffer(offset + 24, 4)
|
||||
|
||||
local cmd_name = get_cmd_name(cmd_buf:le_uint())
|
||||
|
||||
local tree_title = string.format("Genshin Impact KCP Protocol")
|
||||
local subtree = tree:add(hk4e_kcp_protocol, buffer(), tree_title)
|
||||
subtree:add_le(sess, sess_buf)
|
||||
subtree:add_le(conv, conv_buf)
|
||||
subtree:add_le(cmd, cmd_buf):append_text(" (" .. cmd_name .. ")")
|
||||
subtree:add_le(frg, buffer(offset + 9, 1))
|
||||
subtree:add_le(wnd, wnd_buf)
|
||||
subtree:add_le(ts, buffer(offset + 12, 4))
|
||||
subtree:add_le(sn, sn_buf)
|
||||
subtree:add_le(una, buffer(offset + 20, 4))
|
||||
subtree:add_le(len, len_buf)
|
||||
|
||||
local data_len = len_buf:le_uint()
|
||||
if data_len ~= 0 then
|
||||
local data_buf = buffer(offset + 28, data_len)
|
||||
subtree:add(data, data_buf)
|
||||
end
|
||||
|
||||
offset = offset + 28 + data_len
|
||||
end
|
||||
end
|
||||
|
||||
function get_cmd_name(cmd_val)
|
||||
if cmd_val == 81 then
|
||||
return "PSH"
|
||||
elseif cmd_val == 82 then
|
||||
return "ACK"
|
||||
elseif cmd_val == 83 then
|
||||
return "ASK"
|
||||
elseif cmd_val == 84 then
|
||||
return "TELL"
|
||||
end
|
||||
end
|
||||
|
||||
-- 注册到UDP
|
||||
local udp_port = DissectorTable.get("udp.port")
|
||||
-- 注册到22222端口
|
||||
udp_port:add(22222, hk4e_kcp_protocol)
|
||||
+4
-4
@@ -8,17 +8,17 @@ import (
|
||||
)
|
||||
|
||||
// Enet连接控制协议
|
||||
// MM MM MM MM | CC CC CC CC | SS SS SS SS | EE EE EE EE | MM MM MM MM
|
||||
// MM MM MM MM | SS SS SS SS | CC CC CC CC | EE EE EE EE | MM MM MM MM
|
||||
// MM为表示连接状态的幻数 在开头的4字节和结尾的4字节
|
||||
// CC为conv 4字节
|
||||
// SS为sessionId 4字节
|
||||
// CC为conv 4字节
|
||||
// EE为Enet事件类型 4字节
|
||||
|
||||
// Enet Enet协议上报结构体
|
||||
type Enet struct {
|
||||
Addr string
|
||||
Conv uint32
|
||||
SessionId uint32
|
||||
Conv uint32
|
||||
ConnType uint8
|
||||
EnetType uint32
|
||||
}
|
||||
@@ -89,7 +89,7 @@ func BuildEnet(connType uint8, enetType uint32, sessionId uint32, conv uint32) [
|
||||
func ParseEnet(data []byte) (connType uint8, enetType uint32, sessionId uint32, conv uint32, rawConv uint64, err error) {
|
||||
sessionId = binary.BigEndian.Uint32(data[4:8])
|
||||
conv = binary.BigEndian.Uint32(data[8:12])
|
||||
rawConv = binary.BigEndian.Uint64(data[4:12])
|
||||
rawConv = binary.LittleEndian.Uint64(data[4:12])
|
||||
// 提取Enet协议头部和尾部幻数
|
||||
udpPayloadEnetHead := data[:4]
|
||||
udpPayloadEnetTail := data[len(data)-4:]
|
||||
|
||||
+11
-7
@@ -561,14 +561,18 @@ func (s *UDPSession) GetRawConv() uint64 {
|
||||
return s.kcp.conv
|
||||
}
|
||||
|
||||
// GetConv 获取KCP会话id
|
||||
func (s *UDPSession) GetConv() uint32 {
|
||||
return uint32(s.kcp.conv >> 32)
|
||||
}
|
||||
|
||||
// GetSessionId 获取会话id
|
||||
func (s *UDPSession) GetSessionId() uint32 {
|
||||
return uint32(s.kcp.conv >> 0)
|
||||
rawConvData := make([]byte, 8)
|
||||
binary.LittleEndian.PutUint64(rawConvData, s.kcp.conv)
|
||||
return binary.LittleEndian.Uint32(rawConvData[0:4])
|
||||
}
|
||||
|
||||
// GetConv 获取KCP会话id
|
||||
func (s *UDPSession) GetConv() uint32 {
|
||||
rawConvData := make([]byte, 8)
|
||||
binary.LittleEndian.PutUint64(rawConvData, s.kcp.conv)
|
||||
return binary.LittleEndian.Uint32(rawConvData[4:8])
|
||||
}
|
||||
|
||||
// GetRTO gets current rto of the session
|
||||
@@ -913,8 +917,8 @@ func DialWithOptions(raddr string) (*UDPSession, error) {
|
||||
}
|
||||
enet := &Enet{
|
||||
Addr: udpaddr.String(),
|
||||
Conv: 0,
|
||||
SessionId: 0,
|
||||
Conv: 0,
|
||||
ConnType: ConnEnetSyn,
|
||||
EnetType: EnetClientConnectKey,
|
||||
}
|
||||
|
||||
@@ -67,8 +67,8 @@ func (l *Listener) defaultMonitor() {
|
||||
// 客户端前置握手获取conv
|
||||
l.EnetNotify <- &Enet{
|
||||
Addr: from.String(),
|
||||
Conv: conv,
|
||||
SessionId: sessionId,
|
||||
Conv: conv,
|
||||
ConnType: ConnEnetSyn,
|
||||
EnetType: enetType,
|
||||
}
|
||||
@@ -76,8 +76,8 @@ func (l *Listener) defaultMonitor() {
|
||||
// 连接建立
|
||||
l.EnetNotify <- &Enet{
|
||||
Addr: from.String(),
|
||||
Conv: conv,
|
||||
SessionId: sessionId,
|
||||
Conv: conv,
|
||||
ConnType: ConnEnetEst,
|
||||
EnetType: enetType,
|
||||
}
|
||||
@@ -85,8 +85,8 @@ func (l *Listener) defaultMonitor() {
|
||||
// 连接断开
|
||||
l.EnetNotify <- &Enet{
|
||||
Addr: from.String(),
|
||||
Conv: conv,
|
||||
SessionId: sessionId,
|
||||
Conv: conv,
|
||||
ConnType: ConnEnetFin,
|
||||
EnetType: enetType,
|
||||
}
|
||||
@@ -95,9 +95,9 @@ func (l *Listener) defaultMonitor() {
|
||||
}
|
||||
} else {
|
||||
// 正常KCP包
|
||||
conv = binary.BigEndian.Uint32(udpPayload[0:4])
|
||||
sessionId = binary.BigEndian.Uint32(udpPayload[4:8])
|
||||
rawConv = binary.BigEndian.Uint64(udpPayload[0:8])
|
||||
sessionId = binary.LittleEndian.Uint32(udpPayload[0:4])
|
||||
conv = binary.LittleEndian.Uint32(udpPayload[4:8])
|
||||
rawConv = binary.LittleEndian.Uint64(udpPayload[0:8])
|
||||
}
|
||||
l.sessionLock.RLock()
|
||||
conn, exist := l.sessions[rawConv]
|
||||
@@ -108,8 +108,8 @@ func (l *Listener) defaultMonitor() {
|
||||
// 连接地址改变
|
||||
l.EnetNotify <- &Enet{
|
||||
Addr: conn.remote.String(),
|
||||
Conv: conv,
|
||||
SessionId: sessionId,
|
||||
Conv: conv,
|
||||
ConnType: ConnEnetAddrChange,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -126,8 +126,8 @@ func (l *Listener) monitor() {
|
||||
// 客户端前置握手获取conv
|
||||
l.EnetNotify <- &Enet{
|
||||
Addr: msg.Addr.String(),
|
||||
Conv: conv,
|
||||
SessionId: sessionId,
|
||||
Conv: conv,
|
||||
ConnType: ConnEnetSyn,
|
||||
EnetType: enetType,
|
||||
}
|
||||
@@ -135,8 +135,8 @@ func (l *Listener) monitor() {
|
||||
// 连接建立
|
||||
l.EnetNotify <- &Enet{
|
||||
Addr: msg.Addr.String(),
|
||||
Conv: conv,
|
||||
SessionId: sessionId,
|
||||
Conv: conv,
|
||||
ConnType: ConnEnetEst,
|
||||
EnetType: enetType,
|
||||
}
|
||||
@@ -144,8 +144,8 @@ func (l *Listener) monitor() {
|
||||
// 连接断开
|
||||
l.EnetNotify <- &Enet{
|
||||
Addr: msg.Addr.String(),
|
||||
Conv: conv,
|
||||
SessionId: sessionId,
|
||||
Conv: conv,
|
||||
ConnType: ConnEnetFin,
|
||||
EnetType: enetType,
|
||||
}
|
||||
@@ -154,9 +154,9 @@ func (l *Listener) monitor() {
|
||||
}
|
||||
} else {
|
||||
// 正常KCP包
|
||||
conv = binary.BigEndian.Uint32(udpPayload[0:4])
|
||||
sessionId = binary.BigEndian.Uint32(udpPayload[4:8])
|
||||
rawConv = binary.BigEndian.Uint64(udpPayload[0:8])
|
||||
sessionId = binary.LittleEndian.Uint32(udpPayload[0:4])
|
||||
conv = binary.LittleEndian.Uint32(udpPayload[4:8])
|
||||
rawConv = binary.LittleEndian.Uint64(udpPayload[0:8])
|
||||
}
|
||||
l.sessionLock.RLock()
|
||||
conn, exist := l.sessions[rawConv]
|
||||
@@ -167,8 +167,8 @@ func (l *Listener) monitor() {
|
||||
// 连接地址改变
|
||||
l.EnetNotify <- &Enet{
|
||||
Addr: conn.remote.String(),
|
||||
Conv: conv,
|
||||
SessionId: sessionId,
|
||||
Conv: conv,
|
||||
ConnType: ConnEnetAddrChange,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -213,7 +213,6 @@ func (k *KcpConnectManager) acceptHandle(listener *kcp.Listener) {
|
||||
}
|
||||
// 连接建立成功通知
|
||||
k.kcpEventOutput <- &KcpEvent{
|
||||
Conv: conn.GetConv(),
|
||||
SessionId: conn.GetSessionId(),
|
||||
EventId: KcpConnEstNotify,
|
||||
EventMessage: conn.RemoteAddr().String(),
|
||||
@@ -239,8 +238,8 @@ func (k *KcpConnectManager) enetHandle(listener *kcp.Listener) {
|
||||
sessionIdCounter++
|
||||
listener.SendEnetNotifyToPeer(&kcp.Enet{
|
||||
Addr: enetNotify.Addr,
|
||||
Conv: binary.BigEndian.Uint32(random.GetRandomByte(4)),
|
||||
SessionId: sessionIdCounter,
|
||||
Conv: binary.BigEndian.Uint32(random.GetRandomByte(4)),
|
||||
ConnType: kcp.ConnEnetEst,
|
||||
EnetType: enetNotify.EnetType,
|
||||
})
|
||||
@@ -263,7 +262,6 @@ func (k *KcpConnectManager) enetHandle(listener *kcp.Listener) {
|
||||
case kcp.ConnEnetAddrChange:
|
||||
// 连接地址改变通知
|
||||
k.kcpEventOutput <- &KcpEvent{
|
||||
Conv: enetNotify.Conv,
|
||||
SessionId: enetNotify.SessionId,
|
||||
EventId: KcpConnAddrChangeNotify,
|
||||
EventMessage: enetNotify.Addr,
|
||||
@@ -422,7 +420,6 @@ func (k *KcpConnectManager) closeKcpConn(session *Session, enetType uint32) {
|
||||
}
|
||||
// 连接关闭通知
|
||||
k.kcpEventOutput <- &KcpEvent{
|
||||
Conv: conn.GetConv(),
|
||||
SessionId: conn.GetSessionId(),
|
||||
EventId: KcpConnCloseNotify,
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
原神KCP协议(带*为xor加密数据)
|
||||
0 1 2 4 8(字节)
|
||||
+---------------------------------------------------------------------------------------+
|
||||
| conv |
|
||||
| sessionId(le) | conv(le) |
|
||||
+---------------------------------------------------------------------------------------+
|
||||
| cmd | frg | wnd | ts |
|
||||
+---------------------------------------------------------------------------------------+
|
||||
|
||||
@@ -13,7 +13,6 @@ const (
|
||||
)
|
||||
|
||||
type KcpEvent struct {
|
||||
Conv uint32
|
||||
SessionId uint32
|
||||
EventId int
|
||||
EventMessage any
|
||||
@@ -32,8 +31,8 @@ func (k *KcpConnectManager) eventHandle() {
|
||||
// 事件处理
|
||||
for {
|
||||
event := <-k.kcpEventInput
|
||||
logger.Info("kcp manager recv event, Conv: %v, SessionId: %v, EventId: %v, EventMessage: %v",
|
||||
event.Conv, event.SessionId, event.EventId, event.EventMessage)
|
||||
logger.Info("kcp manager recv event, SessionId: %v, EventId: %v, EventMessage: %v",
|
||||
event.SessionId, event.EventId, event.EventMessage)
|
||||
switch event.EventId {
|
||||
case KcpConnForceClose:
|
||||
reason, ok := event.EventMessage.(uint32)
|
||||
|
||||
Reference in new issue
Block a user