ing
This commit is contained in:
parent
7ae03b9168
commit
f95ca6bdf1
|
@ -1,6 +1,7 @@
|
|||
package discovery
|
||||
|
||||
import (
|
||||
"log"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
|
@ -189,19 +190,28 @@ func (d *ofDiscoverer) complexDiscover(s session.DiscoverySession) {
|
|||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
upnp.Scan(s)
|
||||
err := upnp.Scan(s)
|
||||
if nil != err {
|
||||
log.Printf("UPnP %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
mdns.Scan(s)
|
||||
err := mdns.Scan(s)
|
||||
if nil != err {
|
||||
log.Printf("mDNS %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
snmp.Scan(s)
|
||||
err := snmp.Scan(s)
|
||||
if nil != err {
|
||||
log.Printf("SNMP %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
wg.Wait()
|
||||
|
|
172
discovery/protocol/arp/arp.go
Normal file
172
discovery/protocol/arp/arp.go
Normal file
|
@ -0,0 +1,172 @@
|
|||
package arp
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
omd "git.loafle.net/overflow/model/discovery"
|
||||
omm "git.loafle.net/overflow/model/meta"
|
||||
omu "git.loafle.net/overflow/model/util"
|
||||
"git.loafle.net/overflow_scanner/probe/discovery/session"
|
||||
"git.loafle.net/overflow_scanner/probe/internal/pcap"
|
||||
"github.com/google/gopacket"
|
||||
"github.com/google/gopacket/layers"
|
||||
)
|
||||
|
||||
func Scan(discoverySession session.DiscoverySession) error {
|
||||
targetHosts := discoverySession.TargetHosts()
|
||||
if nil == targetHosts || 0 == len(targetHosts) {
|
||||
return nil
|
||||
}
|
||||
|
||||
zone := discoverySession.Zone()
|
||||
|
||||
ps, err := pcap.RetainScanner(zone)
|
||||
if nil != err {
|
||||
return fmt.Errorf("Cannot retain pcap instance %v", err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
pcap.ReleaseScanner(zone)
|
||||
}()
|
||||
|
||||
arpChan := ps.OpenARP()
|
||||
defer func() {
|
||||
ps.CloseARP(arpChan)
|
||||
}()
|
||||
|
||||
timerStopped := make(chan struct{})
|
||||
go func() {
|
||||
hosts := make(map[string]*omd.Host)
|
||||
|
||||
var delay atomic.Value
|
||||
delay.Store(false)
|
||||
ticker := time.NewTicker(time.Millisecond * 500)
|
||||
|
||||
for {
|
||||
select {
|
||||
case packet, ok := <-arpChan:
|
||||
if !ok {
|
||||
// logging.Logger().Debugf("arp channel is closed")
|
||||
return
|
||||
}
|
||||
delay.Store(true)
|
||||
if h := handlePacketARP(zone, targetHosts, hosts, packet); nil != h {
|
||||
if h != nil {
|
||||
log.Println(h)
|
||||
}
|
||||
|
||||
// resultChan <- h
|
||||
}
|
||||
case <-ticker.C:
|
||||
if false == delay.Load().(bool) {
|
||||
ticker.Stop()
|
||||
timerStopped <- struct{}{}
|
||||
return
|
||||
}
|
||||
delay.Store(false)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
if err := sendARP(ps, zone, targetHosts); nil != err {
|
||||
log.Printf("sendARP %v", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
select {
|
||||
case <-timerStopped:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func sendARP(ps pcap.PCapScanner, zone *omd.Zone, targetHosts []net.IP) error {
|
||||
hwAddr, err := net.ParseMAC(zone.Mac)
|
||||
if nil != err {
|
||||
return err
|
||||
}
|
||||
ip := net.ParseIP(zone.Address)
|
||||
if nil == ip {
|
||||
return fmt.Errorf("Discovery: IP(%s) of zone is not valid", zone.Address)
|
||||
}
|
||||
|
||||
ethPacket := makePacketEthernet(hwAddr)
|
||||
arpPacket := makePacketARP(hwAddr, ip.To4())
|
||||
opts := gopacket.SerializeOptions{FixLengths: true, ComputeChecksums: true}
|
||||
buf := gopacket.NewSerializeBuffer()
|
||||
|
||||
for _, targetHost := range targetHosts {
|
||||
arpPacket.DstProtAddress = []byte(targetHost)
|
||||
// log.Printf("ARP:%v", arpPacket)
|
||||
gopacket.SerializeLayers(buf, opts, ðPacket, &arpPacket)
|
||||
if err := ps.WritePacketData(buf.Bytes()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
timer := time.NewTimer(time.Microsecond * 100)
|
||||
|
||||
select {
|
||||
case <-timer.C:
|
||||
}
|
||||
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func handlePacketARP(zone *omd.Zone, targetHosts []net.IP, hosts map[string]*omd.Host, packet *layers.ARP) *omd.Host {
|
||||
if packet.Operation != layers.ARPReply {
|
||||
return nil
|
||||
}
|
||||
|
||||
ip := net.IP(packet.SourceProtAddress)
|
||||
if _, ok := hosts[ip.String()]; ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
inRange := false
|
||||
for _, h := range targetHosts {
|
||||
if h.Equal(ip) {
|
||||
inRange = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !inRange {
|
||||
return nil
|
||||
}
|
||||
|
||||
h := &omd.Host{
|
||||
MetaIPType: omm.ToMetaIPType(omm.MetaIPTypeEnumV4),
|
||||
Address: ip.String(),
|
||||
Mac: net.HardwareAddr(packet.SourceHwAddress).String(),
|
||||
Zone: zone,
|
||||
DiscoveredDate: omu.NowPtr(),
|
||||
}
|
||||
|
||||
hosts[ip.String()] = h
|
||||
|
||||
return h
|
||||
}
|
||||
|
||||
func makePacketEthernet(hw net.HardwareAddr) layers.Ethernet {
|
||||
return layers.Ethernet{
|
||||
SrcMAC: hw,
|
||||
DstMAC: net.HardwareAddr{0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
|
||||
EthernetType: layers.EthernetTypeARP,
|
||||
}
|
||||
}
|
||||
|
||||
func makePacketARP(hw net.HardwareAddr, ip net.IP) layers.ARP {
|
||||
return layers.ARP{
|
||||
AddrType: layers.LinkTypeEthernet,
|
||||
Protocol: layers.EthernetTypeIPv4,
|
||||
HwAddressSize: 6,
|
||||
ProtAddressSize: 4,
|
||||
Operation: layers.ARPRequest,
|
||||
SourceHwAddress: []byte(hw),
|
||||
SourceProtAddress: []byte(ip),
|
||||
DstHwAddress: []byte{0, 0, 0, 0, 0, 0},
|
||||
}
|
||||
}
|
144
discovery/protocol/arp/arp_test.go
Normal file
144
discovery/protocol/arp/arp_test.go
Normal file
|
@ -0,0 +1,144 @@
|
|||
package arp
|
||||
|
||||
import (
|
||||
"net"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
omd "git.loafle.net/overflow/model/discovery"
|
||||
omm "git.loafle.net/overflow/model/meta"
|
||||
"git.loafle.net/overflow_scanner/probe/discovery/session"
|
||||
"git.loafle.net/overflow_scanner/probe/discovery/types"
|
||||
"git.loafle.net/overflow_scanner/probe/internal/pcap"
|
||||
"github.com/google/gopacket/layers"
|
||||
)
|
||||
|
||||
func TestScan(t *testing.T) {
|
||||
s := session.NewMockDiscoverySession(
|
||||
"testRequester",
|
||||
types.DiscoveryRequestTypeHost,
|
||||
&omd.Zone{
|
||||
Network: "192.168.1.0/24",
|
||||
Iface: "enp3s0",
|
||||
MetaIPType: omm.ToMetaIPType(omm.MetaIPTypeEnumV4),
|
||||
Address: "192.168.1.101",
|
||||
Mac: "44:8a:5b:f1:f1:f3",
|
||||
},
|
||||
&omd.DiscoverHost{
|
||||
MetaIPType: omm.ToMetaIPType(omm.MetaIPTypeEnumV4),
|
||||
FirstScanRange: "192.168.1.1",
|
||||
LastScanRange: "192.168.1.254",
|
||||
DiscoveryConfig: &omd.DiscoveryConfig{},
|
||||
},
|
||||
)
|
||||
|
||||
type args struct {
|
||||
discoverySession session.DiscoverySession
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "1",
|
||||
args: args{
|
||||
discoverySession: s,
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if err := Scan(tt.args.discoverySession); (err != nil) != tt.wantErr {
|
||||
t.Errorf("Scan() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_sendARP(t *testing.T) {
|
||||
type args struct {
|
||||
ps pcap.PCapScanner
|
||||
zone *omd.Zone
|
||||
targetHosts []net.IP
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantErr bool
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if err := sendARP(tt.args.ps, tt.args.zone, tt.args.targetHosts); (err != nil) != tt.wantErr {
|
||||
t.Errorf("sendARP() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_handlePacketARP(t *testing.T) {
|
||||
type args struct {
|
||||
zone *omd.Zone
|
||||
targetHosts []net.IP
|
||||
hosts map[string]*omd.Host
|
||||
packet *layers.ARP
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want *omd.Host
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := handlePacketARP(tt.args.zone, tt.args.targetHosts, tt.args.hosts, tt.args.packet); !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("handlePacketARP() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_makePacketEthernet(t *testing.T) {
|
||||
type args struct {
|
||||
hw net.HardwareAddr
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want layers.Ethernet
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := makePacketEthernet(tt.args.hw); !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("makePacketEthernet() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_makePacketARP(t *testing.T) {
|
||||
type args struct {
|
||||
hw net.HardwareAddr
|
||||
ip net.IP
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want layers.ARP
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := makePacketARP(tt.args.hw, tt.args.ip); !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("makePacketARP() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
|
@ -17,23 +17,25 @@ import (
|
|||
"github.com/grandcat/zeroconf"
|
||||
)
|
||||
|
||||
func Scan(discoverySession session.DiscoverySession) {
|
||||
func Scan(discoverySession session.DiscoverySession) error {
|
||||
serviceEntries, err := browse("_services._dns-sd._udp", "local")
|
||||
if nil != err {
|
||||
log.Print("Cannot find service ", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
metaIPTypeEnum := omm.ToMetaIPTypeEnum(discoverySession.Zone().MetaIPType)
|
||||
|
||||
SERVICE_LOOP:
|
||||
for _, serviceEntry := range serviceEntries {
|
||||
name := removeDomainName(serviceEntry.Instance, serviceEntry.Domain)
|
||||
entries, _err := browse(name, serviceEntry.Domain)
|
||||
if nil != _err {
|
||||
log.Print("Cannot find entry ", _err)
|
||||
return
|
||||
continue SERVICE_LOOP
|
||||
}
|
||||
|
||||
LOOP:
|
||||
ENTRY_LOOP:
|
||||
for _, entry := range entries {
|
||||
// log.Print("serviceEntry ", entry)
|
||||
|
||||
|
@ -73,7 +75,7 @@ func Scan(discoverySession session.DiscoverySession) {
|
|||
})
|
||||
|
||||
if 1 > port {
|
||||
continue LOOP
|
||||
continue ENTRY_LOOP
|
||||
}
|
||||
|
||||
p := discoverySession.AddPort(&omd.Port{
|
||||
|
@ -103,7 +105,7 @@ func Scan(discoverySession session.DiscoverySession) {
|
|||
})
|
||||
|
||||
if 1 > port {
|
||||
continue LOOP
|
||||
continue ENTRY_LOOP
|
||||
}
|
||||
|
||||
p := discoverySession.AddPort(&omd.Port{
|
||||
|
@ -124,6 +126,8 @@ func Scan(discoverySession session.DiscoverySession) {
|
|||
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func removeDomainName(instance string, domain string) (service string) {
|
||||
|
|
|
@ -36,17 +36,21 @@ func TestScan(t *testing.T) {
|
|||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "1",
|
||||
args: args{
|
||||
discoverySession: s,
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
Scan(tt.args.discoverySession)
|
||||
if err := Scan(tt.args.discoverySession); (err != nil) != tt.wantErr {
|
||||
t.Errorf("Scan() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -38,10 +38,10 @@ var (
|
|||
}
|
||||
)
|
||||
|
||||
func Scan(discoverySession session.DiscoverySession) {
|
||||
func Scan(discoverySession session.DiscoverySession) error {
|
||||
targetHosts := discoverySession.TargetHosts()
|
||||
if nil == targetHosts || 0 == len(targetHosts) {
|
||||
return
|
||||
return nil
|
||||
}
|
||||
|
||||
var cs []*omcc.SNMPCredential
|
||||
|
@ -84,6 +84,8 @@ func Scan(discoverySession session.DiscoverySession) {
|
|||
}
|
||||
|
||||
wg.Wait()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func scanV2(target net.IP, discoverySession session.DiscoverySession, credential *omcc.SNMPCredential) bool {
|
||||
|
|
|
@ -36,17 +36,21 @@ func TestScan(t *testing.T) {
|
|||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "1",
|
||||
args: args{
|
||||
discoverySession: s,
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
Scan(tt.args.discoverySession)
|
||||
if err := Scan(tt.args.discoverySession); (err != nil) != tt.wantErr {
|
||||
t.Errorf("Scan() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,10 +14,11 @@ const (
|
|||
TargetSSDPAll = "ssdp:all"
|
||||
)
|
||||
|
||||
func Scan(discoverySession session.DiscoverySession) {
|
||||
func Scan(discoverySession session.DiscoverySession) error {
|
||||
devs, err := goupnp.DiscoverDevices(TargetRootDevice)
|
||||
if nil != err {
|
||||
fmt.Println("DiscoverDevices: ", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
LOOP:
|
||||
|
@ -46,4 +47,6 @@ LOOP:
|
|||
DiscoveredDate: omu.NowPtr(),
|
||||
})
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -34,17 +34,21 @@ func TestScan(t *testing.T) {
|
|||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "1",
|
||||
args: args{
|
||||
discoverySession: s,
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
Scan(tt.args.discoverySession)
|
||||
if err := Scan(tt.args.discoverySession); (err != nil) != tt.wantErr {
|
||||
t.Errorf("Scan() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user