test modified

This commit is contained in:
crusader 2018-09-01 15:07:15 +09:00
parent 5956cae93f
commit 6f259e2d9d
14 changed files with 226 additions and 193 deletions

74
__test/test.go Normal file
View File

@ -0,0 +1,74 @@
package __test
import (
"encoding/json"
"fmt"
omd "git.loafle.net/overflow/model/discovery"
omm "git.loafle.net/overflow/model/meta"
)
const (
ZONE_NETWORK = "192.168.1"
ZONE_IFACE = "enp3s0"
ZONE_ADDRESS = "101"
ZONE_MAC = "44:8a:5b:f1:f1:f3"
)
func Zone() *omd.Zone {
return &omd.Zone{
Network: fmt.Sprintf("%s.0/24", ZONE_NETWORK),
Iface: ZONE_IFACE,
MetaIPType: omm.ToMetaIPType(omm.MetaIPTypeEnumV4),
Address: fmt.Sprintf("%s.$s", ZONE_ADDRESS),
Mac: ZONE_MAC,
}
}
func Host(hostName string, address string, mac string) *omd.Host {
return &omd.Host{
MetaIPType: omm.ToMetaIPType(omm.MetaIPTypeEnumV4),
Name: hostName,
Address: fmt.Sprintf("%s.$s", address),
Mac: mac,
Zone: Zone(),
}
}
func Port(host *omd.Host, port string) *omd.Port {
return &omd.Port{
MetaPortType: omm.ToMetaPortType(omm.MetaPortTypeEnumTCP),
PortNumber: json.Number(port),
Host: host,
}
}
func DiscoveryConfig() *omd.DiscoveryConfig {
return &omd.DiscoveryConfig{}
}
func DiscoverHost(discoveryConfig *omd.DiscoveryConfig, firstScanRange int, lastScanRange int, discoverPort *omd.DiscoverPort) *omd.DiscoverHost {
return &omd.DiscoverHost{
MetaIPType: omm.ToMetaIPType(omm.MetaIPTypeEnumV4),
FirstScanRange: fmt.Sprintf("%s.%d", ZONE_NETWORK, firstScanRange),
LastScanRange: fmt.Sprintf("%s.%d", ZONE_NETWORK, lastScanRange),
DiscoveryConfig: discoveryConfig,
DiscoverPort: discoverPort,
}
}
func DiscoverPort(discoveryConfig *omd.DiscoveryConfig, firstScanRange int, lastScanRange int, includeTCP bool, includeUDP bool, discoverService *omd.DiscoverService) *omd.DiscoverPort {
return &omd.DiscoverPort{
FirstScanRange: firstScanRange,
LastScanRange: lastScanRange,
IncludeTCP: includeTCP,
IncludeUDP: includeUDP,
DiscoverService: discoverService,
}
}
func DiscoverService(discoveryConfig *omd.DiscoveryConfig) *omd.DiscoverService {
return &omd.DiscoverService{
DiscoveryConfig: discoveryConfig,
}
}

View File

