This commit is contained in:
crusader 2018-09-01 22:09:33 +09:00
parent daadfb864d
commit 266ffdd674
15 changed files with 490 additions and 30 deletions

View File

@ -28,7 +28,10 @@ type Discoverer interface {
func Instance() Discoverer {
_once.Do(func() {
_instance = newDiscoverer()
i := &ofDiscoverer{}
i.start()
_instance = i
})
return _instance
}
@ -36,13 +39,6 @@ func Instance() Discoverer {
var _instance Discoverer
var _once sync.Once
func newDiscoverer() Discoverer {
i := &ofDiscoverer{}
i.start()
return i
}
type ofDiscoverer struct {
stopChan chan struct{}
stopWg sync.WaitGroup

View File

@ -0,0 +1,449 @@
package discovery
import (
"log"
"reflect"
"sync"
"testing"
omd "git.loafle.net/overflow/model/discovery"
"git.loafle.net/overflow_scanner/probe/__test"
"git.loafle.net/overflow_scanner/probe/discovery/session"
"git.loafle.net/overflow_scanner/probe/discovery/types"
)
func TestInstance(t *testing.T) {
i := Instance()
tests := []struct {
name string
want Discoverer
}{
{
name: "first",
want: i,
},
{
name: "second",
want: i,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Instance(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Instance() = %v, want %v", got, tt.want)
}
})
}
}
func Test_ofDiscoverer_DiscoverHost(t *testing.T) {
i := Instance()
defer i.Shutdown()
stopChan := make(chan struct{})
go func() {
for {
select {
case msg, ok := <-i.Message():
if !ok {
close(stopChan)
}
switch msg.Type() {
case types.DiscoveryMessageTypeHost:
log.Print("Discovered Host: ", msg.Data())
case types.DiscoveryMessageTypePort:
log.Print("Discovered Port: ", msg.Data())
case types.DiscoveryMessageTypeService:
log.Print("Discovered Service: ", msg.Data())
case types.DiscoveryMessageTypeStart:
log.Print("Discovery start: ", msg.Data())
case types.DiscoveryMessageTypeStop:
log.Print("Discovery stop: ", msg.Data())
close(stopChan)
default:
// log.Print(msg.Data())
}
}
}
}()
i.DiscoverHost(
"testRequesterID",
__test.Zone(),
__test.DiscoverHost(
__test.DiscoveryConfig(),
1,
254,
__test.DiscoverPort(
nil,
1,
65535,
true,
false,
__test.DiscoverService(
nil,
),
),
),
)
select {
case <-stopChan:
}
// type fields struct {
// stopChan chan struct{}
// stopWg sync.WaitGroup
// requestQueue chan types.DiscoveryRequest
// messageChan chan types.DiscoveryMessage
// }
// type args struct {
// requesterID string
// zone *omd.Zone
// dh *omd.DiscoverHost
// }
// tests := []struct {
// name string
// fields fields
// args args
// }{
// // TODO: Add test cases.
// }
// for _, tt := range tests {
// t.Run(tt.name, func(t *testing.T) {
// d := &ofDiscoverer{
// stopChan: tt.fields.stopChan,
// stopWg: tt.fields.stopWg,
// requestQueue: tt.fields.requestQueue,
// messageChan: tt.fields.messageChan,
// }
// d.DiscoverHost(tt.args.requesterID, tt.args.zone, tt.args.dh)
// })
// }
}
func Test_ofDiscoverer_DiscoverPort(t *testing.T) {
type fields struct {
stopChan chan struct{}
stopWg sync.WaitGroup
requestQueue chan types.DiscoveryRequest
messageChan chan types.DiscoveryMessage
}
type args struct {
requesterID string
host *omd.Host
dp *omd.DiscoverPort
}
tests := []struct {
name string
fields fields
args args
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := &ofDiscoverer{
stopChan: tt.fields.stopChan,
stopWg: tt.fields.stopWg,
requestQueue: tt.fields.requestQueue,
messageChan: tt.fields.messageChan,
}
d.DiscoverPort(tt.args.requesterID, tt.args.host, tt.args.dp)
})
}
}
func Test_ofDiscoverer_DiscoverService(t *testing.T) {
type fields struct {
stopChan chan struct{}
stopWg sync.WaitGroup
requestQueue chan types.DiscoveryRequest
messageChan chan types.DiscoveryMessage
}
type args struct {
requesterID string
port *omd.Port
ds *omd.DiscoverService
}
tests := []struct {
name string
fields fields
args args
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := &ofDiscoverer{
stopChan: tt.fields.stopChan,
stopWg: tt.fields.stopWg,
requestQueue: tt.fields.requestQueue,
messageChan: tt.fields.messageChan,
}
d.DiscoverService(tt.args.requesterID, tt.args.port, tt.args.ds)
})
}
}
func Test_ofDiscoverer_Message(t *testing.T) {
type fields struct {
stopChan chan struct{}
stopWg sync.WaitGroup
requestQueue chan types.DiscoveryRequest
messageChan chan types.DiscoveryMessage
}
tests := []struct {
name string
fields fields
want <-chan types.DiscoveryMessage
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := &ofDiscoverer{
stopChan: tt.fields.stopChan,
stopWg: tt.fields.stopWg,
requestQueue: tt.fields.requestQueue,
messageChan: tt.fields.messageChan,
}
if got := d.Message(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("ofDiscoverer.Message() = %v, want %v", got, tt.want)
}
})
}
}
func Test_ofDiscoverer_SendMessage(t *testing.T) {
type fields struct {
stopChan chan struct{}
stopWg sync.WaitGroup
requestQueue chan types.DiscoveryRequest
messageChan chan types.DiscoveryMessage
}
type args struct {
discoveryRequest types.DiscoveryRequest
messageType types.DiscoveryMessageType
data interface{}
err error
}
tests := []struct {
name string
fields fields
args args
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := &ofDiscoverer{
stopChan: tt.fields.stopChan,
stopWg: tt.fields.stopWg,
requestQueue: tt.fields.requestQueue,
messageChan: tt.fields.messageChan,
}
d.SendMessage(tt.args.discoveryRequest, tt.args.messageType, tt.args.data, tt.args.err)
})
}
}
func Test_ofDiscoverer_Shutdown(t *testing.T) {
type fields struct {
stopChan chan struct{}
stopWg sync.WaitGroup
requestQueue chan types.DiscoveryRequest
messageChan chan types.DiscoveryMessage
}
tests := []struct {
name string
fields fields
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := &ofDiscoverer{
stopChan: tt.fields.stopChan,
stopWg: tt.fields.stopWg,
requestQueue: tt.fields.requestQueue,
messageChan: tt.fields.messageChan,
}
d.Shutdown()
})
}
}
func Test_ofDiscoverer_start(t *testing.T) {
type fields struct {
stopChan chan struct{}
stopWg sync.WaitGroup
requestQueue chan types.DiscoveryRequest
messageChan chan types.DiscoveryMessage
}
tests := []struct {
name string
fields fields
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := &ofDiscoverer{
stopChan: tt.fields.stopChan,
stopWg: tt.fields.stopWg,
requestQueue: tt.fields.requestQueue,
messageChan: tt.fields.messageChan,
}
d.start()
})
}
}
func Test_ofDiscoverer_enqueue(t *testing.T) {
type fields struct {
stopChan chan struct{}
stopWg sync.WaitGroup
requestQueue chan types.DiscoveryRequest
messageChan chan types.DiscoveryMessage
}
type args struct {
req *ofDiscoveryRequest
}
tests := []struct {
name string
fields fields
args args
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := &ofDiscoverer{
stopChan: tt.fields.stopChan,
stopWg: tt.fields.stopWg,
requestQueue: tt.fields.requestQueue,
messageChan: tt.fields.messageChan,
}
d.enqueue(tt.args.req)
})
}
}
func Test_ofDiscoverer_handleRequest(t *testing.T) {
type fields struct {
stopChan chan struct{}
stopWg sync.WaitGroup
requestQueue chan types.DiscoveryRequest
messageChan chan types.DiscoveryMessage
}
tests := []struct {
name string
fields fields
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := &ofDiscoverer{
stopChan: tt.fields.stopChan,
stopWg: tt.fields.stopWg,
requestQueue: tt.fields.requestQueue,
messageChan: tt.fields.messageChan,
}
d.handleRequest()
})
}
}
func Test_ofDiscoverer_discover(t *testing.T) {
type fields struct {
stopChan chan struct{}
stopWg sync.WaitGroup
requestQueue chan types.DiscoveryRequest
messageChan chan types.DiscoveryMessage
}
type args struct {
req types.DiscoveryRequest
}
tests := []struct {
name string
fields fields
args args
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := &ofDiscoverer{
stopChan: tt.fields.stopChan,
stopWg: tt.fields.stopWg,
requestQueue: tt.fields.requestQueue,
messageChan: tt.fields.messageChan,
}
d.discover(tt.args.req)
})
}
}
func Test_ofDiscoverer_complexDiscover(t *testing.T) {
type fields struct {
stopChan chan struct{}
stopWg sync.WaitGroup
requestQueue chan types.DiscoveryRequest
messageChan chan types.DiscoveryMessage
}
type args struct {
s session.DiscoverySession
}
tests := []struct {
name string
fields fields
args args
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := &ofDiscoverer{
stopChan: tt.fields.stopChan,
stopWg: tt.fields.stopWg,
requestQueue: tt.fields.requestQueue,
messageChan: tt.fields.messageChan,
}
d.complexDiscover(tt.args.s)
})
}
}
func Test_ofDiscoverer_hierarchyDiscover(t *testing.T) {
type fields struct {
stopChan chan struct{}
stopWg sync.WaitGroup
requestQueue chan types.DiscoveryRequest
messageChan chan types.DiscoveryMessage
}
type args struct {
s session.DiscoverySession
}
tests := []struct {
name string
fields fields
args args
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := &ofDiscoverer{
stopChan: tt.fields.stopChan,
stopWg: tt.fields.stopWg,
requestQueue: tt.fields.requestQueue,
messageChan: tt.fields.messageChan,
}
d.hierarchyDiscover(tt.args.s)
})
}
}

