TargetHosts of DiscoverHost added

This commit is contained in:
crusader 2018-08-29 18:48:03 +09:00
parent 9d8de5fbd6
commit cfa3f94692

View File

@ -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
}