gate到robot转发模式

This commit is contained in:
flswld committed 2023-06-15 11:13:03 +08:00
1 parent 8d039dee06
commit 12a8381c43
34 files changed
+3132 -440

No files matched your search

+168
View File
@@ -0,0 +1,168 @@
package client
import (
"math"
"time"
"hk4e/common/config"
hk4egatenet "hk4e/gate/net"
"hk4e/pkg/logger"
"hk4e/pkg/object"
"hk4e/pkg/random"
"hk4e/protocol/cmd"
"hk4e/protocol/proto"
"hk4e/robot/net"
pb "google.golang.org/protobuf/proto"
)
func Logic(account string, session *net.Session) {
ticker := time.NewTicker(time.Second)
tickCounter := uint64(0)
pingSeq := uint32(0)
enterSceneDone := false
sceneBeginTime := uint32(0)
bornPos := new(proto.Vector)
currPos := new(proto.Vector)
avatarEntityId := uint32(0)
moveRot := random.GetRandomFloat32(0.0, 359.9)
moveReliableSeq := uint32(0)
for {
select {
case protoMsg := <-session.RecvChan:
// 从这个管道接收服务器发来的消息
switch protoMsg.CmdId {
case cmd.PlayerLoginRsp:
rsp := protoMsg.PayloadMessage.(*proto.PlayerLoginRsp)
if rsp.Retcode != 0 {
logger.Error("login fail, retCode: %v, account: %v", rsp.Retcode, account)
return
}
logger.Info("robot gs login ok, account: %v", account)
case cmd.DoSetPlayerBornDataNotify:
session.SendMsg(cmd.SetPlayerBornDataReq, &proto.SetPlayerBornDataReq{
AvatarId: 10000007,
NickName: account,
})
case cmd.PlayerDataNotify:
ntf := protoMsg.PayloadMessage.(*proto.PlayerDataNotify)
logger.Info("player name: %v", ntf.NickName)
case cmd.PlayerEnterSceneNotify:
ntf := protoMsg.PayloadMessage.(*proto.PlayerEnterSceneNotify)
bornPos.X, bornPos.Y, bornPos.Z = ntf.Pos.X, ntf.Pos.Y, ntf.Pos.Z
currPos.X, currPos.Y, currPos.Z = ntf.Pos.X, ntf.Pos.Y, ntf.Pos.Z
session.SendMsg(cmd.EnterSceneReadyReq, &proto.EnterSceneReadyReq{EnterSceneToken: ntf.EnterSceneToken})
case cmd.EnterSceneReadyRsp:
ntf := protoMsg.PayloadMessage.(*proto.EnterSceneReadyRsp)
session.SendMsg(cmd.SceneInitFinishReq, &proto.SceneInitFinishReq{EnterSceneToken: ntf.EnterSceneToken})
case cmd.SceneInitFinishRsp:
ntf := protoMsg.PayloadMessage.(*proto.SceneInitFinishRsp)
session.SendMsg(cmd.EnterSceneDoneReq, &proto.EnterSceneDoneReq{EnterSceneToken: ntf.EnterSceneToken})
case cmd.EnterSceneDoneRsp:
ntf := protoMsg.PayloadMessage.(*proto.EnterSceneDoneRsp)
enterSceneDone = true
sceneBeginTime = uint32(time.Now().UnixMilli())
session.SendMsg(cmd.PostEnterSceneReq, &proto.PostEnterSceneReq{EnterSceneToken: ntf.EnterSceneToken})
if config.GetConfig().Hk4eRobot.DosLoopLogin {
session.Close()
}
case cmd.SceneEntityAppearNotify:
ntf := protoMsg.PayloadMessage.(*proto.SceneEntityAppearNotify)
for _, sceneEntityInfo := range ntf.EntityList {
if sceneEntityInfo.EntityType != proto.ProtEntityType_PROT_ENTITY_AVATAR {
continue
}
avatarEntityId = sceneEntityInfo.EntityId
}
}
case <-session.DeadEvent:
logger.Info("robot exit, account: %v", account)
close(session.SendChan)
return
case <-ticker.C:
tickCounter++
if config.GetConfig().Hk4eRobot.ClientMoveEnable {
if enterSceneDone {
for {
dx := float32(float64(config.GetConfig().Hk4eRobot.ClientMoveSpeed) * math.Cos(float64(moveRot/360.0*2*math.Pi)))
dz := float32(float64(config.GetConfig().Hk4eRobot.ClientMoveSpeed) * math.Sin(float64(moveRot/360.0*2*math.Pi)))
if currPos.X-dx > bornPos.X+float32(config.GetConfig().Hk4eRobot.ClientMoveRangeExt) ||
currPos.Z-dz > bornPos.Z+float32(config.GetConfig().Hk4eRobot.ClientMoveRangeExt) ||
currPos.X-dx < bornPos.X-float32(config.GetConfig().Hk4eRobot.ClientMoveRangeExt) ||
currPos.Z-dz < bornPos.Z-float32(config.GetConfig().Hk4eRobot.ClientMoveRangeExt) {
moveRot = random.GetRandomFloat32(0.0, 359.9)
continue
}
currPos.X -= dx
currPos.Z -= dz
break
}
moveReliableSeq += 100
entityMoveInfo := &proto.EntityMoveInfo{
EntityId: avatarEntityId,
MotionInfo: &proto.MotionInfo{
Pos: currPos,
Rot: &proto.Vector{X: 0.0, Y: moveRot, Z: 0.0},
Speed: new(proto.Vector),
State: proto.MotionState_MOTION_RUN,
RefPos: new(proto.Vector),
},
SceneTime: uint32(time.Now().UnixMilli()) - sceneBeginTime,
ReliableSeq: moveReliableSeq,
IsReliable: true,
}
logger.Debug("EntityMoveInfo: %v, account: %v", entityMoveInfo, account)
combatData, err := pb.Marshal(entityMoveInfo)
if err != nil {
logger.Error("marshal EntityMoveInfo error: %v, account: %v", err, account)
continue
}
combatInvocationsNotify := &proto.CombatInvocationsNotify{
InvokeList: []*proto.CombatInvokeEntry{{
CombatData: combatData,
ForwardType: proto.ForwardType_FORWARD_TO_ALL_EXCEPT_CUR,
ArgumentType: proto.CombatTypeArgument_ENTITY_MOVE,
}},
}
var combatInvocationsNotifyPb pb.Message = combatInvocationsNotify
if config.GetConfig().Hk4e.ClientProtoProxyEnable {
clientProtoObj := hk4egatenet.GetClientProtoObjByName("CombatInvocationsNotify", session.ClientCmdProtoMap)
if clientProtoObj == nil {
continue
}
err := object.CopyProtoBufSameField(clientProtoObj, combatInvocationsNotify)
if err != nil {
continue
}
hk4egatenet.ConvServerPbDataToClient(clientProtoObj, session.ClientCmdProtoMap)
combatInvocationsNotifyPb = clientProtoObj
}
body, err := pb.Marshal(combatInvocationsNotifyPb)
if err != nil {
logger.Error("marshal CombatInvocationsNotify error: %v, account: %v", err, account)
continue
}
unionCmdNotify := &proto.UnionCmdNotify{
CmdList: []*proto.UnionCmd{{
Body: body,
MessageId: cmd.CombatInvocationsNotify,
}},
}
if config.GetConfig().Hk4e.ClientProtoProxyEnable {
unionCmdNotify.CmdList[0].MessageId = uint32(session.ClientCmdProtoMap.GetClientCmdIdByCmdName("CombatInvocationsNotify"))
}
session.SendMsg(cmd.UnionCmdNotify, unionCmdNotify)
}
}
if tickCounter%5 != 0 {
continue
}
pingSeq++
// 通过这个接口发消息给服务器
session.SendMsg(cmd.PingReq, &proto.PingReq{
ClientTime: uint32(time.Now().Unix()),
Seq: pingSeq,
})
}
}
}
+4
View File
@@ -6,6 +6,7 @@ max_size = 10485760
[hk4e]
client_proto_proxy_enable = false
forward_mode_enable = false
[hk4e_robot]
region_list_url = "https://hk4e.flswld.com" # 一级dispatch地址
@@ -25,3 +26,6 @@ dos_loop_login = false # 压力测试是否循环登录退出
client_move_enable = false # 是否开启客户端模拟移动
client_move_speed = 10 # 客户端模拟移动速度
client_move_range_ext = 100 # 客户端模拟移动区域半径
[mq]
nats_url = "nats://nats:4222"
+206 -179
View File
@@ -1,9 +1,10 @@
package main
import (
"context"
"encoding/base64"
"encoding/hex"
"math"
"encoding/json"
"os"
"os/signal"
"strconv"
@@ -12,21 +13,23 @@ import (
"syscall"
"time"
hk4egatenet "hk4e/gate/net"
"hk4e/pkg/endec"
"hk4e/pkg/object"
"hk4e/pkg/random"
"hk4e/robot/net"
pb "google.golang.org/protobuf/proto"
"hk4e/common/config"
"hk4e/common/mq"
"hk4e/common/rpc"
"hk4e/node/api"
"hk4e/pkg/endec"
"hk4e/pkg/logger"
"hk4e/protocol/cmd"
"hk4e/protocol/proto"
"hk4e/robot/client"
"hk4e/robot/login"
"hk4e/robot/net"
pb "google.golang.org/protobuf/proto"
)
var APPID string
func main() {
config.InitConfig("application.toml")
logger.InitLogger("robot")
@@ -42,29 +45,49 @@ func main() {
// engine.RunEngine([]int{0, 1, 2, 3}, 4, 1, "0.0.0.0")
// time.Sleep(time.Second * 30)
dispatchInfo, err := login.GetDispatchInfo(config.GetConfig().Hk4eRobot.RegionListUrl,
config.GetConfig().Hk4eRobot.RegionListParam,
config.GetConfig().Hk4eRobot.CurRegionUrl,
config.GetConfig().Hk4eRobot.CurRegionParam,
config.GetConfig().Hk4eRobot.KeyId)
if err != nil {
logger.Error("get dispatch info error: %v", err)
time.Sleep(time.Second)
return
}
if config.GetConfig().Hk4eRobot.DosEnable {
dosBatchNum := int(config.GetConfig().Hk4eRobot.DosBatchNum)
for i := 0; i < int(config.GetConfig().Hk4eRobot.DosTotalNum); i += dosBatchNum {
wg := new(sync.WaitGroup)
wg.Add(dosBatchNum)
for j := 0; j < dosBatchNum; j++ {
go httpLogin(config.GetConfig().Hk4eRobot.Account+"_"+strconv.Itoa(i+j), dispatchInfo, wg)
}
wg.Wait()
if config.GetConfig().Hk4e.ForwardModeEnable {
// natsrpc client
discoveryClient, err := rpc.NewDiscoveryClient()
if err != nil {
logger.Error("find discovery service error: %v", err)
return
}
// 注册到节点服务器
rsp, err := discoveryClient.RegisterServer(context.TODO(), &api.RegisterServerReq{
ServerType: api.ROBOT,
})
if err != nil {
logger.Error("register to node server error: %v", err)
return
}
APPID = rsp.GetAppId()
go func() {
ticker := time.NewTicker(time.Second * 15)
for {
<-ticker.C
_, err := discoveryClient.KeepaliveServer(context.TODO(), &api.KeepaliveServerReq{
ServerType: api.ROBOT,
AppId: APPID,
})
if err != nil {
logger.Error("keepalive error: %v", err)
}
}
}()
defer func() {
_, _ = discoveryClient.CancelServer(context.TODO(), &api.CancelServerReq{
ServerType: api.ROBOT,
AppId: APPID,
})
}()
messageQueue := mq.NewMessageQueue(api.ROBOT, APPID, discoveryClient)
defer messageQueue.Close()
go runForward(messageQueue)
} else {
httpLogin(config.GetConfig().Hk4eRobot.Account, dispatchInfo, nil)
go runRobot()
}
c := make(chan os.Signal, 1)
@@ -83,6 +106,157 @@ func main() {
}
}
func runForward(messageQueue *mq.MessageQueue) {
for {
netMsg := <-messageQueue.GetNetMsg()
if netMsg.OriginServerType != api.DISPATCH {
continue
}
if netMsg.MsgType != mq.MsgTypeServer || netMsg.EventId != mq.ServerForwardDispatchInfoNotify {
continue
}
serverMsg := netMsg.ServerMsg
dispatchInfo := &login.DispatchInfo{
GateIp: serverMsg.ForwardDispatchInfo.GateIp,
GatePort: serverMsg.ForwardDispatchInfo.GatePort,
DispatchKey: serverMsg.ForwardDispatchInfo.DispatchKey,
}
logger.Info("get forward dispatch info ok, gate addr: %v:%v", dispatchInfo.GateIp, dispatchInfo.GatePort)
waitClientConn(messageQueue, dispatchInfo)
}
}
func waitClientConn(messageQueue *mq.MessageQueue, dispatchInfo *login.DispatchInfo) {
for {
netMsg := <-messageQueue.GetNetMsg()
if netMsg.OriginServerType != api.GATE {
continue
}
if netMsg.MsgType != mq.MsgTypeServer || netMsg.EventId != mq.ServerForwardModeClientConnNotify {
continue
}
gateAppId := netMsg.OriginServerAppId
logger.Info("client connect, gateAppId: %v", gateAppId)
for {
netMsg = <-messageQueue.GetNetMsg()
if netMsg.OriginServerType != api.GATE || netMsg.OriginServerAppId != gateAppId {
continue
}
if netMsg.MsgType != mq.MsgTypeGame || netMsg.EventId != mq.NormalMsg {
continue
}
gameMsg := netMsg.GameMsg
if gameMsg.CmdId != cmd.GetPlayerTokenReq {
continue
}
req := gameMsg.PayloadMessage.(*proto.GetPlayerTokenReq)
session, err, rsp := login.GateLogin(dispatchInfo, nil, config.GetConfig().Hk4eRobot.KeyId, req, gameMsg.ClientSeq)
if err != nil {
logger.Error("remote gate login error: %v", err)
continue
}
logger.Info("remote gate login ok, uid: %v", session.Uid)
messageQueue.SendToGate(gateAppId, &mq.NetMsg{
MsgType: mq.MsgTypeGame,
EventId: mq.NormalMsg,
GameMsg: &mq.GameMsg{
UserId: rsp.Uid,
CmdId: cmd.GetPlayerTokenRsp,
ClientSeq: gameMsg.ClientSeq,
PayloadMessage: rsp,
},
})
forwardLoop(session, messageQueue, gateAppId)
return
}
}
}
func forwardLoop(session *net.Session, messageQueue *mq.MessageQueue, gateAppId string) {
for {
select {
case netMsg := <-messageQueue.GetNetMsg():
if netMsg.OriginServerType != api.GATE || netMsg.OriginServerAppId != gateAppId {
continue
}
switch netMsg.MsgType {
case mq.MsgTypeGame:
if netMsg.EventId != mq.NormalMsg {
continue
}
gameMsg := netMsg.GameMsg
session.SendMsgFwd(gameMsg.CmdId, gameMsg.ClientSeq, gameMsg.PayloadMessage)
if gameMsg.CmdId == cmd.PlayerLoginReq {
data, _ := json.Marshal(gameMsg.PayloadMessage)
logger.Debug("PlayerLoginReq: %v", string(data))
}
case mq.MsgTypeServer:
switch netMsg.EventId {
case mq.ServerForwardModeClientCloseNotify:
logger.Info("client conn close, uid: %v", session.Uid)
session.Close()
}
}
case protoMsg := <-session.RecvChan:
gameMsg := new(mq.GameMsg)
gameMsg.UserId = session.Uid
gameMsg.CmdId = protoMsg.CmdId
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
}
gameMsg.PayloadMessageData = payloadMessageData
messageQueue.SendToGate(gateAppId, &mq.NetMsg{
MsgType: mq.MsgTypeGame,
EventId: mq.NormalMsg,
GameMsg: gameMsg,
})
if gameMsg.CmdId == cmd.PlayerLoginRsp {
data, _ := json.Marshal(protoMsg.PayloadMessage)
logger.Debug("PlayerLoginRsp: %v", string(data))
}
case <-session.DeadEvent:
logger.Info("remote gate conn close, uid: %v", session.Uid)
close(session.SendChan)
messageQueue.SendToGate(gateAppId, &mq.NetMsg{
MsgType: mq.MsgTypeServer,
EventId: mq.ServerForwardModeServerCloseNotify,
})
return
}
}
}
func runRobot() {
dispatchInfo, err := login.GetDispatchInfo(config.GetConfig().Hk4eRobot.RegionListUrl,
config.GetConfig().Hk4eRobot.RegionListParam,
config.GetConfig().Hk4eRobot.CurRegionUrl,
config.GetConfig().Hk4eRobot.CurRegionParam,
config.GetConfig().Hk4eRobot.KeyId)
if err != nil {
logger.Error("get dispatch info error: %v", err)
return
}
if config.GetConfig().Hk4eRobot.DosEnable {
dosBatchNum := int(config.GetConfig().Hk4eRobot.DosBatchNum)
for i := 0; i < int(config.GetConfig().Hk4eRobot.DosTotalNum); i += dosBatchNum {
wg := new(sync.WaitGroup)
wg.Add(dosBatchNum)
for j := 0; j < dosBatchNum; j++ {
go httpLogin(config.GetConfig().Hk4eRobot.Account+"_"+strconv.Itoa(i+j), dispatchInfo, wg)
}
wg.Wait()
}
} else {
httpLogin(config.GetConfig().Hk4eRobot.Account, dispatchInfo, nil)
}
}
func httpLogin(account string, dispatchInfo *login.DispatchInfo, wg *sync.WaitGroup) {
defer func() {
if config.GetConfig().Hk4eRobot.DosEnable {
@@ -108,7 +282,7 @@ func httpLogin(account string, dispatchInfo *login.DispatchInfo, wg *sync.WaitGr
}
func gateLogin(account string, dispatchInfo *login.DispatchInfo, accountInfo *login.AccountInfo) {
session, err := login.GateLogin(dispatchInfo, accountInfo, config.GetConfig().Hk4eRobot.KeyId)
session, err, _ := login.GateLogin(dispatchInfo, accountInfo, config.GetConfig().Hk4eRobot.KeyId, nil, 1)
if err != nil {
logger.Error("gate login error: %v", err)
return
@@ -136,152 +310,5 @@ func gateLogin(account string, dispatchInfo *login.DispatchInfo, accountInfo *lo
SecurityLibraryMd5: "574a507ffee2eb6f997d11f71c8ae1fa",
Token: accountInfo.ComboToken,
})
clientLogic(account, session)
}
func clientLogic(account string, session *net.Session) {
ticker := time.NewTicker(time.Second)
tickCounter := uint64(0)
pingSeq := uint32(0)
enterSceneDone := false
sceneBeginTime := uint32(0)
bornPos := new(proto.Vector)
currPos := new(proto.Vector)
avatarEntityId := uint32(0)
moveRot := random.GetRandomFloat32(0.0, 359.9)
moveReliableSeq := uint32(0)
for {
select {
case <-ticker.C:
tickCounter++
if config.GetConfig().Hk4eRobot.ClientMoveEnable {
if enterSceneDone {
for {
dx := float32(float64(config.GetConfig().Hk4eRobot.ClientMoveSpeed) * math.Cos(float64(moveRot/360.0*2*math.Pi)))
dz := float32(float64(config.GetConfig().Hk4eRobot.ClientMoveSpeed) * math.Sin(float64(moveRot/360.0*2*math.Pi)))
if currPos.X-dx > bornPos.X+float32(config.GetConfig().Hk4eRobot.ClientMoveRangeExt) ||
currPos.Z-dz > bornPos.Z+float32(config.GetConfig().Hk4eRobot.ClientMoveRangeExt) ||
currPos.X-dx < bornPos.X-float32(config.GetConfig().Hk4eRobot.ClientMoveRangeExt) ||
currPos.Z-dz < bornPos.Z-float32(config.GetConfig().Hk4eRobot.ClientMoveRangeExt) {
moveRot = random.GetRandomFloat32(0.0, 359.9)
continue
}
currPos.X -= dx
currPos.Z -= dz
break
}
moveReliableSeq += 100
entityMoveInfo := &proto.EntityMoveInfo{
EntityId: avatarEntityId,
MotionInfo: &proto.MotionInfo{
Pos: currPos,
Rot: &proto.Vector{X: 0.0, Y: moveRot, Z: 0.0},
Speed: new(proto.Vector),
State: proto.MotionState_MOTION_RUN,
RefPos: new(proto.Vector),
},
SceneTime: uint32(time.Now().UnixMilli()) - sceneBeginTime,
ReliableSeq: moveReliableSeq,
IsReliable: true,
}
logger.Debug("EntityMoveInfo: %v, account: %v", entityMoveInfo, account)
combatData, err := pb.Marshal(entityMoveInfo)
if err != nil {
logger.Error("marshal EntityMoveInfo error: %v, account: %v", err, account)
continue
}
combatInvocationsNotify := &proto.CombatInvocationsNotify{
InvokeList: []*proto.CombatInvokeEntry{{
CombatData: combatData,
ForwardType: proto.ForwardType_FORWARD_TO_ALL_EXCEPT_CUR,
ArgumentType: proto.CombatTypeArgument_ENTITY_MOVE,
}},
}
var combatInvocationsNotifyPb pb.Message = combatInvocationsNotify
if config.GetConfig().Hk4e.ClientProtoProxyEnable {
clientProtoObj := hk4egatenet.GetClientProtoObjByName("CombatInvocationsNotify", session.ClientCmdProtoMap)
if clientProtoObj == nil {
continue
}
err := object.CopyProtoBufSameField(clientProtoObj, combatInvocationsNotify)
if err != nil {
continue
}
hk4egatenet.ConvServerPbDataToClient(clientProtoObj, session.ClientCmdProtoMap)
combatInvocationsNotifyPb = clientProtoObj
}
body, err := pb.Marshal(combatInvocationsNotifyPb)
if err != nil {
logger.Error("marshal CombatInvocationsNotify error: %v, account: %v", err, account)
continue
}
unionCmdNotify := &proto.UnionCmdNotify{
CmdList: []*proto.UnionCmd{{
Body: body,
MessageId: cmd.CombatInvocationsNotify,
}},
}
if config.GetConfig().Hk4e.ClientProtoProxyEnable {
unionCmdNotify.CmdList[0].MessageId = uint32(session.ClientCmdProtoMap.GetClientCmdIdByCmdName("CombatInvocationsNotify"))
}
session.SendMsg(cmd.UnionCmdNotify, unionCmdNotify)
}
}
if tickCounter%5 != 0 {
continue
}
pingSeq++
// 通过这个接口发消息给服务器
session.SendMsg(cmd.PingReq, &proto.PingReq{
ClientTime: uint32(time.Now().Unix()),
Seq: pingSeq,
})
case protoMsg := <-session.RecvChan:
// 从这个管道接收服务器发来的消息
switch protoMsg.CmdId {
case cmd.PlayerLoginRsp:
rsp := protoMsg.PayloadMessage.(*proto.PlayerLoginRsp)
if rsp.Retcode != 0 {
logger.Error("login fail, retCode: %v, account: %v", rsp.Retcode, account)
return
}
logger.Info("robot gs login ok, account: %v", account)
case cmd.DoSetPlayerBornDataNotify:
session.SendMsg(cmd.SetPlayerBornDataReq, &proto.SetPlayerBornDataReq{
AvatarId: 10000007,
NickName: account,
})
case cmd.PlayerEnterSceneNotify:
ntf := protoMsg.PayloadMessage.(*proto.PlayerEnterSceneNotify)
bornPos.X, bornPos.Y, bornPos.Z = ntf.Pos.X, ntf.Pos.Y, ntf.Pos.Z
currPos.X, currPos.Y, currPos.Z = ntf.Pos.X, ntf.Pos.Y, ntf.Pos.Z
session.SendMsg(cmd.EnterSceneReadyReq, &proto.EnterSceneReadyReq{EnterSceneToken: ntf.EnterSceneToken})
case cmd.EnterSceneReadyRsp:
ntf := protoMsg.PayloadMessage.(*proto.EnterSceneReadyRsp)
session.SendMsg(cmd.SceneInitFinishReq, &proto.SceneInitFinishReq{EnterSceneToken: ntf.EnterSceneToken})
case cmd.SceneInitFinishRsp:
ntf := protoMsg.PayloadMessage.(*proto.SceneInitFinishRsp)
session.SendMsg(cmd.EnterSceneDoneReq, &proto.EnterSceneDoneReq{EnterSceneToken: ntf.EnterSceneToken})
case cmd.EnterSceneDoneRsp:
ntf := protoMsg.PayloadMessage.(*proto.EnterSceneDoneRsp)
enterSceneDone = true
sceneBeginTime = uint32(time.Now().UnixMilli())
session.SendMsg(cmd.PostEnterSceneReq, &proto.PostEnterSceneReq{EnterSceneToken: ntf.EnterSceneToken})
if config.GetConfig().Hk4eRobot.DosLoopLogin {
return
}
case cmd.SceneEntityAppearNotify:
ntf := protoMsg.PayloadMessage.(*proto.SceneEntityAppearNotify)
for _, sceneEntityInfo := range ntf.EntityList {
if sceneEntityInfo.EntityType != proto.ProtEntityType_PROT_ENTITY_AVATAR {
continue
}
avatarEntityId = sceneEntityInfo.EntityId
}
}
case <-session.DeadEvent:
logger.Info("robot exit, account: %v", account)
return
}
}
client.Logic(account, session)
}
+65 -25
View File
@@ -4,93 +4,133 @@ import (
"bytes"
"encoding/base64"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"os"
"strconv"
"hk4e/common/region"
"hk4e/pkg/endec"
"hk4e/pkg/logger"
"hk4e/pkg/random"
"hk4e/pkg/reflection"
"hk4e/protocol/cmd"
"hk4e/protocol/proto"
"hk4e/robot/net"
)
func GateLogin(dispatchInfo *DispatchInfo, accountInfo *AccountInfo, keyId string) (*net.Session, error) {
func GateLogin(dispatchInfo *DispatchInfo, accountInfo *AccountInfo, keyId string, req *proto.GetPlayerTokenReq, clientSeq uint32) (*net.Session, error, *proto.GetPlayerTokenRsp) {
gateAddr := dispatchInfo.GateIp + ":" + strconv.Itoa(int(dispatchInfo.GatePort))
logger.Debug("connect gate addr: %v", gateAddr)
session, err := net.NewSession(gateAddr, dispatchInfo.DispatchKey, 30000)
if err != nil {
return nil, err
return nil, err, nil
}
logger.Debug("connect gate ok")
timeRand := random.GetTimeRand()
clientSeedUint64 := timeRand.Uint64()
clientSeedBuf := new(bytes.Buffer)
err = binary.Write(clientSeedBuf, binary.BigEndian, &clientSeedUint64)
if err != nil {
return nil, err
return nil, err, nil
}
clientSeed := clientSeedBuf.Bytes()
logger.Debug("clientSeed: %v, clientSeedUint64: %v", clientSeed, clientSeedUint64)
signRsaKey, encRsaKeyMap, _ := region.LoadRsaKey()
if signRsaKey == nil || encRsaKeyMap == nil {
return nil, errors.New("load key error"), nil
}
signPubkey, err := endec.RsaParsePubKeyByPrivKey(signRsaKey)
if err != nil {
logger.Error("parse rsa pub key error: %v", err)
return nil, err
return nil, err, nil
}
signRsaKey, err = os.ReadFile("key/region_sign_key_pub.pem")
if err == nil {
logger.Debug("use pub key")
signPubkey, err = endec.RsaParsePubKey(signRsaKey)
if err != nil {
logger.Error("parse rsa pub key error: %v", err)
return nil, err, nil
}
}
clientSeedEnc, err := endec.RsaEncrypt(clientSeed, signPubkey)
if err != nil {
logger.Error("rsa dec error: %v", err)
return nil, err
logger.Error("rsa enc error: %v", err)
return nil, err, nil
}
clientSeedBase64 := base64.StdEncoding.EncodeToString(clientSeedEnc)
getPlayerTokenReq := new(proto.GetPlayerTokenReq)
keyIdInt, err := strconv.Atoi(keyId)
if err != nil {
logger.Error("parse key id error: %v", err)
return nil, err
return nil, err, nil
}
session.SendMsg(cmd.GetPlayerTokenReq, &proto.GetPlayerTokenReq{
AccountToken: accountInfo.ComboToken,
AccountUid: strconv.Itoa(int(accountInfo.AccountId)),
KeyId: uint32(keyIdInt),
ClientRandKey: clientSeedBase64,
AccountType: 1,
ChannelId: 1,
SubChannelId: 1,
PlatformType: 3,
})
if req == nil {
getPlayerTokenReq = &proto.GetPlayerTokenReq{
AccountToken: accountInfo.ComboToken,
AccountUid: strconv.Itoa(int(accountInfo.AccountId)),
KeyId: uint32(keyIdInt),
ClientRandKey: clientSeedBase64,
AccountType: 1,
ChannelId: 1,
SubChannelId: 1,
PlatformType: 3,
}
} else {
ok := reflection.CopyStructSameField(getPlayerTokenReq, req)
if !ok {
return nil, errors.New("copy field error"), nil
}
getPlayerTokenReq.KeyId = uint32(keyIdInt)
getPlayerTokenReq.ClientRandKey = clientSeedBase64
}
session.SendMsgFwd(cmd.GetPlayerTokenReq, clientSeq, getPlayerTokenReq)
getPlayerTokenReqJson, _ := json.Marshal(getPlayerTokenReq)
logger.Debug("GetPlayerTokenReq: %v", string(getPlayerTokenReqJson))
protoMsg := <-session.RecvChan
if protoMsg.CmdId != cmd.GetPlayerTokenRsp {
return nil, errors.New("recv pkt is not GetPlayerTokenRsp")
return nil, errors.New("recv pkt is not GetPlayerTokenRsp"), nil
}
getPlayerTokenRsp := protoMsg.PayloadMessage.(*proto.GetPlayerTokenRsp)
getPlayerTokenRspJson, _ := json.Marshal(getPlayerTokenRsp)
logger.Debug("GetPlayerTokenRsp: %v", string(getPlayerTokenRspJson))
if getPlayerTokenRsp.Retcode != 0 {
return nil, errors.New(fmt.Sprintf("gate login error, retCode: %v", getPlayerTokenRsp.Retcode)), nil
}
logger.Info("gate login ok, uid: %v", getPlayerTokenRsp.Uid)
session.Uid = getPlayerTokenRsp.Uid
// XOR密钥切换
seedEnc, err := base64.StdEncoding.DecodeString(getPlayerTokenRsp.ServerRandKey)
if err != nil {
logger.Error("base64 decode error: %v", err)
return nil, err
return nil, err, nil
}
encPubPrivKey, exist := encRsaKeyMap[keyId]
if !exist {
logger.Error("can not found key id: %v", keyId)
return nil, err
return nil, err, nil
}
privKey, err := endec.RsaParsePrivKey(encPubPrivKey)
if err != nil {
logger.Error("parse rsa pub key error: %v", err)
return nil, err
return nil, err, nil
}
seed, err := endec.RsaDecrypt(seedEnc, privKey)
if err != nil {
logger.Error("rsa enc error: %v", err)
return nil, err
logger.Error("rsa dec error: %v", err)
return nil, err, nil
}
seedUint64 := uint64(0)
err = binary.Read(bytes.NewReader(seed), binary.BigEndian, &seedUint64)
if err != nil {
logger.Error("parse seed error: %v", err)
return nil, err
return nil, err, nil
}
serverSeedUint64 := seedUint64 ^ clientSeedUint64
logger.Debug("seed: %v, seedUint64: %v", seed, seedUint64)
logger.Debug("serverSeedUint64: %v", serverSeedUint64)
logger.Info("change session xor key")
keyBlock := random.NewKeyBlock(serverSeedUint64, true)
xorKey := keyBlock.XorKey()
@@ -99,5 +139,5 @@ func GateLogin(dispatchInfo *DispatchInfo, accountInfo *AccountInfo, keyId strin
session.XorKey = key
session.ClientVersionRandomKey = getPlayerTokenRsp.ClientVersionRandomKey
session.SecurityCmdBuffer = getPlayerTokenRsp.SecurityCmdBuffer
return session, nil
return session, nil, getPlayerTokenRsp
}
+1 -1
View File
@@ -98,7 +98,7 @@ func GetDispatchInfo(regionListUrl string, regionListParam string, curRegionUrl
if regionInfo == nil {
return nil, errors.New("region info is nil")
}
ec2b, err := random.LoadEc2bKey(regionInfo.SecretKey)
ec2b, err := random.LoadEc2bKey(queryCurrRegionHttpRsp.ClientSecretKey)
if err != nil {
return nil, err
}
+33 -9
View File
@@ -27,6 +27,8 @@ type Session struct {
DeadEvent chan bool
ClientVersionRandomKey string
SecurityCmdBuffer []byte
Uid uint32
IsClose bool
}
func NewSession(gateAddr string, dispatchKey []byte, localPort int) (*Session, error) {
@@ -40,7 +42,7 @@ func NewSession(gateAddr string, dispatchKey []byte, localPort int) (*Session, e
}
conn.SetACKNoDelay(true)
conn.SetWriteDelay(false)
conn.SetWindowSize(255, 255)
conn.SetWindowSize(256, 256)
r := &Session{
Conn: conn,
XorKey: dispatchKey,
@@ -49,9 +51,11 @@ func NewSession(gateAddr string, dispatchKey []byte, localPort int) (*Session, e
ServerCmdProtoMap: cmd.NewCmdProtoMap(),
ClientCmdProtoMap: nil,
ClientSeq: 0,
DeadEvent: make(chan bool, 10),
DeadEvent: make(chan bool, 1),
ClientVersionRandomKey: "",
SecurityCmdBuffer: nil,
Uid: 0,
IsClose: false,
}
if config.GetConfig().Hk4e.ClientProtoProxyEnable {
r.ClientCmdProtoMap = client_proto.NewClientCmdProtoMap()
@@ -62,18 +66,40 @@ func NewSession(gateAddr string, dispatchKey []byte, localPort int) (*Session, e
}
func (s *Session) SendMsg(cmdId uint16, msg pb.Message) {
atomic.AddUint32(&s.ClientSeq, 1)
s.SendChan <- &hk4egatenet.ProtoMsg{
ConvId: 0,
CmdId: cmdId,
HeadMessage: &proto.PacketHead{
ClientSequenceId: s.ClientSeq,
ClientSequenceId: atomic.AddUint32(&s.ClientSeq, 1),
SentMs: uint64(time.Now().UnixMilli()),
EnetIsReliable: 1,
},
PayloadMessage: msg,
}
}
func (s *Session) SendMsgFwd(cmdId uint16, clientSeq uint32, msg pb.Message) {
s.SendChan <- &hk4egatenet.ProtoMsg{
ConvId: 0,
CmdId: cmdId,
HeadMessage: &proto.PacketHead{
ClientSequenceId: clientSeq,
SentMs: uint64(time.Now().UnixMilli()),
EnetIsReliable: 1,
},
PayloadMessage: msg,
}
}
func (s *Session) Close() {
if s.IsClose {
return
}
s.IsClose = true
_ = s.Conn.Close()
s.DeadEvent <- true
}
func (s *Session) recvHandle() {
logger.Info("recv handle start")
conn := s.Conn
@@ -84,7 +110,7 @@ func (s *Session) recvHandle() {
recvLen, err := conn.Read(recvBuf)
if err != nil {
logger.Error("exit recv loop, conn read err: %v, convId: %v", err, convId)
_ = conn.Close()
s.Close()
break
}
recvData := recvBuf[:recvLen]
@@ -97,7 +123,6 @@ func (s *Session) recvHandle() {
}
}
}
s.DeadEvent <- true
}
func (s *Session) sendHandle() {
@@ -108,7 +133,7 @@ func (s *Session) sendHandle() {
protoMsg, ok := <-s.SendChan
if !ok {
logger.Error("exit send loop, send chan close, convId: %v", convId)
_ = conn.Close()
s.Close()
break
}
kcpMsg := hk4egatenet.ProtoEncode(protoMsg, s.ServerCmdProtoMap, s.ClientCmdProtoMap)
@@ -121,9 +146,8 @@ func (s *Session) sendHandle() {
_, err := conn.Write(bin)
if err != nil {
logger.Error("exit send loop, conn write err: %v, convId: %v", err, convId)
_ = conn.Close()
s.Close()
break
}
}
s.DeadEvent <- true
}