完善了服务器上下线,http登录负载均衡下发gate地址

This commit is contained in:
flswld
2022-12-24 16:52:08 +08:00
parent 7e86669628
commit f4614b3df6
16 changed files with 569 additions and 119 deletions

View File

@@ -24,6 +24,7 @@ func Run(ctx context.Context, configFile string) error {
db := dao.NewDao()
defer db.CloseDao()
// natsrpc client
client, err := rpc.NewClient()
if err != nil {
return err

View File

@@ -2,7 +2,6 @@ package controller
import (
"context"
"encoding/base64"
"net/http"
"strconv"
@@ -15,25 +14,21 @@ import (
"hk4e/pkg/random"
"github.com/gin-gonic/gin"
pb "google.golang.org/protobuf/proto"
)
type Controller struct {
dao *dao.Dao
discovery *rpc.DiscoveryClient
regionListBase64 string
regionCurrBase64 string
signRsaKey []byte
encRsaKeyMap map[string][]byte
pwdRsaKey []byte
dao *dao.Dao
discovery *rpc.DiscoveryClient
signRsaKey []byte
encRsaKeyMap map[string][]byte
pwdRsaKey []byte
ec2b *random.Ec2b
}
func NewController(dao *dao.Dao, discovery *rpc.DiscoveryClient) (r *Controller) {
r = new(Controller)
r.dao = dao
r.discovery = discovery
r.regionListBase64 = ""
r.regionCurrBase64 = ""
r.signRsaKey, r.encRsaKeyMap, r.pwdRsaKey = region.LoadRsaKey()
rsp, err := r.discovery.GetRegionEc2B(context.TODO(), &api.NullMsg{})
if err != nil {
@@ -45,19 +40,7 @@ func NewController(dao *dao.Dao, discovery *rpc.DiscoveryClient) (r *Controller)
logger.Error("parse region ec2b error: %v", err)
return nil
}
regionCurr, regionList, _ := region.InitRegion(config.CONF.Hk4e.KcpAddr, config.CONF.Hk4e.KcpPort, ec2b)
regionCurrModify, err := pb.Marshal(regionCurr)
if err != nil {
logger.Error("Marshal QueryCurrRegionHttpRsp error")
return nil
}
r.regionCurrBase64 = base64.StdEncoding.EncodeToString(regionCurrModify)
regionListModify, err := pb.Marshal(regionList)
if err != nil {
logger.Error("Marshal QueryRegionListHttpRsp error")
return nil
}
r.regionListBase64 = base64.StdEncoding.EncodeToString(regionListModify)
r.ec2b = ec2b
go r.registerRouter()
return r
}

View File

@@ -9,7 +9,9 @@ import (
"regexp"
"strconv"
"hk4e/dispatch/api"
"hk4e/common/region"
httpapi "hk4e/dispatch/api"
"hk4e/node/api"
"hk4e/pkg/endec"
"hk4e/pkg/logger"
@@ -17,6 +19,7 @@ import (
)
func (c *Controller) query_security_file(context *gin.Context) {
return
file, err := os.ReadFile("static/security_file")
if err != nil {
logger.Error("open security_file error")
@@ -28,14 +31,21 @@ func (c *Controller) query_security_file(context *gin.Context) {
func (c *Controller) query_region_list(context *gin.Context) {
context.Header("Content-type", "text/html; charset=UTF-8")
_, _ = context.Writer.WriteString(c.regionListBase64)
regionListBase64 := region.GetRegionListBase64(c.ec2b)
_, _ = context.Writer.WriteString(regionListBase64)
}
func (c *Controller) query_cur_region(context *gin.Context) {
versionName := context.Query("version")
response := "CAESGE5vdCBGb3VuZCB2ZXJzaW9uIGNvbmZpZw=="
if len(context.Request.URL.RawQuery) > 0 {
response = c.regionCurrBase64
addr, err := c.discovery.GetGateServerAddr(context.Request.Context(), &api.NullMsg{})
if err != nil {
logger.Error("get gate server addr error: %v", err)
return
}
regionCurrBase64 := region.GetRegionCurrBase64(addr.IpAddr, int32(addr.Port), c.ec2b)
response = regionCurrBase64
}
reg, err := regexp.Compile("[0-9]+")
if err != nil {
@@ -62,7 +72,7 @@ func (c *Controller) query_cur_region(context *gin.Context) {
if version >= 275 {
logger.Debug("do hk4e 2.8 rsa logic")
if context.Query("dispatchSeed") == "" {
rsp := &api.QueryCurRegionRspJson{
rsp := &httpapi.QueryCurRegionRspJson{
Content: response,
Sign: "TW9yZSBsb3ZlIGZvciBVQSBQYXRjaCBwbGF5ZXJz",
}
@@ -133,7 +143,7 @@ func (c *Controller) query_cur_region(context *gin.Context) {
logger.Error("rsa verify test fail")
return
}
rsp := &api.QueryCurRegionRspJson{
rsp := &httpapi.QueryCurRegionRspJson{
Content: base64.StdEncoding.EncodeToString(encryptedRegionInfo),
Sign: base64.StdEncoding.EncodeToString(signData),
}