aozhiwei 7d6687bb91 1
2020-11-13 13:29:05 +08:00

234 lines
6.7 KiB
Go

package main
import (
"net/http"
"fmt"
"sync"
"sync/atomic"
"q5"
"f5"
)
type RiskMgr struct {
fixedBlockProvinceCityHash map[string]int32
fixedBlockProvinceCityHashMutex sync.RWMutex
accessKeyId string
accessSecret string
sdkInfoMutex sync.RWMutex
totalPassTimes int64
totalBlockTimes int64
passTimes int64
blockTimes int64
totalHttpMethodBlockTimes int64
httpMethodBlockTimes int64
totalSessionErrBlockTimes int64
sessionErrBlockTimes int64
gameHash map[int64]*GameConf
gameHashMutex sync.RWMutex
clusterConf *MtwServerInfo
}
func (this* RiskMgr) Init() *RiskMgr {
this.fixedBlockProvinceCityHashMutex.Lock()
this.gameHashMutex.Lock()
defer this.fixedBlockProvinceCityHashMutex.Unlock()
defer this.gameHashMutex.Unlock()
this.fixedBlockProvinceCityHash = make(map[string]int32)
this.gameHash = make(map[int64]*GameConf)
for _, val := range G.MetaMgr.GetFixedBlockProvinceCity().GetList() {
this.fixedBlockProvinceCityHash[val] = 1
}
this.accessKeyId = G.MetaMgr.GetAliKey().GetAccessKeyid()
this.accessSecret = G.MetaMgr.GetAliKey().GetAccessSecret()
this.clusterConf = G.MetaMgr.GetCurrServer()
G.HttpServer.RegisterHandle("Analyse", "isOpen", this.__analyseIsOpen)
G.HttpServer.RegisterHandle("Ops", "dump", this.__opsDump)
G.HttpServer.RegisterHandle("Ops", "syncConf", this.__opsSyncConf)
G.HttpServer.RegisterHandle("Ops", "info", this.__opsInfo)
f5.Timer().AddRepeatTimer(1000 * 60,
func (params* q5.XParams) {
},
func (params* q5.XParams) {
this.OutputLog()
})
return this
}
func (this* RiskMgr) UnInit() {
}
func (this* RiskMgr) fetchGameConfList() []*GameConf {
this.gameHashMutex.Lock()
defer this.gameHashMutex.Unlock()
gameConfList := []*GameConf{}
for _, val := range this.gameHash {
gameConfList = append(gameConfList, val)
}
return gameConfList
}
func (this* RiskMgr) OutputLog() {
for _, val := range this.fetchGameConfList() {
f5.SysLog().Info("gameid:%d channel:%d passobj %s",
val.gameId,
val.channel,
val.GetPassObj().ToJsonStr())
f5.SysLog().Info("gameid:%d channel:%d blockobj %s",
val.gameId,
val.channel,
val.GetBlockObj().ToJsonStr())
}
f5.SysLog().Info("global passobj %s", this.GetGlobalPassObj().ToJsonStr())
f5.SysLog().Info("global blockobj %s", this.GetGlobalBlockObj().ToJsonStr())
atomic.StoreInt64(&this.passTimes, 0)
atomic.StoreInt64(&this.blockTimes, 0)
atomic.StoreInt64(&this.passTimes, 0)
atomic.StoreInt64(&this.httpMethodBlockTimes, 0)
atomic.StoreInt64(&this.sessionErrBlockTimes, 0)
}
func (this* RiskMgr) GetGlobalPassObj() *q5.XObject {
passObj := q5.NewMxoObject()
passObj.SetXValue("total", q5.NewXInt64(this.totalPassTimes))
passObj.SetXValue("curr", q5.NewXInt64(this.passTimes))
return passObj.AsXObject()
}
func (this* RiskMgr) GetGlobalBlockObj() *q5.XObject {
blockObj := q5.NewMxoObject()
blockObj.SetXValue("total", q5.NewXInt64(this.totalBlockTimes))
blockObj.SetXValue("curr", q5.NewXInt64(this.blockTimes))
blockObj.SetXValue("total_methoderr", q5.NewXInt64(this.totalHttpMethodBlockTimes))
blockObj.SetXValue("curr_methoderr", q5.NewXInt64(this.httpMethodBlockTimes))
blockObj.SetXValue("total_sessonerr", q5.NewXInt64(this.totalSessionErrBlockTimes))
blockObj.SetXValue("curr_sessionerr", q5.NewXInt64(this.sessionErrBlockTimes))
return blockObj.AsXObject()
}
func (this* RiskMgr) GetSdkInfo(accessKeyId* string, accessSecret* string) {
this.sdkInfoMutex.Lock()
defer this.sdkInfoMutex.Unlock()
*accessKeyId = "" + this.accessKeyId
*accessSecret = "" + this.accessSecret
}
func (this *RiskMgr) ForceGameConf(gameId int32, channel int32) *GameConf {
this.gameHashMutex.Lock()
defer this.gameHashMutex.Unlock()
if val, ok := this.gameHash[q5.MkInt64(gameId, channel)]; ok {
return val
} else {
return nil
}
}
func (this *RiskMgr) GetGameConf(gameId int32, channel int32) *GameConf {
this.gameHashMutex.Lock()
defer this.gameHashMutex.Unlock()
val, _ := this.gameHash[q5.MkInt64(gameId, channel)]
return val
}
func (this *RiskMgr) __analyseIsOpen(w* http.ResponseWriter, r *http.Request) {
var gameConf *GameConf
responseStr := ""
isPass := func () bool {
if r.Method != "POST" {
atomic.AddInt64(&this.totalHttpMethodBlockTimes, 1)
atomic.AddInt64(&this.httpMethodBlockTimes, 1)
return false
}
accountId := q5.Request(r, "account_id").GetString()
sessionId := q5.Request(r, "session_id").GetString()
if !f5.IsValidSessionId(accountId, sessionId, SESSION_KEY) {
atomic.AddInt64(&this.totalSessionErrBlockTimes, 1)
atomic.AddInt64(&this.sessionErrBlockTimes, 1)
return false
}
gameId := f5.ExtractGameIdFromAccountId(accountId)
channel := f5.ExtractChannelFromAccountId(accountId)
gameConf = this.ForceGameConf(gameId, channel)
if gameConf == nil {
return false
}
remoteAddr := q5.GetRequestRemoteAddr(r)
if this.clusterConf.GetTesting() == 1 && q5.Request(r, "ip").GetString() != "" {
remoteAddr = q5.Request(r, "ip").GetString()
}
launchInfo := q5.GetPostBody(r).GetString()
return gameConf.IsPass(remoteAddr, launchInfo)
}
if isPass() {
atomic.AddInt64(&this.totalPassTimes, 1)
atomic.AddInt64(&this.passTimes, 1)
if this.clusterConf.GetTesting() == 1 {
if responseStr == "" {
responseStr = `""`
}
data := fmt.Sprintf(`{"errcode":0, "errmsg":"", "is_open":%d, "data":%s}`,
1,
responseStr)
q5.Response(w, data)
} else {
q5.ResponseInt32Ok(w, "is_open", 1)
}
} else {
atomic.AddInt64(&this.totalBlockTimes, 1)
atomic.AddInt64(&this.blockTimes, 1)
if this.clusterConf.GetTesting() == 1 {
if responseStr == "" {
responseStr = `""`
}
data := fmt.Sprintf(`{"errcode":0, "errmsg":"", "is_open":%d, "data":%s}`,
0,
responseStr)
q5.Response(w, data)
} else {
q5.ResponseInt32Ok(w, "is_open", 0)
}
}
}
func (this *RiskMgr) __opsDump(w* http.ResponseWriter, r *http.Request) {
gameId := q5.Request(r, "gameid").GetInt32()
channel := q5.Request(r, "channel").GetInt32()
gameConf := this.GetGameConf(gameId, channel)
if gameConf == nil {
q5.ResponseErr(w, 1, "数据不存在")
return
}
q5.Response(w, gameConf.Dump().ToJsonStr())
}
func (this *RiskMgr) __opsSyncConf(w* http.ResponseWriter, r *http.Request) {
q5.ResponseOk(w)
}
func (this *RiskMgr) __opsInfo(w* http.ResponseWriter, r *http.Request) {
gameId := q5.Request(r, "gameid").GetInt32()
channel := q5.Request(r, "channel").GetInt32()
gameConf := this.GetGameConf(gameId, channel)
if gameConf == nil {
q5.ResponseErr(w, 1, "数据不存在")
return
}
respObj := q5.NewMxoObject()
respObj.SetXValue("errcode", q5.NewXInt32(0))
respObj.SetXValue("errmsg", q5.NewXString(""))
respObj.SetXObject("pass", gameConf.GetPassObj())
respObj.SetXObject("block", gameConf.GetBlockObj())
q5.Response(w, respObj.ToJsonStr())
}