From 8e1110ef20b299401f5ce42d26d8a0887e7c4442 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Thu, 29 Oct 2020 19:53:10 +0800 Subject: [PATCH] 1 --- server/analyseapi/go.mod | 1 + server/analyseapi/go.sum | 27 ++++++++++++++++++++++ server/analyseapi/metamgr.go | 15 ++++++++++++ server/analyseapi/metawrap.go | 4 ++++ server/analyseapi/riskmgr.go | 39 ++++++++++++++++++++++++-------- server/tools/protobuild/mt.proto | 11 +++++++++ 6 files changed, 87 insertions(+), 10 deletions(-) diff --git a/server/analyseapi/go.mod b/server/analyseapi/go.mod index 632d09a..fd41d96 100644 --- a/server/analyseapi/go.mod +++ b/server/analyseapi/go.mod @@ -9,6 +9,7 @@ require f5 v1.0.0 require mt v1.0.0 require ( + github.com/aliyun/alibaba-cloud-sdk-go v1.61.614 github.com/golang/protobuf v1.4.2 google.golang.org/protobuf v1.23.0 im v1.0.0 diff --git a/server/analyseapi/go.sum b/server/analyseapi/go.sum index 4630df5..e64f1cd 100644 --- a/server/analyseapi/go.sum +++ b/server/analyseapi/go.sum @@ -1,3 +1,8 @@ +github.com/aliyun/alibaba-cloud-sdk-go v1.61.614 h1:FHWBfCJLqPmXTe/rdx1D0Dp0uQkNDcadDTmXEO5YcEE= +github.com/aliyun/alibaba-cloud-sdk-go v1.61.614/go.mod h1:pUKYbK5JQ+1Dfxk80P0qxGqe5dkxDoabbZS7zOcouyA= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/goji/httpauth v0.0.0-20160601135302-2da839ab0f4d/go.mod h1:nnjvkQ9ptGaCkuDUx6wNykzzlUixGxvkme+H/lnzb+A= github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= @@ -8,6 +13,26 @@ github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= +github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM= +github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= +github.com/json-iterator/go v1.1.5 h1:gL2yXlmiIo4+t+y32d4WGwOjKGYcGOuyrg46vadswDE= +github.com/json-iterator/go v1.1.5/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= +github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg= +github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= +github.com/smartystreets/goconvey v0.0.0-20190330032615-68dc04aab96a/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= @@ -16,3 +41,5 @@ google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miE google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= google.golang.org/protobuf v1.23.0 h1:4MY060fB1DLGMB/7MBTLnwQUY6+F09GEiz6SsrNqyzM= google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +gopkg.in/ini.v1 v1.42.0 h1:7N3gPTt50s8GuLortA00n8AqRTk75qOP98+mTPpgzRk= +gopkg.in/ini.v1 v1.42.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= diff --git a/server/analyseapi/metamgr.go b/server/analyseapi/metamgr.go index 3847f84..bc02d47 100644 --- a/server/analyseapi/metamgr.go +++ b/server/analyseapi/metamgr.go @@ -7,6 +7,7 @@ import ( const ( MT_SERVER_INFO = 0 + MT_ALI_KEY = iota MT_IP_WHITE_LIST = iota MT_IP_BLACK_LIST = iota MT_LAUNCH_WHITE_LIST = iota @@ -31,6 +32,11 @@ func (this *MetaMgr) Init() *MetaMgr { Idx: MT_SERVER_INFO, RawMeta: (*mt.ServerInfoMetas)(nil), WrapMeta: (*MtwServerInfo)(nil)}, + f5.MetaClass{ + FileName: configDir + "ali_key.json", + Idx: MT_ALI_KEY, + RawMeta: (*mt.AliKeyInfoMetas)(nil), + WrapMeta: (*MtwAliKeyConf)(nil)}, f5.MetaClass{ FileName: configDir + "ip_whitelist.json", Idx: MT_IP_WHITE_LIST, @@ -70,6 +76,15 @@ func (this *MetaMgr) GetServer(instance_id int32) *MtwServerInfo { } } +func (this *MetaMgr) GetAliKey() *MtwAliKeyConf { + v, ok := this.MetaMgr.GetMetaById(MT_ALI_KEY, 1).(*MtwAliKeyConf) + if ok { + return v + } else { + return nil + } +} + func (this *MetaMgr) GetIpWhiteList() *MtwIpWhiteList { v, ok := this.MetaMgr.GetMetaById(MT_IP_WHITE_LIST, 1).(*MtwIpWhiteList) if ok { diff --git a/server/analyseapi/metawrap.go b/server/analyseapi/metawrap.go index 1f32865..9d4b243 100644 --- a/server/analyseapi/metawrap.go +++ b/server/analyseapi/metawrap.go @@ -6,6 +6,10 @@ type MtwServerInfo struct { *mt.ServerInfo } +type MtwAliKeyConf struct { + *mt.AliKeyConf +} + type MtwIpWhiteList struct { *mt.IpWhiteList } diff --git a/server/analyseapi/riskmgr.go b/server/analyseapi/riskmgr.go index e5f20f8..78cd06e 100644 --- a/server/analyseapi/riskmgr.go +++ b/server/analyseapi/riskmgr.go @@ -3,7 +3,7 @@ package main import ( "net/http" "sync" - //"github.com/aliyun/alibaba-cloud-sdk-go/services/geoip" + "github.com/aliyun/alibaba-cloud-sdk-go/services/geoip" "q5" "f5" ) @@ -23,6 +23,10 @@ type RiskMgr struct { blockProvinceCityHash map[string]int32 blockProvinceCityHashMutex sync.RWMutex + + accessKeyId string + accessSecret string + sdkInfoMutex sync.RWMutex } func (this* RiskMgr) Init() *RiskMgr { @@ -55,6 +59,8 @@ func (this* RiskMgr) Init() *RiskMgr { for _, val := range G.MetaMgr.GetLaunchBlackList().GetList() { this.launchBlackList[val] = 1 } + this.accessKeyId = G.MetaMgr.GetAliKey().GetAccessKeyId() + this.accessSecret = G.MetaMgr.GetAliKey().GetAccessSecret() G.HttpServer.RegisterHandle("Analyse", "isOpen", this.__analyseIsOpen) return this @@ -114,20 +120,33 @@ func (this* RiskMgr) IsBlockZone(gameId int32, channel int32, return false } +func (this* RiskMgr) GetSdkInfo(accessKeyId* string, accessSecret* string) { + this.sdkInfoMutex.Lock() + defer this.sdkInfoMutex.Unlock() + *accessKeyId = "" + this.accessKeyId + *accessSecret = "" + this.accessSecret +} + func (this* RiskMgr) IsSafeZone(gameId int32, channel int32, ip string) bool { - //_, _ := geoip.NewClientWithAccessKey("cn-hangzhou", "", "") - response := "" - xobj := q5.NewXoFromJsonStr(response) - if xobj == nil { + accessKeyId := "" + accessSecret := "" + this.GetSdkInfo(&accessKeyId, &accessSecret) + client, err := geoip.NewClientWithAccessKey("cn-hangzhou", accessKeyId, accessSecret) + if err != nil { + f5.SysLog().Warning("NewClientWithAccessKey error %s", err.Error()) return false } - if !xobj.IsObject() { + + 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 } - country := xobj.GetSimpleStr("Country", "") - province := xobj.GetSimpleStr("Province", "") - city := xobj.GetSimpleStr("City", "") - return !this.IsBlockZone(gameId, channel, country, province, city) + return !this.IsBlockZone(gameId, channel, response.Country, response.Province, response.City) } func (this *RiskMgr) __analyseIsOpen(w http.ResponseWriter, r *http.Request) { diff --git a/server/tools/protobuild/mt.proto b/server/tools/protobuild/mt.proto index ded8eaa..f7f14c0 100644 --- a/server/tools/protobuild/mt.proto +++ b/server/tools/protobuild/mt.proto @@ -2,6 +2,17 @@ package mt; option go_package = ".;mt"; +message AliKeyConf +{ + optional string accessKeyId = 1; + optional string accessSecret = 2; +} + +message AliKeyInfoMetas +{ + repeated AliKeyConf values = 1; +} + message ServerInfo { optional int32 instance_id = 1;