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 {
case msg, ok := <-i.Message():
if !ok {
close(stopChan)
return
}
switch msg.Type() {
case types.DiscoveryMessageTypeHost:
log.Print("Discovered Host: ", msg.Data())
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:
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:
log.Print("Discovery start: ", msg.Data())
case types.DiscoveryMessageTypeStop:

View File

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

View File

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

View File

@ -19,19 +19,19 @@ func scanUDP(discoverySession session.DiscoverySession, targetPort *omd.Port) er
}
matchers := matcher.GetUDPMatchers()
mi := osm.NewMatchInfo(targetPort.Host.Address, portNumber)
matchCtx := osm.NewMatchCtx(targetPort.Host.Address, portNumber)
for _, _matcher := range matchers {
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{
Key: _matcher.Key(),
Port: targetPort,
MetaCryptoType: omm.ToMetaCryptoType(omm.MetaCryptoTypeEnumNONE),
DiscoveredDate: omu.NowPtr(),
Metadata: _matcher.Meta(),
Name: _matcher.Name(),
Metadata: matchCtx.GetAttributes(),
Name: _matcher.Name(matchCtx),
}
discoverySession.AddService(s)