diff --git a/server/analyseapi/metamgr.go b/server/analyseapi/metamgr.go index e555336..3847f84 100644 --- a/server/analyseapi/metamgr.go +++ b/server/analyseapi/metamgr.go @@ -7,7 +7,10 @@ import ( const ( MT_SERVER_INFO = 0 - MT_CONF = iota + MT_IP_WHITE_LIST = iota + MT_IP_BLACK_LIST = iota + MT_LAUNCH_WHITE_LIST = iota + MT_LAUNCH_BLACK_LIST = iota MT_MAX = iota ) @@ -28,6 +31,26 @@ func (this *MetaMgr) Init() *MetaMgr { Idx: MT_SERVER_INFO, RawMeta: (*mt.ServerInfoMetas)(nil), WrapMeta: (*MtwServerInfo)(nil)}, + f5.MetaClass{ + FileName: configDir + "ip_whitelist.json", + Idx: MT_IP_WHITE_LIST, + RawMeta: (*mt.IpWhiteListMetas)(nil), + WrapMeta: (*MtwIpWhiteList)(nil)}, + f5.MetaClass{ + FileName: configDir + "ip_blacklist.json", + Idx: MT_IP_BLACK_LIST, + RawMeta: (*mt.IpBlackListMetas)(nil), + WrapMeta: (*MtwIpBlackList)(nil)}, + f5.MetaClass{ + FileName: configDir + "launch_whitelist.json", + Idx: MT_LAUNCH_BLACK_LIST, + RawMeta: (*mt.LaunchBlackListMetas)(nil), + WrapMeta: (*MtwLaunchBlackList)(nil)}, + f5.MetaClass{ + FileName: configDir + "launch_blacklist.json", + Idx: MT_LAUNCH_BLACK_LIST, + RawMeta: (*mt.LaunchBlackListMetas)(nil), + WrapMeta: (*MtwLaunchBlackList)(nil)}, } this.MetaMgr.RegisterMetaClasses(metaClasses) this.Load() @@ -46,3 +69,39 @@ func (this *MetaMgr) GetServer(instance_id int32) *MtwServerInfo { return nil } } + +func (this *MetaMgr) GetIpWhiteList() *MtwIpWhiteList { + v, ok := this.MetaMgr.GetMetaById(MT_IP_WHITE_LIST, 1).(*MtwIpWhiteList) + if ok { + return v + } else { + return nil + } +} + +func (this *MetaMgr) GetIpBlackList() *MtwIpBlackList { + v, ok := this.MetaMgr.GetMetaById(MT_IP_BLACK_LIST, 1).(*MtwIpBlackList) + if ok { + return v + } else { + return nil + } +} + +func (this *MetaMgr) GetLaunchWhiteList() *MtwLaunchWhiteList { + v, ok := this.MetaMgr.GetMetaById(MT_LAUNCH_WHITE_LIST, 1).(*MtwLaunchWhiteList) + if ok { + return v + } else { + return nil + } +} + +func (this *MetaMgr) GetLaunchBlackList() *MtwLaunchBlackList { + v, ok := this.MetaMgr.GetMetaById(MT_LAUNCH_BLACK_LIST, 1).(*MtwLaunchBlackList) + if ok { + return v + } else { + return nil + } +} diff --git a/server/analyseapi/metawrap.go b/server/analyseapi/metawrap.go index ff1f262..1f32865 100644 --- a/server/analyseapi/metawrap.go +++ b/server/analyseapi/metawrap.go @@ -6,10 +6,18 @@ type MtwServerInfo struct { *mt.ServerInfo } -type MtwWhiteList struct { - *mt.WhiteList +type MtwIpWhiteList struct { + *mt.IpWhiteList } -type MtwBlackList struct { - *mt.BlackList +type MtwIpBlackList struct { + *mt.IpBlackList +} + +type MtwLaunchWhiteList struct { + *mt.LaunchWhiteList +} + +type MtwLaunchBlackList struct { + *mt.LaunchBlackList } diff --git a/server/analyseapi/riskmgr.go b/server/analyseapi/riskmgr.go index a2fb980..28c46b6 100644 --- a/server/analyseapi/riskmgr.go +++ b/server/analyseapi/riskmgr.go @@ -1,20 +1,110 @@ package main -type RiskMgr struct { +import ( + "net/http" + "sync" + "fmt" + "q5" +) +type RiskMgr struct { + 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 } func (this* RiskMgr) Init() *RiskMgr { + this.ipWhiteListMutex.Lock() + this.ipBlackListMutex.Lock() + this.launchWhiteListMutex.Lock() + this.launchBlackListMutex.Lock() + defer this.ipWhiteListMutex.Unlock() + defer this.ipBlackListMutex.Unlock() + defer this.launchWhiteListMutex.Unlock() + defer this.launchBlackListMutex.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) + + 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 + } + + G.HttpServer.RegisterHandle("Analyse", "isOpen", this.__analyseIsOpen) return this } func (this* RiskMgr) UnInit() { } -func (this* RiskMgr) InWhiteList(ip string) bool { - return false +func (this* RiskMgr) InIpWhiteList(ip string) bool { + this.ipWhiteListMutex.Lock() + defer this.ipWhiteListMutex.Unlock() + _, ok := this.ipWhiteList[ip] + return ok } -func (this* RiskMgr) InBlackList(ip string) bool { - return false +func (this* RiskMgr) InIpBlackList(ip string) bool { + this.ipBlackListMutex.Lock() + defer this.ipBlackListMutex.Unlock() + _, ok := this.ipBlackList[ip] + return ok +} + +func (this* RiskMgr) InLaunchWhiteList(launchInfo string) bool { + this.launchWhiteListMutex.Lock() + defer this.launchWhiteListMutex.Unlock() + _, ok := this.launchWhiteList[launchInfo] + return ok +} + +func (this* RiskMgr) InLaunchBlackList(launchInfo string) bool { + this.launchBlackListMutex.Lock() + defer this.launchBlackListMutex.Unlock() + _, ok := this.launchBlackList[launchInfo] + return ok +} + +func (this *RiskMgr) __analyseIsOpen(w http.ResponseWriter, r *http.Request) { + if r.Method != "POST" { + w.Write([]byte(fmt.Sprintf(`{"errcode":0, "errmsg":"", "is_open":%d}`, 1))) + return + } + remoteAddr := q5.GetRequestRemoteAddr(r) + if G.RiskMgr.InIpWhiteList(remoteAddr) { + w.Write([]byte(fmt.Sprintf(`{"errcode":0, "errmsg":"", "is_open":%d}`, 1))) + return + } + if G.RiskMgr.InIpBlackList(remoteAddr) { + w.Write([]byte(fmt.Sprintf(`{"errcode":0, "errmsg":"", "is_open":%d}`, 0))) + return + } + launchInfo := q5.GetPostBody(r).GetString() + if G.RiskMgr.InLaunchWhiteList(launchInfo) { + w.Write([]byte(fmt.Sprintf(`{"errcode":0, "errmsg":"", "is_open":%d}`, 1))) + return + } + if G.RiskMgr.InLaunchBlackList(launchInfo) { + w.Write([]byte(fmt.Sprintf(`{"errcode":0, "errmsg":"", "is_open":%d}`, 0))) + return + } } diff --git a/server/tools/protobuild/mt.proto b/server/tools/protobuild/mt.proto index 1a4281a..ded8eaa 100644 --- a/server/tools/protobuild/mt.proto +++ b/server/tools/protobuild/mt.proto @@ -14,22 +14,42 @@ message ServerInfoMetas repeated ServerInfo values = 1; } -message WhiteList +message IpWhiteList { - repeated string ip_tables = 1; + repeated string list = 1; } -message WhiteListMetas +message IpWhiteListMetas { - repeated WhiteList values = 1; + repeated IpWhiteList values = 1; } -message BlackList +message IpBlackList { - repeated string ip_tables = 1; + repeated string list = 1; } -message BlackListMetas +message IpBlackListMetas { - repeated WhiteList values = 1; + repeated IpWhiteList values = 1; +} + +message LaunchWhiteList +{ + repeated string list = 1; +} + +message LaunchWhiteListMetas +{ + repeated LaunchWhiteList values = 1; +} + +message LaunchBlackList +{ + repeated string list = 1; +} + +message LaunchBlackListMetas +{ + repeated IpWhiteList values = 1; } diff --git a/third_party/q5 b/third_party/q5 index 7442ad5..933ed5c 160000 --- a/third_party/q5 +++ b/third_party/q5 @@ -1 +1 @@ -Subproject commit 7442ad5d81333a6a5c6968dc0df5b1f86e9d4d73 +Subproject commit 933ed5c972526defdfc035714f769ac10e0ce8c5