You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
184 lines
4.2 KiB
184 lines
4.2 KiB
|
1 year ago
|
package call
|
||
|
|
|
||
|
|
import (
|
||
|
|
"fmt"
|
||
|
|
"server/common"
|
||
|
|
"server/config"
|
||
|
|
"server/db"
|
||
|
|
"sync"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"github.com/liangdas/mqant/log"
|
||
|
|
"github.com/olivere/elastic/v7"
|
||
|
|
)
|
||
|
|
|
||
|
|
// WhitePass 白名单鉴权
|
||
|
|
func WhitePass(ip, uuid, path string, version, channel int) bool {
|
||
|
|
if !IsWhiteOpen(channel, version, path) {
|
||
|
|
return true
|
||
|
|
}
|
||
|
|
log.Debug("check white ip:%v,uuid:%v,path:%v,version:%v,channel:%v", ip, uuid, path, version, channel)
|
||
|
|
one := GetWhite(ip, uuid)
|
||
|
|
log.Debug("white:%+v", one)
|
||
|
|
if one != nil {
|
||
|
|
// if one.Version < version {
|
||
|
|
// return false
|
||
|
|
// }
|
||
|
|
return one.PowerPass(path)
|
||
|
|
}
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
|
||
|
|
var (
|
||
|
|
ChannelWhiteMap sync.Map
|
||
|
|
Whites []*common.WhiteList
|
||
|
|
)
|
||
|
|
|
||
|
|
const (
|
||
|
|
SysListType = iota // 用作系统开关时的值
|
||
|
|
WhiteListType
|
||
|
|
BlackListType
|
||
|
|
)
|
||
|
|
|
||
|
|
func ReloadWhite(channel, opt int) {
|
||
|
|
val, ok := ChannelWhiteMap.Load(channel)
|
||
|
|
if !ok {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
w := val.(*common.WhiteList)
|
||
|
|
w.LimitType = opt
|
||
|
|
}
|
||
|
|
|
||
|
|
func IsWhiteOpen(channel, version int, path string) bool {
|
||
|
|
val, ok := ChannelWhiteMap.Load(channel)
|
||
|
|
if !ok {
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
w := val.(*common.WhiteList)
|
||
|
|
if w.LimitType == 2 {
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
// 如果当前版本号大于白名单开启的发布版本,直接全功能限制
|
||
|
|
if version > w.Version {
|
||
|
|
return true
|
||
|
|
}
|
||
|
|
// 如果当前版本小与等于白名单发布版本,只限制部分功能
|
||
|
|
if common.IsLimitMap(path) {
|
||
|
|
return true
|
||
|
|
}
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
|
||
|
|
func GetWhite(ip, uuid string) *common.WhiteList {
|
||
|
|
for i, v := range Whites {
|
||
|
|
if v.Content == ip || v.Content == uuid {
|
||
|
|
return Whites[i]
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func IsWhite(ip, uuid string) bool {
|
||
|
|
for _, v := range Whites {
|
||
|
|
if ip != "" && v.Content == ip {
|
||
|
|
return true
|
||
|
|
}
|
||
|
|
if uuid != "" && v.Content == uuid {
|
||
|
|
return true
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
|
||
|
|
func InitWhite() error {
|
||
|
|
one := []*common.WhiteList{}
|
||
|
|
if _, err := db.ES().QueryList(common.ESIndexBackWhiteList, 0, 5000, elastic.NewTermQuery("ListType", SysListType), &one); err != nil {
|
||
|
|
log.Error("err:%v", err)
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
for _, v := range one {
|
||
|
|
log.Debug("all white:%+v", *v)
|
||
|
|
}
|
||
|
|
for i := range one {
|
||
|
|
ChannelWhiteMap.Store(one[i].Channel, one[i])
|
||
|
|
}
|
||
|
|
Whites = []*common.WhiteList{}
|
||
|
|
q := elastic.NewBoolQuery()
|
||
|
|
// q.MustNot(elastic.NewMatchQuery("ListType", SysListType))
|
||
|
|
q.Must(elastic.NewMatchQuery("ListType", WhiteListType))
|
||
|
|
if _, err := db.ES().QueryList(common.ESIndexBackWhiteList, 0, 5000, q, &Whites); err != nil {
|
||
|
|
log.Error("err:%v", err)
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
for _, v := range Whites {
|
||
|
|
log.Debug("all Whites:%+v", *v)
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// IsExamine 是否是审核人员
|
||
|
|
func IsExamine(ip string) bool {
|
||
|
|
log.Debug("check examine ip:%v", ip)
|
||
|
|
return db.ES().Exist(common.ESIndexBackExamineList, ip)
|
||
|
|
}
|
||
|
|
|
||
|
|
// CheckChannel 检查渠道是否要屏蔽
|
||
|
|
func CheckChannel(cid int, ip string) bool {
|
||
|
|
sip, err := SearchIP(ip)
|
||
|
|
if err != nil {
|
||
|
|
log.Error("err:%v", err)
|
||
|
|
return true
|
||
|
|
}
|
||
|
|
switch cid {
|
||
|
|
case 10002:
|
||
|
|
for _, v := range config.GetConfig().Web.PassRegion {
|
||
|
|
if sip.Subdivisions[0].Names["en"] == v {
|
||
|
|
return true
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
|
||
|
|
// CheckExamine 检查是否进审核服
|
||
|
|
func CheckExamine(version int, ip, gpsID string, channel *common.Channel) (isExamine bool) {
|
||
|
|
if IsWhite(ip, "") || common.IsShareChannel(channel.ChannelID) {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
if version == channel.Version {
|
||
|
|
log.Debug("examine ip:%v", ip)
|
||
|
|
isExamine = true
|
||
|
|
db.ES().InsertToESByIDGO(common.ESIndexBackExamineList, ip, &common.ExamineList{Time: time.Now().Unix()})
|
||
|
|
return
|
||
|
|
}
|
||
|
|
// else if IsExamine(ip) {
|
||
|
|
// isExamine = true
|
||
|
|
// } else
|
||
|
|
if config.GetBase().Release {
|
||
|
|
ipInfo, err := SearchIP(ip)
|
||
|
|
if err != nil {
|
||
|
|
log.Error("err:%v", err)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
if ipInfo.Country.IsoCode == "CN" {
|
||
|
|
isExamine = true
|
||
|
|
return
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if channel.IgnoreOrganic == 2 && len(gpsID) > 0 {
|
||
|
|
if IsOrganic(gpsID, channel.AdjustAppToken, channel.ChannelID, 3) {
|
||
|
|
var count int64
|
||
|
|
if channel.ChannelID >= 46 {
|
||
|
|
count = db.Mysql().Count(&common.PlayerDBInfo{}, fmt.Sprintf("channel_id = %v and deviceid = '%v'", channel.ChannelID, gpsID))
|
||
|
|
} else {
|
||
|
|
count = db.Mysql().Count(&common.PlayerDBInfo{}, fmt.Sprintf("channel_id = %v and (deviceid = '%v' or gps_adid = '%v')", channel.ChannelID, gpsID, gpsID))
|
||
|
|
}
|
||
|
|
if count == 0 {
|
||
|
|
isExamine = true
|
||
|
|
return
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return
|
||
|
|
}
|