2018-08-13 07:19:59 +00:00
|
|
|
package discoverer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"regexp"
|
|
|
|
|
|
|
|
omd "git.loafle.net/overflow/model/discovery"
|
|
|
|
omm "git.loafle.net/overflow/model/meta"
|
|
|
|
omu "git.loafle.net/overflow/model/util"
|
2018-08-23 09:21:48 +00:00
|
|
|
oun "git.loafle.net/overflow/util-go/net"
|
2018-08-13 07:19:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func scanZone(dz *omd.DiscoverZone, resultChan chan interface{}, errChan chan error, doneChan chan<- struct{}, stopChan chan struct{}) {
|
|
|
|
defer func() {
|
|
|
|
doneChan <- struct{}{}
|
|
|
|
}()
|
|
|
|
|
|
|
|
var err error
|
|
|
|
var ifaces []net.Interface
|
|
|
|
var addrs []net.Addr
|
2018-08-23 09:21:48 +00:00
|
|
|
var ipnet *oun.IPNet
|
|
|
|
var zones []*oun.IPNet
|
2018-08-13 07:19:59 +00:00
|
|
|
// var gwIP net.IP
|
|
|
|
// var gwIFace string
|
|
|
|
|
|
|
|
// if gwIP, gwIFace, err = gateway.DiscoverGateway(); nil != err {
|
|
|
|
// logChan <- err
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
|
|
|
|
if ifaces, err = net.Interfaces(); nil != err {
|
|
|
|
errChan <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-23 09:21:48 +00:00
|
|
|
zones = make([]*oun.IPNet, 0)
|
2018-08-13 07:19:59 +00:00
|
|
|
|
|
|
|
for _, i := range ifaces {
|
|
|
|
|
|
|
|
if addrs, err = i.Addrs(); nil != err {
|
|
|
|
errChan <- err
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, addr := range addrs {
|
|
|
|
|
2018-08-23 09:21:48 +00:00
|
|
|
if _, ipnet, err = oun.ParseCIDR(addr.String()); nil != err {
|
2018-08-13 07:19:59 +00:00
|
|
|
errChan <- err
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if ipnet.IP.IsLoopback() || checkSameZone(zones, ipnet) || checkExclude(dz.ExcludePatterns, i.Name) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
zones = append(zones, ipnet)
|
|
|
|
|
|
|
|
z := &omd.Zone{
|
|
|
|
Network: ipnet.String(),
|
|
|
|
Iface: i.Name,
|
|
|
|
Mac: i.HardwareAddr.String(),
|
|
|
|
Address: addr.String(),
|
|
|
|
DiscoveredDate: omu.NowPtr(),
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ipnet.Version() {
|
|
|
|
case 6:
|
|
|
|
z.MetaIPType = omm.ToMetaIPType(omm.MetaIPTypeEnumV6)
|
|
|
|
default:
|
|
|
|
z.MetaIPType = omm.ToMetaIPType(omm.MetaIPTypeEnumV4)
|
|
|
|
}
|
|
|
|
|
|
|
|
resultChan <- z
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkExclude(ep []string, iface string) bool {
|
|
|
|
var r *regexp.Regexp
|
|
|
|
var err error
|
|
|
|
for _, p := range ep {
|
|
|
|
if r, err = regexp.Compile(p); nil != err {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if r.MatchString(iface) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-08-23 09:21:48 +00:00
|
|
|
func checkSameZone(zones []*oun.IPNet, ipnet *oun.IPNet) bool {
|
2018-08-13 07:19:59 +00:00
|
|
|
for _, i := range zones {
|
|
|
|
if i.Contains(ipnet.IP) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|