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,33 +6,28 @@ import (
omd "git.loafle.net/overflow/model/discovery"
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/types"
"github.com/grandcat/zeroconf"
)
func TestScan(t *testing.T) {
s := session.MockDiscoverySession()
s.InitWithRequest(
discovery.MockDiscoveryRequest(
"testRequester",
types.DiscoveryRequestTypeHost,
[]interface{}{
&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",
},
},
),
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 {

View File

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

View File

@ -1,38 +1,74 @@
package snmp
import (
"net"
"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/types"
)
func TestSNMPScan(t *testing.T) {
s := session.MockDiscoverySession()
s.InitWithRequest(
discovery.MockDiscoveryRequest(
"testRequester",
types.DiscoveryRequestTypeHost,
[]interface{}{
&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",
},
},
),
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{},
},
)
Scan(s)
t.Log(msg)
type args struct {
discoverySession session.DiscoverySession
}
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 (
"fmt"
"log"
omd "git.loafle.net/overflow/model/discovery"
omu "git.loafle.net/overflow/model/util"
@ -28,8 +27,6 @@ LOOP:
continue LOOP
}
log.Print(rd)
discoverySession.AddHost(&omd.Host{
MetaIPType: discoverySession.Zone().MetaIPType,
Name: rd.FriendlyName,

View File

@ -5,32 +5,27 @@ import (
omd "git.loafle.net/overflow/model/discovery"
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/types"
)
func TestScan(t *testing.T) {
s := session.MockDiscoverySession()
s.InitWithRequest(
discovery.MockDiscoveryRequest(
"testRequester",
types.DiscoveryRequestTypeHost,
[]interface{}{
&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",
},
},
),
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 {

View File

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