This commit is contained in:
crusader 2018-09-03 23:01:54 +09:00
parent dd8ccb2069
commit f5af8e1473
4 changed files with 23 additions and 24 deletions

View File

@ -54,15 +54,15 @@ func Test_ofDiscoverer_DiscoverHost(t *testing.T) {
select { select {
case msg, ok := <-i.Message(): case msg, ok := <-i.Message():
if !ok { if !ok {
close(stopChan) return
} }
switch msg.Type() { switch msg.Type() {
case types.DiscoveryMessageTypeHost: case types.DiscoveryMessageTypeHost:
log.Print("Discovered Host: ", msg.Data()) log.Print("Discovered Host: ", msg.Data())
case types.DiscoveryMessageTypePort: case types.DiscoveryMessageTypePort:
log.Print("Discovered Port: ", msg.Data(), " \n Host: ", msg.Data().(*omd.Port).Host) log.Print("Discovered Port: ", msg.Data(), " Host: ", msg.Data().(*omd.Port).Host)
case types.DiscoveryMessageTypeService: case types.DiscoveryMessageTypeService:
log.Print("Discovered Service: ", msg.Data(), " \n Port: ", msg.Data().(*omd.Service).Port, " \n Host: ", msg.Data().(*omd.Service).Port.Host) log.Print("Discovered Service: ", msg.Data(), " Port: ", msg.Data().(*omd.Service).Port, " Host: ", msg.Data().(*omd.Service).Port.Host)
case types.DiscoveryMessageTypeStart: case types.DiscoveryMessageTypeStart:
log.Print("Discovery start: ", msg.Data()) log.Print("Discovery start: ", msg.Data())
case types.DiscoveryMessageTypeStop: case types.DiscoveryMessageTypeStop:

View File

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

View File

@ -23,7 +23,7 @@ func scanTCP(discoverySession session.DiscoverySession, targetPort *omd.Port) er
return nil return nil
} }
info := osm.NewMatchInfo(hostAddress, portNumber) matchCtx := osm.NewMatchCtx(hostAddress, portNumber)
connectors := newConnectors() connectors := newConnectors()
buf := make([]byte, 1024) buf := make([]byte, 1024)
@ -46,16 +46,16 @@ LOOP:
} }
if 0 < n { if 0 < n {
discoveredMatcher = hadlePrePacket(info, _connector, conn, osm.NewPacket(buf, n)) discoveredMatcher = hadlePrePacket(matchCtx, _connector, conn, osm.NewPacket(buf, n))
} else { } else {
conn.Close() conn.Close()
discoveredMatcher = hadlePostPacket(info, _connector, targetPort, nil) discoveredMatcher = hadlePostPacket(matchCtx, _connector, targetPort, nil)
} }
if nil != discoveredMatcher { if nil != discoveredMatcher {
if "HTTP" == discoveredMatcher.Key() { if "HTTP" == discoveredMatcher.Key() {
hsm := matcher.GetHTTPSubMatchers() hsm := matcher.GetHTTPSubMatchers()
if _discoveredMatcher := hadlePostPacket(info, _connector, targetPort, hsm); _discoveredMatcher != nil { if _discoveredMatcher := hadlePostPacket(matchCtx, _connector, targetPort, hsm); _discoveredMatcher != nil {
discoveredMatcher = _discoveredMatcher discoveredMatcher = _discoveredMatcher
} }
} }
@ -76,8 +76,8 @@ LOOP:
discoverySession.AddService(&omd.Service{ discoverySession.AddService(&omd.Service{
MetaCryptoType: discoveredConnector.metaCryptoType(), MetaCryptoType: discoveredConnector.metaCryptoType(),
Key: discoveredMatcher.Key(), Key: discoveredMatcher.Key(),
Name: discoveredMatcher.Name(), Name: discoveredMatcher.Name(matchCtx),
Meta: discoveredMatcher.Meta(), Meta: matchCtx.GetAttributes(),
DiscoveredDate: omu.NowPtr(), DiscoveredDate: omu.NowPtr(),
Port: targetPort, Port: targetPort,
}) })
@ -86,7 +86,7 @@ LOOP:
return nil return nil
} }
func hadlePrePacket(info osm.MatchInfo, _connector connector, conn net.Conn, packet *osm.Packet) osm.Matcher { func hadlePrePacket(matchCtx *osm.MatchCtx, _connector connector, conn net.Conn, packet *osm.Packet) osm.Matcher {
defer func() { defer func() {
conn.Close() conn.Close()
}() }()
@ -97,11 +97,11 @@ func hadlePrePacket(info osm.MatchInfo, _connector connector, conn net.Conn, pac
LOOP: LOOP:
for _, matcher := range matchers { for _, matcher := range matchers {
if err := matcher.Match(info, 0, packet); err != nil { if err := matcher.Match(matchCtx, 0, packet); err != nil {
continue LOOP continue LOOP
} }
packetCount := matcher.PacketCount() packetCount := matcher.PacketCount(matchCtx)
if 0 == packetCount { if 0 == packetCount {
return matcher return matcher
@ -109,7 +109,7 @@ LOOP:
INNER_LOOP: INNER_LOOP:
for j := 0; j < packetCount; j++ { for j := 0; j < packetCount; j++ {
_packet := matcher.Packet(j) _packet := matcher.Packet(matchCtx, j)
if err := conn.SetWriteDeadline(time.Now().Add(1 * time.Second)); nil != err { if err := conn.SetWriteDeadline(time.Now().Add(1 * time.Second)); nil != err {
return nil return nil
@ -127,7 +127,7 @@ LOOP:
return nil return nil
} }
if err := matcher.Match(info, j+1, osm.NewPacket(buf, n)); nil == err { if err := matcher.Match(matchCtx, j+1, osm.NewPacket(buf, n)); nil == err {
discoveredMatcher = matcher discoveredMatcher = matcher
} else { } else {
discoveredMatcher = nil discoveredMatcher = nil
@ -143,7 +143,7 @@ LOOP:
return nil return nil
} }
func hadlePostPacket(info osm.MatchInfo, _connector connector, targetPort *omd.Port, limitedMatchers []osm.Matcher) osm.Matcher { func hadlePostPacket(matchCtx *osm.MatchCtx, _connector connector, targetPort *omd.Port, limitedMatchers []osm.Matcher) osm.Matcher {
matchers := matcher.GetTCPMatchers(false) matchers := matcher.GetTCPMatchers(false)
if nil != limitedMatchers { if nil != limitedMatchers {
matchers = limitedMatchers matchers = limitedMatchers
@ -154,7 +154,7 @@ func hadlePostPacket(info osm.MatchInfo, _connector connector, targetPort *omd.P
LOOP: LOOP:
for _, matcher := range matchers { for _, matcher := range matchers {
packetCount := matcher.PacketCount() packetCount := matcher.PacketCount(matchCtx)
if 0 == packetCount { if 0 == packetCount {
continue LOOP continue LOOP
} }
@ -166,7 +166,7 @@ LOOP:
INNER_LOOP: INNER_LOOP:
for j := 0; j < packetCount; j++ { for j := 0; j < packetCount; j++ {
_packet := matcher.Packet(j) _packet := matcher.Packet(matchCtx, j)
if err := conn.SetWriteDeadline(time.Now().Add(1 * time.Second)); nil != err { if err := conn.SetWriteDeadline(time.Now().Add(1 * time.Second)); nil != err {
break INNER_LOOP break INNER_LOOP
@ -181,7 +181,7 @@ LOOP:
} }
n, err := conn.Read(buf) n, err := conn.Read(buf)
if nil != err { if nil != err {
if !matcher.HasResponse(j) { if !matcher.HasResponse(matchCtx, j) {
discoveredMatcher = matcher discoveredMatcher = matcher
} }
@ -190,7 +190,7 @@ LOOP:
// log.Printf("res: %s", string(buf[:n])) // log.Printf("res: %s", string(buf[:n]))
if err := matcher.Match(info, j+1, osm.NewPacket(buf, n)); err == nil { if err := matcher.Match(matchCtx, j+1, osm.NewPacket(buf, n)); err == nil {
if packetCount-1 == j { if packetCount-1 == j {
discoveredMatcher = matcher discoveredMatcher = matcher

View File

@ -19,19 +19,19 @@ func scanUDP(discoverySession session.DiscoverySession, targetPort *omd.Port) er
} }
matchers := matcher.GetUDPMatchers() matchers := matcher.GetUDPMatchers()
mi := osm.NewMatchInfo(targetPort.Host.Address, portNumber) matchCtx := osm.NewMatchCtx(targetPort.Host.Address, portNumber)
for _, _matcher := range matchers { for _, _matcher := range matchers {
p := osm.NewPacket(targetPort.UDPLayer.LayerPayload(), len(targetPort.UDPLayer.LayerPayload())) p := osm.NewPacket(targetPort.UDPLayer.LayerPayload(), len(targetPort.UDPLayer.LayerPayload()))
if err := _matcher.Match(mi, 0, p); err == nil { if err := _matcher.Match(matchCtx, 0, p); err == nil {
s := &omd.Service{ s := &omd.Service{
Key: _matcher.Key(), Key: _matcher.Key(),
Port: targetPort, Port: targetPort,
MetaCryptoType: omm.ToMetaCryptoType(omm.MetaCryptoTypeEnumNONE), MetaCryptoType: omm.ToMetaCryptoType(omm.MetaCryptoTypeEnumNONE),
DiscoveredDate: omu.NowPtr(), DiscoveredDate: omu.NowPtr(),
Metadata: _matcher.Meta(), Metadata: matchCtx.GetAttributes(),
Name: _matcher.Name(), Name: _matcher.Name(matchCtx),
} }
discoverySession.AddService(s) discoverySession.AddService(s)