Merge branch 'dev' into permission
This commit is contained in:
commit
1b00034511
@ -1,6 +1,7 @@
|
|||||||
{
|
{
|
||||||
"gamesapi_url": "https://game2006sapi-test.kingsome.cn",
|
"gamesapi_url": "https://game2006sapi-test.kingsome.cn",
|
||||||
"redirect_url": "https://game2006api-test.kingsome.cn",
|
"redirect_url": "https://game2006api-test.kingsome.cn",
|
||||||
|
"redirect_secret_key": "~kCu8jYS)rJ5Ay_pZS_rT#&jOl)Qo0m)",
|
||||||
"max_concurrent_num": 10,
|
"max_concurrent_num": 10,
|
||||||
"request_over_time": 30
|
"request_over_time": 30
|
||||||
}
|
}
|
||||||
|
@ -34,6 +34,7 @@ require (
|
|||||||
github.com/goccy/go-json v0.10.2 // indirect
|
github.com/goccy/go-json v0.10.2 // indirect
|
||||||
github.com/golang/protobuf v1.5.0 // indirect
|
github.com/golang/protobuf v1.5.0 // indirect
|
||||||
github.com/gomodule/redigo v1.8.3 // indirect
|
github.com/gomodule/redigo v1.8.3 // indirect
|
||||||
|
github.com/google/uuid v1.6.0 // indirect
|
||||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||||
github.com/jinzhu/now v1.1.5 // indirect
|
github.com/jinzhu/now v1.1.5 // indirect
|
||||||
github.com/json-iterator/go v1.1.12 // indirect
|
github.com/json-iterator/go v1.1.12 // indirect
|
||||||
|
@ -36,6 +36,8 @@ github.com/gomodule/redigo v1.8.3/go.mod h1:P9dn9mFrCBvWhGE1wpxx6fgq7BAeLBk+UUUz
|
|||||||
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
|
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
|
||||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||||
|
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||||
|
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
|
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
|
||||||
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
|
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
|
||||||
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
|
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
|
||||||
|
@ -1,18 +1,20 @@
|
|||||||
package middleware
|
package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"errors"
|
||||||
"f5"
|
"f5"
|
||||||
|
"io/ioutil"
|
||||||
|
"jccommon"
|
||||||
|
"main/service"
|
||||||
"mt"
|
"mt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"q5"
|
|
||||||
"main/service"
|
|
||||||
"jccommon"
|
|
||||||
"io/ioutil"
|
|
||||||
"bytes"
|
|
||||||
net_url "net/url"
|
net_url "net/url"
|
||||||
|
"q5"
|
||||||
"strings"
|
"strings"
|
||||||
"errors"
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CaForward(c *gin.Context) {
|
func CaForward(c *gin.Context) {
|
||||||
@ -27,6 +29,11 @@ func CaForward(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
cLock := service.SApiForward.AcquireLock(accountId)
|
cLock := service.SApiForward.AcquireLock(accountId)
|
||||||
|
if cLock == nil {
|
||||||
|
f5.RspErr(c, 500, "system busy")
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
defer service.SApiForward.ReleaseLock(cLock)
|
defer service.SApiForward.ReleaseLock(cLock)
|
||||||
service.SApiForward.IncTotalTimes()
|
service.SApiForward.IncTotalTimes()
|
||||||
beginTick := q5.GetTickCount()
|
beginTick := q5.GetTickCount()
|
||||||
@ -38,46 +45,58 @@ func CaForward(c *gin.Context) {
|
|||||||
if !q5.StrContains(newUrl, "?") {
|
if !q5.StrContains(newUrl, "?") {
|
||||||
newUrl = newUrl + "?"
|
newUrl = newUrl + "?"
|
||||||
}
|
}
|
||||||
|
params := []*[]string{}
|
||||||
|
nonce := uuid.New().String()
|
||||||
|
nowTime := f5.GetApp().GetRealSeconds()
|
||||||
|
u := net_url.Values{}
|
||||||
{
|
{
|
||||||
u := net_url.Values{}
|
|
||||||
for k, v := range c.Request.URL.Query() {
|
for k, v := range c.Request.URL.Query() {
|
||||||
u.Set(k, v[0])
|
u.Set(k, v[0])
|
||||||
|
q5.AppendSlice(¶ms, &[]string{k, v[0]})
|
||||||
}
|
}
|
||||||
newUrl += u.Encode()
|
u.Set("__nonce", nonce)
|
||||||
|
u.Set("__timestamp", q5.ToString(nowTime))
|
||||||
}
|
}
|
||||||
|
|
||||||
var httpRequest *http.Request
|
var httpRequest *http.Request
|
||||||
var createErr error
|
var createErr error
|
||||||
switch strings.ToUpper(c.Request.Method) {
|
switch strings.ToUpper(c.Request.Method) {
|
||||||
case "GET": {
|
case "GET":
|
||||||
service.SApiForward.IncGetTimes()
|
{
|
||||||
httpRequest, createErr = http.NewRequest("GET", newUrl, nil)
|
service.SApiForward.IncGetTimes()
|
||||||
if !f5.IsOnlineEnv() {
|
u.Set("__sign", service.SApiForward.Sign(params, nonce, nowTime, ""))
|
||||||
f5.GetSysLog().Info("CaForward method:%s newUrl:%s ", c.Request.Method, newUrl)
|
newUrl += u.Encode()
|
||||||
}
|
httpRequest, createErr = http.NewRequest("GET", newUrl, nil)
|
||||||
}
|
|
||||||
case "POST": {
|
|
||||||
service.SApiForward.IncPostTimes()
|
|
||||||
if postData, err := c.GetRawData(); err == nil {
|
|
||||||
httpRequest, createErr = http.NewRequest("POST", newUrl, bytes.NewBuffer(postData))
|
|
||||||
contentType := c.GetHeader("Content-Type")
|
|
||||||
if contentType != "" {
|
|
||||||
httpRequest.Header.Set("Content-Type", contentType)
|
|
||||||
}
|
|
||||||
if !f5.IsOnlineEnv() {
|
if !f5.IsOnlineEnv() {
|
||||||
f5.GetSysLog().Info("CaForward method:%s newUrl:%s Content-Type:%s postData:%s",
|
f5.GetSysLog().Info("CaForward method:%s newUrl:%s ", c.Request.Method, newUrl)
|
||||||
c.Request.Method,
|
|
||||||
newUrl,
|
|
||||||
contentType,
|
|
||||||
postData)
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
createErr = err
|
|
||||||
}
|
}
|
||||||
}
|
case "POST":
|
||||||
default: {
|
{
|
||||||
createErr = errors.New("method error")
|
service.SApiForward.IncPostTimes()
|
||||||
}
|
if postData, err := c.GetRawData(); err == nil {
|
||||||
|
u.Set("__sign", service.SApiForward.Sign(params, nonce, nowTime, string(postData)))
|
||||||
|
newUrl += u.Encode()
|
||||||
|
httpRequest, createErr = http.NewRequest("POST", newUrl, bytes.NewBuffer(postData))
|
||||||
|
contentType := c.GetHeader("Content-Type")
|
||||||
|
if contentType != "" {
|
||||||
|
httpRequest.Header.Set("Content-Type", contentType)
|
||||||
|
}
|
||||||
|
if !f5.IsOnlineEnv() {
|
||||||
|
f5.GetSysLog().Info("CaForward method:%s newUrl:%s Content-Type:%s postData:%s",
|
||||||
|
c.Request.Method,
|
||||||
|
newUrl,
|
||||||
|
contentType,
|
||||||
|
postData)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
createErr = err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
{
|
||||||
|
createErr = errors.New("method error")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if createErr != nil {
|
if createErr != nil {
|
||||||
service.SApiForward.IncCreateErrTimes()
|
service.SApiForward.IncCreateErrTimes()
|
||||||
|
@ -26,6 +26,10 @@ func (this *ConfigTable) GetMaxConcurrentNum() int32 {
|
|||||||
return this.selfConf.GetMaxConcurrentNum()
|
return this.selfConf.GetMaxConcurrentNum()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *ConfigTable) GetRedirectSecretKey() string {
|
||||||
|
return this.selfConf.GetRedirectSecretKey()
|
||||||
|
}
|
||||||
|
|
||||||
func (this *ConfigTable) PostInit1() {
|
func (this *ConfigTable) PostInit1() {
|
||||||
this.selfConf = this.GetById(int64(0))
|
this.selfConf = this.GetById(int64(0))
|
||||||
if this.selfConf == nil {
|
if this.selfConf == nil {
|
||||||
|
@ -21,6 +21,7 @@ type Config struct {
|
|||||||
redirect_url string
|
redirect_url string
|
||||||
max_concurrent_num int32
|
max_concurrent_num int32
|
||||||
request_over_time int32
|
request_over_time int32
|
||||||
|
redirect_secret_key string
|
||||||
|
|
||||||
_flags1_ uint64
|
_flags1_ uint64
|
||||||
_flags2_ uint64
|
_flags2_ uint64
|
||||||
@ -106,6 +107,14 @@ func (this *Config) HasRequestOverTime() bool {
|
|||||||
return (this._flags1_ & (uint64(1) << 7)) > 0
|
return (this._flags1_ & (uint64(1) << 7)) > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *Config) GetRedirectSecretKey() string {
|
||||||
|
return this.redirect_secret_key
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Config) HasRedirectSecretKey() bool {
|
||||||
|
return (this._flags1_ & (uint64(1) << 8)) > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
func (this *GamesapiCluster) LoadFromKv(kv map[string]interface{}) {
|
func (this *GamesapiCluster) LoadFromKv(kv map[string]interface{}) {
|
||||||
f5.ReadMetaTableField(&this.instance_id, "instance_id", &this._flags1_, 1, kv)
|
f5.ReadMetaTableField(&this.instance_id, "instance_id", &this._flags1_, 1, kv)
|
||||||
@ -121,4 +130,5 @@ func (this *Config) LoadFromKv(kv map[string]interface{}) {
|
|||||||
f5.ReadMetaTableField(&this.redirect_url, "redirect_url", &this._flags1_, 5, kv)
|
f5.ReadMetaTableField(&this.redirect_url, "redirect_url", &this._flags1_, 5, kv)
|
||||||
f5.ReadMetaTableField(&this.max_concurrent_num, "max_concurrent_num", &this._flags1_, 6, kv)
|
f5.ReadMetaTableField(&this.max_concurrent_num, "max_concurrent_num", &this._flags1_, 6, kv)
|
||||||
f5.ReadMetaTableField(&this.request_over_time, "request_over_time", &this._flags1_, 7, kv)
|
f5.ReadMetaTableField(&this.request_over_time, "request_over_time", &this._flags1_, 7, kv)
|
||||||
|
f5.ReadMetaTableField(&this.redirect_secret_key, "redirect_secret_key", &this._flags1_, 8, kv)
|
||||||
}
|
}
|
||||||
|
@ -18,4 +18,5 @@ message Config
|
|||||||
optional string redirect_url = 5;
|
optional string redirect_url = 5;
|
||||||
optional int32 max_concurrent_num = 6;
|
optional int32 max_concurrent_num = 6;
|
||||||
optional int32 request_over_time = 7;
|
optional int32 request_over_time = 7;
|
||||||
|
optional string redirect_secret_key = 8;
|
||||||
}
|
}
|
||||||
|
@ -1,25 +1,37 @@
|
|||||||
package service
|
package service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"f5"
|
||||||
|
"mt"
|
||||||
"q5"
|
"q5"
|
||||||
"sync"
|
"sync"
|
||||||
"mt"
|
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type sApiForward struct {
|
type sApiForward struct {
|
||||||
userCache []*SApiForwardLockCache
|
userCache []*SApiForwardLockCache
|
||||||
|
insessTimes int32
|
||||||
|
total int32
|
||||||
|
getTimes int32
|
||||||
|
postTimes int32
|
||||||
|
createErrTimes int32
|
||||||
|
doErrTimes int32
|
||||||
|
okTimes int32
|
||||||
|
readRspErrTimes int32
|
||||||
|
refuseTimes int32
|
||||||
|
maxCostTime int64
|
||||||
}
|
}
|
||||||
|
|
||||||
type SApiForwardLockCache struct {
|
type SApiForwardLockCache struct {
|
||||||
lock *sync.Mutex
|
lock *sync.Mutex
|
||||||
userHash *map[string]*SApiForwardLock
|
userHash *map[string]*SApiForwardLock
|
||||||
}
|
}
|
||||||
|
|
||||||
type SApiForwardLock struct {
|
type SApiForwardLock struct {
|
||||||
accountId string
|
accountId string
|
||||||
lockTimes int32
|
lockTimes int32
|
||||||
lock *sync.Mutex
|
lock *sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *sApiForward) init() {
|
func (this *sApiForward) init() {
|
||||||
@ -37,10 +49,11 @@ func (this *sApiForward) unInit() {
|
|||||||
|
|
||||||
func (this *sApiForward) AcquireLock(accountId string) *SApiForwardLock {
|
func (this *sApiForward) AcquireLock(accountId string) *SApiForwardLock {
|
||||||
crc32 := q5.Crc32(accountId)
|
crc32 := q5.Crc32(accountId)
|
||||||
c := this.userCache[int64(crc32) % int64(len(this.userCache))]
|
c := this.userCache[int64(crc32)%int64(len(this.userCache))]
|
||||||
u := this.getOrCreate(c, accountId)
|
u := this.getOrCreate(c, accountId)
|
||||||
if atomic.AddInt32(&u.lockTimes, 1) > mt.Table.Config.GetMaxConcurrentNum() {
|
if atomic.AddInt32(&u.lockTimes, 1) > mt.Table.Config.GetMaxConcurrentNum() {
|
||||||
atomic.AddInt32(&u.lockTimes, -1)
|
atomic.AddInt32(&u.lockTimes, -1)
|
||||||
|
this.IncRefuseTimes()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
u.lock.Lock()
|
u.lock.Lock()
|
||||||
@ -53,39 +66,48 @@ func (this *sApiForward) ReleaseLock(l *SApiForwardLock) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (this *sApiForward) IncInvalidSessionTimes() {
|
func (this *sApiForward) IncInvalidSessionTimes() {
|
||||||
|
atomic.AddInt32(&this.insessTimes, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *sApiForward) IncTotalTimes() {
|
func (this *sApiForward) IncTotalTimes() {
|
||||||
|
atomic.AddInt32(&this.total, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *sApiForward) IncGetTimes() {
|
func (this *sApiForward) IncGetTimes() {
|
||||||
|
atomic.AddInt32(&this.getTimes, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *sApiForward) IncPostTimes() {
|
func (this *sApiForward) IncPostTimes() {
|
||||||
|
atomic.AddInt32(&this.postTimes, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *sApiForward) IncCreateErrTimes() {
|
func (this *sApiForward) IncCreateErrTimes() {
|
||||||
|
atomic.AddInt32(&this.createErrTimes, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *sApiForward) IncDoErrTimes() {
|
func (this *sApiForward) IncDoErrTimes() {
|
||||||
|
atomic.AddInt32(&this.doErrTimes, 1)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *sApiForward) IncOkTimes() {
|
func (this *sApiForward) IncOkTimes() {
|
||||||
|
atomic.AddInt32(&this.okTimes, 1)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *sApiForward) IncReadRspErrTimes() {
|
func (this *sApiForward) IncReadRspErrTimes() {
|
||||||
|
atomic.AddInt32(&this.readRspErrTimes, 1)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *sApiForward) UpdateCostTime(costTime int64) {
|
func (this *sApiForward) IncRefuseTimes() {
|
||||||
|
atomic.AddInt32(&this.refuseTimes, 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *sApiForward) UpdateCostTime(costTime int64) {
|
||||||
|
if this.maxCostTime < costTime {
|
||||||
|
this.maxCostTime = costTime
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *sApiForward) getOrCreate(c *SApiForwardLockCache, accountId string) *SApiForwardLock {
|
func (this *sApiForward) getOrCreate(c *SApiForwardLockCache, accountId string) *SApiForwardLock {
|
||||||
@ -100,3 +122,50 @@ func (this *sApiForward) getOrCreate(c *SApiForwardLockCache, accountId string)
|
|||||||
return u
|
return u
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *sApiForward) Sign(params []*[]string, nonce string, timeStamp int64, postData string) string {
|
||||||
|
signData := ""
|
||||||
|
q5.Sort(params, func(a *[]string, b *[]string) bool {
|
||||||
|
return (*a)[0] < (*b)[0]
|
||||||
|
})
|
||||||
|
for _, v := range params {
|
||||||
|
signData += (*v)[0] + "=" + (*v)[1] + "&"
|
||||||
|
}
|
||||||
|
signData += nonce + q5.ToString(timeStamp) + postData + mt.Table.Config.GetRedirectSecretKey()
|
||||||
|
return q5.Md5Str(signData)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *sApiForward) outputMonitorLog() {
|
||||||
|
logtimes := 0
|
||||||
|
for {
|
||||||
|
f5.GetSysLog().Info("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<")
|
||||||
|
f5.GetSysLog().Info("total:%d, invalid_session:%d, get:%d,post:%d, create_error:%d, do_error:%d, ok:%d, read_rsp_err:%d, refuse:%d, max_cost_time:%d",
|
||||||
|
this.total,
|
||||||
|
this.insessTimes,
|
||||||
|
this.getTimes,
|
||||||
|
this.postTimes,
|
||||||
|
this.createErrTimes,
|
||||||
|
this.doErrTimes,
|
||||||
|
this.okTimes,
|
||||||
|
this.readRspErrTimes,
|
||||||
|
this.refuseTimes,
|
||||||
|
this.maxCostTime)
|
||||||
|
f5.GetSysLog().Info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
|
||||||
|
|
||||||
|
logtimes++
|
||||||
|
if logtimes > 6 {
|
||||||
|
logtimes = 0
|
||||||
|
this.insessTimes = 0
|
||||||
|
this.total = 0
|
||||||
|
this.getTimes = 0
|
||||||
|
this.postTimes = 0
|
||||||
|
this.createErrTimes = 0
|
||||||
|
this.doErrTimes = 0
|
||||||
|
this.okTimes = 0
|
||||||
|
this.readRspErrTimes = 0
|
||||||
|
this.refuseTimes = 0
|
||||||
|
this.maxCostTime = 0
|
||||||
|
}
|
||||||
|
time.Sleep(time.Second * 10)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -6,6 +6,8 @@ type serviceMgr struct {
|
|||||||
func (this *serviceMgr) Init() {
|
func (this *serviceMgr) Init() {
|
||||||
SApiForward = new(sApiForward)
|
SApiForward = new(sApiForward)
|
||||||
SApiForward.init()
|
SApiForward.init()
|
||||||
|
|
||||||
|
go SApiForward.outputMonitorLog()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *serviceMgr) UnInit() {
|
func (this *serviceMgr) UnInit() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user