aozhiwei 6c4a9292f4 1
2021-01-07 13:50:05 +08:00

197 lines
6.5 KiB
Go

package main
import (
"sync"
"sync/atomic"
"github.com/aliyun/alibaba-cloud-sdk-go/services/geoip"
"f5"
"q5"
)
type GameConf struct {
gameId int32
channel int32
syncTimes int64
lastSyncTime int64
lastActiveTime int64
launchWhiteList map[string]int32
launchWhiteListMutex sync.RWMutex
launchBlackList map[string]int32
launchBlackListMutex sync.RWMutex
totalPassTimes int64
totalBlockTimes int64
passTimes int64
blockTimes int64
totalIpWhiteListPassTimes int64
ipWhiteListPassTimes int64
totalLaunchWhiteListPassTimes int64
launchWhiteListPassTimes int64
totalSafeZonePassTimes int64
safeZonePassTimes int64
totalHttpMethodBlockTimes int64
httpMethodBlockTimes int64
totalSessionErrBlockTimes int64
sessionErrBlockTimes int64
totalIpBlackListBlockTimes int64
ipBlackListBlockTimes int64
totalLaunchBlackListBlockTimes int64
launchBlackListBlockTimes int64
totalUnSafeZoneBlockTimes int64
unSafeZoneBlockTimes int64
}
func (this *GameConf) IsPass(remoteAddr string, launchInfo string) bool {
if G.MetaMgr.InIpWhiteList(remoteAddr) {
atomic.AddInt64(&this.totalIpWhiteListPassTimes, 1)
atomic.AddInt64(&this.ipWhiteListPassTimes, 1)
return true
}
if G.MetaMgr.InIpBlackList(remoteAddr) {
atomic.AddInt64(&this.totalIpBlackListBlockTimes, 1)
atomic.AddInt64(&this.ipBlackListBlockTimes, 1)
return false
}
/*if G.RiskMgr.InLaunchWhiteList(gameId, channel, launchInfo) {
atomic.AddInt64(&this.totalLaunchWhiteListPassTimes, 1)
atomic.AddInt64(&this.launchWhiteListPassTimes, 1)
return true
}
if G.RiskMgr.InLaunchBlackList(gameId, channel, launchInfo) {
atomic.AddInt64(&this.totalLaunchBlackListBlockTimes, 1)
atomic.AddInt64(&this.launchBlackListBlockTimes, 1)
return false
}*/
/*if G.RiskMgr.IsSafeZone(gameId, channel, remoteAddr, &responseStr) {
atomic.AddInt64(&this.totalSafeZonePassTimes, 1)
atomic.AddInt64(&this.safeZonePassTimes, 1)
return true
} else {
atomic.AddInt64(&this.totalUnSafeZoneBlockTimes, 1)
atomic.AddInt64(&this.unSafeZoneBlockTimes, 1)
return false
}*/
return false
}
func (this* GameConf) InLaunchWhiteList(gameId int32, channel int32, launchInfo string) bool {
this.launchWhiteListMutex.Lock()
defer this.launchWhiteListMutex.Unlock()
_, ok := this.launchWhiteList[launchInfo]
return ok
}
func (this* GameConf) InLaunchBlackList(gameId int32, channel int32, launchInfo string) bool {
this.launchBlackListMutex.Lock()
defer this.launchBlackListMutex.Unlock()
_, ok := this.launchBlackList[launchInfo]
return ok
}
func (this* GameConf) Init() {
this.launchWhiteListMutex.Lock()
this.launchBlackListMutex.Lock()
defer this.launchWhiteListMutex.Unlock()
defer this.launchBlackListMutex.Unlock()
this.launchWhiteList = make(map[string]int32)
this.launchBlackList = make(map[string]int32)
for _, val := range G.MetaMgr.GetLaunchWhiteList().GetList() {
this.launchWhiteList[val] = 1
}
for _, val := range G.MetaMgr.GetLaunchBlackList().GetList() {
this.launchBlackList[val] = 1
}
this.lastActiveTime = f5.App.NowUnix()
f5.App.AddIMMsg(IM_GAMECONF_CREATE, new(q5.XParams).Init(func (params* q5.XParams) {
params.Sender.SetInt32(this.gameId)
params.Param1.SetInt32(this.channel)
}))
}
func (this* GameConf) IsSafeZone(ip string, responseStr* string) bool {
accessKeyId := ""
accessSecret := ""
G.MetaMgr.GetSdkInfo(&accessKeyId, &accessSecret)
client, err := geoip.NewClientWithAccessKey("cn-hangzhou", accessKeyId, accessSecret)
if err != nil {
f5.SysLog().Warning("NewClientWithAccessKey error %s", err.Error())
return false
}
request := geoip.CreateDescribeIpv4LocationRequest()
request.Scheme = "https"
request.Ip = ip
response, err := client.DescribeIpv4Location(request)
if err != nil {
f5.SysLog().Warning("Ipv4Location error %s", err.Error())
return false
}
*responseStr = q5.EncodeJson(response)
return !G.MetaMgr.IsBlockZone(response.Country, response.Province, response.City)
}
func (this* GameConf) Dump() *q5.MutableXObject {
this.launchWhiteListMutex.Lock()
this.launchBlackListMutex.Lock()
defer this.launchWhiteListMutex.Unlock()
defer this.launchBlackListMutex.Unlock()
fillRespObj := func(respObj* q5.MutableXObject, attrName string, mapObj* map[string]int32) {
list := q5.NewMxoArray()
for key, _ := range *mapObj {
list.PushXValue(q5.NewXString(key))
}
respObj.SetXObject(attrName, list.AsXObject())
}
respObj := q5.NewMxoObject()
respObj.SetXValue("errcode", q5.NewXInt32(0))
respObj.SetXValue("errmsg", q5.NewXString(""))
//fillRespObj(respObj, "ip_white_list", &this.ipWhiteList)
//fillRespObj(respObj, "ip_black_list", &this.ipBlackList)
fillRespObj(respObj, "launch_white_list", &this.launchWhiteList)
fillRespObj(respObj, "launch_black_list", &this.launchBlackList)
//fillRespObj(respObj, "fixed_block_province_city_list", &this.fixedBlockProvinceCityHash)
return respObj
}
func (this* GameConf) GetPassObj() *q5.XObject {
passObj := q5.NewMxoObject()
/*passObj.SetXValue("total_white_ip_times", q5.NewXInt64(this.totalIpWhiteListPassTimes))
passObj.SetXValue("curr_white_ip_times", q5.NewXInt64(this.ipWhiteListPassTimes))
passObj.SetXValue("total_white_launch_times", q5.NewXInt64(this.totalLaunchWhiteListPassTimes))
passObj.SetXValue("curr_white_launch_times", q5.NewXInt64(this.launchWhiteListPassTimes))
passObj.SetXValue("total_safezone_times", q5.NewXInt64(this.totalSafeZonePassTimes))
passObj.SetXValue("curr_safezone_times", q5.NewXInt64(this.safeZonePassTimes))*/
return passObj.AsXObject()
}
func (this* GameConf) GetBlockObj() *q5.XObject {
blockObj := q5.NewMxoObject()
/*blockObj.SetXValue("total_method_err_times", q5.NewXInt64(this.totalHttpMethodBlockTimes))
blockObj.SetXValue("curr_method_err_times", q5.NewXInt64(this.httpMethodBlockTimes))
blockObj.SetXValue("total_session_err_times", q5.NewXInt64(this.totalSessionErrBlockTimes))
blockObj.SetXValue("curr_session_err_times", q5.NewXInt64(this.sessionErrBlockTimes))
blockObj.SetXValue("total_black_ip_times", q5.NewXInt64(this.totalIpBlackListBlockTimes))
blockObj.SetXValue("curr_black_ip_times", q5.NewXInt64(this.ipBlackListBlockTimes))
blockObj.SetXValue("total_black_launch_times", q5.NewXInt64(this.totalLaunchBlackListBlockTimes))
blockObj.SetXValue("curr_black_launch_times", q5.NewXInt64(this.launchBlackListBlockTimes))
blockObj.SetXValue("total_unsafezone_times", q5.NewXInt64(this.totalUnSafeZoneBlockTimes))
blockObj.SetXValue("curr_unsafezone_times", q5.NewXInt64(this.unSafeZoneBlockTimes))*/
return blockObj.AsXObject()
}
func (this* GameConf) Active() {
this.lastActiveTime = f5.App.NowUnix()
}
func (this* GameConf) SyncConf() {
}