Files
2025-06-20 03:10:44 +08:00

149 lines
4.0 KiB
Go

package app
import (
"context"
"encoding/base64"
"encoding/hex"
"os"
"os/signal"
"strconv"
"strings"
"sync"
"syscall"
"time"
"hk4e/common/config"
"hk4e/pkg/endec"
"hk4e/protocol/cmd"
"hk4e/protocol/proto"
"hk4e/robot/client"
"hk4e/robot/login"
"github.com/flswld/halo/logger"
)
var APPVERSION string
func Run(ctx context.Context) error {
logger.InitLogger(&logger.Config{
AppName: "robot",
Level: logger.ParseLevel(config.GetConfig().Logger.Level),
TrackLine: config.GetConfig().Logger.TrackLine,
TrackThread: config.GetConfig().Logger.TrackThread,
EnableFile: config.GetConfig().Logger.EnableFile,
DisableColor: config.GetConfig().Logger.DisableColor,
EnableJson: config.GetConfig().Logger.EnableJson,
})
defer func() {
logger.CloseLogger()
}()
logger.Warn("robot start")
defer func() {
logger.Warn("robot exit")
}()
go runRobot()
c := make(chan os.Signal, 1)
if !config.GetConfig().Hk4e.StandaloneModeEnable {
signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
}
for {
select {
case <-ctx.Done():
return nil
case s := <-c:
logger.Warn("get a signal %s", s.String())
switch s {
case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT:
return nil
case syscall.SIGHUP:
default:
return nil
}
}
}
}
func runRobot() {
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), wg)
}
wg.Wait()
time.Sleep(time.Millisecond * 10)
}
} else {
httpLogin(config.GetConfig().Hk4eRobot.Account, nil)
}
}
func httpLogin(account string, wg *sync.WaitGroup) {
defer func() {
if config.GetConfig().Hk4eRobot.DosEnable {
wg.Done()
}
}()
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
}
accountInfo, err := login.AccountLogin(config.GetConfig().Hk4eRobot.LoginSdkUrl, account, config.GetConfig().Hk4eRobot.Password)
if err != nil {
logger.Error("account login error: %v", err)
return
}
logger.Info("robot http login ok, account: %v", account)
go func() {
for {
gateLogin(account, dispatchInfo, accountInfo)
if !config.GetConfig().Hk4eRobot.DosLoopLogin {
break
}
time.Sleep(time.Second)
continue
}
}()
}
func gateLogin(account string, dispatchInfo *login.DispatchInfo, accountInfo *login.AccountInfo) {
session, err := login.GateLogin(dispatchInfo, accountInfo, config.GetConfig().Hk4eRobot.KeyId)
if err != nil {
logger.Error("gate login error: %v", err)
return
}
logger.Info("robot gate login ok, account: %v", account)
clientVersionHashData, err := hex.DecodeString(
endec.Sha1Str(config.GetConfig().Hk4eRobot.ClientVersion + session.ClientVersionRandomKey + "mhy2020"),
)
if err != nil {
logger.Error("gen clientVersionHashData error: %v", err)
return
}
checksumClientVersion := strings.Split(config.GetConfig().Hk4eRobot.ClientVersion, "_")[0]
session.SendMsg(cmd.PlayerLoginReq, &proto.PlayerLoginReq{
AccountType: 1,
SubChannelId: 1,
LanguageType: 2,
PlatformType: 3,
Checksum: "$008094416f86a051270e64eb0b405a38825",
ChecksumClientVersion: checksumClientVersion,
ClientDataVersion: 11793813,
ClientVerisonHash: base64.StdEncoding.EncodeToString(clientVersionHashData),
ClientVersion: config.GetConfig().Hk4eRobot.ClientVersion,
SecurityCmdReply: session.SecurityCmdBuffer,
SecurityLibraryMd5: "574a507ffee2eb6f997d11f71c8ae1fa",
Token: accountInfo.ComboToken,
})
client.Logic(account, session)
}