ing
This commit is contained in:
parent
558e13da2e
commit
be654c1723
|
@ -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) {
|
||||
}
|
|
@ -6,19 +6,15 @@ 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(
|
||||
s := session.NewMockDiscoverySession(
|
||||
"testRequester",
|
||||
types.DiscoveryRequestTypeHost,
|
||||
[]interface{}{
|
||||
&omd.Zone{
|
||||
Network: "192.168.1.0/24",
|
||||
Iface: "enp3s0",
|
||||
|
@ -30,9 +26,8 @@ func TestScan(t *testing.T) {
|
|||
MetaIPType: omm.ToMetaIPType(omm.MetaIPTypeEnumV4),
|
||||
FirstScanRange: "192.168.1.1",
|
||||
LastScanRange: "192.168.1.254",
|
||||
DiscoveryConfig: &omd.DiscoveryConfig{},
|
||||
},
|
||||
},
|
||||
),
|
||||
)
|
||||
|
||||
type args struct {
|
||||
|
|
|
@ -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: "",
|
||||
|
|
|
@ -1,20 +1,20 @@
|
|||
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(
|
||||
func TestScan(t *testing.T) {
|
||||
s := session.NewMockDiscoverySession(
|
||||
"testRequester",
|
||||
types.DiscoveryRequestTypeHost,
|
||||
[]interface{}{
|
||||
&omd.Zone{
|
||||
Network: "192.168.1.0/24",
|
||||
Iface: "enp3s0",
|
||||
|
@ -26,13 +26,49 @@ func TestSNMPScan(t *testing.T) {
|
|||
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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -5,18 +5,14 @@ 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(
|
||||
s := session.NewMockDiscoverySession(
|
||||
"testRequester",
|
||||
types.DiscoveryRequestTypeHost,
|
||||
[]interface{}{
|
||||
&omd.Zone{
|
||||
Network: "192.168.1.0/24",
|
||||
Iface: "enp3s0",
|
||||
|
@ -28,9 +24,8 @@ func TestScan(t *testing.T) {
|
|||
MetaIPType: omm.ToMetaIPType(omm.MetaIPTypeEnumV4),
|
||||
FirstScanRange: "192.168.1.1",
|
||||
LastScanRange: "192.168.1.254",
|
||||
DiscoveryConfig: &omd.DiscoveryConfig{},
|
||||
},
|
||||
},
|
||||
),
|
||||
)
|
||||
|
||||
type args struct {
|
||||
|
|
|
@ -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 {
|
||||
|
|
62
discovery/types/mock-types.go
Normal file
62
discovery/types/mock-types.go
Normal 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user