@ -263,6 +263,36 @@ func (d *ofDiscoverer) hierarchyDiscover(s session.DiscoverySession) {
}
}()
// post complexDiscover
if nil != s.DiscoverPort() {
discoveredHosts := s.DiscoveredAllHosts()
for _, h := range discoveredHosts {
wg.Add(1)
go func(h *omd.Host) {
defer wg.Done()
port.Scan(s, h)
}(h)
}
}
wg.Wait()
if nil != s.DiscoverService() {
discoveredPorts := s.DiscoveredAllPorts()
for _, hostPorts := range discoveredPorts {
for _, ports := range hostPorts {
for _, p := range ports {
wg.Add(1)
go func(p *omd.Port) {
defer wg.Done()
service.Scan(s, p)
}(p)
}
}
}
}
wg.Wait()
// exec hierarchy Discovery
if nil != s.DiscoverHost() {
wg.Add(1)
go func() {

View File

@ -10,6 +10,7 @@ import (
omd "git.loafle.net/overflow/model/discovery"
omm "git.loafle.net/overflow/model/meta"
omu "git.loafle.net/overflow/model/util"
oub "git.loafle.net/overflow/util-go/benchmark"
"git.loafle.net/overflow_scanner/probe/discovery/session"
"git.loafle.net/overflow_scanner/probe/internal/pcap"
"github.com/google/gopacket"
@ -17,6 +18,8 @@ import (
)
func Scan(discoverySession session.DiscoverySession) error {
oub.Elapsed("mDNS")
targetHosts := discoverySession.TargetHosts()
if nil == targetHosts || 0 == len(targetHosts) {
return nil

View File

@ -6,7 +6,7 @@ import (
"testing"
omd "git.loafle.net/overflow/model/discovery"
omm "git.loafle.net/overflow/model/meta"
"git.loafle.net/overflow_scanner/probe/__test"
"git.loafle.net/overflow_scanner/probe/discovery/session"
"git.loafle.net/overflow_scanner/probe/discovery/types"
"git.loafle.net/overflow_scanner/probe/internal/pcap"
@ -17,19 +17,8 @@ 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{},
},
__test.Zone(),
__test.DiscoverHost(__test.DiscoveryConfig(), 1, 254, nil),
)
type args struct {

View File

@ -6,7 +6,7 @@ import (
"testing"
omd "git.loafle.net/overflow/model/discovery"
omm "git.loafle.net/overflow/model/meta"
"git.loafle.net/overflow_scanner/probe/__test"
"git.loafle.net/overflow_scanner/probe/discovery/session"
"git.loafle.net/overflow_scanner/probe/discovery/types"
"git.loafle.net/overflow_scanner/probe/internal/pcap"
@ -17,19 +17,8 @@ func Test_scanV4(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{},
},
__test.Zone(),
__test.DiscoverHost(__test.DiscoveryConfig(), 1, 254, nil),
)
type args struct {

View File

@ -13,11 +13,13 @@ import (
omd "git.loafle.net/overflow/model/discovery"
omm "git.loafle.net/overflow/model/meta"
omu "git.loafle.net/overflow/model/util"
oub "git.loafle.net/overflow/util-go/benchmark"
"git.loafle.net/overflow_scanner/probe/discovery/session"
"github.com/grandcat/zeroconf"
)
func Scan(discoverySession session.DiscoverySession) error {
oub.Elapsed("mDNS")
serviceEntries, err := browse("_services._dns-sd._udp", "local")
if nil != err {
log.Print("Cannot find service ", err)
@ -179,14 +181,14 @@ func browse(service string, domain string) ([]*zeroconf.ServiceEntry, error) {
serviceEntries := make([]*zeroconf.ServiceEntry, 0)
go func(_entryChan <-chan *zeroconf.ServiceEntry) {
go func(entryChan <-chan *zeroconf.ServiceEntry) {
var delay atomic.Value
delay.Store(false)
ticker := time.NewTicker(time.Second * 1)
for {
select {
case entry, ok := <-_entryChan:
case entry, ok := <-entryChan:
if !ok {
return
}
@ -203,7 +205,7 @@ func browse(service string, domain string) ([]*zeroconf.ServiceEntry, error) {
}
}(entryChan)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(30))
ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(3))
defer cancel()
err = resolver.Browse(ctx, service, domain, entryChan)
if err != nil {

View File

@ -4,8 +4,7 @@ import (
"reflect"
"testing"
omd "git.loafle.net/overflow/model/discovery"
omm "git.loafle.net/overflow/model/meta"
"git.loafle.net/overflow_scanner/probe/__test"
"git.loafle.net/overflow_scanner/probe/discovery/session"
"git.loafle.net/overflow_scanner/probe/discovery/types"
"github.com/grandcat/zeroconf"
@ -15,19 +14,8 @@ 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{},
},
__test.Zone(),
__test.DiscoverHost(__test.DiscoveryConfig(), 1, 254, nil),
)
type args struct {

View File

@ -5,8 +5,7 @@ import (
"testing"
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/__test"
"git.loafle.net/overflow_scanner/probe/discovery/session"
"git.loafle.net/overflow_scanner/probe/discovery/types"
)
@ -15,19 +14,8 @@ 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{},
},
__test.Zone(),
__test.DiscoverHost(__test.DiscoveryConfig(), 1, 254, nil),
)
type args struct {

View File

@ -5,7 +5,7 @@ import (
"time"
omd "git.loafle.net/overflow/model/discovery"
omm "git.loafle.net/overflow/model/meta"
"git.loafle.net/overflow_scanner/probe/__test"
"git.loafle.net/overflow_scanner/probe/discovery/session"
"git.loafle.net/overflow_scanner/probe/discovery/types"
)
@ -14,33 +14,27 @@ 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{},
DiscoverPort: &omd.DiscoverPort{
FirstScanRange: 1,
LastScanRange: 65535,
IncludeTCP: true,
},
},
__test.Zone(),
__test.DiscoverHost(
__test.DiscoveryConfig(),
1,
254,
__test.DiscoverPort(
nil,
1,
65535,
true,
false,
nil,
),
),
)
targetHost := &omd.Host{
MetaIPType: omm.ToMetaIPType(omm.MetaIPTypeEnumV4),
Name: "atGame",
Address: "192.168.1.1",
Mac: "00:11:32:7f:20:61",
Zone: s.Zone(),
}
targetHost := __test.Host(
"atGame",
"1",
"00:11:32:7f:20:61",
)
type args struct {
discoverySession session.DiscoverySession
@ -85,7 +79,7 @@ func Test_scanPort(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
scanPort(tt.args.discoverySession, tt.args.ports, tt.args.targetHost, tt.args.port, tt.args.timeout)
tryConnect(tt.args.discoverySession, tt.args.ports, tt.args.targetHost, tt.args.port, tt.args.timeout)
})
}
}

View File

@ -5,7 +5,7 @@ import (
"testing"
omd "git.loafle.net/overflow/model/discovery"
omm "git.loafle.net/overflow/model/meta"
"git.loafle.net/overflow_scanner/probe/__test"
"git.loafle.net/overflow_scanner/probe/discovery/session"
"git.loafle.net/overflow_scanner/probe/discovery/types"
"git.loafle.net/overflow_scanner/probe/internal/pcap"
@ -16,33 +16,27 @@ func Test_scanV4(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{},
DiscoverPort: &omd.DiscoverPort{
FirstScanRange: 1,
LastScanRange: 1024,
IncludeTCP: true,
},
},
__test.Zone(),
__test.DiscoverHost(
__test.DiscoveryConfig(),
1,
254,
__test.DiscoverPort(
nil,
1,
65535,
true,
false,
nil,
),
),
)
targetHost := &omd.Host{
MetaIPType: omm.ToMetaIPType(omm.MetaIPTypeEnumV4),
Name: "atGame",
Address: "192.168.1.1",
Mac: "00:11:32:7f:20:61",
Zone: s.Zone(),
}
targetHost := __test.Host(
"atGame",
"1",
"00:11:32:7f:20:61",
)
type args struct {
discoverySession session.DiscoverySession

View File

@ -3,8 +3,7 @@ package upnp
import (
"testing"
omd "git.loafle.net/overflow/model/discovery"
omm "git.loafle.net/overflow/model/meta"
"git.loafle.net/overflow_scanner/probe/__test"
"git.loafle.net/overflow_scanner/probe/discovery/session"
"git.loafle.net/overflow_scanner/probe/discovery/types"
)
@ -13,19 +12,8 @@ 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{},
},
__test.Zone(),
__test.DiscoverHost(__test.DiscoveryConfig(), 1, 254, nil),
)
type args struct {

View File

@ -36,8 +36,11 @@ type DiscoverySession interface {
AddService(service *omd.Service) *omd.Service
DiscoveredHost(address string) *omd.Host
DiscoveredAllHosts() map[string]*omd.Host
DiscoveredPort(host *omd.Host, portNumber int) map[string]*omd.Port
DiscoveredAllPorts() map[*omd.Host]map[json.Number]map[string]*omd.Port
DiscoveredService(port *omd.Port, name string) map[string]*omd.Service
DiscoveredAllServices() map[*omd.Port]map[string]map[string]*omd.Service
}
type ofDiscoverySession struct {
@ -257,6 +260,10 @@ func (ds *ofDiscoverySession) DiscoveredHost(address string) *omd.Host {
return h
}
func (ds *ofDiscoverySession) DiscoveredAllHosts() map[string]*omd.Host {
return ds.hosts
}
func (ds *ofDiscoverySession) DiscoveredPort(host *omd.Host, portNumber int) map[string]*omd.Port {
h, _ := ds.findHost(host, false)
if nil == h {
@ -276,6 +283,10 @@ func (ds *ofDiscoverySession) DiscoveredPort(host *omd.Host, portNumber int) map
return ports
}
func (ds *ofDiscoverySession) DiscoveredAllPorts() map[*omd.Host]map[json.Number]map[string]*omd.Port {
return ds.ports
}
func (ds *ofDiscoverySession) DiscoveredService(port *omd.Port, name string) map[string]*omd.Service {
p, _ := ds.findPort(port, false)
if nil == p {
@ -295,6 +306,10 @@ func (ds *ofDiscoverySession) DiscoveredService(port *omd.Port, name string) map
return services
}
func (ds *ofDiscoverySession) DiscoveredAllServices() map[*omd.Port]map[string]map[string]*omd.Service {
return ds.services
}
func (ds *ofDiscoverySession) findHost(host *omd.Host, add bool) (h *omd.Host, modified bool) {
modified = false
var ok bool

View File

@ -1,14 +1,13 @@
package service
import (
"encoding/json"
"net"
"reflect"
"testing"
omd "git.loafle.net/overflow/model/discovery"
omm "git.loafle.net/overflow/model/meta"
osm "git.loafle.net/overflow/service_matcher-go"
"git.loafle.net/overflow_scanner/probe/__test"
"git.loafle.net/overflow_scanner/probe/discovery/session"
"git.loafle.net/overflow_scanner/probe/discovery/types"
)
@ -17,64 +16,29 @@ func Test_scanTCP(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{},
DiscoverPort: &omd.DiscoverPort{
FirstScanRange: 1,
LastScanRange: 1024,
IncludeTCP: true,
DiscoverService: &omd.DiscoverService{},
},
},
__test.Zone(),
__test.DiscoverHost(
__test.DiscoveryConfig(),
1,
254,
__test.DiscoverPort(
nil,
1,
65535,
true,
false,
__test.DiscoverService(
nil,
),
),
),
)
targetHost := &omd.Host{
MetaIPType: omm.ToMetaIPType(omm.MetaIPTypeEnumV4),
Name: "atGame",
Address: "192.168.1.1",
Mac: "00:11:32:7f:20:61",
Zone: s.Zone(),
}
targetPort80 := &omd.Port{
MetaPortType: omm.ToMetaPortType(omm.MetaPortTypeEnumTCP),
PortNumber: json.Number("80"),
Host: targetHost,
}
targetPort139 := &omd.Port{
MetaPortType: omm.ToMetaPortType(omm.MetaPortTypeEnumTCP),
PortNumber: json.Number("139"),
Host: targetHost,
}
targetPort443 := &omd.Port{
MetaPortType: omm.ToMetaPortType(omm.MetaPortTypeEnumTCP),
PortNumber: json.Number("443"),
Host: targetHost,
}
targetPort445 := &omd.Port{
MetaPortType: omm.ToMetaPortType(omm.MetaPortTypeEnumTCP),
PortNumber: json.Number("445"),
Host: targetHost,
}
targetPort548 := &omd.Port{
MetaPortType: omm.ToMetaPortType(omm.MetaPortTypeEnumTCP),
PortNumber: json.Number("548"),
Host: targetHost,
}
targetHost := __test.Host(
"atGame",
"1",
"00:11:32:7f:20:61",
)
type args struct {
discoverySession session.DiscoverySession
@ -89,7 +53,10 @@ func Test_scanTCP(t *testing.T) {
name: "80",
args: args{
discoverySession: s,
targetPort: targetPort80,
targetPort: __test.Port(
targetHost,
"80",
),
},
wantErr: false,
},
@ -97,7 +64,10 @@ func Test_scanTCP(t *testing.T) {
name: "139",
args: args{
discoverySession: s,
targetPort: targetPort139,
targetPort: __test.Port(
targetHost,
"139",
),
},
wantErr: false,
},
@ -105,7 +75,10 @@ func Test_scanTCP(t *testing.T) {
name: "443",
args: args{
discoverySession: s,
targetPort: targetPort443,
targetPort: __test.Port(
targetHost,
"443",
),
},
wantErr: false,
},
@ -113,7 +86,10 @@ func Test_scanTCP(t *testing.T) {
name: "445",
args: args{
discoverySession: s,
targetPort: targetPort445,
targetPort: __test.Port(
targetHost,
"445",
),
},
wantErr: false,
},
@ -121,7 +97,10 @@ func Test_scanTCP(t *testing.T) {
name: "548",
args: args{
discoverySession: s,
targetPort: targetPort548,
targetPort: __test.Port(
targetHost,
"548",
),
},
wantErr: false,
},

View File

@ -35,7 +35,7 @@ func New() *ossw.Server {
sh.Name = "Probe"
sh.Services = services
sh.RegisterServlet("/", ss)
sh.RegisterServlet("/scanner", ss)
s := &ossw.Server{
ServerHandler: sh,