89 lines
1.7 KiB
Go
89 lines
1.7 KiB
Go
package discovery
|
|
|
|
import (
|
|
"net"
|
|
"regexp"
|
|
"strings"
|
|
|
|
discoveryM "git.loafle.net/overflow/overflow_commons_go/modules/discovery/model"
|
|
)
|
|
|
|
func scanZone(dz *discoveryM.DiscoveryZone, 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
|
|
var ipnet *net.IPNet
|
|
var zones []*net.IPNet
|
|
// 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
|
|
}
|
|
|
|
zones = make([]*net.IPNet, 0)
|
|
|
|
for _, i := range ifaces {
|
|
|
|
if addrs, err = i.Addrs(); nil != err {
|
|
errChan <- err
|
|
continue
|
|
}
|
|
|
|
for _, addr := range addrs {
|
|
|
|
if _, ipnet, err = net.ParseCIDR(addr.String()); nil != err {
|
|
errChan <- err
|
|
continue
|
|
}
|
|
if ipnet.IP.IsLoopback() || checkSameZone(zones, ipnet) || checkExclude(dz.ExcludePatterns, i.Name) {
|
|
continue
|
|
}
|
|
|
|
zones = append(zones, ipnet)
|
|
|
|
z := &discoveryM.Zone{
|
|
Network: ipnet.String(),
|
|
Iface: i.Name,
|
|
Mac: i.HardwareAddr.String(),
|
|
IP: strings.Split(addr.String(), "/")[0],
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
func checkSameZone(zones []*net.IPNet, ipnet *net.IPNet) bool {
|
|
for _, i := range zones {
|
|
if i.Contains(ipnet.IP) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|