抓包未解析透传
This commit is contained in:
1 parent
b46a4a2ef1
commit
aaae9fab34
7 files changed
+119
-74
No files matched your search
+15
-6
@@ -3,11 +3,6 @@ package main
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"hk4e/cmd/nats"
|
||||
cfg "hk4e/common/config"
|
||||
dispatchapp "hk4e/dispatch/app"
|
||||
@@ -16,6 +11,11 @@ import (
|
||||
gsapp "hk4e/gs/app"
|
||||
nodeapp "hk4e/node/app"
|
||||
"hk4e/pkg/logger"
|
||||
robotapp "hk4e/robot/app"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -41,6 +41,7 @@ func main() {
|
||||
ctxGate, cancelGate := context.WithCancel(context.Background())
|
||||
ctxGs, cancelGs := context.WithCancel(context.Background())
|
||||
ctxGm, cancelGm := context.WithCancel(context.Background())
|
||||
ctxRobot, cancelRobot := context.WithCancel(context.Background())
|
||||
|
||||
go func() {
|
||||
err := nats.RunNatsServer(ctxNats)
|
||||
@@ -100,6 +101,14 @@ func main() {
|
||||
cancelGs()
|
||||
}()
|
||||
|
||||
go func() {
|
||||
err := robotapp.Run(ctxRobot)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
cancelGm()
|
||||
}()
|
||||
|
||||
c := make(chan os.Signal, 1)
|
||||
signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
|
||||
for {
|
||||
@@ -108,7 +117,7 @@ func main() {
|
||||
logger.Warn("get a signal %s", s.String())
|
||||
switch s {
|
||||
case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT:
|
||||
cancelGm()
|
||||
cancelRobot()
|
||||
<-stopChan
|
||||
return
|
||||
case syscall.SIGHUP:
|
||||
|
||||
+17
-11
@@ -150,18 +150,24 @@ func (m *MessageQueue) parseNetMsg(rawData []byte) *NetMsg {
|
||||
return nil
|
||||
}
|
||||
if netMsg.EventId == NormalMsg {
|
||||
// protobuf PayloadMessage
|
||||
payloadMessage := m.cmdProtoMap.GetProtoObjFastNewByCmdId(gameMsg.CmdId)
|
||||
if payloadMessage == nil {
|
||||
logger.Error("get protobuf obj by cmd id error: %v", err)
|
||||
return nil
|
||||
if !gameMsg.NotParse {
|
||||
// protobuf PayloadMessage
|
||||
payloadMessage := m.cmdProtoMap.GetProtoObjFastNewByCmdId(gameMsg.CmdId)
|
||||
if payloadMessage == nil {
|
||||
logger.Error("get protobuf obj by cmd id error: %v", err)
|
||||
return nil
|
||||
}
|
||||
err = pb.Unmarshal(gameMsg.PayloadMessageData, payloadMessage)
|
||||
if err != nil {
|
||||
logger.Error("parse bin to payload msg error: %v", err)
|
||||
return nil
|
||||
}
|
||||
gameMsg.PayloadMessage = payloadMessage
|
||||
} else {
|
||||
payloadMessageData := make([]byte, len(gameMsg.PayloadMessageData))
|
||||
copy(payloadMessageData, gameMsg.PayloadMessageData)
|
||||
gameMsg.PayloadMessageData = payloadMessageData
|
||||
}
|
||||
err = pb.Unmarshal(gameMsg.PayloadMessageData, payloadMessage)
|
||||
if err != nil {
|
||||
logger.Error("parse bin to payload msg error: %v", err)
|
||||
return nil
|
||||
}
|
||||
gameMsg.PayloadMessage = payloadMessage
|
||||
}
|
||||
}
|
||||
return netMsg
|
||||
|
||||
@@ -31,6 +31,7 @@ type GameMsg struct {
|
||||
ClientSeq uint32
|
||||
PayloadMessage pb.Message `msgpack:"-"`
|
||||
PayloadMessageData []byte
|
||||
NotParse bool
|
||||
}
|
||||
|
||||
const (
|
||||
|
||||
@@ -2,7 +2,6 @@ package net
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
"strings"
|
||||
@@ -26,8 +25,7 @@ type ProtoMsg struct {
|
||||
HeadMessage *proto.PacketHead
|
||||
PayloadMessage pb.Message
|
||||
NotParse bool
|
||||
HeadRaw string
|
||||
PayloadRaw string
|
||||
PayloadRaw []byte
|
||||
}
|
||||
|
||||
type ProtoMessage struct {
|
||||
@@ -39,14 +37,20 @@ func ProtoDecode(kcpMsg *KcpMsg,
|
||||
serverCmdProtoMap *cmd.CmdProtoMap, clientCmdProtoMap *client_proto.ClientCmdProtoMap) (protoMsgList []*ProtoMsg) {
|
||||
protoMsgList = make([]*ProtoMsg, 0)
|
||||
notParseDumpRaw := func() {
|
||||
headMsg := new(proto.PacketHead)
|
||||
err := pb.Unmarshal(kcpMsg.HeadData, headMsg)
|
||||
if err != nil {
|
||||
logger.Error("unmarshal head data err: %v", err)
|
||||
}
|
||||
payloadRaw := make([]byte, len(kcpMsg.ProtoData))
|
||||
copy(payloadRaw, kcpMsg.ProtoData)
|
||||
protoMsgList = append(protoMsgList, &ProtoMsg{
|
||||
SessionId: kcpMsg.SessionId,
|
||||
CmdId: kcpMsg.CmdId,
|
||||
HeadMessage: nil,
|
||||
HeadMessage: headMsg,
|
||||
PayloadMessage: nil,
|
||||
NotParse: true,
|
||||
HeadRaw: base64.StdEncoding.EncodeToString(kcpMsg.HeadData),
|
||||
PayloadRaw: base64.StdEncoding.EncodeToString(kcpMsg.ProtoData),
|
||||
PayloadRaw: payloadRaw,
|
||||
})
|
||||
}
|
||||
if config.GetConfig().Hk4e.ClientProtoProxyEnable {
|
||||
@@ -256,6 +260,10 @@ func ProtoEncode(protoMsg *ProtoMsg,
|
||||
} else {
|
||||
kcpMsg.HeadData = nil
|
||||
}
|
||||
if protoMsg.NotParse {
|
||||
kcpMsg.ProtoData = protoMsg.PayloadRaw
|
||||
return kcpMsg
|
||||
}
|
||||
if protoMsg.CmdId == cmd.UnionCmdNotify && config.GetConfig().Hk4e.ForwardModeEnable && config.GetConfig().Hk4e.ClientProtoProxyEnable {
|
||||
// 处理聚合消息
|
||||
unionCmdNotify, ok := protoMsg.PayloadMessage.(*proto.UnionCmdNotify)
|
||||
|
||||
+14
-7
@@ -349,15 +349,20 @@ func (k *KcpConnManager) forwardClientMsgToRobotHandle(protoMsg *ProtoMsg, sessi
|
||||
CmdId: protoMsg.CmdId,
|
||||
ClientSeq: protoMsg.HeadMessage.ClientSequenceId,
|
||||
PayloadMessageData: nil,
|
||||
NotParse: protoMsg.NotParse,
|
||||
}
|
||||
// 在这里直接序列化成二进制数据 终结PayloadMessage的生命周期并回收进缓存池
|
||||
payloadMessageData, err := pb.Marshal(protoMsg.PayloadMessage)
|
||||
if err != nil {
|
||||
logger.Error("parse payload msg to bin error: %v, stack: %v", err, logger.Stack())
|
||||
return
|
||||
if !protoMsg.NotParse {
|
||||
// 在这里直接序列化成二进制数据 终结PayloadMessage的生命周期并回收进缓存池
|
||||
payloadMessageData, err := pb.Marshal(protoMsg.PayloadMessage)
|
||||
if err != nil {
|
||||
logger.Error("parse payload msg to bin error: %v, stack: %v", err, logger.Stack())
|
||||
return
|
||||
}
|
||||
k.serverCmdProtoMap.PutProtoObjCache(protoMsg.CmdId, protoMsg.PayloadMessage)
|
||||
gameMsg.PayloadMessageData = payloadMessageData
|
||||
} else {
|
||||
gameMsg.PayloadMessageData = protoMsg.PayloadRaw
|
||||
}
|
||||
k.serverCmdProtoMap.PutProtoObjCache(protoMsg.CmdId, protoMsg.PayloadMessage)
|
||||
gameMsg.PayloadMessageData = payloadMessageData
|
||||
// 转发到Robot
|
||||
k.messageQueue.SendToRobot(session.robotServerAppId, &mq.NetMsg{
|
||||
MsgType: mq.MsgTypeGame,
|
||||
@@ -409,6 +414,8 @@ func (k *KcpConnManager) forwardRobotMsgToClientHandle(session *Session) {
|
||||
CmdId: gameMsg.CmdId,
|
||||
HeadMessage: k.getHeadMsg(gameMsg.ClientSeq),
|
||||
PayloadMessage: gameMsg.PayloadMessage,
|
||||
NotParse: gameMsg.NotParse,
|
||||
PayloadRaw: gameMsg.PayloadMessageData,
|
||||
}
|
||||
if len(session.sendChan) == SessionSendChanLen {
|
||||
logger.Error("session send chan is full, sessionId: %v", protoMsg.SessionId)
|
||||
|
||||
+16
-7
@@ -272,7 +272,11 @@ func forwardLoop(session *net.Session, messageQueue *mq.MessageQueue, gateAppId
|
||||
req.ChecksumClientVersion = config.GetConfig().Hk4eRobot.ForwardChecksumClientVersion
|
||||
}
|
||||
}
|
||||
session.SendMsgFwd(gameMsg.CmdId, gameMsg.ClientSeq, gameMsg.PayloadMessage)
|
||||
if !gameMsg.NotParse {
|
||||
session.SendMsgFwd(gameMsg.CmdId, gameMsg.ClientSeq, gameMsg.PayloadMessage)
|
||||
} else {
|
||||
session.SendMsgRaw(gameMsg.CmdId, gameMsg.ClientSeq, gameMsg.PayloadMessageData)
|
||||
}
|
||||
case mq.MsgTypeServer:
|
||||
switch netMsg.EventId {
|
||||
case mq.ServerForwardModeClientCloseNotify:
|
||||
@@ -287,13 +291,18 @@ func forwardLoop(session *net.Session, messageQueue *mq.MessageQueue, gateAppId
|
||||
if protoMsg.HeadMessage != nil {
|
||||
gameMsg.ClientSeq = protoMsg.HeadMessage.ClientSequenceId
|
||||
}
|
||||
// 在这里直接序列化成二进制数据 防止发送的消息内包含各种游戏数据指针 而造成并发读写的问题
|
||||
payloadMessageData, err := pb.Marshal(protoMsg.PayloadMessage)
|
||||
if err != nil {
|
||||
logger.Error("parse payload msg to bin error: %v", err)
|
||||
continue
|
||||
if !protoMsg.NotParse {
|
||||
// 在这里直接序列化成二进制数据 防止发送的消息内包含各种游戏数据指针 而造成并发读写的问题
|
||||
payloadMessageData, err := pb.Marshal(protoMsg.PayloadMessage)
|
||||
if err != nil {
|
||||
logger.Error("parse payload msg to bin error: %v", err)
|
||||
continue
|
||||
}
|
||||
gameMsg.PayloadMessageData = payloadMessageData
|
||||
} else {
|
||||
gameMsg.PayloadMessageData = protoMsg.PayloadRaw
|
||||
gameMsg.NotParse = true
|
||||
}
|
||||
gameMsg.PayloadMessageData = payloadMessageData
|
||||
messageQueue.SendToGate(gateAppId, &mq.NetMsg{
|
||||
MsgType: mq.MsgTypeGame,
|
||||
EventId: mq.NormalMsg,
|
||||
|
||||
+42
-37
@@ -1,6 +1,7 @@
|
||||
package net
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
@@ -27,7 +28,6 @@ type Packet struct {
|
||||
HeadMsg string `json:"head_msg"`
|
||||
PayloadMsg string `json:"payload_msg"`
|
||||
NotParse bool `json:"not_parse"`
|
||||
HeadRaw string `json:"head_raw"`
|
||||
PayloadRaw string `json:"payload_raw"`
|
||||
}
|
||||
|
||||
@@ -110,6 +110,21 @@ func (s *Session) SendMsgFwd(cmdId uint16, clientSeq uint32, msg pb.Message) {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Session) SendMsgRaw(cmdId uint16, clientSeq uint32, raw []byte) {
|
||||
s.SendChan <- &hk4egatenet.ProtoMsg{
|
||||
SessionId: 0,
|
||||
CmdId: cmdId,
|
||||
HeadMessage: &proto.PacketHead{
|
||||
ClientSequenceId: clientSeq,
|
||||
SentMs: uint64(time.Now().UnixMilli()),
|
||||
EnetIsReliable: 1,
|
||||
},
|
||||
PayloadMessage: nil,
|
||||
NotParse: true,
|
||||
PayloadRaw: raw,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Session) Close() {
|
||||
if s.IsClose {
|
||||
return
|
||||
@@ -138,41 +153,24 @@ func (s *Session) recvHandle() {
|
||||
for _, v := range kcpMsgList {
|
||||
protoMsgList := hk4egatenet.ProtoDecode(v, s.ServerCmdProtoMap, s.ClientCmdProtoMap)
|
||||
for _, vv := range protoMsgList {
|
||||
if vv.NotParse {
|
||||
packet := &Packet{
|
||||
Time: uint64(time.Now().UnixMilli()),
|
||||
Dir: "RECV",
|
||||
CmdId: uint32(vv.CmdId),
|
||||
CmdName: "",
|
||||
HeadMsg: "",
|
||||
PayloadMsg: "",
|
||||
NotParse: true,
|
||||
HeadRaw: vv.HeadRaw,
|
||||
PayloadRaw: vv.PayloadRaw,
|
||||
}
|
||||
packetData, _ := json.Marshal(packet)
|
||||
s.PktLock.Lock()
|
||||
s.PktList = append(s.PktList, packet)
|
||||
if s.PktCapWsConn != nil {
|
||||
err := s.PktCapWsConn.WriteMessage(websocket.TextMessage, packetData)
|
||||
if err != nil {
|
||||
s.PktCapWsConn = nil
|
||||
}
|
||||
}
|
||||
s.PktLock.Unlock()
|
||||
continue
|
||||
}
|
||||
s.RecvChan <- vv
|
||||
cmdName := string(vv.PayloadMessage.ProtoReflect().Descriptor().FullName())
|
||||
headMsg, _ := json.Marshal(vv.HeadMessage)
|
||||
payloadMsg, _ := json.Marshal(vv.PayloadMessage)
|
||||
packet := &Packet{
|
||||
Time: uint64(time.Now().UnixMilli()),
|
||||
Dir: "RECV",
|
||||
CmdId: uint32(vv.CmdId),
|
||||
CmdName: cmdName,
|
||||
HeadMsg: string(headMsg),
|
||||
PayloadMsg: string(payloadMsg),
|
||||
CmdName: "",
|
||||
HeadMsg: "",
|
||||
PayloadMsg: "",
|
||||
NotParse: vv.NotParse,
|
||||
PayloadRaw: base64.StdEncoding.EncodeToString(vv.PayloadRaw),
|
||||
}
|
||||
headMsg, _ := json.Marshal(vv.HeadMessage)
|
||||
packet.HeadMsg = string(headMsg)
|
||||
if !vv.NotParse {
|
||||
cmdName := string(vv.PayloadMessage.ProtoReflect().Descriptor().FullName())
|
||||
payloadMsg, _ := json.Marshal(vv.PayloadMessage)
|
||||
packet.CmdName = cmdName
|
||||
packet.PayloadMsg = string(payloadMsg)
|
||||
}
|
||||
packetData, _ := json.Marshal(packet)
|
||||
s.PktLock.Lock()
|
||||
@@ -213,16 +211,23 @@ func (s *Session) sendHandle() {
|
||||
s.Close()
|
||||
break
|
||||
}
|
||||
cmdName := string(protoMsg.PayloadMessage.ProtoReflect().Descriptor().FullName())
|
||||
headMsg, _ := json.Marshal(protoMsg.HeadMessage)
|
||||
payloadMsg, _ := json.Marshal(protoMsg.PayloadMessage)
|
||||
packet := &Packet{
|
||||
Time: uint64(time.Now().UnixMilli()),
|
||||
Dir: "SEND",
|
||||
CmdId: uint32(protoMsg.CmdId),
|
||||
CmdName: cmdName,
|
||||
HeadMsg: string(headMsg),
|
||||
PayloadMsg: string(payloadMsg),
|
||||
CmdName: "",
|
||||
HeadMsg: "",
|
||||
PayloadMsg: "",
|
||||
NotParse: protoMsg.NotParse,
|
||||
PayloadRaw: base64.StdEncoding.EncodeToString(protoMsg.PayloadRaw),
|
||||
}
|
||||
headMsg, _ := json.Marshal(protoMsg.HeadMessage)
|
||||
packet.HeadMsg = string(headMsg)
|
||||
if !protoMsg.NotParse {
|
||||
cmdName := string(protoMsg.PayloadMessage.ProtoReflect().Descriptor().FullName())
|
||||
payloadMsg, _ := json.Marshal(protoMsg.PayloadMessage)
|
||||
packet.CmdName = cmdName
|
||||
packet.PayloadMsg = string(payloadMsg)
|
||||
}
|
||||
packetData, _ := json.Marshal(packet)
|
||||
s.PktLock.Lock()
|
||||
|
||||
Reference in new issue
Block a user