Compare commits
9 Commits
feat/instg
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| fc9b32b115 | |||
| ec4c3c0413 | |||
| 456f1970eb | |||
| b1dfb88f71 | |||
| a7266f4bfa | |||
| 2ea538995a | |||
| 174cd09ba6 | |||
| 62ebfa17ea | |||
| 8de7ab4a67 |
20
app/app.go
20
app/app.go
@@ -17,24 +17,18 @@ type Program struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *Program) Init(_ svc.Environment) error {
|
func (p *Program) Init(_ svc.Environment) error {
|
||||||
base := &ModuleBase{}
|
p.moduleList = append(p.moduleList, &module.Log{Cfg: config.Get().Log})
|
||||||
if err := base.Init(); err != nil {
|
p.moduleList = append(p.moduleList, &module.DB{Cfg: config.Get().DB, AppName: config.Get().App.Name})
|
||||||
return err
|
p.moduleList = append(p.moduleList, &module.Snowflake{})
|
||||||
}
|
|
||||||
p.moduleList = append(p.moduleList, base)
|
|
||||||
p.moduleList = append(p.moduleList, (&module.DB{}).Bind(config.Get().DB, common.KeyDiscoverServiceNameGateway))
|
|
||||||
p.moduleList = append(p.moduleList, &ModuleWebServer{})
|
p.moduleList = append(p.moduleList, &ModuleWebServer{})
|
||||||
p.moduleList = append(p.moduleList, &ModuleWebsocketServer{})
|
p.moduleList = append(p.moduleList, &ModuleWebsocketServer{})
|
||||||
p.moduleList = append(p.moduleList, &ModuleLoginQueue{})
|
p.moduleList = append(p.moduleList, &ModuleLoginQueue{})
|
||||||
p.moduleList = append(p.moduleList, (&module.Grpc{}).Bind(grpc_server.NewServer(config.Get().Serve.Grpc)))
|
p.moduleList = append(p.moduleList, &module.Grpc{Server: grpc_server.NewServer(config.Get().Serve.Grpc)})
|
||||||
p.moduleList = append(p.moduleList, (&module.Prometheus{}).Bind(config.Get().Metric))
|
p.moduleList = append(p.moduleList, &module.Prometheus{Cfg: config.Get().Metric})
|
||||||
p.moduleList = append(p.moduleList, (&module.Tracer{}).Bind(config.Get().Metric, common.KeyDiscoverServiceNameGateway))
|
p.moduleList = append(p.moduleList, &module.Tracer{Cfg: config.Get().Metric, ServiceName: common.KeyDiscoverServiceNameGateway})
|
||||||
p.moduleList = append(p.moduleList, &module.Discover{})
|
p.moduleList = append(p.moduleList, &module.Discover{})
|
||||||
|
|
||||||
for i, m := range p.moduleList {
|
for _, m := range p.moduleList {
|
||||||
if i == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if err := m.Init(); err != nil {
|
if err := m.Init(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
27
app/base.go
27
app/base.go
@@ -1,27 +0,0 @@
|
|||||||
package app
|
|
||||||
|
|
||||||
import (
|
|
||||||
"git.hlsq.asia/mmorpg/service-common/log"
|
|
||||||
"git.hlsq.asia/mmorpg/service-common/module"
|
|
||||||
"git.hlsq.asia/mmorpg/service-common/utils"
|
|
||||||
"git.hlsq.asia/mmorpg/service-gateway/config"
|
|
||||||
"math/rand"
|
|
||||||
)
|
|
||||||
|
|
||||||
// ModuleBase 基础模块,或者一些零散的模块
|
|
||||||
type ModuleBase struct {
|
|
||||||
module.DefaultModule
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *ModuleBase) Init() error {
|
|
||||||
// 配置
|
|
||||||
if err := config.LoadConfig(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
cfg := config.Get()
|
|
||||||
// 日志
|
|
||||||
log.Init(cfg.Log.Debug, cfg.Log.MaxSize, cfg.Log.MaxBackups, cfg.Log.MaxAge, cfg.Log.Level)
|
|
||||||
// 雪花
|
|
||||||
utils.InitSnowflake(int64(rand.Intn(1000)))
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -13,19 +13,19 @@ metric:
|
|||||||
address: "0.0.0.0"
|
address: "0.0.0.0"
|
||||||
port: 18504
|
port: 18504
|
||||||
jaeger:
|
jaeger:
|
||||||
endpoint: "localhost:4317"
|
endpoint: "127.0.0.1:4317"
|
||||||
|
|
||||||
db:
|
db:
|
||||||
etcd:
|
etcd:
|
||||||
endpoints: [ "localhost:2379" ]
|
endpoints: [ "127.0.0.1:2379" ]
|
||||||
redis:
|
redis:
|
||||||
addr: "localhost:6379"
|
addr: "127.0.0.1:6379"
|
||||||
password: "lQ7aM8oB6lK0iD5k"
|
password: "lQ7aM8oB6lK0iD5k"
|
||||||
db: 0
|
db: 0
|
||||||
|
|
||||||
serve:
|
serve:
|
||||||
grpc:
|
grpc:
|
||||||
address: "10.0.40.199"
|
address: "127.0.0.1"
|
||||||
port: 18500
|
port: 18500
|
||||||
ttl: 20
|
ttl: 20
|
||||||
socket:
|
socket:
|
||||||
@@ -40,6 +40,5 @@ serve:
|
|||||||
port: 18503
|
port: 18503
|
||||||
|
|
||||||
auth:
|
auth:
|
||||||
secret: "bMa3mU4oCsX2KBex5o7GzwSnACpumFq3SdlDXYZgVTU="
|
shortExpire: 3600
|
||||||
shortExpire: 60
|
longExpire: 604800
|
||||||
longExpire: 10080
|
|
||||||
|
|||||||
@@ -14,7 +14,6 @@ type Config struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type AuthConfig struct {
|
type AuthConfig struct {
|
||||||
Secret string `yaml:"secret"`
|
|
||||||
ShortExpire int64 `yaml:"shortExpire"`
|
ShortExpire int64 `yaml:"shortExpire"`
|
||||||
LongExpire int64 `yaml:"longExpire"`
|
LongExpire int64 `yaml:"longExpire"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,6 +40,5 @@ serve:
|
|||||||
port: 18503
|
port: 18503
|
||||||
|
|
||||||
auth:
|
auth:
|
||||||
secret: "bMa3mU4oCsX2KBex5o7GzwSnACpumFq3SdlDXYZgVTU="
|
shortExpire: 3600
|
||||||
shortExpire: 15
|
longExpire: 604800
|
||||||
longExpire: 10080
|
|
||||||
6
go.mod
6
go.mod
@@ -4,16 +4,16 @@ go 1.24.0
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
bou.ke/monkey v1.0.2
|
bou.ke/monkey v1.0.2
|
||||||
git.hlsq.asia/mmorpg/service-common v0.0.0-20260123132258-4d661d08134e
|
git.hlsq.asia/mmorpg/service-common v0.0.0-20260207051302-0ca8a0ccbb14
|
||||||
github.com/alicebob/miniredis/v2 v2.35.0
|
github.com/alicebob/miniredis/v2 v2.35.0
|
||||||
github.com/gin-contrib/cors v1.7.6
|
github.com/gin-contrib/cors v1.7.6
|
||||||
github.com/gin-gonic/gin v1.11.0
|
github.com/gin-gonic/gin v1.11.0
|
||||||
github.com/golang/mock v1.6.0
|
github.com/golang/mock v1.6.0
|
||||||
|
github.com/google/uuid v1.6.0
|
||||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.27.3
|
github.com/grpc-ecosystem/grpc-gateway/v2 v2.27.3
|
||||||
github.com/judwhite/go-svc v1.2.1
|
github.com/judwhite/go-svc v1.2.1
|
||||||
github.com/panjf2000/gnet/v2 v2.9.7
|
github.com/panjf2000/gnet/v2 v2.9.7
|
||||||
github.com/prometheus/client_golang v1.20.5
|
github.com/prometheus/client_golang v1.20.5
|
||||||
github.com/redis/go-redis/v9 v9.10.0
|
|
||||||
github.com/stretchr/testify v1.11.1
|
github.com/stretchr/testify v1.11.1
|
||||||
go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin v0.64.0
|
go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin v0.64.0
|
||||||
go.uber.org/zap v1.27.0
|
go.uber.org/zap v1.27.0
|
||||||
@@ -58,7 +58,6 @@ require (
|
|||||||
github.com/golang-jwt/jwt/v5 v5.3.0 // indirect
|
github.com/golang-jwt/jwt/v5 v5.3.0 // indirect
|
||||||
github.com/golang/protobuf v1.5.4 // indirect
|
github.com/golang/protobuf v1.5.4 // indirect
|
||||||
github.com/golang/snappy v0.0.4 // indirect
|
github.com/golang/snappy v0.0.4 // indirect
|
||||||
github.com/google/uuid v1.6.0 // indirect
|
|
||||||
github.com/hashicorp/go-uuid v1.0.3 // indirect
|
github.com/hashicorp/go-uuid v1.0.3 // indirect
|
||||||
github.com/jcmturner/aescts/v2 v2.0.0 // indirect
|
github.com/jcmturner/aescts/v2 v2.0.0 // indirect
|
||||||
github.com/jcmturner/dnsutils/v2 v2.0.0 // indirect
|
github.com/jcmturner/dnsutils/v2 v2.0.0 // indirect
|
||||||
@@ -86,6 +85,7 @@ require (
|
|||||||
github.com/quic-go/qpack v0.6.0 // indirect
|
github.com/quic-go/qpack v0.6.0 // indirect
|
||||||
github.com/quic-go/quic-go v0.57.1 // indirect
|
github.com/quic-go/quic-go v0.57.1 // indirect
|
||||||
github.com/rcrowley/go-metrics v0.0.0-20250401214520-65e299d6c5c9 // indirect
|
github.com/rcrowley/go-metrics v0.0.0-20250401214520-65e299d6c5c9 // indirect
|
||||||
|
github.com/redis/go-redis/v9 v9.10.0 // indirect
|
||||||
github.com/sagikazarmark/locafero v0.11.0 // indirect
|
github.com/sagikazarmark/locafero v0.11.0 // indirect
|
||||||
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 // indirect
|
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 // indirect
|
||||||
github.com/spf13/afero v1.15.0 // indirect
|
github.com/spf13/afero v1.15.0 // indirect
|
||||||
|
|||||||
4
go.sum
4
go.sum
@@ -2,8 +2,8 @@ bou.ke/monkey v1.0.2 h1:kWcnsrCNUatbxncxR/ThdYqbytgOIArtYWqcQLQzKLI=
|
|||||||
bou.ke/monkey v1.0.2/go.mod h1:OqickVX3tNx6t33n1xvtTtu85YN5s6cKwVug+oHMaIA=
|
bou.ke/monkey v1.0.2/go.mod h1:OqickVX3tNx6t33n1xvtTtu85YN5s6cKwVug+oHMaIA=
|
||||||
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
|
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
|
||||||
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
|
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
|
||||||
git.hlsq.asia/mmorpg/service-common v0.0.0-20260123132258-4d661d08134e h1:jcDwaL280D5BVGCfcIC3mQCjmwPz5fxX7NO1qf6nAIo=
|
git.hlsq.asia/mmorpg/service-common v0.0.0-20260207051302-0ca8a0ccbb14 h1:4r3tNWRzGVY3Xx6UiGjJJnwoMoWlVqbyGrljxl5d/nQ=
|
||||||
git.hlsq.asia/mmorpg/service-common v0.0.0-20260123132258-4d661d08134e/go.mod h1:mMhZcumphj6gaVTppVYsMTkd+5HupmQgAc53Pd4MH9I=
|
git.hlsq.asia/mmorpg/service-common v0.0.0-20260207051302-0ca8a0ccbb14/go.mod h1:mMhZcumphj6gaVTppVYsMTkd+5HupmQgAc53Pd4MH9I=
|
||||||
github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0=
|
github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0=
|
||||||
github.com/BurntSushi/toml v1.2.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
github.com/BurntSushi/toml v1.2.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
||||||
github.com/IBM/sarama v1.46.3 h1:njRsX6jNlnR+ClJ8XmkO+CM4unbrNr/2vB5KK6UA+IE=
|
github.com/IBM/sarama v1.46.3 h1:njRsX6jNlnR+ClJ8XmkO+CM4unbrNr/2vB5KK6UA+IE=
|
||||||
|
|||||||
@@ -10,11 +10,12 @@ const (
|
|||||||
ServiceStatusStopping // 停止中
|
ServiceStatusStopping // 停止中
|
||||||
)
|
)
|
||||||
|
|
||||||
var GatewaySID string
|
var GatewaySID int64
|
||||||
|
|
||||||
const (
|
const (
|
||||||
KeyGatewayAccessToken = "gateway:token:access:%v"
|
KeyGatewayAccessToken = "gateway:token:access:"
|
||||||
KeyGatewayRefreshToken = "gateway:token:refresh:%v"
|
KeyGatewayRefreshToken = "gateway:token:refresh:"
|
||||||
|
KeyGatewaySession = "gateway:session:"
|
||||||
|
|
||||||
KeyGatewayInfo = "gateway:info:%v"
|
KeyGatewayInfo = "gateway:info:%v"
|
||||||
HFieldInfoGatewaySID = "gateway_sid"
|
HFieldInfoGatewaySID = "gateway_sid"
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ func (s *Server) ToClient(server grpc_pb.Gateway_ToClientServer) error {
|
|||||||
if args, err := server.Recv(); err != nil {
|
if args, err := server.Recv(); err != nil {
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
if args.USN == "" {
|
if args.USN == -1 {
|
||||||
|
|
||||||
//utils.WorkerPool(ws_handler.UserMgr.GetAllInterface(), func(task interface{}) {
|
//utils.WorkerPool(ws_handler.UserMgr.GetAllInterface(), func(task interface{}) {
|
||||||
// client := task.(*ws_handler.Client)
|
// client := task.(*ws_handler.Client)
|
||||||
|
|||||||
@@ -1,18 +1,14 @@
|
|||||||
package http_handler
|
package http_handler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
"git.hlsq.asia/mmorpg/service-common/db/redis"
|
"git.hlsq.asia/mmorpg/service-common/db/redis"
|
||||||
"git.hlsq.asia/mmorpg/service-common/log"
|
"git.hlsq.asia/mmorpg/service-common/log"
|
||||||
"git.hlsq.asia/mmorpg/service-common/net/grpc/grpc_client"
|
"git.hlsq.asia/mmorpg/service-common/net/grpc/grpc_client"
|
||||||
"git.hlsq.asia/mmorpg/service-common/net/http/http_resp"
|
"git.hlsq.asia/mmorpg/service-common/net/http/http_resp"
|
||||||
"git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb"
|
"git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb"
|
||||||
"git.hlsq.asia/mmorpg/service-common/utils"
|
"git.hlsq.asia/mmorpg/service-common/utils"
|
||||||
"git.hlsq.asia/mmorpg/service-gateway/config"
|
|
||||||
"git.hlsq.asia/mmorpg/service-gateway/internal/global"
|
"git.hlsq.asia/mmorpg/service-gateway/internal/global"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// 这个模块处理用户登录
|
// 这个模块处理用户登录
|
||||||
@@ -24,7 +20,7 @@ type LoginReq struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type LoginResp struct {
|
type LoginResp struct {
|
||||||
USN string `json:"usn"`
|
USN int64 `json:"usn"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
AccessToken string `json:"accessToken"`
|
AccessToken string `json:"accessToken"`
|
||||||
RefreshToken string `json:"refreshToken"`
|
RefreshToken string `json:"refreshToken"`
|
||||||
@@ -43,7 +39,7 @@ func Login(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
usn, name := "", ""
|
usn, name := int64(0), ""
|
||||||
if req.Phone != "" {
|
if req.Phone != "" {
|
||||||
// TODO 校验验证码
|
// TODO 校验验证码
|
||||||
login, err := client.PhoneLogin(c, &grpc_pb.PhoneLoginReq{
|
login, err := client.PhoneLogin(c, &grpc_pb.PhoneLoginReq{
|
||||||
@@ -71,7 +67,12 @@ func Login(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
at, rt, err := genToken(c, usn)
|
at, rt, err := sessionLogin(c, usn)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("Login sessionLogin error: %v, usn: %v", err, usn)
|
||||||
|
http_resp.JsonOK(c, http_resp.Error(http_resp.Failed))
|
||||||
|
return
|
||||||
|
}
|
||||||
http_resp.JsonOK(c, http_resp.Success(&LoginResp{
|
http_resp.JsonOK(c, http_resp.Success(&LoginResp{
|
||||||
USN: usn,
|
USN: usn,
|
||||||
Name: name,
|
Name: name,
|
||||||
@@ -81,7 +82,7 @@ func Login(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type RefreshTokenReq struct {
|
type RefreshTokenReq struct {
|
||||||
RefreshToken string `json:"refreshToken" binding:"required,min=1"`
|
RefreshToken string `json:"refreshToken"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type RefreshTokenResp struct {
|
type RefreshTokenResp struct {
|
||||||
@@ -95,18 +96,27 @@ func RefreshToken(c *gin.Context) {
|
|||||||
http_resp.JsonBadRequest(c)
|
http_resp.JsonBadRequest(c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
claims, err := utils.ParseToken(req.RefreshToken, config.Get().Auth.Secret)
|
if req.RefreshToken == "" {
|
||||||
|
cookie, err := c.Cookie("refresh_token")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http_resp.JsonUnauthorized(c)
|
http_resp.JsonUnauthorized(c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if redis.GetClient().Get(c, fmt.Sprintf(global.KeyGatewayRefreshToken, claims.USN)).Val() != req.RefreshToken {
|
req.RefreshToken = cookie
|
||||||
|
}
|
||||||
|
|
||||||
|
usn, _ := redis.GetClient().HGet(c, global.KeyGatewayRefreshToken+req.RefreshToken, (&utils.UserSession{}).GetUsnKey()).Int64()
|
||||||
|
if usn == 0 {
|
||||||
http_resp.JsonUnauthorized(c)
|
http_resp.JsonUnauthorized(c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
at, rt, err := genToken(c, claims.USN)
|
|
||||||
|
if err := sessionLogout(c, req.RefreshToken); err != nil {
|
||||||
|
log.Errorf("RefreshToken sessionLogout error: %v, usn: %v", err, usn)
|
||||||
|
}
|
||||||
|
at, rt, err := sessionLogin(c, usn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("RefreshToken genToken error: %v, usn: %v", err, claims.USN)
|
log.Errorf("RefreshToken sessionLogin error: %v, usn: %v", err, usn)
|
||||||
http_resp.JsonOK(c, http_resp.Error(http_resp.Failed))
|
http_resp.JsonOK(c, http_resp.Error(http_resp.Failed))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -117,23 +127,29 @@ func RefreshToken(c *gin.Context) {
|
|||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
func genToken(ctx context.Context, usn string) (string, string, error) {
|
type LogoutReq struct {
|
||||||
at, err := genTokenOne(ctx, global.KeyGatewayAccessToken, usn, time.Duration(config.Get().Auth.ShortExpire)*time.Minute)
|
RefreshToken string `json:"refreshToken"`
|
||||||
if err != nil {
|
|
||||||
return "", "", err
|
|
||||||
}
|
|
||||||
rt, err := genTokenOne(ctx, global.KeyGatewayRefreshToken, usn, time.Duration(config.Get().Auth.LongExpire)*time.Minute)
|
|
||||||
if err != nil {
|
|
||||||
return "", "", err
|
|
||||||
}
|
|
||||||
return at, rt, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func genTokenOne(ctx context.Context, key string, usn string, ttl time.Duration) (string, error) {
|
type LogoutResp struct {
|
||||||
token, err := utils.GenToken(usn, config.Get().Auth.Secret, ttl)
|
}
|
||||||
|
|
||||||
|
func Logout(c *gin.Context) {
|
||||||
|
req := &LogoutReq{}
|
||||||
|
if err := c.ShouldBindJSON(req); err != nil {
|
||||||
|
http_resp.JsonBadRequest(c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if req.RefreshToken == "" {
|
||||||
|
cookie, err := c.Cookie("refresh_token")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
http_resp.JsonUnauthorized(c)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
redis.GetClient().Set(ctx, fmt.Sprintf(key, usn), token, ttl)
|
req.RefreshToken = cookie
|
||||||
return token, err
|
}
|
||||||
|
if err := sessionLogout(c, req.RefreshToken); err != nil {
|
||||||
|
log.Errorf("Logout sessionLogout error: %v", err)
|
||||||
|
}
|
||||||
|
http_resp.JsonOK(c, http_resp.Success(&LogoutResp{}))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,23 +2,18 @@ package http_handler
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bou.ke/monkey"
|
"bou.ke/monkey"
|
||||||
"context"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.hlsq.asia/mmorpg/service-common/db/redis"
|
|
||||||
"git.hlsq.asia/mmorpg/service-common/net/grpc/grpc_client"
|
"git.hlsq.asia/mmorpg/service-common/net/grpc/grpc_client"
|
||||||
"git.hlsq.asia/mmorpg/service-common/net/http/http_resp"
|
"git.hlsq.asia/mmorpg/service-common/net/http/http_resp"
|
||||||
"git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb"
|
"git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb"
|
||||||
"git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb/mocks"
|
"git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb/mocks"
|
||||||
"git.hlsq.asia/mmorpg/service-common/utils"
|
"git.hlsq.asia/mmorpg/service-common/utils"
|
||||||
"git.hlsq.asia/mmorpg/service-gateway/internal/global"
|
|
||||||
"git.hlsq.asia/mmorpg/service-gateway/internal/testutil"
|
"git.hlsq.asia/mmorpg/service-gateway/internal/testutil"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/golang/mock/gomock"
|
"github.com/golang/mock/gomock"
|
||||||
redis2 "github.com/redis/go-redis/v9"
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/suite"
|
"github.com/stretchr/testify/suite"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -26,17 +21,6 @@ type LoginTestSuite struct {
|
|||||||
testutil.TestSuite
|
testutil.TestSuite
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ts *LoginTestSuite) TestGenToken() {
|
|
||||||
usn := utils.RandInt(1, 100000000)
|
|
||||||
at, rt, err := genToken(context.Background(), strconv.Itoa(usn))
|
|
||||||
ts.Assert().NoError(err)
|
|
||||||
|
|
||||||
redisAt := redis.GetClient().Get(context.Background(), fmt.Sprintf(global.KeyGatewayAccessToken, usn)).Val()
|
|
||||||
ts.Assert().Equal(at, redisAt)
|
|
||||||
redisRt := redis.GetClient().Get(context.Background(), fmt.Sprintf(global.KeyGatewayRefreshToken, usn)).Val()
|
|
||||||
ts.Assert().Equal(rt, redisRt)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ts *LoginTestSuite) TestLogin() {
|
func (ts *LoginTestSuite) TestLogin() {
|
||||||
gin.SetMode(gin.TestMode)
|
gin.SetMode(gin.TestMode)
|
||||||
|
|
||||||
@@ -90,7 +74,7 @@ func (ts *LoginTestSuite) TestLogin() {
|
|||||||
client := mocks.NewMockUserClient(ctrl)
|
client := mocks.NewMockUserClient(ctrl)
|
||||||
client.EXPECT().
|
client.EXPECT().
|
||||||
PhoneLogin(gomock.Any(), gomock.Any()).
|
PhoneLogin(gomock.Any(), gomock.Any()).
|
||||||
Return(&grpc_pb.PhoneLoginResp{USN: "1", Name: "hh"}, nil)
|
Return(&grpc_pb.PhoneLoginResp{USN: 1, Name: "hh"}, nil)
|
||||||
|
|
||||||
monkey.Patch(grpc_client.UserNewClientLB, func() (grpc_pb.UserClient, error) {
|
monkey.Patch(grpc_client.UserNewClientLB, func() (grpc_pb.UserClient, error) {
|
||||||
return client, nil
|
return client, nil
|
||||||
@@ -130,41 +114,41 @@ func (ts *LoginTestSuite) TestRefreshToken() {
|
|||||||
utils.AssertResponse(&ts.Suite, w, http.StatusOK, http_resp.TokenInvalid)
|
utils.AssertResponse(&ts.Suite, w, http.StatusOK, http_resp.TokenInvalid)
|
||||||
})
|
})
|
||||||
|
|
||||||
ts.Run("Redis Get Failed", func() {
|
//ts.Run("Redis Get Failed", func() {
|
||||||
monkey.Patch(utils.ParseToken, func(tokenString string, secret string) (*utils.Claims, error) {
|
// monkey.Patch(utils.ParseToken, func(tokenString string, secret string) (*utils.Claims, error) {
|
||||||
if tokenString == "abc" {
|
// if tokenString == "abc" {
|
||||||
return &utils.Claims{USN: "1"}, nil
|
// return &utils.Claims{USN: 1}, nil
|
||||||
}
|
// }
|
||||||
return nil, assert.AnError
|
// return nil, assert.AnError
|
||||||
})
|
// })
|
||||||
defer monkey.Unpatch(utils.ParseToken)
|
// defer monkey.Unpatch(utils.ParseToken)
|
||||||
|
//
|
||||||
redis.GetClient().Set(context.Background(), fmt.Sprintf(global.KeyGatewayRefreshToken, 1), "ab", redis2.KeepTTL)
|
// redis.GetClient().Set(context.Background(), global.KeyGatewayRefreshToken+1, "ab", redis2.KeepTTL)
|
||||||
|
//
|
||||||
w, c := utils.CreateTestContext("POST", "/", &RefreshTokenReq{
|
// w, c := utils.CreateTestContext("POST", "/", &RefreshTokenReq{
|
||||||
RefreshToken: "abc",
|
// RefreshToken: "abc",
|
||||||
})
|
// })
|
||||||
RefreshToken(c)
|
// RefreshToken(c)
|
||||||
utils.AssertResponse(&ts.Suite, w, http.StatusOK, http_resp.TokenInvalid)
|
// utils.AssertResponse(&ts.Suite, w, http.StatusOK, http_resp.TokenInvalid)
|
||||||
})
|
//})
|
||||||
|
//
|
||||||
ts.Run("OK", func() {
|
//ts.Run("OK", func() {
|
||||||
monkey.Patch(utils.ParseToken, func(tokenString string, secret string) (*utils.Claims, error) {
|
// monkey.Patch(utils.ParseToken, func(tokenString string, secret string) (*utils.Claims, error) {
|
||||||
if tokenString == "abc" {
|
// if tokenString == "abc" {
|
||||||
return &utils.Claims{USN: "1"}, nil
|
// return &utils.Claims{USN: 1}, nil
|
||||||
}
|
// }
|
||||||
return nil, assert.AnError
|
// return nil, assert.AnError
|
||||||
})
|
// })
|
||||||
defer monkey.Unpatch(utils.ParseToken)
|
// defer monkey.Unpatch(utils.ParseToken)
|
||||||
|
//
|
||||||
redis.GetClient().Set(context.Background(), fmt.Sprintf(global.KeyGatewayRefreshToken, 1), "abc", redis2.KeepTTL)
|
// redis.GetClient().Set(context.Background(), fmt.Sprintf(global.KeyGatewayRefreshToken, 1), "abc", redis2.KeepTTL)
|
||||||
|
//
|
||||||
w, c := utils.CreateTestContext("POST", "/", &RefreshTokenReq{
|
// w, c := utils.CreateTestContext("POST", "/", &RefreshTokenReq{
|
||||||
RefreshToken: "abc",
|
// RefreshToken: "abc",
|
||||||
})
|
// })
|
||||||
RefreshToken(c)
|
// RefreshToken(c)
|
||||||
utils.AssertResponse(&ts.Suite, w, http.StatusOK, http_resp.OK)
|
// utils.AssertResponse(&ts.Suite, w, http.StatusOK, http_resp.OK)
|
||||||
})
|
//})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLoginTestSuite(t *testing.T) {
|
func TestLoginTestSuite(t *testing.T) {
|
||||||
|
|||||||
108
internal/handler/http_handler/session.go
Normal file
108
internal/handler/http_handler/session.go
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
package http_handler
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.hlsq.asia/mmorpg/service-common/db/redis"
|
||||||
|
"git.hlsq.asia/mmorpg/service-common/utils"
|
||||||
|
"git.hlsq.asia/mmorpg/service-gateway/config"
|
||||||
|
"git.hlsq.asia/mmorpg/service-gateway/internal/global"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
scriptLoginSha1 string
|
||||||
|
scriptLogin = `
|
||||||
|
local at_key = KEYS[1] .. ARGV[1]
|
||||||
|
local rt_key = KEYS[2] .. ARGV[2]
|
||||||
|
local session_key = KEYS[3] .. ARGV[3]
|
||||||
|
|
||||||
|
redis.call("HSET", at_key,
|
||||||
|
"usn", ARGV[3],
|
||||||
|
"ip", ARGV[6],
|
||||||
|
"ua", ARGV[7],
|
||||||
|
"rt", ARGV[2]
|
||||||
|
)
|
||||||
|
redis.call("EXPIRE", at_key, tonumber(ARGV[4]))
|
||||||
|
|
||||||
|
redis.call("HSET", rt_key,
|
||||||
|
"usn", ARGV[3],
|
||||||
|
"ip", ARGV[6],
|
||||||
|
"ua", ARGV[7],
|
||||||
|
"at", ARGV[1]
|
||||||
|
)
|
||||||
|
redis.call("EXPIRE", rt_key, tonumber(ARGV[5]))
|
||||||
|
|
||||||
|
local all_rts = redis.call("SMEMBERS", session_key)
|
||||||
|
-- 只允许最大10个会话,多了的随机踢掉
|
||||||
|
if #all_rts >= 10 then
|
||||||
|
local rk = KEYS[2] .. all_rts[1]
|
||||||
|
local ak = KEYS[1] .. redis.call("HGET", rk, "at")
|
||||||
|
redis.call("DEL", rk)
|
||||||
|
redis.call("DEL", ak)
|
||||||
|
end
|
||||||
|
-- 清理已经失效的会话
|
||||||
|
for i, rt in ipairs(all_rts) do
|
||||||
|
if redis.call("EXISTS", KEYS[2] .. rt) == 0 then
|
||||||
|
redis.call("SREM", session_key, rt)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
redis.call("SADD", session_key, ARGV[2])
|
||||||
|
redis.call("EXPIRE", session_key, tonumber(ARGV[5]) + 600)
|
||||||
|
|
||||||
|
return 1
|
||||||
|
`
|
||||||
|
)
|
||||||
|
|
||||||
|
func sessionLogin(c *gin.Context, usn int64) (string, string, error) {
|
||||||
|
if scriptLoginSha1 == "" {
|
||||||
|
scriptLoginSha1 = redis.GetClient().ScriptLoad(c, scriptLogin).Val()
|
||||||
|
}
|
||||||
|
at := uuid.New().String()
|
||||||
|
rt := uuid.New().String()
|
||||||
|
err := redis.GetClient().EvalSha(
|
||||||
|
c, scriptLoginSha1,
|
||||||
|
[]string{global.KeyGatewayAccessToken, global.KeyGatewayRefreshToken, global.KeyGatewaySession},
|
||||||
|
at,
|
||||||
|
rt,
|
||||||
|
usn,
|
||||||
|
config.Get().Auth.ShortExpire,
|
||||||
|
config.Get().Auth.LongExpire,
|
||||||
|
c.RemoteIP(),
|
||||||
|
c.GetHeader("User-Agent"),
|
||||||
|
).Err()
|
||||||
|
if err != nil {
|
||||||
|
return "", "", utils.ErrorsWrap(err)
|
||||||
|
}
|
||||||
|
c.SetCookie("refresh_token", rt, int(config.Get().Auth.LongExpire), "/", ".hlsq.asia", true, true)
|
||||||
|
|
||||||
|
return at, rt, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
scriptLogoutSha1 string
|
||||||
|
scriptLogout = `
|
||||||
|
local rt_key = KEYS[2] .. ARGV[1]
|
||||||
|
local usn = redis.call("HGET", rt_key, "usn")
|
||||||
|
local at = redis.call("HGET", rt_key, "at")
|
||||||
|
local at_key = KEYS[1] .. at
|
||||||
|
local session_key = KEYS[3] .. usn
|
||||||
|
|
||||||
|
redis.call("DEL", at_key)
|
||||||
|
redis.call("DEL", rt_key)
|
||||||
|
redis.call("SREM", session_key, ARGV[1])
|
||||||
|
|
||||||
|
return 1
|
||||||
|
`
|
||||||
|
)
|
||||||
|
|
||||||
|
func sessionLogout(c *gin.Context, rt string) error {
|
||||||
|
if scriptLogoutSha1 == "" {
|
||||||
|
scriptLogoutSha1 = redis.GetClient().ScriptLoad(c, scriptLogout).Val()
|
||||||
|
}
|
||||||
|
return redis.GetClient().EvalSha(
|
||||||
|
c, scriptLogoutSha1,
|
||||||
|
[]string{global.KeyGatewayAccessToken, global.KeyGatewayRefreshToken, global.KeyGatewaySession},
|
||||||
|
rt,
|
||||||
|
).Err()
|
||||||
|
}
|
||||||
@@ -23,17 +23,16 @@ type Client struct {
|
|||||||
heartBeat time.Time // 最后一次心跳
|
heartBeat time.Time // 最后一次心跳
|
||||||
|
|
||||||
Status int32 // 状态:0 登陆中 1 正常 2 离线
|
Status int32 // 状态:0 登陆中 1 正常 2 离线
|
||||||
USN string // 用户ID
|
USN int64 // 用户ID
|
||||||
SceneSID string // 场景服ID
|
SceneSID int64 // 场景服ID
|
||||||
InstanceID int32 // 副本ID,副本类型
|
InstanceID int32 // 副本ID,副本类型
|
||||||
UniqueNo string // 副本唯一编号
|
UniqueNo int64 // 副本唯一编号
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(usn string, conn socket.ISocketConn) *Client {
|
func NewClient(conn socket.ISocketConn) *Client {
|
||||||
client := &Client{
|
client := &Client{
|
||||||
USN: usn,
|
|
||||||
conn: conn,
|
conn: conn,
|
||||||
logger: log.GetLogger().Named(fmt.Sprintf("usn:%v", usn)),
|
logger: log.GetLogger(),
|
||||||
heartBeat: time.Now(),
|
heartBeat: time.Now(),
|
||||||
mailChan: make(chan Event, 1024),
|
mailChan: make(chan Event, 1024),
|
||||||
}
|
}
|
||||||
@@ -43,6 +42,11 @@ func NewClient(usn string, conn socket.ISocketConn) *Client {
|
|||||||
return client
|
return client
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Client) SetUSN(usn int64) {
|
||||||
|
c.USN = usn
|
||||||
|
c.logger = log.GetLogger().Named(fmt.Sprintf("usn:%v", usn))
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Client) Loop() {
|
func (c *Client) Loop() {
|
||||||
defer func() {
|
defer func() {
|
||||||
if err := recover(); err != nil {
|
if err := recover(); err != nil {
|
||||||
|
|||||||
@@ -75,7 +75,7 @@ func (c *Client) onEnter(msg *ss_pb.C2S_EnterInstance) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) onLeave() {
|
func (c *Client) onLeave() {
|
||||||
if c.SceneSID == "" {
|
if c.SceneSID == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
client, err := grpc_client.SceneNewClient(c.SceneSID)
|
client, err := grpc_client.SceneNewClient(c.SceneSID)
|
||||||
@@ -94,7 +94,7 @@ func (c *Client) onLeave() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) onAction(msg *ss_pb.C2S_Action) {
|
func (c *Client) onAction(msg *ss_pb.C2S_Action) {
|
||||||
if c.SceneSID == "" {
|
if c.SceneSID == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err := grpc_client.SendMessageToScene(c.SceneSID, grpc_client.FunAction, &grpc_pb.ActionReq{
|
if err := grpc_client.SendMessageToScene(c.SceneSID, grpc_client.FunAction, &grpc_pb.ActionReq{
|
||||||
|
|||||||
@@ -8,35 +8,35 @@ import (
|
|||||||
var UserMgr *userManager
|
var UserMgr *userManager
|
||||||
|
|
||||||
type userManager struct {
|
type userManager struct {
|
||||||
userMap map[string]*Client
|
userMap map[int64]*Client
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
UserMgr = &userManager{
|
UserMgr = &userManager{
|
||||||
userMap: make(map[string]*Client),
|
userMap: make(map[int64]*Client),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *userManager) Add(usn string, client *Client) {
|
func (m *userManager) Add(usn int64, client *Client) {
|
||||||
m.Lock()
|
m.Lock()
|
||||||
defer m.Unlock()
|
defer m.Unlock()
|
||||||
m.userMap[usn] = client
|
m.userMap[usn] = client
|
||||||
global.OnlineUsersGauge.Inc()
|
global.OnlineUsersGauge.Inc()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *userManager) Delete(usn string) {
|
func (m *userManager) Delete(usn int64) {
|
||||||
m.Lock()
|
m.Lock()
|
||||||
defer m.Unlock()
|
defer m.Unlock()
|
||||||
delete(m.userMap, usn)
|
delete(m.userMap, usn)
|
||||||
global.OnlineUsersGauge.Dec()
|
global.OnlineUsersGauge.Dec()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *userManager) GetAll() map[string]*Client {
|
func (m *userManager) GetAll() map[int64]*Client {
|
||||||
m.RLock()
|
m.RLock()
|
||||||
defer m.RUnlock()
|
defer m.RUnlock()
|
||||||
|
|
||||||
copyMap := make(map[string]*Client, len(m.userMap))
|
copyMap := make(map[int64]*Client, len(m.userMap))
|
||||||
for k, v := range m.userMap {
|
for k, v := range m.userMap {
|
||||||
copyMap[k] = v
|
copyMap[k] = v
|
||||||
}
|
}
|
||||||
@@ -54,7 +54,7 @@ func (m *userManager) GetAllInterface() []interface{} {
|
|||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *userManager) GetByUSN(usn string) *Client {
|
func (m *userManager) GetByUSN(usn int64) *Client {
|
||||||
m.RLock()
|
m.RLock()
|
||||||
defer m.RUnlock()
|
defer m.RUnlock()
|
||||||
return m.userMap[usn]
|
return m.userMap[usn]
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"git.hlsq.asia/mmorpg/service-common/net/grpc/grpc_client"
|
"git.hlsq.asia/mmorpg/service-common/net/grpc/grpc_client"
|
||||||
"git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb"
|
"git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb"
|
||||||
"git.hlsq.asia/mmorpg/service-common/proto/ss/ss_pb"
|
"git.hlsq.asia/mmorpg/service-common/proto/ss/ss_pb"
|
||||||
|
"git.hlsq.asia/mmorpg/service-common/utils"
|
||||||
"git.hlsq.asia/mmorpg/service-gateway/internal/global"
|
"git.hlsq.asia/mmorpg/service-gateway/internal/global"
|
||||||
"git.hlsq.asia/mmorpg/service-gateway/internal/handler/ws_handler/client"
|
"git.hlsq.asia/mmorpg/service-gateway/internal/handler/ws_handler/client"
|
||||||
"sync"
|
"sync"
|
||||||
@@ -152,7 +153,9 @@ func (l *Login) StartLogin(user *User) {
|
|||||||
|
|
||||||
// CheckToken 校验Token是否有效
|
// CheckToken 校验Token是否有效
|
||||||
func (l *Login) CheckToken(user *User) bool {
|
func (l *Login) CheckToken(user *User) bool {
|
||||||
return redis.GetClient().Get(l.ctx, fmt.Sprintf(global.KeyGatewayAccessToken, user.Cli.USN)).Val() == user.Token
|
usn, _ := redis.GetClient().HGet(context.Background(), global.KeyGatewayAccessToken+user.Token, (&utils.UserSession{}).GetUsnKey()).Int64()
|
||||||
|
user.Cli.SetUSN(usn)
|
||||||
|
return usn > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// CheckOnline 校验是否在线
|
// CheckOnline 校验是否在线
|
||||||
@@ -161,7 +164,7 @@ func (l *Login) CheckOnline(user *User) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// KickUser 把玩家踢下线
|
// KickUser 把玩家踢下线
|
||||||
func (l *Login) KickUser(gatewaySID string, usn string) bool {
|
func (l *Login) KickUser(gatewaySID int64, usn int64) bool {
|
||||||
gc, err := grpc_client.GatewayNewClient(gatewaySID)
|
gc, err := grpc_client.GatewayNewClient(gatewaySID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("KickUser cannot find gateway client: %v, sid: %v", err, gatewaySID)
|
log.Errorf("KickUser cannot find gateway client: %v, sid: %v", err, gatewaySID)
|
||||||
|
|||||||
@@ -78,7 +78,7 @@ func (q *QueueUp) Dequeue() (*client.Client, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetPosition 返回用户前面还有多少人在排队
|
// GetPosition 返回用户前面还有多少人在排队
|
||||||
func (q *QueueUp) GetPosition(usn string) (int64, bool) {
|
func (q *QueueUp) GetPosition(usn int64) (int64, bool) {
|
||||||
val, ok := q.waiting.Load(usn)
|
val, ok := q.waiting.Load(usn)
|
||||||
if !ok {
|
if !ok {
|
||||||
return 0, false
|
return 0, false
|
||||||
@@ -88,7 +88,7 @@ func (q *QueueUp) GetPosition(usn string) (int64, bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// RemoveUser 安全移除用户(标记为取消)
|
// RemoveUser 安全移除用户(标记为取消)
|
||||||
func (q *QueueUp) RemoveUser(usn string) bool {
|
func (q *QueueUp) RemoveUser(usn int64) bool {
|
||||||
_, loaded := q.waiting.LoadAndDelete(usn)
|
_, loaded := q.waiting.LoadAndDelete(usn)
|
||||||
return loaded
|
return loaded
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,9 +2,10 @@ package http_gateway
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"git.hlsq.asia/mmorpg/service-common/db/redis"
|
||||||
"git.hlsq.asia/mmorpg/service-common/net/http/http_resp"
|
"git.hlsq.asia/mmorpg/service-common/net/http/http_resp"
|
||||||
"git.hlsq.asia/mmorpg/service-common/utils"
|
"git.hlsq.asia/mmorpg/service-common/utils"
|
||||||
"git.hlsq.asia/mmorpg/service-gateway/config"
|
"git.hlsq.asia/mmorpg/service-gateway/internal/global"
|
||||||
"github.com/gin-contrib/cors"
|
"github.com/gin-contrib/cors"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
@@ -63,17 +64,18 @@ func authJwt() gin.HandlerFunc {
|
|||||||
c.Abort()
|
c.Abort()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
claims, err := utils.ParseToken(token, config.Get().Auth.Secret)
|
|
||||||
if err != nil {
|
usn, _ := redis.GetClient().HGet(c, global.KeyGatewayAccessToken+token, (&utils.UserSession{}).GetUsnKey()).Int64()
|
||||||
|
if usn == 0 {
|
||||||
http_resp.JsonUnauthorized(c)
|
http_resp.JsonUnauthorized(c)
|
||||||
c.Abort()
|
c.Abort()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 这里将Header写到请求中,grpc-gateway框架会读取然后传给grpc服务
|
// 这里将Header写到请求中,grpc-gateway框架会读取然后传给grpc服务
|
||||||
c.Request.Header.Set("X-Usn", claims.USN)
|
c.Request.Header.Set("X-Usn", utils.Int64ToString(usn))
|
||||||
// 这里写到上下文中,打日志
|
// 这里写到上下文中,打日志
|
||||||
c.Set("usn", claims.USN)
|
c.Set("usn", usn)
|
||||||
c.Next()
|
c.Next()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import (
|
|||||||
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
|
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
|
||||||
"go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin"
|
"go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin"
|
||||||
"google.golang.org/protobuf/encoding/protojson"
|
"google.golang.org/protobuf/encoding/protojson"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func InitServeMux() *runtime.ServeMux {
|
func InitServeMux() *runtime.ServeMux {
|
||||||
@@ -49,7 +50,13 @@ func InitRouter() *gin.Engine {
|
|||||||
gin.Recovery(),
|
gin.Recovery(),
|
||||||
ginLogger(log.GetLogger().Named("GIN")),
|
ginLogger(log.GetLogger().Named("GIN")),
|
||||||
cors.New(corsConfig()),
|
cors.New(corsConfig()),
|
||||||
otelgin.Middleware(common.KeyDiscoverServiceNameGateway),
|
otelgin.Middleware(
|
||||||
|
common.KeyDiscoverServiceNameGateway,
|
||||||
|
otelgin.WithSpanNameFormatter(func(c *gin.Context) string {
|
||||||
|
method := strings.ToUpper(c.Request.Method)
|
||||||
|
return method + " " + c.Request.URL.Path
|
||||||
|
}),
|
||||||
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
r.HandleMethodNotAllowed = true
|
r.HandleMethodNotAllowed = true
|
||||||
@@ -66,7 +73,7 @@ func InitRouter() *gin.Engine {
|
|||||||
auth.Use(authJwt())
|
auth.Use(authJwt())
|
||||||
|
|
||||||
// 网关
|
// 网关
|
||||||
initBaseRoute(auth)
|
initGatewayPath(auth)
|
||||||
// 用户中心
|
// 用户中心
|
||||||
initUserPath(auth)
|
initUserPath(auth)
|
||||||
// 奇怪的知识-服务端
|
// 奇怪的知识-服务端
|
||||||
@@ -75,14 +82,17 @@ func InitRouter() *gin.Engine {
|
|||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
func initBaseRoute(r *gin.RouterGroup) {
|
func initGatewayPath(r *gin.RouterGroup) {
|
||||||
g := r.Group("/gw")
|
g := r.Group("/" + common.KeyDiscoverServiceNameGateway)
|
||||||
|
|
||||||
g.POST("/open/login", http_handler.Login)
|
g.POST("/open/login", http_handler.Login)
|
||||||
g.POST("/open/refresh_token", http_handler.RefreshToken)
|
g.POST("/open/refresh_token", http_handler.RefreshToken)
|
||||||
|
g.POST("/open/logout", http_handler.Logout)
|
||||||
}
|
}
|
||||||
|
|
||||||
func initUserPath(r *gin.RouterGroup) {
|
func initUserPath(r *gin.RouterGroup) {
|
||||||
g := r.Group("/user")
|
g := r.Group("/" + common.KeyDiscoverServiceNameUser)
|
||||||
|
|
||||||
client, err := grpc_client.UserNewClientLB()
|
client, err := grpc_client.UserNewClientLB()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("get user conn failed: %v", err)
|
log.Errorf("get user conn failed: %v", err)
|
||||||
@@ -99,7 +109,8 @@ func initUserPath(r *gin.RouterGroup) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func initQgdzsPath(r *gin.RouterGroup) {
|
func initQgdzsPath(r *gin.RouterGroup) {
|
||||||
g := r.Group("/qgdzs")
|
g := r.Group("/" + common.KeyDiscoverServiceNameQgdzs)
|
||||||
|
|
||||||
client, err := grpc_client.QgdzsNewClientLB()
|
client, err := grpc_client.QgdzsNewClientLB()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("get qgdzs conn failed: %v", err)
|
log.Errorf("get qgdzs conn failed: %v", err)
|
||||||
|
|||||||
@@ -4,8 +4,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"git.hlsq.asia/mmorpg/service-common/log"
|
"git.hlsq.asia/mmorpg/service-common/log"
|
||||||
"git.hlsq.asia/mmorpg/service-common/net/socket"
|
"git.hlsq.asia/mmorpg/service-common/net/socket"
|
||||||
"git.hlsq.asia/mmorpg/service-common/utils"
|
|
||||||
"git.hlsq.asia/mmorpg/service-gateway/config"
|
|
||||||
"git.hlsq.asia/mmorpg/service-gateway/internal/handler/ws_handler/client"
|
"git.hlsq.asia/mmorpg/service-gateway/internal/handler/ws_handler/client"
|
||||||
"git.hlsq.asia/mmorpg/service-gateway/internal/handler/ws_handler/login"
|
"git.hlsq.asia/mmorpg/service-gateway/internal/handler/ws_handler/login"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
@@ -27,16 +25,11 @@ func (g *GatewayWsServer) OnHandShake(conn socket.ISocketConn) socket.Action {
|
|||||||
g.logger.Warnf("token is invalid")
|
g.logger.Warnf("token is invalid")
|
||||||
return socket.Close
|
return socket.Close
|
||||||
}
|
}
|
||||||
claims, err := utils.ParseToken(token, config.Get().Auth.Secret)
|
|
||||||
if err != nil {
|
|
||||||
g.logger.Warnf("token is invalid")
|
|
||||||
return socket.Close
|
|
||||||
}
|
|
||||||
|
|
||||||
cli := client.NewClient(claims.USN, conn)
|
cli := client.NewClient(conn)
|
||||||
conn.SetParam("client", cli)
|
conn.SetParam("client", cli)
|
||||||
if !login.GetLoginQueue().AddToLoginQueue(&login.User{Cli: cli, Token: token}) {
|
if !login.GetLoginQueue().AddToLoginQueue(&login.User{Cli: cli, Token: token}) {
|
||||||
g.logger.Warnf("AddToLoginQueue err, login queue full, usn: %v", claims.USN)
|
g.logger.Warnf("AddToLoginQueue err, login queue full")
|
||||||
return socket.Close
|
return socket.Close
|
||||||
}
|
}
|
||||||
return socket.None
|
return socket.None
|
||||||
@@ -44,7 +37,7 @@ func (g *GatewayWsServer) OnHandShake(conn socket.ISocketConn) socket.Action {
|
|||||||
|
|
||||||
func (g *GatewayWsServer) OnMessage(conn socket.ISocketConn, bytes []byte) socket.Action {
|
func (g *GatewayWsServer) OnMessage(conn socket.ISocketConn, bytes []byte) socket.Action {
|
||||||
cli, ok := conn.GetParam("client").(*client.Client)
|
cli, ok := conn.GetParam("client").(*client.Client)
|
||||||
if !ok || cli.USN == "" || cli.Status != 1 {
|
if !ok || cli.USN == 0 || cli.Status != 1 {
|
||||||
return socket.Close
|
return socket.Close
|
||||||
}
|
}
|
||||||
cli.OnEvent(&client.ClientEvent{Msg: bytes})
|
cli.OnEvent(&client.ClientEvent{Msg: bytes})
|
||||||
@@ -53,7 +46,7 @@ func (g *GatewayWsServer) OnMessage(conn socket.ISocketConn, bytes []byte) socke
|
|||||||
|
|
||||||
func (g *GatewayWsServer) OnPong(conn socket.ISocketConn) {
|
func (g *GatewayWsServer) OnPong(conn socket.ISocketConn) {
|
||||||
cli, ok := conn.GetParam("client").(*client.Client)
|
cli, ok := conn.GetParam("client").(*client.Client)
|
||||||
if !ok || cli.USN == "" {
|
if !ok || cli.USN == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
cli.OnEvent(&client.PongEvent{})
|
cli.OnEvent(&client.PongEvent{})
|
||||||
|
|||||||
@@ -28,7 +28,6 @@ func (ts *TestSuite) SetupSuite() {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
Auth: &config.AuthConfig{
|
Auth: &config.AuthConfig{
|
||||||
Secret: "test",
|
|
||||||
ShortExpire: 15,
|
ShortExpire: 15,
|
||||||
LongExpire: 10080,
|
LongExpire: 10080,
|
||||||
},
|
},
|
||||||
|
|||||||
4
main.go
4
main.go
@@ -3,11 +3,15 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.hlsq.asia/mmorpg/service-gateway/app"
|
"git.hlsq.asia/mmorpg/service-gateway/app"
|
||||||
|
"git.hlsq.asia/mmorpg/service-gateway/config"
|
||||||
"github.com/judwhite/go-svc"
|
"github.com/judwhite/go-svc"
|
||||||
"syscall"
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
if err := config.LoadConfig(); err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
if err := svc.Run(&app.Program{}, syscall.SIGINT, syscall.SIGTERM, syscall.SIGKILL); err != nil {
|
if err := svc.Run(&app.Program{}, syscall.SIGINT, syscall.SIGTERM, syscall.SIGKILL); err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user