add gameconf

This commit is contained in:
aozhiwei 2021-01-07 13:35:52 +08:00
parent 23167aac8d
commit 910ba5968f
4 changed files with 283 additions and 283 deletions

View File

@ -0,0 +1,232 @@
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
blockRegionHash map[string]int32
blockRegionHashMutex 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()
this.blockRegionHashMutex.Lock()
defer this.launchWhiteListMutex.Unlock()
defer this.launchBlackListMutex.Unlock()
defer this.blockRegionHashMutex.Unlock()
this.launchWhiteList = make(map[string]int32)
this.launchBlackList = make(map[string]int32)
this.blockRegionHash = 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
}
for _, val := range G.MetaMgr.GetFixedBlockRegion().GetList() {
this.blockRegionHash[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) IsBlockZone(country string, province string, city string) bool {
if country == "" || province == "" || city == "" {
return true
}
if country != "中国" {
return true
}
/*this.blockRegionHashMutex.Lock()
defer this.blockRegionHashMutex.Unlock()
if _, ok := this.blockRegionHash[province]; ok {
return true
}
if _, ok := this.blockRegionHash[city]; ok {
return true
}
if _, ok := this.blockRegionHash[province + "/" + city]; ok {
return true
}*/
return false
}
func (this* GameConf) IsSafeZone(ip string, response_str* string) bool {
accessKeyId := ""
accessSecret := ""
G.RiskMgr.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
}
*response_str = q5.EncodeJson(response)
return !this.IsBlockZone(response.Country, response.Province, response.City)
}
func (this* GameConf) Dump() *q5.MutableXObject {
this.launchWhiteListMutex.Lock()
this.launchBlackListMutex.Lock()
this.blockRegionHashMutex.Lock()
//this.fixedBlockProvinceCityHashMutex.Lock()
defer this.launchWhiteListMutex.Unlock()
defer this.launchBlackListMutex.Unlock()
defer this.blockRegionHashMutex.Unlock()
//defer this.fixedBlockProvinceCityHashMutex.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, "block_province_city_list", &this.blockRegionHash)
//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() {
}

View File

