mirror of
https://github.com/FlourishingWorld/hk4e.git
synced 2026-02-04 15:52:27 +08:00
优化架构
This commit is contained in:
@@ -10,13 +10,8 @@ var CONF *Config = nil
|
||||
// 配置
|
||||
type Config struct {
|
||||
HttpPort int `toml:"http_port"`
|
||||
KcpPort int `toml:"kcp_port"`
|
||||
Logger Logger `toml:"logger"`
|
||||
Air Air `toml:"air"`
|
||||
Database Database `toml:"database"`
|
||||
Light Light `toml:"light"`
|
||||
Routes []Routes `toml:"routes"`
|
||||
Wxmp Wxmp `toml:"wxmp"`
|
||||
Hk4e Hk4e `toml:"hk4e"`
|
||||
MQ MQ `toml:"mq"`
|
||||
}
|
||||
@@ -28,55 +23,18 @@ type Logger struct {
|
||||
TrackLine bool `toml:"track_line"`
|
||||
}
|
||||
|
||||
// 注册中心配置
|
||||
type Air struct {
|
||||
Addr string `toml:"addr"`
|
||||
Port int `toml:"port"`
|
||||
ServiceName string `toml:"service_name"`
|
||||
}
|
||||
|
||||
// 数据库配置
|
||||
type Database struct {
|
||||
Url string `toml:"url"`
|
||||
}
|
||||
|
||||
// RPC框架配置
|
||||
type Light struct {
|
||||
Port int `toml:"port"`
|
||||
}
|
||||
|
||||
// 路由配置
|
||||
type Routes struct {
|
||||
ServiceName string `toml:"service_name"`
|
||||
ServicePredicates string `toml:"service_predicates"`
|
||||
StripPrefix int `toml:"strip_prefix"`
|
||||
}
|
||||
|
||||
// FWDN服务
|
||||
type Fwdn struct {
|
||||
FwdnCron string `toml:"fwdn_cron"`
|
||||
TestCron string `toml:"test_cron"`
|
||||
QQMailAddr string `toml:"qq_mail_addr"`
|
||||
QQMailName string `toml:"qq_mail_name"`
|
||||
QQMailToken string `toml:"qq_mail_token"`
|
||||
FwMailAddr string `toml:"fw_mail_addr"`
|
||||
}
|
||||
|
||||
// 微信公众号
|
||||
type Wxmp struct {
|
||||
AppId string `toml:"app_id"`
|
||||
RawId string `toml:"raw_id"`
|
||||
Token string `toml:"token"`
|
||||
EncodingAesKey string `toml:"encoding_aes_key"`
|
||||
Fwdn Fwdn `toml:"fwdn"`
|
||||
}
|
||||
|
||||
// 原神相关
|
||||
type Hk4e struct {
|
||||
KcpPort int `toml:"kcp_port"`
|
||||
KcpAddr string `toml:"kcp_addr"`
|
||||
ResourcePath string `toml:"resource_path"`
|
||||
GachaHistoryServer string `toml:"gacha_history_server"`
|
||||
LoginSdkUrl string `toml:"login_sdk_url"`
|
||||
}
|
||||
|
||||
// 消息队列
|
||||
@@ -93,6 +51,7 @@ func InitConfig(filePath string) {
|
||||
func (c *Config) loadConfigFile(filePath string) {
|
||||
_, err := toml.DecodeFile(filePath, &c)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("application.toml load fail ! err: %v", err))
|
||||
info := fmt.Sprintf("config file load error: %v\n", err)
|
||||
panic(info)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
package dto
|
||||
|
||||
type ResponseResult struct {
|
||||
Code int32 `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
Data any `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
func NewResponseResult(code int32, msg string, data any) (r *ResponseResult) {
|
||||
r = new(ResponseResult)
|
||||
r.Code = code
|
||||
r.Msg = msg
|
||||
r.Data = data
|
||||
return r
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
module common
|
||||
|
||||
go 1.19
|
||||
|
||||
require github.com/BurntSushi/toml v0.3.1
|
||||
@@ -1,2 +0,0 @@
|
||||
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
|
||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||
175
common/region/region.go
Normal file
175
common/region/region.go
Normal file
@@ -0,0 +1,175 @@
|
||||
package region
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
pb "google.golang.org/protobuf/proto"
|
||||
"hk4e/common/utils/endec"
|
||||
"hk4e/logger"
|
||||
"hk4e/protocol/proto"
|
||||
"os"
|
||||
)
|
||||
|
||||
func LoadRsaKey() (signRsaKey []byte, encRsaKeyMap map[string][]byte, pwdRsaKey []byte) {
|
||||
var err error = nil
|
||||
encRsaKeyMap = make(map[string][]byte)
|
||||
signRsaKey, err = os.ReadFile("static/region_sign_key.pem")
|
||||
if err != nil {
|
||||
logger.LOG.Error("open region_sign_key.pem error: %v", err)
|
||||
return nil, nil, nil
|
||||
}
|
||||
encKeyIdList := []string{"2", "3", "4", "5"}
|
||||
for _, v := range encKeyIdList {
|
||||
encRsaKeyMap[v], err = os.ReadFile("static/region_enc_key_" + v + ".pem")
|
||||
if err != nil {
|
||||
logger.LOG.Error("open region_enc_key_3.pem error: %v", err)
|
||||
return nil, nil, nil
|
||||
}
|
||||
}
|
||||
pwdRsaKey, err = os.ReadFile("static/account_password_key.pem")
|
||||
if err != nil {
|
||||
logger.LOG.Error("open account_password_key.pem error: %v", err)
|
||||
return nil, nil, nil
|
||||
}
|
||||
return signRsaKey, encRsaKeyMap, pwdRsaKey
|
||||
}
|
||||
|
||||
func InitRegion(kcpAddr string, kcpPort int) (*proto.QueryCurrRegionHttpRsp, *proto.QueryRegionListHttpRsp) {
|
||||
dispatchKey, err := os.ReadFile("static/dispatchKey.bin")
|
||||
if err != nil {
|
||||
logger.LOG.Error("open dispatchKey.bin error: %v", err)
|
||||
return nil, nil
|
||||
}
|
||||
dispatchSeed, err := os.ReadFile("static/dispatchSeed.bin")
|
||||
if err != nil {
|
||||
logger.LOG.Error("open dispatchSeed.bin error: %v", err)
|
||||
return nil, nil
|
||||
}
|
||||
// RegionCurr
|
||||
regionCurr := new(proto.QueryCurrRegionHttpRsp)
|
||||
regionCurr.RegionInfo = &proto.RegionInfo{
|
||||
GateserverIp: kcpAddr,
|
||||
GateserverPort: uint32(kcpPort),
|
||||
SecretKey: dispatchSeed,
|
||||
}
|
||||
// RegionList
|
||||
customConfigStr := `
|
||||
{
|
||||
"sdkenv": "2",
|
||||
"checkdevice": "false",
|
||||
"loadPatch": "false",
|
||||
"showexception": "false",
|
||||
"regionConfig": "pm|fk|add",
|
||||
"downloadMode": "0",
|
||||
}
|
||||
`
|
||||
customConfig := []byte(customConfigStr)
|
||||
endec.Xor(customConfig, dispatchKey)
|
||||
serverList := make([]*proto.RegionSimpleInfo, 0)
|
||||
server := &proto.RegionSimpleInfo{
|
||||
Name: "os_usa",
|
||||
Title: "America",
|
||||
Type: "DEV_PUBLIC",
|
||||
DispatchUrl: "https://osusadispatch.yuanshen.com/query_cur_region",
|
||||
}
|
||||
serverList = append(serverList, server)
|
||||
regionList := new(proto.QueryRegionListHttpRsp)
|
||||
regionList.RegionList = serverList
|
||||
regionList.ClientSecretKey = dispatchSeed
|
||||
regionList.ClientCustomConfigEncrypted = customConfig
|
||||
regionList.EnableLoginPc = true
|
||||
return regionCurr, regionList
|
||||
}
|
||||
|
||||
// 新的region构建方式已经不需要读取query_region_list和query_cur_region文件了 并跳过了版本校验的blk补丁下载
|
||||
func _InitRegion(log *logger.Logger, kcpAddr string, kcpPort int) (*proto.QueryCurrRegionHttpRsp, *proto.QueryRegionListHttpRsp) {
|
||||
// TODO 总有一天要把这些烦人的数据全部自己构造别再他妈的读文件了
|
||||
// 加载文件
|
||||
regionListKeyFile, err := os.ReadFile("static/query_region_list_key")
|
||||
if err != nil {
|
||||
log.Error("open query_region_list_key error")
|
||||
return nil, nil
|
||||
}
|
||||
regionCurrKeyFile, err := os.ReadFile("static/query_cur_region_key")
|
||||
if err != nil {
|
||||
log.Error("open query_cur_region_key error")
|
||||
return nil, nil
|
||||
}
|
||||
regionListFile, err := os.ReadFile("static/query_region_list")
|
||||
if err != nil {
|
||||
log.Error("open query_region_list error")
|
||||
return nil, nil
|
||||
}
|
||||
regionCurrFile, err := os.ReadFile("static/query_cur_region")
|
||||
if err != nil {
|
||||
log.Error("open query_cur_region error")
|
||||
return nil, nil
|
||||
}
|
||||
regionListKeyBin, err := base64.StdEncoding.DecodeString(string(regionListKeyFile))
|
||||
if err != nil {
|
||||
log.Error("decode query_region_list_key error")
|
||||
return nil, nil
|
||||
}
|
||||
regionCurrKeyBin, err := base64.StdEncoding.DecodeString(string(regionCurrKeyFile))
|
||||
if err != nil {
|
||||
log.Error("decode query_cur_region_key error")
|
||||
return nil, nil
|
||||
}
|
||||
regionListBin, err := base64.StdEncoding.DecodeString(string(regionListFile))
|
||||
if err != nil {
|
||||
log.Error("decode query_region_list error")
|
||||
return nil, nil
|
||||
}
|
||||
regionCurrBin, err := base64.StdEncoding.DecodeString(string(regionCurrFile))
|
||||
if err != nil {
|
||||
log.Error("decode query_cur_region error")
|
||||
return nil, nil
|
||||
}
|
||||
regionListKey := new(proto.QueryRegionListHttpRsp)
|
||||
err = pb.Unmarshal(regionListKeyBin, regionListKey)
|
||||
if err != nil {
|
||||
log.Error("Unmarshal QueryRegionListHttpRsp error")
|
||||
return nil, nil
|
||||
}
|
||||
regionCurrKey := new(proto.QueryCurrRegionHttpRsp)
|
||||
err = pb.Unmarshal(regionCurrKeyBin, regionCurrKey)
|
||||
if err != nil {
|
||||
log.Error("Unmarshal QueryCurrRegionHttpRsp error")
|
||||
return nil, nil
|
||||
}
|
||||
regionList := new(proto.QueryRegionListHttpRsp)
|
||||
err = pb.Unmarshal(regionListBin, regionList)
|
||||
if err != nil {
|
||||
log.Error("Unmarshal QueryRegionListHttpRsp error")
|
||||
return nil, nil
|
||||
}
|
||||
regionCurr := new(proto.QueryCurrRegionHttpRsp)
|
||||
err = pb.Unmarshal(regionCurrBin, regionCurr)
|
||||
if err != nil {
|
||||
log.Error("Unmarshal QueryCurrRegionHttpRsp error")
|
||||
return nil, nil
|
||||
}
|
||||
secretKey, err := os.ReadFile("static/dispatchSeed.bin")
|
||||
if err != nil {
|
||||
log.Error("open dispatchSeed.bin error")
|
||||
return nil, nil
|
||||
}
|
||||
// RegionCurr
|
||||
regionInfo := regionCurr.GetRegionInfo()
|
||||
regionInfo.GateserverIp = kcpAddr
|
||||
regionInfo.GateserverPort = uint32(kcpPort)
|
||||
regionInfo.SecretKey = secretKey
|
||||
regionCurr.RegionInfo = regionInfo
|
||||
// RegionList
|
||||
serverList := make([]*proto.RegionSimpleInfo, 0)
|
||||
server := &proto.RegionSimpleInfo{
|
||||
Name: "os_usa",
|
||||
Title: "America",
|
||||
Type: "DEV_PUBLIC",
|
||||
DispatchUrl: "https://osusadispatch.yuanshen.com/query_cur_region",
|
||||
}
|
||||
serverList = append(serverList, server)
|
||||
regionList.RegionList = serverList
|
||||
regionList.ClientSecretKey = regionListKey.ClientSecretKey
|
||||
regionList.ClientCustomConfigEncrypted = regionListKey.ClientCustomConfigEncrypted
|
||||
return regionCurr, regionList
|
||||
}
|
||||
35
common/region/region_test.go
Normal file
35
common/region/region_test.go
Normal file
File diff suppressed because one or more lines are too long
75
common/utils/httpclient/http_client.go
Normal file
75
common/utils/httpclient/http_client.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package httpclient
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
var httpClient http.Client
|
||||
|
||||
func init() {
|
||||
httpClient = http.Client{
|
||||
Transport: &http.Transport{
|
||||
DisableKeepAlives: true,
|
||||
},
|
||||
Timeout: time.Second * 60,
|
||||
}
|
||||
}
|
||||
|
||||
func Get[T any](url string, token string) (*T, error) {
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if token != "" {
|
||||
req.Header.Set("Authorization", "Bearer"+" "+token)
|
||||
}
|
||||
rsp, err := httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
data, err := ioutil.ReadAll(rsp.Body)
|
||||
_ = rsp.Body.Close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
responseData := new(T)
|
||||
err = json.Unmarshal(data, responseData)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return responseData, nil
|
||||
}
|
||||
|
||||
func Post[T any](url string, body any, token string) (*T, error) {
|
||||
reqData, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(reqData))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
if token != "" {
|
||||
req.Header.Set("Authorization", "Bearer"+" "+token)
|
||||
}
|
||||
rsp, err := httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rspData, err := ioutil.ReadAll(rsp.Body)
|
||||
_ = rsp.Body.Close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
responseData := new(T)
|
||||
err = json.Unmarshal(rspData, responseData)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return responseData, nil
|
||||
}
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func ObjectDeepCopy(src, dest any) error {
|
||||
func DeepCopy(src, dest any) error {
|
||||
var buf bytes.Buffer
|
||||
if err := gob.NewEncoder(&buf).Encode(src); err != nil {
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user