Merge branch 'dev' into permission
This commit is contained in:
commit
1b00034511
@ -1,6 +1,7 @@
|
||||
{
|
||||
"gamesapi_url": "https://game2006sapi-test.kingsome.cn",
|
||||
"redirect_url": "https://game2006api-test.kingsome.cn",
|
||||
"redirect_secret_key": "~kCu8jYS)rJ5Ay_pZS_rT#&jOl)Qo0m)",
|
||||
"max_concurrent_num": 10,
|
||||
"request_over_time": 30
|
||||
}
|
||||
|
@ -34,6 +34,7 @@ require (
|
||||
github.com/goccy/go-json v0.10.2 // indirect
|
||||
github.com/golang/protobuf v1.5.0 // 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/now v1.1.5 // 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/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
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/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
|
||||
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
|
||||
|
@ -1,18 +1,20 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"f5"
|
||||
"io/ioutil"
|
||||
"jccommon"
|
||||
"main/service"
|
||||
"mt"
|
||||
"net/http"
|
||||
"q5"
|
||||
"main/service"
|
||||
"jccommon"
|
||||
"io/ioutil"
|
||||
"bytes"
|
||||
net_url "net/url"
|
||||
"q5"
|
||||
"strings"
|
||||
"errors"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func CaForward(c *gin.Context) {
|
||||
@ -27,6 +29,11 @@ func CaForward(c *gin.Context) {
|
||||
}
|
||||
|
||||
cLock := service.SApiForward.AcquireLock(accountId)
|
||||
if cLock == nil {
|
||||
f5.RspErr(c, 500, "system busy")
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
defer service.SApiForward.ReleaseLock(cLock)
|
||||
service.SApiForward.IncTotalTimes()
|
||||
beginTick := q5.GetTickCount()
|
||||
@ -38,46 +45,58 @@ func CaForward(c *gin.Context) {
|
||||
if !q5.StrContains(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() {
|
||||
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 createErr error
|
||||
switch strings.ToUpper(c.Request.Method) {
|
||||
case "GET": {
|
||||
service.SApiForward.IncGetTimes()
|
||||
httpRequest, createErr = http.NewRequest("GET", newUrl, nil)
|
||||
if !f5.IsOnlineEnv() {
|
||||
f5.GetSysLog().Info("CaForward method:%s newUrl:%s ", c.Request.Method, newUrl)
|
||||
}
|
||||
}
|
||||
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)
|
||||
}
|
||||
case "GET":
|
||||
{
|
||||
service.SApiForward.IncGetTimes()
|
||||
u.Set("__sign", service.SApiForward.Sign(params, nonce, nowTime, ""))
|
||||
newUrl += u.Encode()
|
||||
httpRequest, createErr = http.NewRequest("GET", newUrl, nil)
|
||||
if !f5.IsOnlineEnv() {
|
||||
f5.GetSysLog().Info("CaForward method:%s newUrl:%s Content-Type:%s postData:%s",
|
||||
c.Request.Method,
|
||||
newUrl,
|
||||
contentType,
|
||||
postData)
|
||||
f5.GetSysLog().Info("CaForward method:%s newUrl:%s ", c.Request.Method, newUrl)
|
||||
}
|
||||
} else {
|
||||
createErr = err
|
||||
}
|
||||
}
|
||||
default: {
|
||||
createErr = errors.New("method error")
|
||||
}
|
||||
case "POST":
|
||||
{
|
||||
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 {
|
||||
service.SApiForward.IncCreateErrTimes()
|
||||
|
@ -26,6 +26,10 @@ func (this *ConfigTable) GetMaxConcurrentNum() int32 {
|
||||
return this.selfConf.GetMaxConcurrentNum()
|
||||
}
|
||||
|
||||
func (this *ConfigTable) GetRedirectSecretKey() string {
|
||||
return this.selfConf.GetRedirectSecretKey()
|
||||
}
|
||||
|
||||
func (this *ConfigTable) PostInit1() {
|
||||
this.selfConf = this.GetById(int64(0))
|
||||
if this.selfConf == nil {
|
||||
|
@ -21,6 +21,7 @@ type Config struct {
|
||||
redirect_url string
|
||||
max_concurrent_num int32
|
||||
request_over_time int32
|
||||
redirect_secret_key string
|
||||
|
||||
_flags1_ uint64
|
||||
_flags2_ uint64
|
||||
@ -106,6 +107,14 @@ func (this *Config) HasRequestOverTime() bool {
|
||||
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{}) {
|
||||
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.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.redirect_secret_key, "redirect_secret_key", &this._flags1_, 8, kv)
|
||||
}
|
||||
|
@ -18,4 +18,5 @@ message Config
|
||||
optional string redirect_url = 5;
|
||||
optional int32 max_concurrent_num = 6;
|
||||
optional int32 request_over_time = 7;
|
||||
optional string redirect_secret_key = 8;
|
||||
}
|
||||
|
@ -1,25 +1,37 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"f5"
|
||||
"mt"
|
||||
"q5"
|
||||
"sync"
|
||||
"mt"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
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 {
|
||||
lock *sync.Mutex
|
||||
lock *sync.Mutex
|
||||
userHash *map[string]*SApiForwardLock
|
||||
}
|
||||
|
||||
type SApiForwardLock struct {
|
||||
accountId string
|
||||
lockTimes int32
|
||||
lock *sync.Mutex
|
||||
lock *sync.Mutex
|
||||
}
|
||||
|
||||
func (this *sApiForward) init() {
|
||||
@ -37,10 +49,11 @@ func (this *sApiForward) unInit() {
|
||||
|
||||
func (this *sApiForward) AcquireLock(accountId string) *SApiForwardLock {
|
||||
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)
|
||||
if atomic.AddInt32(&u.lockTimes, 1) > mt.Table.Config.GetMaxConcurrentNum() {
|
||||
atomic.AddInt32(&u.lockTimes, -1)
|
||||
this.IncRefuseTimes()
|
||||
return nil
|
||||
}
|
||||
u.lock.Lock()
|
||||
@ -53,39 +66,48 @@ func (this *sApiForward) ReleaseLock(l *SApiForwardLock) {
|
||||
}
|
||||
|
||||
func (this *sApiForward) IncInvalidSessionTimes() {
|
||||
|
||||
atomic.AddInt32(&this.insessTimes, 1)
|
||||
}
|
||||
|
||||
func (this *sApiForward) IncTotalTimes() {
|
||||
|
||||
atomic.AddInt32(&this.total, 1)
|
||||
}
|
||||
|
||||
func (this *sApiForward) IncGetTimes() {
|
||||
|
||||
atomic.AddInt32(&this.getTimes, 1)
|
||||
}
|
||||
|
||||
func (this *sApiForward) IncPostTimes() {
|
||||
|
||||
atomic.AddInt32(&this.postTimes, 1)
|
||||
}
|
||||
|
||||
func (this *sApiForward) IncCreateErrTimes() {
|
||||
|
||||
atomic.AddInt32(&this.createErrTimes, 1)
|
||||
}
|
||||
|
||||
func (this *sApiForward) IncDoErrTimes() {
|
||||
atomic.AddInt32(&this.doErrTimes, 1)
|
||||
|
||||
}
|
||||
|
||||
func (this *sApiForward) IncOkTimes() {
|
||||
atomic.AddInt32(&this.okTimes, 1)
|
||||
|
||||
}
|
||||
|
||||
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 {
|
||||
@ -100,3 +122,50 @@ func (this *sApiForward) getOrCreate(c *SApiForwardLockCache, accountId string)
|
||||
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() {
|
||||
SApiForward = new(sApiForward)
|
||||
SApiForward.init()
|
||||
|
||||
go SApiForward.outputMonitorLog()
|
||||
}
|
||||
|
||||
func (this *serviceMgr) UnInit() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user