@ -3,6 +3,7 @@ package main
import (
"f5"
"mt"
"sync"
)
const (
@ -18,6 +19,16 @@ const (
type MetaMgr struct {
f5.MetaMgr
ipWhiteList map[string]int32
ipWhiteListMutex sync.RWMutex
ipBlackList map[string]int32
ipBlackListMutex sync.RWMutex
fixedBlockRegionHash map[string]int32
fixedBlockRegionHashMutex sync.RWMutex
}
func (this *MetaMgr) Init() *MetaMgr {
@ -66,6 +77,7 @@ func (this *MetaMgr) Init() *MetaMgr {
}
this.MetaMgr.RegisterMetaClasses(metaClasses)
this.Load()
this.secondInit()
return this
}
@ -73,6 +85,30 @@ func (this *MetaMgr) UnInit() {
this.MetaMgr.UnInit()
}
func (this *MetaMgr) secondInit() {
this.ipWhiteListMutex.Lock()
this.ipBlackListMutex.Lock()
this.fixedBlockRegionHashMutex.Lock()
defer this.ipWhiteListMutex.Unlock()
defer this.ipBlackListMutex.Unlock()
defer this.fixedBlockRegionHashMutex.Unlock()
this.ipWhiteList = make(map[string]int32)
this.ipBlackList = make(map[string]int32)
this.fixedBlockRegionHash = make(map[string]int32)
for _, val := range this.GetIpWhiteList().GetList() {
this.ipWhiteList[val] = 1
}
for _, val := range this.GetIpBlackList().GetList() {
this.ipBlackList[val] = 1
}
for _, val := range this.GetFixedBlockRegion().GetList() {
this.fixedBlockRegionHash[val] = 1
}
}
func (this *MetaMgr) GetServer(instance_id int32) *MtwServerInfo {
v, ok := this.MetaMgr.GetMetaById(MT_SERVER_INFO, instance_id).(*MtwServerInfo)
if ok {
@ -139,3 +175,18 @@ func (this *MetaMgr) GetFixedBlockRegion() *MtwBlockProvinceCity {
return nil
}
}
func (this *MetaMgr) InIpWhiteList(ip string) bool {
this.ipWhiteListMutex.Lock()
defer this.ipWhiteListMutex.Unlock()
_, ok := this.ipWhiteList[ip]
return ok
}
func (this* MetaMgr) InIpBlackList(ip string) bool {
this.ipBlackListMutex.Lock()
defer this.ipBlackListMutex.Unlock()
_, ok := this.ipBlackList[ip]
return ok
}

View File

@ -9,9 +9,6 @@ import (
)
type RiskMgr struct {
fixedBlockRegionHash map[string]int32
fixedBlockRegionHashMutex sync.RWMutex
accessKeyId string
accessSecret string
sdkInfoMutex sync.RWMutex
@ -33,17 +30,10 @@ type RiskMgr struct {
}
func (this* RiskMgr) Init() *RiskMgr {
this.fixedBlockRegionHashMutex.Lock()
this.gameHashMutex.Lock()
defer this.fixedBlockRegionHashMutex.Unlock()
defer this.gameHashMutex.Unlock()
this.fixedBlockRegionHash = make(map[string]int32)
this.gameHash = make(map[int64]*GameConf)
for _, val := range G.MetaMgr.GetFixedBlockRegion().GetList() {
this.fixedBlockRegionHash[val] = 1
}
this.accessKeyId = G.MetaMgr.GetAliKey().GetAccessKeyid()
this.accessSecret = G.MetaMgr.GetAliKey().GetAccessSecret()
this.clusterConf = G.MetaMgr.GetCurrServer()

View File

@ -1,274 +1 @@
package main
import (
"sync"
"github.com/aliyun/alibaba-cloud-sdk-go/services/geoip"
"f5"
"q5"
)
type GameConf struct {
gameId int32
channel int32
syncTimes int64
lastSyncTime int64
lastActiveTime int64
ipWhiteList map[string]int32
ipWhiteListMutex sync.RWMutex
ipBlackList map[string]int32
ipBlackListMutex sync.RWMutex
launchWhiteList map[string]int32
launchWhiteListMutex sync.RWMutex
launchBlackList map[string]int32
launchBlackListMutex sync.RWMutex
blockRegionHash map[string]int32
blockRegionHashMutex 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 {
/*
remoteAddr := q5.GetRequestRemoteAddr(r)
if this.clusterConf.GetTesting() == 1 && q5.Request(r, "ip").GetString() != "" {
remoteAddr = q5.Request(r, "ip").GetString()
}
if G.RiskMgr.InIpWhiteList(remoteAddr) {
atomic.AddInt64(&this.totalIpWhiteListPassTimes, 1)
atomic.AddInt64(&this.ipWhiteListPassTimes, 1)
return true
}
if G.RiskMgr.InIpBlackList(remoteAddr) {
atomic.AddInt64(&this.totalIpBlackListBlockTimes, 1)
atomic.AddInt64(&this.ipBlackListBlockTimes, 1)
return false
}
launchInfo := q5.GetPostBody(r).GetString()
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) InIpWhiteList(ip string) bool {
this.ipWhiteListMutex.Lock()
defer this.ipWhiteListMutex.Unlock()
_, ok := this.ipWhiteList[ip]
return ok
}
func (this* GameConf) InIpBlackList(ip string) bool {
this.ipBlackListMutex.Lock()
defer this.ipBlackListMutex.Unlock()
_, ok := this.ipBlackList[ip]
return ok
}
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.ipWhiteListMutex.Lock()
this.ipBlackListMutex.Lock()
this.launchWhiteListMutex.Lock()
this.launchBlackListMutex.Lock()
this.blockRegionHashMutex.Lock()
defer this.ipWhiteListMutex.Unlock()
defer this.ipBlackListMutex.Unlock()
defer this.launchWhiteListMutex.Unlock()
defer this.launchBlackListMutex.Unlock()
defer this.blockRegionHashMutex.Unlock()
this.ipWhiteList = make(map[string]int32)
this.ipBlackList = make(map[string]int32)
this.launchWhiteList = make(map[string]int32)
this.launchBlackList = make(map[string]int32)
this.blockRegionHash = make(map[string]int32)
for _, val := range G.MetaMgr.GetIpWhiteList().GetList() {
this.ipWhiteList[val] = 1
}
for _, val := range G.MetaMgr.GetIpBlackList().GetList() {
this.ipBlackList[val] = 1
}
for _, val := range G.MetaMgr.GetLaunchWhiteList().GetList() {
this.launchWhiteList[val] = 1
}
for _, val := range G.MetaMgr.GetLaunchBlackList().GetList() {
this.launchBlackList[val] = 1
}
for _, val := range G.MetaMgr.GetFixedBlockRegion().GetList() {
this.blockRegionHash[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) IsBlockZone(country string, province string, city string) bool {
if country == "" || province == "" || city == "" {
return true
}
if country != "中国" {
return true
}
/*this.blockRegionHashMutex.Lock()
defer this.blockRegionHashMutex.Unlock()
if _, ok := this.blockRegionHash[province]; ok {
return true
}
if _, ok := this.blockRegionHash[city]; ok {
return true
}
if _, ok := this.blockRegionHash[province + "/" + city]; ok {
return true
}*/
return false
}
func (this* GameConf) IsSafeZone(ip string, response_str* string) bool {
accessKeyId := ""
accessSecret := ""
G.RiskMgr.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
}
*response_str = q5.EncodeJson(response)
return !this.IsBlockZone(response.Country, response.Province, response.City)
}
func (this* GameConf) Dump() *q5.MutableXObject {
this.ipWhiteListMutex.Lock()
this.ipBlackListMutex.Lock()
this.launchWhiteListMutex.Lock()
this.launchBlackListMutex.Lock()
this.blockRegionHashMutex.Lock()
//this.fixedBlockProvinceCityHashMutex.Lock()
defer this.ipWhiteListMutex.Unlock()
defer this.ipBlackListMutex.Unlock()
defer this.launchWhiteListMutex.Unlock()
defer this.launchBlackListMutex.Unlock()
defer this.blockRegionHashMutex.Unlock()
//defer this.fixedBlockProvinceCityHashMutex.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, "block_province_city_list", &this.blockRegionHash)
//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() {
}