2018-08-12 10:46:46 +00:00
|
|
|
package discovery
|
|
|
|
|
|
|
|
import (
|
2018-08-29 09:48:03 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"git.loafle.net/commons/util-go/net/cidr"
|
2018-08-12 10:46:46 +00:00
|
|
|
"git.loafle.net/overflow/model/meta"
|
|
|
|
)
|
|
|
|
|
|
|
|
type DiscoverHost struct {
|
|
|
|
MetaIPType *meta.MetaIPType `json:"metaIPType,omitempty"`
|
|
|
|
|
|
|
|
FirstScanRange string `json:"firstScanRange,omitempty"`
|
|
|
|
LastScanRange string `json:"lastScanRange,omitempty"`
|
|
|
|
ExcludeHosts []string `json:"excludeHosts,omitempty"`
|
|
|
|
IncludeHosts []string `json:"includeHosts,omitempty"`
|
|
|
|
|
|
|
|
DiscoverPort *DiscoverPort `json:"discoverPort,omitempty"`
|
|
|
|
}
|
2018-08-29 09:48:03 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|