机器人gate登录

This commit is contained in:
flswld
2023-02-14 17:20:17 +08:00
parent c25473b070
commit 4de697cf9a
7 changed files with 236 additions and 80 deletions

View File

@@ -101,10 +101,6 @@ func (c *Controller) queryCurRegion(context *gin.Context) {
return return
} }
logger.Debug("do hk4e 2.8 rsa logic") logger.Debug("do hk4e 2.8 rsa logic")
if context.Query("dispatchSeed") == "" {
rspError()
return
}
keyId := context.Query("key_id") keyId := context.Query("key_id")
encPubPrivKey, exist := c.encRsaKeyMap[keyId] encPubPrivKey, exist := c.encRsaKeyMap[keyId]
if !exist { if !exist {

View File

@@ -2,11 +2,14 @@ package httpclient
import ( import (
"bytes" "bytes"
"crypto/tls"
"encoding/json" "encoding/json"
"io" "io"
"net/http" "net/http"
"strings" "strings"
"time" "time"
"hk4e/pkg/logger"
) )
var httpClient http.Client var httpClient http.Client
@@ -14,6 +17,9 @@ var httpClient http.Client
func init() { func init() {
httpClient = http.Client{ httpClient = http.Client{
Transport: &http.Transport{ Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
DisableKeepAlives: true, DisableKeepAlives: true,
}, },
Timeout: time.Second * 10, Timeout: time.Second * 10,
@@ -37,6 +43,7 @@ func GetJson[T any](url string, authToken string) (*T, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
logger.Debug("http get rsp data: %v", string(data))
responseData := new(T) responseData := new(T)
err = json.Unmarshal(data, responseData) err = json.Unmarshal(data, responseData)
if err != nil { if err != nil {
@@ -87,6 +94,7 @@ func PostJson[T any](url string, body any, authToken string) (*T, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
logger.Debug("http post rsp data: %v", string(rspData))
responseData := new(T) responseData := new(T)
err = json.Unmarshal(rspData, responseData) err = json.Unmarshal(rspData, responseData)
if err != nil { if err != nil {

14
robot/cmd/dpdk_test.go Normal file
View File

@@ -0,0 +1,14 @@
//go:build linux
// +build linux
package main
import (
"testing"
"github.com/FlourishingWorld/dpdk-go/engine"
)
func TestDpdk(t *testing.T) {
_ = engine.InitEngine("00:0C:29:3E:3E:DF", "192.168.199.199", "255.255.255.0", "192.168.199.1")
}

View File

@@ -1,9 +1,9 @@
package main package main
import ( import (
"encoding/base64"
"os" "os"
"os/signal" "os/signal"
"strconv"
"syscall" "syscall"
"time" "time"
@@ -13,51 +13,59 @@ import (
"hk4e/protocol/cmd" "hk4e/protocol/cmd"
"hk4e/protocol/proto" "hk4e/protocol/proto"
"hk4e/robot/login" "hk4e/robot/login"
"hk4e/robot/net"
"github.com/FlourishingWorld/dpdk-go/engine"
) )
func main() { func main() {
config.InitConfig("application.toml") config.InitConfig("application.toml")
logger.InitLogger("robot") logger.InitLogger("robot")
err := engine.InitEngine("00:0C:29:3E:3E:DF", "192.168.199.199", "255.255.255.0", "192.168.199.1") // // DPDK模式需开启
if err != nil { // err := engine.InitEngine("00:0C:29:3E:3E:DF", "192.168.199.199", "255.255.255.0", "192.168.199.1")
panic(err) // if err != nil {
} // panic(err)
engine.RunEngine([]int{0, 1, 2, 3}, 4, 1, "0.0.0.0") // }
// engine.RunEngine([]int{0, 1, 2, 3}, 4, 1, "0.0.0.0")
// time.Sleep(time.Second * 30)
time.Sleep(time.Second * 30) dispatchInfo, err := login.GetDispatchInfo("https://hk4e.flswld.com",
"https://hk4e.flswld.com/query_cur_region",
dispatchInfo, err := login.GetDispatchInfo("https://hk4e.flswld.com", "?version=OSRELWin3.2.0") "",
"?version=OSRELWin3.2.0&key_id=5",
"5")
if err != nil { if err != nil {
panic(err) panic(err)
} }
accountInfo, err := login.AccountLogin("https://hk4e.flswld.com", "flswld", "123456") accountInfo, err := login.AccountLogin("https://hk4e.flswld.com", "test123@@12345678", base64.StdEncoding.EncodeToString([]byte{0x00}))
if err != nil {
panic(err)
}
session, err := login.GateLogin(dispatchInfo, accountInfo, "5")
if err != nil { if err != nil {
panic(err) panic(err)
} }
gateAddr := dispatchInfo.GateIp + strconv.Itoa(int(dispatchInfo.GatePort))
session := net.NewSession(gateAddr, dispatchInfo.DispatchKey, 30000)
go func() { go func() {
protoMsg := <-session.RecvChan for {
logger.Debug("protoMsg: %v", protoMsg) // 从这个管道接收服务器发来的消息
protoMsg := <-session.RecvChan
logger.Debug("recv protoMsg: %v", protoMsg)
}
}() }()
go func() { go func() {
session.SendChan <- &hk4egatenet.ProtoMsg{ for {
ConvId: 0, time.Sleep(time.Second)
CmdId: cmd.GetPlayerTokenReq, // 通过这个管道发消息给服务器
HeadMessage: &proto.PacketHead{ session.SendChan <- &hk4egatenet.ProtoMsg{
ClientSequenceId: 1, ConvId: 0,
SentMs: uint64(time.Now().UnixMilli()), CmdId: cmd.PingReq,
}, HeadMessage: &proto.PacketHead{
PayloadMessage: &proto.GetPlayerTokenReq{ ClientSequenceId: 0,
AccountToken: accountInfo.ComboToken, SentMs: uint64(time.Now().UnixMilli()),
AccountUid: strconv.Itoa(int(accountInfo.AccountId)), },
KeyId: 5, PayloadMessage: &proto.PingReq{
ClientRandKey: "", ClientTime: uint32(time.Now().UnixMilli()),
}, Seq: 0,
},
}
} }
}() }()
@@ -67,7 +75,10 @@ func main() {
s := <-c s := <-c
switch s { switch s {
case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT: case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT:
engine.StopEngine()
// // DPDK模式需开启
// engine.StopEngine()
time.Sleep(time.Second) time.Sleep(time.Second)
return return
case syscall.SIGHUP: case syscall.SIGHUP:

106
robot/login/gate_login.go Normal file
View File

@@ -0,0 +1,106 @@
package login
import (
"bytes"
"encoding/base64"
"encoding/binary"
"errors"
"strconv"
"time"
"hk4e/common/region"
hk4egatenet "hk4e/gate/net"
"hk4e/pkg/endec"
"hk4e/pkg/logger"
"hk4e/pkg/random"
"hk4e/protocol/cmd"
"hk4e/protocol/proto"
"hk4e/robot/net"
)
func GateLogin(dispatchInfo *DispatchInfo, accountInfo *AccountInfo, keyId string) (*net.Session, error) {
gateAddr := dispatchInfo.GateIp + ":" + strconv.Itoa(int(dispatchInfo.GatePort))
session, err := net.NewSession(gateAddr, dispatchInfo.DispatchKey, 30000)
if err != nil {
return nil, err
}
timeRand := random.GetTimeRand()
clientSeedUint64 := timeRand.Uint64()
clientSeedBuf := new(bytes.Buffer)
err = binary.Write(clientSeedBuf, binary.BigEndian, &clientSeedUint64)
if err != nil {
return nil, err
}
clientSeed := clientSeedBuf.Bytes()
signRsaKey, encRsaKeyMap, _ := region.LoadRsaKey()
signPubkey, err := endec.RsaParsePubKeyByPrivKey(signRsaKey)
if err != nil {
logger.Error("parse rsa pub key error: %v", err)
return nil, err
}
clientSeedEnc, err := endec.RsaEncrypt(clientSeed, signPubkey)
if err != nil {
logger.Error("rsa dec error: %v", err)
return nil, err
}
clientSeedBase64 := base64.StdEncoding.EncodeToString(clientSeedEnc)
keyIdInt, err := strconv.Atoi(keyId)
if err != nil {
logger.Error("parse key id error: %v", err)
return nil, err
}
session.SendChan <- &hk4egatenet.ProtoMsg{
ConvId: 0,
CmdId: cmd.GetPlayerTokenReq,
HeadMessage: &proto.PacketHead{
ClientSequenceId: 0,
SentMs: uint64(time.Now().UnixMilli()),
},
PayloadMessage: &proto.GetPlayerTokenReq{
AccountToken: accountInfo.ComboToken,
AccountUid: strconv.Itoa(int(accountInfo.AccountId)),
KeyId: uint32(keyIdInt),
ClientRandKey: clientSeedBase64,
},
}
protoMsg := <-session.RecvChan
if protoMsg.CmdId != cmd.GetPlayerTokenRsp {
return nil, errors.New("recv pkt is not GetPlayerTokenRsp")
}
// XOR密钥切换
getPlayerTokenRsp := protoMsg.PayloadMessage.(*proto.GetPlayerTokenRsp)
seedEnc, err := base64.StdEncoding.DecodeString(getPlayerTokenRsp.ServerRandKey)
if err != nil {
logger.Error("base64 decode error: %v", err)
return nil, err
}
encPubPrivKey, exist := encRsaKeyMap[keyId]
if !exist {
logger.Error("can not found key id: %v", keyId)
return nil, err
}
privKey, err := endec.RsaParsePrivKey(encPubPrivKey)
if err != nil {
logger.Error("parse rsa pub key error: %v", err)
return nil, err
}
seed, err := endec.RsaDecrypt(seedEnc, privKey)
if err != nil {
logger.Error("rsa enc error: %v", err)
return nil, err
}
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
}
serverSeedUint64 := seedUint64 ^ clientSeedUint64
logger.Info("change session xor key")
keyBlock := random.NewKeyBlock(serverSeedUint64, true)
xorKey := keyBlock.XorKey()
key := make([]byte, 4096)
copy(key, xorKey[:])
session.XorKey = key
return session, nil
}

View File

@@ -3,9 +3,12 @@ package login
import ( import (
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"math"
"strconv" "strconv"
"hk4e/common/region"
"hk4e/dispatch/api" "hk4e/dispatch/api"
"hk4e/pkg/endec"
"hk4e/pkg/httpclient" "hk4e/pkg/httpclient"
"hk4e/pkg/logger" "hk4e/pkg/logger"
"hk4e/pkg/random" "hk4e/pkg/random"
@@ -21,9 +24,9 @@ type DispatchInfo struct {
DispatchKey []byte DispatchKey []byte
} }
func GetDispatchInfo(url string, clientParam string) (*DispatchInfo, error) { func GetDispatchInfo(regionListUrl string, curRegionUrl string, regionListParam string, curRegionParam string, keyId string) (*DispatchInfo, error) {
logger.Info("http get url: %v", url+"/query_region_list"+clientParam) logger.Info("http get url: %v", regionListUrl+"/query_region_list"+regionListParam)
regionListBase64, err := httpclient.GetRaw(url+"/query_region_list"+clientParam, "") regionListBase64, err := httpclient.GetRaw(regionListUrl+"/query_region_list"+regionListParam, "")
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -40,17 +43,51 @@ func GetDispatchInfo(url string, clientParam string) (*DispatchInfo, error) {
if len(queryRegionListHttpRsp.RegionList) == 0 { if len(queryRegionListHttpRsp.RegionList) == 0 {
return nil, errors.New("no region found") return nil, errors.New("no region found")
} }
selectRegion := queryRegionListHttpRsp.RegionList[0] if curRegionUrl == "" {
logger.Info("select region: %v", selectRegion) selectRegion := queryRegionListHttpRsp.RegionList[0]
logger.Info("http get url: %v", selectRegion.DispatchUrl+clientParam) logger.Info("select region: %v", selectRegion)
regionCurrBase64, err := httpclient.GetRaw(selectRegion.DispatchUrl+clientParam, "") curRegionUrl = selectRegion.DispatchUrl
}
logger.Info("http get url: %v", curRegionUrl+curRegionParam)
regionCurrJson, err := httpclient.GetRaw(curRegionUrl+curRegionParam, "")
if err != nil { if err != nil {
return nil, err return nil, err
} }
regionCurrData, err := base64.StdEncoding.DecodeString(regionCurrBase64) queryCurRegionRspJson := new(api.QueryCurRegionRspJson)
err = json.Unmarshal([]byte(regionCurrJson), queryCurRegionRspJson)
if err != nil { if err != nil {
return nil, err return nil, err
} }
encryptedRegionInfo, err := base64.StdEncoding.DecodeString(queryCurRegionRspJson.Content)
if err != nil {
return nil, err
}
chunkSize := 256
regionInfoLength := len(encryptedRegionInfo)
numChunks := int(math.Ceil(float64(regionInfoLength) / float64(chunkSize)))
regionCurrData := make([]byte, 0)
_, encRsaKeyMap, _ := region.LoadRsaKey()
encPubPrivKey, exist := encRsaKeyMap[keyId]
if !exist {
logger.Error("can not found key id: %v", keyId)
return nil, err
}
for i := 0; i < numChunks; i++ {
from := i * chunkSize
to := int(math.Min(float64((i+1)*chunkSize), float64(regionInfoLength)))
chunk := encryptedRegionInfo[from:to]
privKey, err := endec.RsaParsePrivKey(encPubPrivKey)
if err != nil {
logger.Error("parse rsa priv key error: %v", err)
return nil, err
}
decrypt, err := endec.RsaDecrypt(chunk, privKey)
if err != nil {
logger.Error("rsa dec error: %v", err)
return nil, err
}
regionCurrData = append(regionCurrData, decrypt...)
}
queryCurrRegionHttpRsp := new(proto.QueryCurrRegionHttpRsp) queryCurrRegionHttpRsp := new(proto.QueryCurrRegionHttpRsp)
err = pb.Unmarshal(regionCurrData, queryCurrRegionHttpRsp) err = pb.Unmarshal(regionCurrData, queryCurrRegionHttpRsp)
if err != nil { if err != nil {

View File

@@ -1,54 +1,48 @@
package net package net
import ( import (
"strconv"
"time" "time"
"hk4e/gate/kcp"
hk4egatenet "hk4e/gate/net" hk4egatenet "hk4e/gate/net"
"hk4e/pkg/logger" "hk4e/pkg/logger"
"hk4e/pkg/random"
"hk4e/protocol/cmd" "hk4e/protocol/cmd"
"github.com/FlourishingWorld/dpdk-go/protocol/kcp"
) )
type Session struct { type Session struct {
SendChan chan *hk4egatenet.ProtoMsg Conn *kcp.UDPSession
RecvChan chan *hk4egatenet.ProtoMsg XorKey []byte
conn *kcp.UDPSession SendChan chan *hk4egatenet.ProtoMsg
seed uint64 // TODO 密钥交换后收到的服务器生成的seed RecvChan chan *hk4egatenet.ProtoMsg
xorKey []byte
changeXorKeyFin bool
useMagicSeed bool
} }
func NewSession(gateAddr string, dispatchKey []byte, localPort int) (r *Session) { func NewSession(gateAddr string, dispatchKey []byte, localPort int) (*Session, error) {
conn, err := kcp.DialWithOptions(gateAddr, "0.0.0.0:"+strconv.Itoa(localPort)) // // DPDK模式需开启
// conn, err := kcp.DialWithOptions(gateAddr, "0.0.0.0:"+strconv.Itoa(localPort))
conn, err := kcp.DialWithOptions(gateAddr)
if err != nil { if err != nil {
logger.Error("kcp client conn to server error: %v", err) logger.Error("kcp client conn to server error: %v", err)
return return nil, err
} }
conn.SetACKNoDelay(true) conn.SetACKNoDelay(true)
conn.SetWriteDelay(false) conn.SetWriteDelay(false)
sendChan := make(chan *hk4egatenet.ProtoMsg, 1000) sendChan := make(chan *hk4egatenet.ProtoMsg, 1000)
recvChan := make(chan *hk4egatenet.ProtoMsg, 1000) recvChan := make(chan *hk4egatenet.ProtoMsg, 1000)
r = &Session{ r := &Session{
SendChan: sendChan, Conn: conn,
RecvChan: recvChan, XorKey: dispatchKey,
conn: conn, SendChan: sendChan,
seed: 0, RecvChan: recvChan,
xorKey: dispatchKey,
changeXorKeyFin: false,
useMagicSeed: true,
} }
go r.recvHandle() go r.recvHandle()
go r.sendHandle() go r.sendHandle()
return r return r, nil
} }
func (s *Session) recvHandle() { func (s *Session) recvHandle() {
logger.Info("recv handle start") logger.Info("recv handle start")
conn := s.conn conn := s.Conn
convId := conn.GetConv() convId := conn.GetConv()
recvBuf := make([]byte, hk4egatenet.PacketMaxLen) recvBuf := make([]byte, hk4egatenet.PacketMaxLen)
dataBuf := make([]byte, 0, 1500) dataBuf := make([]byte, 0, 1500)
@@ -62,21 +56,11 @@ func (s *Session) recvHandle() {
} }
recvData := recvBuf[:recvLen] recvData := recvBuf[:recvLen]
kcpMsgList := make([]*hk4egatenet.KcpMsg, 0) kcpMsgList := make([]*hk4egatenet.KcpMsg, 0)
hk4egatenet.DecodeBinToPayload(recvData, &dataBuf, convId, &kcpMsgList, s.xorKey) hk4egatenet.DecodeBinToPayload(recvData, &dataBuf, convId, &kcpMsgList, s.XorKey)
for _, v := range kcpMsgList { for _, v := range kcpMsgList {
protoMsgList := hk4egatenet.ProtoDecode(v, cmd.NewCmdProtoMap(), nil) protoMsgList := hk4egatenet.ProtoDecode(v, cmd.NewCmdProtoMap(), nil)
for _, vv := range protoMsgList { for _, vv := range protoMsgList {
s.RecvChan <- vv s.RecvChan <- vv
if s.changeXorKeyFin == false && vv.CmdId == cmd.GetPlayerTokenRsp {
// XOR密钥切换
logger.Info("change session xor key, convId: %v", convId)
s.changeXorKeyFin = true
keyBlock := random.NewKeyBlock(s.seed, s.useMagicSeed)
xorKey := keyBlock.XorKey()
key := make([]byte, 4096)
copy(key, xorKey[:])
s.xorKey = key
}
} }
} }
} }
@@ -84,7 +68,7 @@ func (s *Session) recvHandle() {
func (s *Session) sendHandle() { func (s *Session) sendHandle() {
logger.Info("send handle start") logger.Info("send handle start")
conn := s.conn conn := s.Conn
convId := conn.GetConv() convId := conn.GetConv()
for { for {
protoMsg, ok := <-s.SendChan protoMsg, ok := <-s.SendChan
@@ -98,7 +82,7 @@ func (s *Session) sendHandle() {
logger.Error("decode kcp msg is nil, convId: %v", convId) logger.Error("decode kcp msg is nil, convId: %v", convId)
continue continue
} }
bin := hk4egatenet.EncodePayloadToBin(kcpMsg, s.xorKey) bin := hk4egatenet.EncodePayloadToBin(kcpMsg, s.XorKey)
_ = conn.SetWriteDeadline(time.Now().Add(time.Second * hk4egatenet.ConnSendTimeout)) _ = conn.SetWriteDeadline(time.Now().Add(time.Second * hk4egatenet.ConnSendTimeout))
_, err := conn.Write(bin) _, err := conn.Write(bin)
if err != nil { if err != nil {