1
This commit is contained in:
parent
8a19d47445
commit
fcf9c6eaf6
@ -3,7 +3,6 @@ package app
|
|||||||
import (
|
import (
|
||||||
"f5"
|
"f5"
|
||||||
//. "main/global"
|
//. "main/global"
|
||||||
"main/middleware"
|
|
||||||
"mt"
|
"mt"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -30,7 +29,6 @@ func (this *app) Init() {
|
|||||||
f5.LoadMetaTable(mt.Table)
|
f5.LoadMetaTable(mt.Table)
|
||||||
this.registerDataSources()
|
this.registerDataSources()
|
||||||
this.initCb()
|
this.initCb()
|
||||||
f5.GetApp().GetGinEngine().Use(middleware.CaCheck)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *app) UnInit() {
|
func (this *app) UnInit() {
|
||||||
|
@ -1,133 +1,4 @@
|
|||||||
package middleware
|
package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"f5"
|
|
||||||
"fmt"
|
|
||||||
"mt"
|
|
||||||
"net/http"
|
|
||||||
"q5"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
//. "main/global"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type RedirectInfo struct {
|
|
||||||
trace_id string
|
|
||||||
ori_req *gin.Context
|
|
||||||
sig string
|
|
||||||
sigtime int64
|
|
||||||
}
|
|
||||||
|
|
||||||
var requestCache = q5.ConcurrentMap[string, []*RedirectInfo]{}
|
|
||||||
var redirectRequest = q5.ConcurrentMap[string, *RedirectInfo]{}
|
|
||||||
|
|
||||||
func CaCheck(c *gin.Context) {
|
|
||||||
accountId := c.DefaultQuery("account_id", "")
|
|
||||||
|
|
||||||
if accountId == "" || !lockAccount(accountId) {
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
|
||||||
"errcode": 1004,
|
|
||||||
"errmsg": "Too many requests",
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
emptyreq := false
|
|
||||||
|
|
||||||
defer unlockAccount(accountId, emptyreq)
|
|
||||||
|
|
||||||
cache, exist := requestCache.Load(accountId)
|
|
||||||
if !exist {
|
|
||||||
pcache := make([]*RedirectInfo, 0, mt.Table.Config.GetMaxCache())
|
|
||||||
cache = &pcache
|
|
||||||
}
|
|
||||||
|
|
||||||
info := new(RedirectInfo)
|
|
||||||
info.ori_req = c
|
|
||||||
info.sigtime = f5.GetApp().GetRealSeconds()
|
|
||||||
info.trace_id = fmt.Sprintf("%x%02x-%s", info.sigtime, len(*cache), accountId)
|
|
||||||
info.sig = q5.Md5Str(c.Request.URL.RawQuery + info.trace_id)
|
|
||||||
|
|
||||||
if len(*cache) < int(mt.Table.Config.GetMaxCache()) {
|
|
||||||
*cache = append(*cache, info)
|
|
||||||
requestCache.Store(accountId, *cache)
|
|
||||||
unlockAccount(accountId, emptyreq)
|
|
||||||
|
|
||||||
for {
|
|
||||||
time.Sleep(time.Millisecond * 100)
|
|
||||||
|
|
||||||
if lockAccount(accountId) {
|
|
||||||
reqlist, _ := requestCache.Load(accountId)
|
|
||||||
if len(*reqlist) > 0 && (*reqlist)[0].trace_id == info.trace_id {
|
|
||||||
req := (*reqlist)[0]
|
|
||||||
(*reqlist) = (*reqlist)[1:]
|
|
||||||
|
|
||||||
if req.sigtime+int64(mt.Table.Config.GetById(0).GetRequestOverTime()) > f5.GetApp().GetRealSeconds() {
|
|
||||||
redirectRequest.Store(accountId, req)
|
|
||||||
CaForward(req.ori_req, req.sig, req.trace_id)
|
|
||||||
redirectRequest.Delete(accountId)
|
|
||||||
} else {
|
|
||||||
req.ori_req.JSON(http.StatusOK, gin.H{
|
|
||||||
"errcode": 1004,
|
|
||||||
"errmsg": "Too many players",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(*reqlist) > 0 {
|
|
||||||
requestCache.Store(accountId, *reqlist)
|
|
||||||
} else {
|
|
||||||
requestCache.Delete(accountId)
|
|
||||||
emptyreq = true
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
unlockAccount(accountId, emptyreq)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
|
||||||
"errcode": 1004,
|
|
||||||
"errmsg": "Too many requests",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func VerifySig(c *gin.Context) {
|
|
||||||
accountId := c.DefaultQuery("account_id", "")
|
|
||||||
traceId := c.DefaultQuery("trace_id", "")
|
|
||||||
sig := c.DefaultQuery("sig", "")
|
|
||||||
|
|
||||||
if accountId == "" || traceId == "" || sig == "" {
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
|
||||||
"errcode": 1002,
|
|
||||||
"errmsg": "empty params",
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
failresp := gin.H{
|
|
||||||
"errcode": 1002,
|
|
||||||
"errmsg": "sig unavailable",
|
|
||||||
}
|
|
||||||
|
|
||||||
req, exist := redirectRequest.Load(accountId)
|
|
||||||
if !exist {
|
|
||||||
c.JSON(http.StatusOK, failresp)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if (*req).sig != sig || (*req).trace_id != traceId ||
|
|
||||||
(*req).sigtime+int64(mt.Table.Config.GetById(0).GetRequestOverTime()) < f5.GetApp().GetRealSeconds() {
|
|
||||||
c.JSON(http.StatusOK, failresp)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
|
||||||
"errcode": 0,
|
|
||||||
"errmsg": "",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
@ -5,47 +5,83 @@ import (
|
|||||||
"mt"
|
"mt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"q5"
|
"q5"
|
||||||
|
"jccommon"
|
||||||
|
"io/ioutil"
|
||||||
|
"bytes"
|
||||||
|
net_url "net/url"
|
||||||
|
"strings"
|
||||||
|
"errors"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CaForward(c *gin.Context, sig string, traceid string) {
|
func CaForward(c *gin.Context) {
|
||||||
|
accountId := c.DefaultQuery("account_id", "")
|
||||||
|
sessionId := c.DefaultQuery("session_id", "")
|
||||||
|
|
||||||
queryParams := c.Request.URL.Query()
|
if !jccommon.IsValidSessionId(accountId, sessionId) {
|
||||||
params := map[string]string{}
|
f5.RspErr(c, 500, "invalid session_id")
|
||||||
for k, v := range queryParams {
|
c.Abort()
|
||||||
params[k] = v[0]
|
|
||||||
}
|
|
||||||
if sig != "" {
|
|
||||||
params["sig"] = sig
|
|
||||||
params["trace_id"] = traceid
|
|
||||||
}
|
|
||||||
|
|
||||||
fullrequrl := mt.Table.Config.GetById(0).GetRedirectUrl() + c.Request.URL.Path[5:]
|
|
||||||
cb := func(rsp f5.HttpCliResponse) {
|
|
||||||
if rsp.GetErr() != nil {
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
|
||||||
"errcode": 1004,
|
|
||||||
"errmsg": rsp.GetErr(),
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.String(http.StatusOK, rsp.GetRawData())
|
newUrl := mt.Table.Config.GetById(0).GetRedirectUrl() + c.Request.URL.Path[5:]
|
||||||
|
if !q5.StrContains(newUrl, "?") {
|
||||||
|
newUrl = newUrl + "?"
|
||||||
|
}
|
||||||
|
{
|
||||||
|
u := net_url.Values{}
|
||||||
|
for k, v := range c.Request.URL.Query() {
|
||||||
|
u.Set(k, v[0])
|
||||||
|
}
|
||||||
|
newUrl += u.Encode()
|
||||||
}
|
}
|
||||||
|
|
||||||
switch c.Request.Method {
|
var httpRequest *http.Request
|
||||||
case "GET":
|
var createErr error
|
||||||
f5.GetHttpCliMgr().SendGoStyleRequest(
|
switch strings.ToUpper(c.Request.Method) {
|
||||||
fullrequrl,
|
case "GET": {
|
||||||
params,
|
httpRequest, createErr = http.NewRequest("GET", newUrl, nil)
|
||||||
cb)
|
}
|
||||||
case "POST":
|
case "POST": {
|
||||||
f5.GetHttpCliMgr().SendGoStylePost(
|
if postData, err := c.GetRawData(); err == nil {
|
||||||
fullrequrl,
|
httpRequest, createErr = http.NewRequest("POST", newUrl, bytes.NewBuffer(postData))
|
||||||
params,
|
contentType := c.GetHeader("Content-Type")
|
||||||
c.ContentType(),
|
if contentType != "" {
|
||||||
q5.GetPostBody(c.Request),
|
httpRequest.Header.Set("Content-Type", contentType)
|
||||||
cb)
|
}
|
||||||
|
} else {
|
||||||
|
createErr = err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
default: {
|
||||||
|
createErr = errors.New("method error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if createErr != nil {
|
||||||
|
f5.RspErr(c, 500, "create request error")
|
||||||
|
c.Abort()
|
||||||
|
f5.GetSysLog().Info("CaForward create request url:%s error:%s", newUrl, createErr)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
client := &http.Client{}
|
||||||
|
if resp, err := client.Do(httpRequest); err == nil {
|
||||||
|
defer resp.Body.Close()
|
||||||
|
if bytes, err := ioutil.ReadAll(resp.Body); err == nil {
|
||||||
|
c.String(200, string(bytes))
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
f5.RspErr(c, 500, "read response error")
|
||||||
|
c.Abort()
|
||||||
|
f5.GetSysLog().Info("CaForward read response url:%s eror:%s", newUrl, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
f5.RspErr(c, 500, "client.Do error")
|
||||||
|
c.Abort()
|
||||||
|
f5.GetSysLog().Info("CaForward client.Do url:%s error:%s", newUrl, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
@ -13,8 +13,7 @@ type routerMgr struct {
|
|||||||
|
|
||||||
func (this *routerMgr) Init() {
|
func (this *routerMgr) Init() {
|
||||||
redirectGroup := f5.GetApp().GetGinEngine().Group("/sapi")
|
redirectGroup := f5.GetApp().GetGinEngine().Group("/sapi")
|
||||||
redirectGroup.Any("webapp/index.php?", middleware.CaCheck)
|
redirectGroup.Any("webapp/index.php?", middleware.CaForward)
|
||||||
redirectGroup.Any("check", middleware.VerifySig)
|
|
||||||
|
|
||||||
f5.GetSysLog().Info("routerMgr.init")
|
f5.GetSysLog().Info("routerMgr.init")
|
||||||
}
|
}
|
||||||
|
@ -84,3 +84,7 @@ func CalcContributionScore(nfts []*NftStacking) float64 {
|
|||||||
score = baseScore * (1 + rate)
|
score = baseScore * (1 + rate)
|
||||||
return score
|
return score
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func IsValidSessionId(accountId string, sessionId string) bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user