From cfa3f946927ddc50d8006d74c76f019b8659d38c Mon Sep 17 00:00:00 2001 From: crusader Date: Wed, 29 Aug 2018 18:48:03 +0900 Subject: [PATCH] TargetHosts of DiscoverHost added --- discovery/DiscoverHost.go | 51 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/discovery/DiscoverHost.go b/discovery/DiscoverHost.go index 18c2a0d..f3cd4cf 100644 --- a/discovery/DiscoverHost.go +++ b/discovery/DiscoverHost.go @@ -1,6 +1,10 @@ package discovery import ( + "fmt" + "net" + + "git.loafle.net/commons/util-go/net/cidr" "git.loafle.net/overflow/model/meta" ) @@ -14,3 +18,50 @@ type DiscoverHost struct { DiscoverPort *DiscoverPort `json:"discoverPort,omitempty"` } + +func (dh *DiscoverHost) TargetHosts(network string) ([]net.IP, error) { + cr, err := cidr.NewCIDRRanger(network) + if nil != err { + return nil, err + } + + var firstIP net.IP + if "" != dh.FirstScanRange { + firstIP = net.ParseIP(dh.FirstScanRange) + if nil == firstIP { + return nil, fmt.Errorf("IP(%v) of FirstScanRange host is not valid", firstIP) + } + } + var lastIP net.IP + if "" != dh.LastScanRange { + lastIP = net.ParseIP(dh.LastScanRange) + if nil == lastIP { + return nil, fmt.Errorf("IP(%v) of LastScanRange host is not valid", lastIP) + } + } + + includeIPs := make([]net.IP, 0) + for _, iHost := range dh.IncludeHosts { + iIP := net.ParseIP(iHost) + if nil == iIP { + return nil, fmt.Errorf("IP(%v) of include host is not valid", iHost) + } + includeIPs = append(includeIPs, iIP) + } + + excludeIPs := make([]net.IP, 0) + for _, eHost := range dh.ExcludeHosts { + eIP := net.ParseIP(eHost) + if nil == eIP { + return nil, fmt.Errorf("IP(%v) of exclude host is not valid", eHost) + } + excludeIPs = append(excludeIPs, eIP) + } + + ranges, err := cr.Ranges(firstIP, lastIP, includeIPs, excludeIPs) + if nil != err { + return nil, err + } + + return ranges, nil +}