ing
This commit is contained in:
parent
de44a6bd7e
commit
cb7a0cea64
|
@ -1,11 +1,8 @@
|
|||
package types
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"sync"
|
||||
|
||||
"git.loafle.net/commons_go/logging"
|
||||
)
|
||||
|
||||
type discoverTestZoneCallback func(host *DiscoveryZone)
|
||||
|
@ -49,7 +46,7 @@ func (z *DiscoveryZone) CidrString() string {
|
|||
|
||||
_, ipnet, err := net.ParseCIDR(str)
|
||||
if nil != err {
|
||||
logging.Logger.Error(fmt.Sprintf("ParseCIDR: %v", err))
|
||||
//logging.Logger.Error(fmt.Sprintf("ParseCIDR: %v", err))
|
||||
}
|
||||
|
||||
return ipnet.String()
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
package: git.loafle.net/overflow/overflow_discovery
|
||||
import:
|
||||
- package: git.loafle.net/commons_go/util
|
||||
- package: git.loafle.net/commons_go/logging
|
||||
- package: github.com/google/gopacket
|
||||
version: v1.1.14
|
||||
|
|
16
net/config.go
Normal file
16
net/config.go
Normal file
|
@ -0,0 +1,16 @@
|
|||
package net
|
||||
|
||||
type DiscoveryConfig struct {
|
||||
}
|
||||
|
||||
type DiscoveryZoneConfig struct {
|
||||
}
|
||||
|
||||
type DiscoveryHostConfig struct {
|
||||
}
|
||||
|
||||
type DiscoveryPortConfig struct {
|
||||
}
|
||||
|
||||
type DiscoveryServiceConfig struct {
|
||||
}
|
16
net/model/host.go
Normal file
16
net/model/host.go
Normal file
|
@ -0,0 +1,16 @@
|
|||
package model
|
||||
|
||||
import "git.loafle.net/overflow/overflow_probe/model/timestamp"
|
||||
|
||||
type DiscoveryHost struct {
|
||||
Zone *DiscoveryZone `json:"-"`
|
||||
ID int `json:"id,omitempty"`
|
||||
IP string `json:"ip"`
|
||||
Mac string `json:"mac"`
|
||||
|
||||
Os string `json:"os,omitempty"`
|
||||
Target bool `json:"target,omitempty"`
|
||||
|
||||
CreateDate timestamp.Timestamp `json:"createDate,omitempty"`
|
||||
UpdateDate timestamp.Timestamp `json:"updateDate,omitempty"`
|
||||
}
|
4
net/model/network.go
Normal file
4
net/model/network.go
Normal file
|
@ -0,0 +1,4 @@
|
|||
package model
|
||||
|
||||
type DiscoveryNetwork struct {
|
||||
}
|
11
net/model/zone.go
Normal file
11
net/model/zone.go
Normal file
|
@ -0,0 +1,11 @@
|
|||
package model
|
||||
|
||||
type DiscoveryZone struct {
|
||||
ID int `json:"id,omitempty"`
|
||||
IP string `json:"ip"`
|
||||
Netmask string `json:"netmask"`
|
||||
Iface string `json:"iface"`
|
||||
Mac string `json:"mac"`
|
||||
FirstScanRange int64 `json:"firstScanRange"`
|
||||
LastScanRange int64 `json:"lastScanRange"`
|
||||
}
|
13
net/net.go
Normal file
13
net/net.go
Normal file
|
@ -0,0 +1,13 @@
|
|||
package net
|
||||
|
||||
type Discoverer interface {
|
||||
}
|
||||
|
||||
type discovery struct {
|
||||
}
|
||||
|
||||
func NewDiscoverer() Discoverer {
|
||||
d := &discovery{}
|
||||
|
||||
return d
|
||||
}
|
61
net/zone.go
Normal file
61
net/zone.go
Normal file
|
@ -0,0 +1,61 @@
|
|||
package net
|
||||
|
||||
import "net"
|
||||
import "git.loafle.net/overflow/overflow_discovery/net/model"
|
||||
|
||||
func ScanZone(endChan chan<- bool, zoneChan chan<- *model.DiscoveryZone, logChan chan<- error) {
|
||||
var err error
|
||||
var ifaces []net.Interface
|
||||
var addrs []net.Addr
|
||||
var ipnet *net.IPNet
|
||||
var zones []*net.IPNet
|
||||
|
||||
if ifaces, err = net.Interfaces(); nil != err {
|
||||
logChan <- err
|
||||
return
|
||||
}
|
||||
|
||||
zones = make([]*net.IPNet, 0)
|
||||
|
||||
for _, i := range ifaces {
|
||||
|
||||
if addrs, err = i.Addrs(); nil != err {
|
||||
logChan <- err
|
||||
continue
|
||||
}
|
||||
|
||||
for _, addr := range addrs {
|
||||
// log.Printf("addr: %s", addr.String())
|
||||
if _, ipnet, err = net.ParseCIDR(addr.String()); nil != err {
|
||||
logChan <- err
|
||||
continue
|
||||
}
|
||||
if ipnet.IP.IsLoopback() || checkSameZone(zones, ipnet) {
|
||||
continue
|
||||
}
|
||||
|
||||
// log.Printf("zone: %s", ipnet.String())
|
||||
zones = append(zones, ipnet)
|
||||
|
||||
dz := &model.DiscoveryZone{
|
||||
Iface: i.Name,
|
||||
Mac: i.HardwareAddr.String(),
|
||||
IP: ipnet.IP.String(),
|
||||
}
|
||||
|
||||
zoneChan <- dz
|
||||
}
|
||||
}
|
||||
|
||||
endChan <- true
|
||||
|
||||
}
|
||||
|
||||
func checkSameZone(zones []*net.IPNet, ipnet *net.IPNet) bool {
|
||||
for _, i := range zones {
|
||||
if i.Contains(ipnet.IP) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
35
net/zone_test.go
Normal file
35
net/zone_test.go
Normal file
|
@ -0,0 +1,35 @@
|
|||
package net
|
||||
|
||||
import (
|
||||
"log"
|
||||
"testing"
|
||||
|
||||
"git.loafle.net/overflow/overflow_discovery/net/model"
|
||||
)
|
||||
|
||||
func TestScanZone(t *testing.T) {
|
||||
endChan := make(chan bool)
|
||||
zoneChan := make(chan *model.DiscoveryZone, 4)
|
||||
logChan := make(chan error, 4)
|
||||
|
||||
defer func() {
|
||||
close(endChan)
|
||||
close(zoneChan)
|
||||
close(logChan)
|
||||
}()
|
||||
|
||||
go ScanZone(endChan, zoneChan, logChan)
|
||||
|
||||
Loop:
|
||||
for {
|
||||
select {
|
||||
case zone := <-zoneChan:
|
||||
log.Printf("zone: %v", zone)
|
||||
case err := <-logChan:
|
||||
log.Printf("log: %v", err)
|
||||
case <-endChan:
|
||||
break Loop
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user