diff --git a/common/config/config.go b/common/config/config.go index abca5357..6cf3aeec 100644 --- a/common/config/config.go +++ b/common/config/config.go @@ -57,23 +57,25 @@ type Hk4e struct { // Hk4eRobot 原神机器人 type Hk4eRobot struct { - RegionListUrl string `toml:"region_list_url"` // 一级dispatch地址 - RegionListParam string `toml:"region_list_param"` // 一级dispatch的url参数 - SelectRegionIndex int32 `toml:"select_region_index"` // 选择的二级dispatch索引 - CurRegionUrl string `toml:"cur_region_url"` // 二级dispatch地址 可强制指定 为空则使用一级dispatch获取的地址 - CurRegionParam string `toml:"cur_region_param"` // 二级dispatch的url参数 - KeyId string `toml:"key_id"` // 客户端密钥编号 - LoginSdkUrl string `toml:"login_sdk_url"` // sdk登录服务器地址 - Account string `toml:"account"` // 帐号 - Password string `toml:"password"` // base64编码的rsa公钥加密后的密码 - ClientVersion string `toml:"client_version"` // 客户端版本号 - DosEnable bool `toml:"dos_enable"` // 是否开启压力测试 - DosTotalNum int32 `toml:"dos_total_num"` // 压力测试总并发数量 帐号自动添加后缀编号 - DosBatchNum int32 `toml:"dos_batch_num"` // 压力测试每批登录并发数量 - DosLoopLogin bool `toml:"dos_loop_login"` // 压力测试是否循环登录退出 - ClientMoveEnable bool `toml:"client_move_enable"` // 是否开启客户端模拟移动 - ClientMoveSpeed int32 `toml:"client_move_speed"` // 客户端模拟移动速度 - ClientMoveRangeExt int32 `toml:"client_move_range_ext"` // 客户端模拟移动区域半径 + RegionListUrl string `toml:"region_list_url"` // 一级dispatch地址 + RegionListParam string `toml:"region_list_param"` // 一级dispatch的url参数 + SelectRegionIndex int32 `toml:"select_region_index"` // 选择的二级dispatch索引 + CurRegionUrl string `toml:"cur_region_url"` // 二级dispatch地址 可强制指定 为空则使用一级dispatch获取的地址 + CurRegionParam string `toml:"cur_region_param"` // 二级dispatch的url参数 + KeyId string `toml:"key_id"` // 客户端密钥编号 + LoginSdkUrl string `toml:"login_sdk_url"` // sdk登录服务器地址 + Account string `toml:"account"` // 帐号 + Password string `toml:"password"` // base64编码的rsa公钥加密后的密码 + ClientVersion string `toml:"client_version"` // 客户端版本号 + DosEnable bool `toml:"dos_enable"` // 是否开启压力测试 + DosTotalNum int32 `toml:"dos_total_num"` // 压力测试总并发数量 帐号自动添加后缀编号 + DosBatchNum int32 `toml:"dos_batch_num"` // 压力测试每批登录并发数量 + DosLoopLogin bool `toml:"dos_loop_login"` // 压力测试是否循环登录退出 + ClientMoveEnable bool `toml:"client_move_enable"` // 是否开启客户端模拟移动 + ClientMoveSpeed int32 `toml:"client_move_speed"` // 客户端模拟移动速度 + ClientMoveRangeExt int32 `toml:"client_move_range_ext"` // 客户端模拟移动区域半径 + ForwardChecksum string `toml:"forward_checksum"` // 转发模式强制指定校验和 + ForwardChecksumClientVersion string `toml:"forward_checksum_client_version"` // 转发模式强制指定校验和客户端版本 } // MQ 消息队列 diff --git a/robot/cmd/application.toml b/robot/cmd/application.toml index c16a3a72..56521dbd 100644 --- a/robot/cmd/application.toml +++ b/robot/cmd/application.toml @@ -26,6 +26,8 @@ dos_loop_login = false # 压力测试是否循环登录退出 client_move_enable = false # 是否开启客户端模拟移动 client_move_speed = 10 # 客户端模拟移动速度 client_move_range_ext = 100 # 客户端模拟移动区域半径 +forward_checksum = "" # 转发模式强制指定校验和 +forward_checksum_client_version = "" # 转发模式强制指定校验和客户端版本 [mq] nats_url = "nats://nats:4222" diff --git a/robot/cmd/main.go b/robot/cmd/main.go index a3b822cd..ff2939d3 100644 --- a/robot/cmd/main.go +++ b/robot/cmd/main.go @@ -5,6 +5,7 @@ import ( "encoding/base64" "encoding/hex" "encoding/json" + "net/http" "os" "os/signal" "strconv" @@ -25,6 +26,8 @@ import ( "hk4e/robot/login" "hk4e/robot/net" + "github.com/gin-gonic/gin" + "github.com/gorilla/websocket" pb "google.golang.org/protobuf/proto" ) @@ -106,7 +109,52 @@ func main() { } } +var ForwardSession *net.Session = nil + +func packetCaptureWebsocket(ctx *gin.Context) { + upgrader := websocket.Upgrader{ + HandshakeTimeout: 10 * time.Second, + ReadBufferSize: 1024, + WriteBufferSize: 1024, + CheckOrigin: func(r *http.Request) bool { + return true + }, + } + wsConn, err := upgrader.Upgrade(ctx.Writer, ctx.Request, nil) + if err != nil { + logger.Error("websocket upgrade error: %v", err) + _, _ = ctx.Writer.WriteString("500") + return + } + if ForwardSession == nil { + _ = wsConn.Close() + _, _ = ctx.Writer.WriteString("500") + return + } + ForwardSession.PktCapWsConn = wsConn +} + +func packetCaptureAll(ctx *gin.Context) { + if ForwardSession == nil { + _, _ = ctx.Writer.WriteString("500") + return + } + ForwardSession.PktListLock.Lock() + data, _ := json.Marshal(ForwardSession.PktList) + ForwardSession.PktListLock.Unlock() + _, _ = ctx.Writer.WriteString(string(data)) +} + func runForward(messageQueue *mq.MessageQueue) { + gin.SetMode(gin.DebugMode) + engine := gin.Default() + engine.GET("/packet/capture/websocket", packetCaptureWebsocket) + engine.GET("/packet/capture/all", packetCaptureAll) + err := engine.Run(":9999") + if err != nil { + logger.Error("gin run error: %v", err) + return + } for { netMsg := <-messageQueue.GetNetMsg() if netMsg.OriginServerType != api.DISPATCH { @@ -155,6 +203,7 @@ func waitClientConn(messageQueue *mq.MessageQueue, dispatchInfo *login.DispatchI logger.Error("remote gate login error: %v", err) continue } + ForwardSession = session logger.Info("remote gate login ok, uid: %v", session.Uid) messageQueue.SendToGate(gateAppId, &mq.NetMsg{ MsgType: mq.MsgTypeGame, @@ -185,11 +234,18 @@ func forwardLoop(session *net.Session, messageQueue *mq.MessageQueue, gateAppId continue } gameMsg := netMsg.GameMsg - session.SendMsgFwd(gameMsg.CmdId, gameMsg.ClientSeq, gameMsg.PayloadMessage) if gameMsg.CmdId == cmd.PlayerLoginReq { - data, _ := json.Marshal(gameMsg.PayloadMessage) + req := gameMsg.PayloadMessage.(*proto.PlayerLoginReq) + data, _ := json.Marshal(req) logger.Debug("PlayerLoginReq: %v", string(data)) + if config.GetConfig().Hk4eRobot.ForwardChecksum != "" { + req.Checksum = config.GetConfig().Hk4eRobot.ForwardChecksum + } + if config.GetConfig().Hk4eRobot.ForwardChecksumClientVersion != "" { + req.ChecksumClientVersion = config.GetConfig().Hk4eRobot.ForwardChecksumClientVersion + } } + session.SendMsgFwd(gameMsg.CmdId, gameMsg.ClientSeq, gameMsg.PayloadMessage) case mq.MsgTypeServer: switch netMsg.EventId { case mq.ServerForwardModeClientCloseNotify: diff --git a/robot/net/session.go b/robot/net/session.go index 8ec54c13..c57d5720 100644 --- a/robot/net/session.go +++ b/robot/net/session.go @@ -1,6 +1,8 @@ package net import ( + "encoding/json" + "sync" "sync/atomic" "time" @@ -13,9 +15,19 @@ import ( "hk4e/protocol/cmd" "hk4e/protocol/proto" + "github.com/gorilla/websocket" pb "google.golang.org/protobuf/proto" ) +type Packet struct { + Time uint64 `json:"time"` + Dir string `json:"dir"` + CmdId uint32 `json:"cmd_id"` + CmdName string `json:"cmd_name"` + HeadMsg string `json:"head_msg"` + PayloadMsg string `json:"payload_msg"` +} + type Session struct { Conn *kcp.UDPSession XorKey []byte @@ -29,6 +41,9 @@ type Session struct { SecurityCmdBuffer []byte Uid uint32 IsClose bool + PktList []*Packet + PktListLock sync.Mutex + PktCapWsConn *websocket.Conn } func NewSession(gateAddr string, dispatchKey []byte, localPort int) (*Session, error) { @@ -56,6 +71,8 @@ func NewSession(gateAddr string, dispatchKey []byte, localPort int) (*Session, e SecurityCmdBuffer: nil, Uid: 0, IsClose: false, + PktList: make([]*Packet, 0), + PktCapWsConn: nil, } if config.GetConfig().Hk4e.ClientProtoProxyEnable { r.ClientCmdProtoMap = client_proto.NewClientCmdProtoMap() @@ -120,6 +137,29 @@ func (s *Session) recvHandle() { protoMsgList := hk4egatenet.ProtoDecode(v, s.ServerCmdProtoMap, s.ClientCmdProtoMap) for _, vv := range protoMsgList { s.RecvChan <- vv + if config.GetConfig().Hk4e.ForwardModeEnable { + 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), + } + if s.PktCapWsConn != nil { + packetData, _ := json.Marshal(packet) + err := s.PktCapWsConn.WriteMessage(websocket.TextMessage, packetData) + if err != nil { + s.PktCapWsConn = nil + } + } + s.PktListLock.Lock() + s.PktList = append(s.PktList, packet) + s.PktListLock.Unlock() + } } } } @@ -149,5 +189,28 @@ func (s *Session) sendHandle() { s.Close() break } + if config.GetConfig().Hk4e.ForwardModeEnable { + 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), + } + if s.PktCapWsConn != nil { + packetData, _ := json.Marshal(packet) + err := s.PktCapWsConn.WriteMessage(websocket.TextMessage, packetData) + if err != nil { + s.PktCapWsConn = nil + } + } + s.PktListLock.Lock() + s.PktList = append(s.PktList, packet) + s.PktListLock.Unlock() + } } }