View File

@ -21,7 +21,7 @@ func (dm *ofDiscoveryMessage) Type() types.DiscoveryMessageType {
return dm.messageType
}
func (dm *ofDiscoveryMessage) Result() interface{} {
func (dm *ofDiscoveryMessage) Data() interface{} {
return dm.data
}

View File

@ -59,7 +59,7 @@ func Scan(discoverySession session.DiscoverySession) error {
delay.Store(true)
if h := handlePacketARP(zone, targetHosts, hosts, packet); nil != h {
if h != nil {
log.Println(h)
discoverySession.AddHost(h)
}
// resultChan <- h

View File

@ -59,7 +59,7 @@ func scanV4(discoverySession session.DiscoverySession) error {
delay.Store(true)
if h := handlePacketICMP4(zone, targetHosts, hosts, packet); nil != h {
if h != nil {
log.Println(h)
discoverySession.AddHost(h)
}
// resultChan <- h

View File

@ -59,7 +59,7 @@ func scanV6(discoverySession session.DiscoverySession) error {
delay.Store(true)
if h := handlePacketICMP6(zone, targetHosts, hosts, packet); nil != h {
if h != nil {
log.Println(h)
discoverySession.AddHost(h)
}
// resultChan <- h

View File

@ -39,7 +39,7 @@ SERVICE_LOOP:
ENTRY_LOOP:
for _, entry := range entries {
log.Print("serviceEntry ", entry)
// log.Print("serviceEntry ", entry)
name := entry.Instance // HP\ LaserJet\ P1505n
service := entry.Service // _pdl-datastream._tcp

View File

@ -3,7 +3,6 @@ package snmp
import (
"encoding/json"
"fmt"
"log"
"net"
"strconv"
"sync"
@ -149,14 +148,14 @@ func scanV2(target net.IP, discoverySession session.DiscoverySession, credential
Host: h,
})
s := discoverySession.AddService(&omd.Service{
discoverySession.AddService(&omd.Service{
MetaCryptoType: omm.ToMetaCryptoType(omm.MetaCryptoTypeEnumNONE),
Key: "SNMP",
Name: "SNMP V2c",
Port: p,
})
log.Printf("Host: %v, Port: %v, Service: %v", h, p, s)
// log.Printf("Host: %v, Port: %v, Service: %v", h, p, s)
return true
}

View File

@ -3,7 +3,6 @@ package connection
import (
"context"
"encoding/json"
"log"
"net"
"os/exec"
"strconv"
@ -96,7 +95,7 @@ func tryConnect(discoverySession session.DiscoverySession, ports map[int]*omd.Po
ports[port] = p
log.Printf("port: %v", p)
discoverySession.AddPort(p)
}
func Ulimit() int64 {

View File

@ -48,8 +48,7 @@ func scanV4(discoverySession session.DiscoverySession, targetHost *omd.Host) err
}
delay.Store(true)
if p := handlePacketTCP4(discoverySession, targetHost, ports, packet); nil != p {
// resultChan <- p
log.Println(p)
discoverySession.AddPort(p)
}
case <-ticker.C:
if false == delay.Load().(bool) {

View File

@ -2,7 +2,6 @@ package upnp
import (
"fmt"
"log"
omd "git.loafle.net/overflow/model/discovery"
omu "git.loafle.net/overflow/model/util"
@ -29,7 +28,7 @@ LOOP:
continue LOOP
}
log.Print(rd)
// log.Print(rd)
discoverySession.AddHost(&omd.Host{
MetaIPType: discoverySession.Zone().MetaIPType,

View File

@ -11,10 +11,15 @@ import (
type connector interface {
dial(targetPort *omd.Port) (net.Conn, error)
metaCryptoType() *omm.MetaCryptoType
}
type noneConnector struct {
metaCryptoType *omm.MetaCryptoType
_metaCryptoType *omm.MetaCryptoType
}
func (c *noneConnector) metaCryptoType() *omm.MetaCryptoType {
return c._metaCryptoType
}
func (c *noneConnector) dial(targetPort *omd.Port) (net.Conn, error) {
@ -29,7 +34,11 @@ func (c *noneConnector) dial(targetPort *omd.Port) (net.Conn, error) {
}
type tlsConnector struct {
metaCryptoType *omm.MetaCryptoType
_metaCryptoType *omm.MetaCryptoType
}
func (c *tlsConnector) metaCryptoType() *omm.MetaCryptoType {
return c._metaCryptoType
}
func (c *tlsConnector) dial(targetPort *omd.Port) (net.Conn, error) {
@ -57,10 +66,10 @@ func (c *tlsConnector) dial(targetPort *omd.Port) (net.Conn, error) {
func newConnectors() []connector {
return []connector{
&noneConnector{
metaCryptoType: omm.ToMetaCryptoType(omm.MetaCryptoTypeEnumNONE),
_metaCryptoType: omm.ToMetaCryptoType(omm.MetaCryptoTypeEnumNONE),
},
&tlsConnector{
metaCryptoType: omm.ToMetaCryptoType(omm.MetaCryptoTypeEnumTLS),
_metaCryptoType: omm.ToMetaCryptoType(omm.MetaCryptoTypeEnumTLS),
},
}
}

View File

@ -2,11 +2,11 @@ package service
import (
"fmt"
"log"
"net"
"time"
omd "git.loafle.net/overflow/model/discovery"
omu "git.loafle.net/overflow/model/util"
osm "git.loafle.net/overflow/service_matcher-go"
ouej "git.loafle.net/overflow/util-go/encoding/json"
"git.loafle.net/overflow_scanner/probe/discovery/session"
@ -25,6 +25,7 @@ func scanTCP(discoverySession session.DiscoverySession, targetPort *omd.Port) er
buf := make([]byte, 1024)
var discoveredMatcher osm.Matcher
var discoveredConnector connector
LOOP:
for _, _connector := range connectors {
@ -55,13 +56,22 @@ LOOP:
discoveredMatcher = _discoveredMatcher
}
}
discoveredConnector = _connector
break LOOP
}
}
if nil != discoveredMatcher {
log.Printf("discovered matcher: %s(%s) %v", discoveredMatcher.Name(), discoveredMatcher.Key(), discoveredMatcher)
// log.Printf("discovered matcher: %s(%s) %v", discoveredMatcher.Name(), discoveredMatcher.Key(), discoveredMatcher)
discoverySession.AddService(&omd.Service{
MetaCryptoType: discoveredConnector.metaCryptoType(),
Key: discoveredMatcher.Key(),
Name: discoveredMatcher.Name(),
Meta: discoveredMatcher.Meta(),
DiscoveredDate: omu.NowPtr(),
Port: targetPort,
})
}
return nil

View File

@ -24,7 +24,7 @@ func (dm *MockDiscoveryMessage) Type() DiscoveryMessageType {
return dm.messageType
}
func (dm *MockDiscoveryMessage) Result() interface{} {
func (dm *MockDiscoveryMessage) Data() interface{} {
return dm.data
}

View File

@ -18,7 +18,7 @@ const (
type DiscoveryMessage interface {
Request() DiscoveryRequest
Type() DiscoveryMessageType
Result() interface{}
Data() interface{}
Error() error
Release()
}