This commit is contained in:
crusader 2018-08-31 11:32:25 +09:00
parent 558e13da2e
commit be654c1723
8 changed files with 186 additions and 96 deletions

View File

@ -1,19 +0,0 @@
package discovery
import "git.loafle.net/overflow_scanner/probe/discovery/types"
func MockDiscoveryRequest(requesterID string, requestType types.DiscoveryRequestType, params ...interface{}) *mockDiscoveryRequest {
m := &mockDiscoveryRequest{}
m.requesterID = requesterID
m.requestType = requestType
m.params = params
return m
}
type mockDiscoveryRequest struct {
ofDiscoveryRequest
}
func (dr *mockDiscoveryRequest) SendMessage(messageType types.DiscoveryMessageType, data interface{}, err error) {
}

View File

@ -6,19 +6,15 @@ import (
omd "git.loafle.net/overflow/model/discovery" omd "git.loafle.net/overflow/model/discovery"
omm "git.loafle.net/overflow/model/meta" omm "git.loafle.net/overflow/model/meta"
"git.loafle.net/overflow_scanner/probe/discovery"
"git.loafle.net/overflow_scanner/probe/discovery/session" "git.loafle.net/overflow_scanner/probe/discovery/session"
"git.loafle.net/overflow_scanner/probe/discovery/types" "git.loafle.net/overflow_scanner/probe/discovery/types"
"github.com/grandcat/zeroconf" "github.com/grandcat/zeroconf"
) )
func TestScan(t *testing.T) { func TestScan(t *testing.T) {
s := session.MockDiscoverySession() s := session.NewMockDiscoverySession(
s.InitWithRequest(
discovery.MockDiscoveryRequest(
"testRequester", "testRequester",
types.DiscoveryRequestTypeHost, types.DiscoveryRequestTypeHost,
[]interface{}{
&omd.Zone{ &omd.Zone{
Network: "192.168.1.0/24", Network: "192.168.1.0/24",
Iface: "enp3s0", Iface: "enp3s0",
@ -30,9 +26,8 @@ func TestScan(t *testing.T) {
MetaIPType: omm.ToMetaIPType(omm.MetaIPTypeEnumV4), MetaIPType: omm.ToMetaIPType(omm.MetaIPTypeEnumV4),
FirstScanRange: "192.168.1.1", FirstScanRange: "192.168.1.1",
LastScanRange: "192.168.1.254", LastScanRange: "192.168.1.254",
DiscoveryConfig: &omd.DiscoveryConfig{},
}, },
},
),
) )
type args struct { type args struct {

View File

@ -3,8 +3,11 @@ package snmp
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"log"
"net" "net"
"strconv" "strconv"
"sync"
"time"
omcc "git.loafle.net/overflow/model/config/credential" omcc "git.loafle.net/overflow/model/config/credential"
omd "git.loafle.net/overflow/model/discovery" omd "git.loafle.net/overflow/model/discovery"
@ -16,7 +19,7 @@ import (
const ( const (
defaultPort = 161 defaultPort = 161
defaultTimeout = 3 defaultTimeout = 1
defaultCommunity = "public" defaultCommunity = "public"
) )
@ -36,7 +39,8 @@ var (
) )
func Scan(discoverySession session.DiscoverySession) { func Scan(discoverySession session.DiscoverySession) {
if nil == discoverySession.TargetHosts() || 0 == len(discoverySession.TargetHosts()) { targetHosts := discoverySession.TargetHosts()
if nil == targetHosts || 0 == len(targetHosts) {
return return
} }
@ -57,11 +61,15 @@ func Scan(discoverySession session.DiscoverySession) {
credentials[_c.Version] = append(credentials[_c.Version], _c) credentials[_c.Version] = append(credentials[_c.Version], _c)
} }
var wg sync.WaitGroup
_2cCS, ok := credentials["2c"] _2cCS, ok := credentials["2c"]
if ok { if ok {
wg.Add(1)
go func() { go func() {
defer wg.Done()
LOOP: LOOP:
for _, target := range discoverySession.TargetHosts() { for _, target := range targetHosts {
for _, c := range _2cCS { for _, c := range _2cCS {
if scanV2(target, discoverySession, c) { if scanV2(target, discoverySession, c) {
continue LOOP continue LOOP
@ -70,17 +78,22 @@ func Scan(discoverySession session.DiscoverySession) {
} }
}() }()
} }
wg.Wait()
} }
func scanV2(target net.IP, discoverySession session.DiscoverySession, credential *omcc.SNMPCredential) bool { func scanV2(target net.IP, discoverySession session.DiscoverySession, credential *omcc.SNMPCredential) bool {
address := fmt.Sprintf("%s:%d", target.String(), credential.Port) address := fmt.Sprintf("%s:%s", target.String(), credential.Port.String())
timeout, _ := credential.Timeout.Int64()
snmp, err := snmpgo.NewSNMP(snmpgo.SNMPArguments{ snmp, err := snmpgo.NewSNMP(snmpgo.SNMPArguments{
Version: snmpgo.V2c, Version: snmpgo.V2c,
Address: address, Address: address,
Timeout: time.Second * time.Duration(timeout),
Retries: 1, Retries: 1,
Community: credential.Community, Community: credential.Community,
}) })
if err != nil { if err != nil {
// ch <- &SNMPResponse{host, nil, err} // ch <- &SNMPResponse{host, nil, err}
return false return false
@ -114,6 +127,8 @@ func scanV2(target net.IP, discoverySession session.DiscoverySession, credential
meta[val.Oid.String()] = val.Variable.String() meta[val.Oid.String()] = val.Variable.String()
} }
log.Print(meta)
h := discoverySession.AddHost(&omd.Host{ h := discoverySession.AddHost(&omd.Host{
MetaIPType: discoverySession.Zone().MetaIPType, MetaIPType: discoverySession.Zone().MetaIPType,
Name: "", Name: "",

View File

@ -1,20 +1,20 @@
package snmp package snmp
import ( import (
"net"
"testing" "testing"
"git.loafle.net/overflow_scanner/probe/discovery" omcc "git.loafle.net/overflow/model/config/credential"
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/session"
"git.loafle.net/overflow_scanner/probe/discovery/types" "git.loafle.net/overflow_scanner/probe/discovery/types"
) )
func TestSNMPScan(t *testing.T) { func TestScan(t *testing.T) {
s := session.MockDiscoverySession() s := session.NewMockDiscoverySession(
s.InitWithRequest(
discovery.MockDiscoveryRequest(
"testRequester", "testRequester",
types.DiscoveryRequestTypeHost, types.DiscoveryRequestTypeHost,
[]interface{}{
&omd.Zone{ &omd.Zone{
Network: "192.168.1.0/24", Network: "192.168.1.0/24",
Iface: "enp3s0", Iface: "enp3s0",
@ -26,13 +26,49 @@ func TestSNMPScan(t *testing.T) {
MetaIPType: omm.ToMetaIPType(omm.MetaIPTypeEnumV4), MetaIPType: omm.ToMetaIPType(omm.MetaIPTypeEnumV4),
FirstScanRange: "192.168.1.1", FirstScanRange: "192.168.1.1",
LastScanRange: "192.168.1.254", LastScanRange: "192.168.1.254",
DiscoveryConfig: &omd.DiscoveryConfig{},
}, },
},
),
) )
Scan(s) type args struct {
discoverySession session.DiscoverySession
t.Log(msg) }
tests := []struct {
name string
args args
}{
{
name: "1",
args: args{
discoverySession: s,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Scan(tt.args.discoverySession)
})
}
}
func Test_scanV2(t *testing.T) {
type args struct {
target net.IP
discoverySession session.DiscoverySession
credential *omcc.SNMPCredential
}
tests := []struct {
name string
args args
want bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := scanV2(tt.args.target, tt.args.discoverySession, tt.args.credential); got != tt.want {
t.Errorf("scanV2() = %v, want %v", got, tt.want)
}
})
}
} }

View File

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

View File

@ -5,18 +5,14 @@ import (
omd "git.loafle.net/overflow/model/discovery" omd "git.loafle.net/overflow/model/discovery"
omm "git.loafle.net/overflow/model/meta" omm "git.loafle.net/overflow/model/meta"
"git.loafle.net/overflow_scanner/probe/discovery"
"git.loafle.net/overflow_scanner/probe/discovery/session" "git.loafle.net/overflow_scanner/probe/discovery/session"
"git.loafle.net/overflow_scanner/probe/discovery/types" "git.loafle.net/overflow_scanner/probe/discovery/types"
) )
func TestScan(t *testing.T) { func TestScan(t *testing.T) {
s := session.MockDiscoverySession() s := session.NewMockDiscoverySession(
s.InitWithRequest(
discovery.MockDiscoveryRequest(
"testRequester", "testRequester",
types.DiscoveryRequestTypeHost, types.DiscoveryRequestTypeHost,
[]interface{}{
&omd.Zone{ &omd.Zone{
Network: "192.168.1.0/24", Network: "192.168.1.0/24",
Iface: "enp3s0", Iface: "enp3s0",
@ -28,9 +24,8 @@ func TestScan(t *testing.T) {
MetaIPType: omm.ToMetaIPType(omm.MetaIPTypeEnumV4), MetaIPType: omm.ToMetaIPType(omm.MetaIPTypeEnumV4),
FirstScanRange: "192.168.1.1", FirstScanRange: "192.168.1.1",
LastScanRange: "192.168.1.254", LastScanRange: "192.168.1.254",
DiscoveryConfig: &omd.DiscoveryConfig{},
}, },
},
),
) )
type args struct { type args struct {

View File

@ -2,10 +2,19 @@ package session
import ( import (
omd "git.loafle.net/overflow/model/discovery" omd "git.loafle.net/overflow/model/discovery"
"git.loafle.net/overflow_scanner/probe/discovery/types"
) )
func MockDiscoverySession() *mockDiscoverySession { func NewMockDiscoverySession(requesterID string, discoveryRequestType types.DiscoveryRequestType, params ...interface{}) DiscoverySession {
return &mockDiscoverySession{} s := &mockDiscoverySession{}
s.InitWithRequest(
types.NewMockDiscoveryRequest(
requesterID,
discoveryRequestType,
params...,
),
)
return s
} }
type mockDiscoverySession struct { type mockDiscoverySession struct {

View File

@ -0,0 +1,62 @@
package types
func NewMockDiscoveryMessage(request DiscoveryRequest, messageType DiscoveryMessageType, data interface{}, err error) DiscoveryMessage {
return &MockDiscoveryMessage{
request: request,
messageType: messageType,
data: data,
err: err,
}
}
type MockDiscoveryMessage struct {
request DiscoveryRequest
messageType DiscoveryMessageType
data interface{}
err error
}
func (dm *MockDiscoveryMessage) Request() DiscoveryRequest {
return dm.request
}
func (dm *MockDiscoveryMessage) Type() DiscoveryMessageType {
return dm.messageType
}
func (dm *MockDiscoveryMessage) Result() interface{} {
return dm.data
}
func (dm *MockDiscoveryMessage) Error() error {
return dm.err
}
func (dm *MockDiscoveryMessage) Release() {
}
func NewMockDiscoveryRequest(requesterID string, requestType DiscoveryRequestType, params ...interface{}) DiscoveryRequest {
return &MockDiscoveryRequest{
requesterID: requesterID,
requestType: requestType,
params: params,
}
}
type MockDiscoveryRequest struct {
requesterID string
requestType DiscoveryRequestType
params []interface{}
}
func (dr *MockDiscoveryRequest) RequesterID() string {
return dr.requesterID
}
func (dr *MockDiscoveryRequest) RequestType() DiscoveryRequestType {
return dr.requestType
}
func (dr *MockDiscoveryRequest) Params() []interface{} {
return dr.params
}