gate到robot转发模式
This commit is contained in:
1 parent
8d039dee06
commit
12a8381c43
34 files changed
+3132
-440
No files matched your search
+46
-10
@@ -6,33 +6,69 @@ import (
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"hk4e/common/config"
|
||||
"hk4e/common/mq"
|
||||
"hk4e/common/rpc"
|
||||
"hk4e/dispatch/controller"
|
||||
"hk4e/dispatch/dao"
|
||||
"hk4e/node/api"
|
||||
"hk4e/pkg/logger"
|
||||
)
|
||||
|
||||
var APPID string
|
||||
|
||||
func Run(ctx context.Context, configFile string) error {
|
||||
config.InitConfig(configFile)
|
||||
|
||||
logger.InitLogger("dispatch")
|
||||
logger.Warn("dispatch start")
|
||||
defer func() {
|
||||
logger.CloseLogger()
|
||||
}()
|
||||
|
||||
db := dao.NewDao()
|
||||
defer db.CloseDao()
|
||||
|
||||
// natsrpc client
|
||||
discoveryClient, err := rpc.NewDiscoveryClient()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_ = controller.NewController(db, discoveryClient)
|
||||
// 注册到节点服务器
|
||||
rsp, err := discoveryClient.RegisterServer(context.TODO(), &api.RegisterServerReq{
|
||||
ServerType: api.DISPATCH,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
APPID = rsp.GetAppId()
|
||||
go func() {
|
||||
ticker := time.NewTicker(time.Second * 15)
|
||||
for {
|
||||
<-ticker.C
|
||||
_, err := discoveryClient.KeepaliveServer(context.TODO(), &api.KeepaliveServerReq{
|
||||
ServerType: api.DISPATCH,
|
||||
AppId: APPID,
|
||||
})
|
||||
if err != nil {
|
||||
logger.Error("keepalive error: %v", err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
defer func() {
|
||||
_, _ = discoveryClient.CancelServer(context.TODO(), &api.CancelServerReq{
|
||||
ServerType: api.DISPATCH,
|
||||
AppId: APPID,
|
||||
})
|
||||
}()
|
||||
|
||||
logger.InitLogger("dispatch_" + APPID)
|
||||
logger.Warn("dispatch start, appid: %v", APPID)
|
||||
defer func() {
|
||||
logger.CloseLogger()
|
||||
}()
|
||||
|
||||
messageQueue := mq.NewMessageQueue(api.DISPATCH, APPID, discoveryClient)
|
||||
defer messageQueue.Close()
|
||||
|
||||
db := dao.NewDao()
|
||||
defer db.CloseDao()
|
||||
|
||||
_ = controller.NewController(db, discoveryClient, messageQueue)
|
||||
|
||||
c := make(chan os.Signal, 1)
|
||||
signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"strconv"
|
||||
|
||||
"hk4e/common/config"
|
||||
"hk4e/common/mq"
|
||||
"hk4e/common/region"
|
||||
"hk4e/common/rpc"
|
||||
"hk4e/dispatch/dao"
|
||||
@@ -24,9 +25,10 @@ type Controller struct {
|
||||
encRsaKeyMap map[string][]byte
|
||||
pwdRsaKey []byte
|
||||
ec2b *random.Ec2b
|
||||
messageQueue *mq.MessageQueue
|
||||
}
|
||||
|
||||
func NewController(dao *dao.Dao, discovery *rpc.DiscoveryClient) (r *Controller) {
|
||||
func NewController(dao *dao.Dao, discovery *rpc.DiscoveryClient, messageQueue *mq.MessageQueue) (r *Controller) {
|
||||
r = new(Controller)
|
||||
r.dao = dao
|
||||
r.discovery = discovery
|
||||
@@ -42,6 +44,7 @@ func NewController(dao *dao.Dao, discovery *rpc.DiscoveryClient) (r *Controller)
|
||||
return nil
|
||||
}
|
||||
r.ec2b = ec2b
|
||||
r.messageQueue = messageQueue
|
||||
go r.registerRouter()
|
||||
return r
|
||||
}
|
||||
|
||||
@@ -3,19 +3,27 @@ package controller
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"math"
|
||||
"net/http"
|
||||
"os"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"hk4e/common/config"
|
||||
"hk4e/common/mq"
|
||||
"hk4e/common/region"
|
||||
httpapi "hk4e/dispatch/api"
|
||||
"hk4e/node/api"
|
||||
"hk4e/pkg/endec"
|
||||
"hk4e/pkg/httpclient"
|
||||
"hk4e/pkg/logger"
|
||||
"hk4e/pkg/random"
|
||||
"hk4e/protocol/proto"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
pb "google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
func (c *Controller) querySecurityFile(context *gin.Context) {
|
||||
@@ -32,7 +40,45 @@ func (c *Controller) querySecurityFile(context *gin.Context) {
|
||||
|
||||
func (c *Controller) queryRegionList(context *gin.Context) {
|
||||
context.Header("Content-type", "text/html; charset=UTF-8")
|
||||
regionListBase64 := region.GetRegionListBase64(c.ec2b)
|
||||
var regionListBase64 = ""
|
||||
if !config.GetConfig().Hk4e.ForwardModeEnable {
|
||||
regionList := region.GetRegionList(c.ec2b)
|
||||
regionListData, err := pb.Marshal(regionList)
|
||||
if err != nil {
|
||||
logger.Error("pb marshal QueryRegionListHttpRsp error: %v", err)
|
||||
_, _ = context.Writer.WriteString("500")
|
||||
return
|
||||
}
|
||||
regionListBase64 = base64.StdEncoding.EncodeToString(regionListData)
|
||||
} else {
|
||||
var err error = nil
|
||||
url := context.Request.RequestURI
|
||||
param := url[strings.Index(url, "?"):]
|
||||
regionListBase64Raw, err := httpclient.GetRaw(config.GetConfig().Hk4e.ForwardRegionUrl + param)
|
||||
if err != nil {
|
||||
_, _ = context.Writer.WriteString("500")
|
||||
return
|
||||
}
|
||||
regionListDataRaw, err := base64.StdEncoding.DecodeString(regionListBase64Raw)
|
||||
if err != nil {
|
||||
_, _ = context.Writer.WriteString("500")
|
||||
return
|
||||
}
|
||||
queryRegionListHttpRsp := new(proto.QueryRegionListHttpRsp)
|
||||
err = pb.Unmarshal(regionListDataRaw, queryRegionListHttpRsp)
|
||||
if err != nil {
|
||||
_, _ = context.Writer.WriteString("500")
|
||||
return
|
||||
}
|
||||
logger.Debug("QueryRegionListHttpRsp: %+v", queryRegionListHttpRsp)
|
||||
queryRegionListHttpRsp.RegionList[0].DispatchUrl = config.GetConfig().Hk4e.DispatchUrl
|
||||
regionListData, err := pb.Marshal(queryRegionListHttpRsp)
|
||||
if err != nil {
|
||||
_, _ = context.Writer.WriteString("500")
|
||||
return
|
||||
}
|
||||
regionListBase64 = base64.StdEncoding.EncodeToString(regionListData)
|
||||
}
|
||||
_, _ = context.Writer.WriteString(regionListBase64)
|
||||
}
|
||||
|
||||
@@ -81,6 +127,13 @@ func (c *Controller) queryCurRegion(context *gin.Context) {
|
||||
rspError()
|
||||
return
|
||||
}
|
||||
keyId := context.Query("key_id")
|
||||
encPubPrivKey, exist := c.encRsaKeyMap[keyId]
|
||||
if !exist {
|
||||
logger.Error("can not found key id: %v", keyId)
|
||||
rspError()
|
||||
return
|
||||
}
|
||||
version, versionStr := c.getClientVersionByName(versionName)
|
||||
if version == 0 {
|
||||
rspError()
|
||||
@@ -94,33 +147,113 @@ func (c *Controller) queryCurRegion(context *gin.Context) {
|
||||
rspError()
|
||||
return
|
||||
}
|
||||
regionCurrBase64 := region.GetRegionCurrBase64(addr.KcpAddr, int32(addr.KcpPort), c.ec2b)
|
||||
var regionCurr *proto.QueryCurrRegionHttpRsp = nil
|
||||
if !config.GetConfig().Hk4e.ForwardModeEnable {
|
||||
regionCurr = region.GetRegionCurr(addr.KcpAddr, int32(addr.KcpPort), c.ec2b)
|
||||
} else {
|
||||
url := context.Request.RequestURI
|
||||
param := url[strings.Index(url, "?"):]
|
||||
regionCurrJson, err := httpclient.GetRaw(config.GetConfig().Hk4e.ForwardDispatchUrl + param)
|
||||
if err != nil {
|
||||
rspError()
|
||||
return
|
||||
}
|
||||
queryCurRegionRspJson := new(httpapi.QueryCurRegionRspJson)
|
||||
err = json.Unmarshal([]byte(regionCurrJson), queryCurRegionRspJson)
|
||||
if err != nil {
|
||||
rspError()
|
||||
return
|
||||
}
|
||||
encryptedRegionInfo, err := base64.StdEncoding.DecodeString(queryCurRegionRspJson.Content)
|
||||
if err != nil {
|
||||
rspError()
|
||||
return
|
||||
}
|
||||
chunkSize := 256
|
||||
regionInfoLength := len(encryptedRegionInfo)
|
||||
numChunks := int(math.Ceil(float64(regionInfoLength) / float64(chunkSize)))
|
||||
regionCurrData := make([]byte, 0)
|
||||
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)
|
||||
rspError()
|
||||
return
|
||||
}
|
||||
decrypt, err := endec.RsaDecrypt(chunk, privKey)
|
||||
if err != nil {
|
||||
logger.Error("rsa dec error: %v", err)
|
||||
rspError()
|
||||
return
|
||||
}
|
||||
regionCurrData = append(regionCurrData, decrypt...)
|
||||
}
|
||||
queryCurrRegionHttpRsp := new(proto.QueryCurrRegionHttpRsp)
|
||||
err = pb.Unmarshal(regionCurrData, queryCurrRegionHttpRsp)
|
||||
if err != nil {
|
||||
rspError()
|
||||
return
|
||||
}
|
||||
logger.Debug("QueryCurrRegionHttpRsp: %+v", queryCurrRegionHttpRsp)
|
||||
robotServerAppId, err := c.discovery.GetServerAppId(context, &api.GetServerAppIdReq{
|
||||
ServerType: api.ROBOT,
|
||||
})
|
||||
if err != nil {
|
||||
logger.Error("get robot server appid error: %v", err)
|
||||
rspError()
|
||||
return
|
||||
}
|
||||
ec2b, err := random.LoadEc2bKey(queryCurrRegionHttpRsp.ClientSecretKey)
|
||||
if err != nil {
|
||||
logger.Error("parse ec2b error: %v", err)
|
||||
rspError()
|
||||
return
|
||||
}
|
||||
c.messageQueue.SendToRobot(robotServerAppId.AppId, &mq.NetMsg{
|
||||
MsgType: mq.MsgTypeServer,
|
||||
EventId: mq.ServerForwardDispatchInfoNotify,
|
||||
ServerMsg: &mq.ServerMsg{
|
||||
ForwardDispatchInfo: &mq.ForwardDispatchInfo{
|
||||
GateIp: queryCurrRegionHttpRsp.RegionInfo.GateserverIp,
|
||||
GatePort: queryCurrRegionHttpRsp.RegionInfo.GateserverPort,
|
||||
DispatchKey: ec2b.XorKey(),
|
||||
},
|
||||
},
|
||||
})
|
||||
regionCurr = queryCurrRegionHttpRsp
|
||||
regionCurr.ClientSecretKey = c.ec2b.Bytes()
|
||||
regionCurr.RegionInfo.GateserverIp = addr.KcpAddr
|
||||
regionCurr.RegionInfo.GateserverPort = addr.KcpPort
|
||||
endec.Xor(regionCurr.RegionCustomConfigEncrypted, ec2b.XorKey())
|
||||
logger.Info("RegionCustomConfigEncrypted: %v", string(regionCurr.RegionCustomConfigEncrypted))
|
||||
endec.Xor(regionCurr.RegionCustomConfigEncrypted, c.ec2b.XorKey())
|
||||
endec.Xor(regionCurr.ClientRegionCustomConfigEncrypted, ec2b.XorKey())
|
||||
logger.Info("ClientRegionCustomConfigEncrypted: %v", string(regionCurr.ClientRegionCustomConfigEncrypted))
|
||||
endec.Xor(regionCurr.ClientRegionCustomConfigEncrypted, c.ec2b.XorKey())
|
||||
}
|
||||
regionCurrData, err := pb.Marshal(regionCurr)
|
||||
if err != nil {
|
||||
logger.Error("pb marshal QueryCurrRegionHttpRsp error: %v", err)
|
||||
rspError()
|
||||
return
|
||||
}
|
||||
if version < 275 {
|
||||
context.Header("Content-type", "text/html; charset=UTF-8")
|
||||
regionCurrBase64 := base64.StdEncoding.EncodeToString(regionCurrData)
|
||||
_, _ = context.Writer.WriteString(regionCurrBase64)
|
||||
return
|
||||
}
|
||||
keyId := context.Query("key_id")
|
||||
encPubPrivKey, exist := c.encRsaKeyMap[keyId]
|
||||
if !exist {
|
||||
logger.Error("can not found key id: %v", keyId)
|
||||
rspError()
|
||||
return
|
||||
}
|
||||
regionInfo, err := base64.StdEncoding.DecodeString(regionCurrBase64)
|
||||
if err != nil {
|
||||
logger.Error("decode region info error: %v", err)
|
||||
rspError()
|
||||
return
|
||||
}
|
||||
chunkSize := 256 - 11
|
||||
regionInfoLength := len(regionInfo)
|
||||
regionInfoLength := len(regionCurrData)
|
||||
numChunks := int(math.Ceil(float64(regionInfoLength) / float64(chunkSize)))
|
||||
encryptedRegionInfo := make([]byte, 0)
|
||||
for i := 0; i < numChunks; i++ {
|
||||
from := i * chunkSize
|
||||
to := int(math.Min(float64((i+1)*chunkSize), float64(regionInfoLength)))
|
||||
chunk := regionInfo[from:to]
|
||||
chunk := regionCurrData[from:to]
|
||||
pubKey, err := endec.RsaParsePubKeyByPrivKey(encPubPrivKey)
|
||||
if err != nil {
|
||||
logger.Error("parse rsa pub key error: %v", err)
|
||||
@@ -158,13 +291,13 @@ func (c *Controller) queryCurRegion(context *gin.Context) {
|
||||
rspError()
|
||||
return
|
||||
}
|
||||
signData, err := endec.RsaSign(regionInfo, signPrivkey)
|
||||
signData, err := endec.RsaSign(regionCurrData, signPrivkey)
|
||||
if err != nil {
|
||||
logger.Error("rsa sign error: %v", err)
|
||||
rspError()
|
||||
return
|
||||
}
|
||||
ok, err := endec.RsaVerify(regionInfo, signData, &signPrivkey.PublicKey)
|
||||
ok, err := endec.RsaVerify(regionCurrData, signData, &signPrivkey.PublicKey)
|
||||
if err != nil {
|
||||
logger.Error("rsa verify error: %v", err)
|
||||
rspError()
|
||||
|
||||
Reference in new issue
Block a user