diff --git a/Gopkg.lock b/Gopkg.lock index f0c5440..6b09598 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -87,6 +87,7 @@ packages = [ "context", "encoding/json", + "net", "net/cidr", "net/converter", "reflect", @@ -95,7 +96,7 @@ "time/scheduler/storage", "time/scheduler/task" ] - revision = "1966a985759721fd451171a78a0b8f4524afc644" + revision = "4d4017d214d2a8fdde59d774254f421991fabe7e" [[projects]] branch = "master" @@ -110,11 +111,12 @@ "core/util", "model/data", "model/discovery", + "model/meta", "model/sensorconfig", "service/container/discovery", "service/probe" ] - revision = "078a39712d33c131303cf2705cca185da18cc196" + revision = "846c510a1ae54597ef1b1589613dfca7a3f7a3ed" [[projects]] branch = "master" @@ -213,6 +215,6 @@ [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "1b6a1dbfca3cfb6cedd6a61fa2e3aac45cae3541a95ea40766264b926a64dd24" + inputs-digest = "70618d25eb22451d93f8eb5b0d9a744c3cdc9a8cb83089e70c19f8d4fcff959c" solver-name = "gps-cdcl" solver-version = 1 diff --git a/internal/discoverer/discoverer.go b/internal/discoverer/discoverer.go index e359a54..d80f200 100644 --- a/internal/discoverer/discoverer.go +++ b/internal/discoverer/discoverer.go @@ -154,10 +154,12 @@ func (d *defaultDiscoverer) innerDiscoverZone(wg *sync.WaitGroup, dataChan chan if nil != dz.DiscoverHost { cr, _ := cidr.NewCIDRRanger(z.Network) dh := &ocmd.DiscoverHost{ - FirstScanRangeV4: cr.First().String(), - LastScanRangeV4: cr.Last().String(), - DiscoverPort: dz.DiscoverHost.DiscoverPort, + MetaIPType: z.MetaIPType, + FirstScanRange: cr.First().String(), + LastScanRange: cr.Last().String(), + DiscoverPort: dz.DiscoverHost.DiscoverPort, } + wg.Add(1) go d.innerDiscoverHost(wg, dataChan, z, dh) } diff --git a/internal/discoverer/host.go b/internal/discoverer/host.go index feabcfc..d16cf4e 100644 --- a/internal/discoverer/host.go +++ b/internal/discoverer/host.go @@ -2,9 +2,9 @@ package discoverer import ( "fmt" - "net" ocmd "git.loafle.net/overflow/commons-go/model/discovery" + ocmm "git.loafle.net/overflow/commons-go/model/meta" "git.loafle.net/overflow/container_discovery/internal/discoverer/ipv4" "git.loafle.net/overflow/container_discovery/internal/discoverer/ipv6" ) @@ -14,19 +14,13 @@ func scanHost(zone *ocmd.Zone, dh *ocmd.DiscoverHost, resultChan chan interface{ doneChan <- struct{}{} }() - _, ipNet, err := net.ParseCIDR(zone.Network) - if nil != err { - errChan <- err - return - } - switch len(ipNet.IP) { - case net.IPv4len: + switch ocmm.ToMetaIPTypeEnum(dh.MetaIPType) { + case ocmm.MetaIPTypeEnumV4: ipv4.ScanHost(zone, dh, resultChan, errChan, stopChan) - case net.IPv6len: + case ocmm.MetaIPTypeEnumV6: ipv6.ScanHost(zone, dh, resultChan, errChan, stopChan) - default: - errChan <- fmt.Errorf("Discovery: Not supported ip length") - return + errChan <- fmt.Errorf("Discovery: Not supported MetaIPType") } + } diff --git a/internal/discoverer/ipv4/host.go b/internal/discoverer/ipv4/host.go index b90210d..632b00c 100644 --- a/internal/discoverer/ipv4/host.go +++ b/internal/discoverer/ipv4/host.go @@ -86,7 +86,6 @@ func ScanHost(zone *ocmd.Zone, dh *ocmd.DiscoverHost, resultChan chan interface{ case <-timerStopped: return } - } func sendARP(ps pcap.PCapScanner, zone *ocmd.Zone, hostRanges []net.IP, stopChan chan struct{}) error { @@ -94,9 +93,9 @@ func sendARP(ps pcap.PCapScanner, zone *ocmd.Zone, hostRanges []net.IP, stopChan if nil != err { return err } - ip := net.ParseIP(zone.IPV4) + ip := net.ParseIP(zone.Address) if nil == ip { - return fmt.Errorf("Discovery: IP(%s) of zone is not valid", zone.IPV4) + return fmt.Errorf("Discovery: IP(%s) of zone is not valid", zone.Address) } ethPacket := makePacketEthernet(hwAddr) @@ -146,7 +145,7 @@ func handlePacketARP(zone *ocmd.Zone, hostRanges []net.IP, hosts map[string]*ocm } h := &ocmd.Host{ - IPV4: ip.String(), + Address: ip.String(), Mac: net.HardwareAddr(packet.SourceHwAddress).String(), Zone: zone, DiscoveredDate: occu.NowPtr(), @@ -159,22 +158,22 @@ func handlePacketARP(zone *ocmd.Zone, hostRanges []net.IP, hosts map[string]*ocm func getTargetHostRange(dh *ocmd.DiscoverHost, cr cidr.CIDRRanger) ([]net.IP, error) { var firstIP net.IP - if "" != dh.FirstScanRangeV4 { - firstIP = net.ParseIP(dh.FirstScanRangeV4) + if "" != dh.FirstScanRange { + firstIP = net.ParseIP(dh.FirstScanRange) if nil == firstIP { return nil, fmt.Errorf("Discovery: IP(%v) of FirstScanRange host is not valid", firstIP) } } var lastIP net.IP - if "" != dh.LastScanRangeV4 { - lastIP = net.ParseIP(dh.LastScanRangeV4) + if "" != dh.LastScanRange { + lastIP = net.ParseIP(dh.LastScanRange) if nil == lastIP { return nil, fmt.Errorf("Discovery: IP(%v) of LastScanRange host is not valid", lastIP) } } includeIPs := make([]net.IP, 0) - for _, iHost := range dh.IncludeHostsV4 { + for _, iHost := range dh.IncludeHosts { iIP := net.ParseIP(iHost) if nil == iIP { return nil, fmt.Errorf("Discovery: IP(%v) of include host is not valid", iHost) @@ -183,7 +182,7 @@ func getTargetHostRange(dh *ocmd.DiscoverHost, cr cidr.CIDRRanger) ([]net.IP, er } excludeIPs := make([]net.IP, 0) - for _, eHost := range dh.ExcludeHostsV4 { + for _, eHost := range dh.ExcludeHosts { eIP := net.ParseIP(eHost) if nil == eIP { return nil, fmt.Errorf("Discovery: IP(%v) of exclude host is not valid", eHost) diff --git a/internal/discoverer/ipv4/port_tcp.go b/internal/discoverer/ipv4/port_tcp.go index 96a53ec..556c397 100644 --- a/internal/discoverer/ipv4/port_tcp.go +++ b/internal/discoverer/ipv4/port_tcp.go @@ -10,9 +10,9 @@ import ( "time" "git.loafle.net/commons/logging-go" - occc "git.loafle.net/overflow/commons-go/core/constants" occu "git.loafle.net/overflow/commons-go/core/util" ocmd "git.loafle.net/overflow/commons-go/model/discovery" + ocmm "git.loafle.net/overflow/commons-go/model/meta" "git.loafle.net/overflow/container_discovery/internal/pcap" "github.com/google/gopacket" @@ -33,9 +33,9 @@ func scanPortTCP(host *ocmd.Host, dp *ocmd.DiscoverPort, resultChan chan interfa go pcap.ReleaseScanner(host.Zone) }() - tcpChan := ps.OpenTCP(host.IPV4) + tcpChan := ps.OpenTCP(host.Address) defer func() { - go ps.CloseTCP(host.IPV4, tcpChan) + go ps.CloseTCP(host.Address, tcpChan) }() timerStopped := make(chan struct{}) @@ -134,14 +134,14 @@ func handlePacketTCP(host *ocmd.Host, dp *ocmd.DiscoverPort, ports map[int]*ocmd } port := int(packet.SrcPort) - logging.Logger().Debugf("Discovery: IP of TCP(%d) src %s", port, host.IPV4) + logging.Logger().Debugf("Discovery: IP of TCP(%d) src %s", port, host.Address) if _, ok := ports[port]; ok || !dp.Contains(port) { return nil } p := &ocmd.Port{ - PortType: occc.PortTypeTCP, + MetaPortType: ocmm.ToMetaPortType(ocmm.MetaPortTypeEnumTCP), PortNumber: json.Number(strconv.Itoa(port)), DiscoveredDate: occu.NowPtr(), } @@ -162,13 +162,13 @@ type PortPacketTCP struct { func makePacketPortTCP(host *ocmd.Host) (*PortPacketTCP, error) { packetTCP := &PortPacketTCP{} - srcIP := net.ParseIP(host.Zone.IPV4) + srcIP := net.ParseIP(host.Zone.Address) if nil == srcIP { - return nil, fmt.Errorf("Discovery: IP(%s) of zone is not valid", host.Zone.IPV4) + return nil, fmt.Errorf("Discovery: IP(%s) of zone is not valid", host.Zone.Address) } - dstIP := net.ParseIP(host.IPV4) + dstIP := net.ParseIP(host.Address) if nil == dstIP { - return nil, fmt.Errorf("Discovery: IP(%s) of host is not valid", host.IPV4) + return nil, fmt.Errorf("Discovery: IP(%s) of host is not valid", host.Address) } packetTCP.IP = &layers.IPv4{ diff --git a/internal/discoverer/ipv4/port_udp.go b/internal/discoverer/ipv4/port_udp.go index 3bbc86b..0ccab19 100644 --- a/internal/discoverer/ipv4/port_udp.go +++ b/internal/discoverer/ipv4/port_udp.go @@ -10,9 +10,9 @@ import ( "time" "git.loafle.net/commons/logging-go" - occc "git.loafle.net/overflow/commons-go/core/constants" occu "git.loafle.net/overflow/commons-go/core/util" ocmd "git.loafle.net/overflow/commons-go/model/discovery" + ocmm "git.loafle.net/overflow/commons-go/model/meta" "git.loafle.net/overflow/container_discovery/internal/pcap" "git.loafle.net/overflow/container_discovery/internal/matcher" @@ -34,9 +34,9 @@ func scanPortUDP(host *ocmd.Host, dp *ocmd.DiscoverPort, resultChan chan interfa pcap.ReleaseScanner(host.Zone) }() - udpChan := ps.OpenUDP(host.IPV4) + udpChan := ps.OpenUDP(host.Address) defer func() { - ps.CloseUDP(host.IPV4, udpChan) + ps.CloseUDP(host.Address, udpChan) }() timerStopped := make(chan struct{}) @@ -85,9 +85,9 @@ func scanPortUDP(host *ocmd.Host, dp *ocmd.DiscoverPort, resultChan chan interfa } func sendUDP(host *ocmd.Host, dp *ocmd.DiscoverPort, stopChan chan struct{}) error { - ip := net.ParseIP(host.IPV4) + ip := net.ParseIP(host.Address) if nil == ip { - return fmt.Errorf("Discovery: IP(%s) of host is not valid", host.IPV4) + return fmt.Errorf("Discovery: IP(%s) of host is not valid", host.Address) } ms := matcher.GetUDPMatchers() @@ -141,7 +141,7 @@ func sendUDP(host *ocmd.Host, dp *ocmd.DiscoverPort, stopChan chan struct{}) err func handlePacketUDP(host *ocmd.Host, dp *ocmd.DiscoverPort, ports map[int]*ocmd.Port, packet gopacket.Packet) *ocmd.Port { ipLayer := packet.Layer(layers.LayerTypeIPv4) - if ipLayer.(*layers.IPv4).SrcIP.String() == host.Zone.IPV4 { + if ipLayer.(*layers.IPv4).SrcIP.String() == host.Zone.Address { return nil } @@ -157,7 +157,7 @@ func handlePacketUDP(host *ocmd.Host, dp *ocmd.DiscoverPort, ports map[int]*ocmd } p := &ocmd.Port{ - PortType: occc.PortTypeUDP, + MetaPortType: ocmm.ToMetaPortType(ocmm.MetaPortTypeEnumUDP), PortNumber: json.Number(strconv.Itoa(port)), UDPLayer: udpLayer, DiscoveredDate: occu.NowPtr(), diff --git a/internal/discoverer/ipv4/service.go b/internal/discoverer/ipv4/service.go index 8e9a34a..3946e0b 100644 --- a/internal/discoverer/ipv4/service.go +++ b/internal/discoverer/ipv4/service.go @@ -4,9 +4,9 @@ import ( "fmt" cuej "git.loafle.net/commons/util-go/encoding/json" - occc "git.loafle.net/overflow/commons-go/core/constants" occu "git.loafle.net/overflow/commons-go/core/util" ocmd "git.loafle.net/overflow/commons-go/model/discovery" + ocmm "git.loafle.net/overflow/commons-go/model/meta" "github.com/google/gopacket/layers" ) @@ -17,14 +17,13 @@ func ScanService(port *ocmd.Port, ds *ocmd.DiscoverService, resultChan chan inte return } - switch port.PortType { - case occc.PortTypeTCP: + switch ocmm.ToMetaPortTypeEnum(port.MetaPortType) { + case ocmm.MetaPortTypeEnumTCP: if !scanServiceTCP(port, ds, resultChan, errChan, stopChan) { - if dName, ok := layers.TCPPortNames[layers.TCPPort(portNumber)]; ok { sName := fmt.Sprintf("Not Supported Service. Perhaps %s[%d]", dName, portNumber) s := &ocmd.Service{ - ServiceName: sName, + Name: sName, DiscoveredDate: occu.NowPtr(), Port: port, } @@ -32,12 +31,12 @@ func ScanService(port *ocmd.Port, ds *ocmd.DiscoverService, resultChan chan inte } } - case occc.PortTypeUDP: + case ocmm.MetaPortTypeEnumUDP: if !scanServiceUDP(port, ds, resultChan, errChan, stopChan) { if dName, ok := layers.UDPPortNames[layers.UDPPort(portNumber)]; ok { sName := fmt.Sprintf("Not Supported Service. Perhaps %s[%d]", dName, portNumber) s := &ocmd.Service{ - ServiceName: sName, + Name: sName, DiscoveredDate: occu.NowPtr(), Port: port, } diff --git a/internal/discoverer/ipv4/service_conn.go b/internal/discoverer/ipv4/service_conn.go index 028a3b9..a2dc704 100644 --- a/internal/discoverer/ipv4/service_conn.go +++ b/internal/discoverer/ipv4/service_conn.go @@ -5,19 +5,21 @@ import ( "fmt" "net" "time" + + ocmm "git.loafle.net/overflow/commons-go/model/meta" ) type serviceConnector interface { - CryptoType() string + MetaCryptoType() *ocmm.MetaCryptoType Dial(ip string, port int) (net.Conn, error) } type normalServiceConn struct { - cryptoType string + metaCryptoType *ocmm.MetaCryptoType } -func (nsc *normalServiceConn) CryptoType() string { - return nsc.cryptoType +func (nsc *normalServiceConn) MetaCryptoType() *ocmm.MetaCryptoType { + return nsc.metaCryptoType } func (nsc *normalServiceConn) Dial(ip string, port int) (net.Conn, error) { @@ -31,11 +33,11 @@ func (nsc *normalServiceConn) Dial(ip string, port int) (net.Conn, error) { } type tlsServiceConn struct { - cryptoType string + metaCryptoType *ocmm.MetaCryptoType } -func (tsc *tlsServiceConn) CryptoType() string { - return tsc.cryptoType +func (tsc *tlsServiceConn) MetaCryptoType() *ocmm.MetaCryptoType { + return tsc.metaCryptoType } func (tsc *tlsServiceConn) Dial(ip string, port int) (net.Conn, error) { diff --git a/internal/discoverer/ipv4/service_tcp.go b/internal/discoverer/ipv4/service_tcp.go index 46d198b..3cf5910 100644 --- a/internal/discoverer/ipv4/service_tcp.go +++ b/internal/discoverer/ipv4/service_tcp.go @@ -8,14 +8,14 @@ import ( "git.loafle.net/commons/logging-go" csm "git.loafle.net/commons/service_matcher-go" cuej "git.loafle.net/commons/util-go/encoding/json" - occc "git.loafle.net/overflow/commons-go/core/constants" occu "git.loafle.net/overflow/commons-go/core/util" ocmd "git.loafle.net/overflow/commons-go/model/discovery" + ocmm "git.loafle.net/overflow/commons-go/model/meta" "git.loafle.net/overflow/container_discovery/internal/matcher" ) func scanServiceTCP(port *ocmd.Port, ds *ocmd.DiscoverService, resultChan chan interface{}, errChan chan error, stopChan chan struct{}) bool { - hostIP := port.Host.IPV4 + hostIP := port.Host.Address portNumber, err := cuej.NumberToInt(port.PortNumber) if err != nil { errChan <- fmt.Errorf("Discovery: Service scan on %s:%s error has occurred %v ", hostIP, port.PortNumber, err) @@ -27,10 +27,10 @@ func scanServiceTCP(port *ocmd.Port, ds *ocmd.DiscoverService, resultChan chan i scs := []serviceConnector{ &normalServiceConn{ - cryptoType: occc.CryptoTypeNONE.String(), + metaCryptoType: ocmm.ToMetaCryptoType(ocmm.MetaCryptoTypeEnumNONE), }, &tlsServiceConn{ - cryptoType: occc.CryptoTypeTLS.String(), + metaCryptoType: ocmm.ToMetaCryptoType(ocmm.MetaCryptoTypeEnumTLS), }, } @@ -40,15 +40,15 @@ LOOP: conn, err := sc.Dial(hostIP, portNumber) if err != nil { - errChan <- fmt.Errorf("Discovery: Service scan[%s] on %s:%d error has occurred %v ", sc.CryptoType(), hostIP, portNumber, err) + errChan <- fmt.Errorf("Discovery: Service scan[%s] on %s:%d error has occurred %v ", sc.MetaCryptoType().Key, hostIP, portNumber, err) return false } - logging.Logger().Debugf("Discovery: Service scan connected[%s:%d] %s", hostIP, portNumber, sc.CryptoType()) + logging.Logger().Debugf("Discovery: Service scan connected[%s:%d] %s", hostIP, portNumber, sc.MetaCryptoType().Key) buf := make([]byte, 1024) if err := conn.SetReadDeadline(time.Now().Add(1 * time.Second)); nil != err { - logging.Logger().Debugf("Discovery: cannot set readdeadline connected[%s:%d] %s", hostIP, portNumber, sc.CryptoType()) + logging.Logger().Debugf("Discovery: cannot set readdeadline connected[%s:%d] %s", hostIP, portNumber, sc.MetaCryptoType().Key) return false } rn, err := conn.Read(buf) @@ -88,7 +88,7 @@ func hadlePrePacket(info csm.MatchInfo, sc serviceConnector, conn net.Conn, pack }() // logging.Logger().Debugf("Discovery: Service scan pre packet length[%d], buf[%v]", packet.Len, packet.Buffer) - logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d pre packet length[%d]", sc.CryptoType(), info.IP(), info.Port(), packet.Len) + logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d pre packet length[%d]", sc.MetaCryptoType().Key, info.IP(), info.Port(), packet.Len) ms := matcher.GetTCPMatchers(true) buf := make([]byte, 1024) @@ -103,9 +103,9 @@ LOOP: if 0 == packetCount { s = &ocmd.Service{ - ServiceName: m.Name(), - CryptoType: occc.ToCryptoType(sc.CryptoType()), - DiscoveredDate: occu.NowPtr(), + Name: m.Name(), + MetaCryptoTypeKey: sc.MetaCryptoType(), + DiscoveredDate: occu.NowPtr(), } break LOOP } @@ -115,39 +115,39 @@ LOOP: for j := 0; j < packetCount; j++ { tPacket := m.Packet(j) // logging.Logger().Debugf("Discovery: Service scan send packet length[%d], buf[%v]", tPacket.Len, tPacket.Buffer) - logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d send packet length[%d]", sc.CryptoType(), info.IP(), info.Port(), tPacket.Len) + logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d send packet length[%d]", sc.MetaCryptoType().Key, info.IP(), info.Port(), tPacket.Len) if err := conn.SetWriteDeadline(time.Now().Add(1 * time.Second)); nil != err { - logging.Logger().Debugf("Discovery: cannot set writeDeadLine Service scan[%s] on %s:%d send packet length[%d]", sc.CryptoType(), info.IP(), info.Port(), tPacket.Len) + logging.Logger().Debugf("Discovery: cannot set writeDeadLine Service scan[%s] on %s:%d send packet length[%d]", sc.MetaCryptoType().Key, info.IP(), info.Port(), tPacket.Len) break LOOP } wn, err := conn.Write(tPacket.Buffer) if nil != err { - logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d send packet error %v", sc.CryptoType(), info.IP(), info.Port(), err) + logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d send packet error %v", sc.MetaCryptoType().Key, info.IP(), info.Port(), err) continue LOOP } if wn != tPacket.Len { - logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d send packet length[%d] not same with %d", sc.CryptoType(), info.IP(), info.Port(), wn, tPacket.Len) + logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d send packet length[%d] not same with %d", sc.MetaCryptoType().Key, info.IP(), info.Port(), wn, tPacket.Len) continue LOOP } if err := conn.SetReadDeadline(time.Now().Add(1 * time.Second)); nil != err { - logging.Logger().Debugf("Discovery: cannot set readDeadLine Service scan[%s] on %s:%d send packet length[%d]", sc.CryptoType(), info.IP(), info.Port(), tPacket.Len) + logging.Logger().Debugf("Discovery: cannot set readDeadLine Service scan[%s] on %s:%d send packet length[%d]", sc.MetaCryptoType().Key, info.IP(), info.Port(), tPacket.Len) break LOOP } rn, err := conn.Read(buf) if nil != err { - logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d receive packet error %v", sc.CryptoType(), info.IP(), info.Port(), err) + logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d receive packet error %v", sc.MetaCryptoType().Key, info.IP(), info.Port(), err) break LOOP } if m.Match(info, j+1, csm.NewPacket(buf, rn)) { // logging.Logger().Debugf("Discovery: Service scan receive match length[%d], buf[%v]", rn, buf) - logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d receive match length[%d]", sc.CryptoType(), info.IP(), info.Port(), rn) + logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d receive match length[%d]", sc.MetaCryptoType().Key, info.IP(), info.Port(), rn) found = true } else { // logging.Logger().Debugf("Discovery: Service scan receive not match length[%d], buf[%v]", rn, buf) - logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d receive not match length[%d]", sc.CryptoType(), info.IP(), info.Port(), rn) + logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d receive not match length[%d]", sc.MetaCryptoType().Key, info.IP(), info.Port(), rn) found = false continue LOOP } @@ -155,9 +155,9 @@ LOOP: if found { s = &ocmd.Service{ - ServiceName: m.Name(), - CryptoType: occc.ToCryptoType(sc.CryptoType()), - DiscoveredDate: occu.NowPtr(), + Name: m.Name(), + MetaCryptoTypeKey: sc.MetaCryptoType(), + DiscoveredDate: occu.NowPtr(), } break LOOP } @@ -181,7 +181,7 @@ LOOP: conn, err := sc.Dial(info.IP(), info.Port()) if err != nil { - logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d socket dial error %v", sc.CryptoType(), info.IP(), info.Port(), err) + logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d socket dial error %v", sc.MetaCryptoType().Key, info.IP(), info.Port(), err) break LOOP } @@ -189,57 +189,57 @@ LOOP: for j := 0; j < packetCount; j++ { tPacket := m.Packet(j) // logging.Logger().Debugf("Discovery: Service scan send packet length[%d], buf[%v]", tPacket.Len, tPacket.Buffer) - logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d send packet length[%d]", sc.CryptoType(), info.IP(), info.Port(), tPacket.Len) + logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d send packet length[%d]", sc.MetaCryptoType().Key, info.IP(), info.Port(), tPacket.Len) if err := conn.SetWriteDeadline(time.Now().Add(1 * time.Second)); nil != err { - logging.Logger().Debugf("Discovery: cannot set writeDeadLine Service scan[%s] on %s:%d send packet length[%d]", sc.CryptoType(), info.IP(), info.Port(), tPacket.Len) + logging.Logger().Debugf("Discovery: cannot set writeDeadLine Service scan[%s] on %s:%d send packet length[%d]", sc.MetaCryptoType().Key, info.IP(), info.Port(), tPacket.Len) break } wn, err := conn.Write(tPacket.Buffer) if nil != err { - logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d send packet error %v", sc.CryptoType(), info.IP(), info.Port(), err) + logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d send packet error %v", sc.MetaCryptoType().Key, info.IP(), info.Port(), err) break } if wn != tPacket.Len { - logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d send packet length[%d] not same with %d", sc.CryptoType(), info.IP(), info.Port(), wn, tPacket.Len) + logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d send packet length[%d] not same with %d", sc.MetaCryptoType().Key, info.IP(), info.Port(), wn, tPacket.Len) break } if err := conn.SetReadDeadline(time.Now().Add(1 * time.Second)); nil != err { - logging.Logger().Debugf("Discovery: cannot set readDeadLine Service scan[%s] on %s:%d send packet length[%d]", sc.CryptoType(), info.IP(), info.Port(), tPacket.Len) + logging.Logger().Debugf("Discovery: cannot set readDeadLine Service scan[%s] on %s:%d send packet length[%d]", sc.MetaCryptoType().Key, info.IP(), info.Port(), tPacket.Len) break } rn, err := conn.Read(buf) if nil != err { if !m.HasResponse(j) { s = &ocmd.Service{ - ServiceName: m.Name(), - CryptoType: occc.ToCryptoType(sc.CryptoType()), - DiscoveredDate: occu.NowPtr(), + Name: m.Name(), + MetaCryptoTypeKey: sc.MetaCryptoType(), + DiscoveredDate: occu.NowPtr(), } break } - logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d receive packet error %v", sc.CryptoType(), info.IP(), info.Port(), err) + logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d receive packet error %v", sc.MetaCryptoType().Key, info.IP(), info.Port(), err) break } if m.Match(info, j, csm.NewPacket(buf, rn)) { if packetCount-1 == j { s = &ocmd.Service{ - ServiceName: m.Name(), - CryptoType: occc.ToCryptoType(sc.CryptoType()), - DiscoveredDate: occu.NowPtr(), + Name: m.Name(), + MetaCryptoTypeKey: sc.MetaCryptoType(), + DiscoveredDate: occu.NowPtr(), } break } // logging.Logger().Debugf("Discovery: Service scan receive match length[%d], buf[%v]", rn, buf) - logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d receive match length[%d]", sc.CryptoType(), info.IP(), info.Port(), rn) + logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d receive match length[%d]", sc.MetaCryptoType().Key, info.IP(), info.Port(), rn) continue } else { // logging.Logger().Debugf("Discovery: Service scan receive not match length[%d], buf[%v]", rn, buf) - logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d receive not match length[%d]", sc.CryptoType(), info.IP(), info.Port(), rn) + logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d receive not match length[%d]", sc.MetaCryptoType().Key, info.IP(), info.Port(), rn) break } } diff --git a/internal/discoverer/ipv4/service_udp.go b/internal/discoverer/ipv4/service_udp.go index d45ccee..221ff33 100644 --- a/internal/discoverer/ipv4/service_udp.go +++ b/internal/discoverer/ipv4/service_udp.go @@ -18,7 +18,7 @@ func scanServiceUDP(port *ocmd.Port, ds *ocmd.DiscoverService, resultChan chan i } ms := matcher.GetUDPMatchers() - mi := csm.NewMatchInfo(port.Host.IPV4, portNumber) + mi := csm.NewMatchInfo(port.Host.Address, portNumber) for i := 0; i < len(ms); i++ { m := ms[i] @@ -26,7 +26,7 @@ func scanServiceUDP(port *ocmd.Port, ds *ocmd.DiscoverService, resultChan chan i if m.Match(mi, 0, p) { s := &ocmd.Service{ - ServiceName: m.Name(), + Name: m.Name(), Port: port, DiscoveredDate: occu.NowPtr(), } diff --git a/internal/discoverer/port.go b/internal/discoverer/port.go index f3eebec..bec436c 100644 --- a/internal/discoverer/port.go +++ b/internal/discoverer/port.go @@ -2,9 +2,9 @@ package discoverer import ( "fmt" - "net" ocmd "git.loafle.net/overflow/commons-go/model/discovery" + ocmm "git.loafle.net/overflow/commons-go/model/meta" "git.loafle.net/overflow/container_discovery/internal/discoverer/ipv4" "git.loafle.net/overflow/container_discovery/internal/discoverer/ipv6" ) @@ -14,19 +14,13 @@ func scanPort(host *ocmd.Host, dp *ocmd.DiscoverPort, resultChan chan interface{ doneChan <- struct{}{} }() - _, ipNet, err := net.ParseCIDR(host.Zone.Network) - if nil != err { - errChan <- err - return - } - switch len(ipNet.IP) { - case net.IPv4len: + switch ocmm.ToMetaIPTypeEnum(host.MetaIPType) { + case ocmm.MetaIPTypeEnumV4: ipv4.ScanPort(host, dp, resultChan, errChan, stopChan) - case net.IPv6len: + case ocmm.MetaIPTypeEnumV6: ipv6.ScanPort(host, dp, resultChan, errChan, stopChan) - default: - errChan <- fmt.Errorf("Discovery: Not supported ip length") - return + errChan <- fmt.Errorf("Discovery: Not supported MetaIPType") } + } diff --git a/internal/discoverer/service.go b/internal/discoverer/service.go index 562544b..80587d4 100644 --- a/internal/discoverer/service.go +++ b/internal/discoverer/service.go @@ -2,9 +2,9 @@ package discoverer import ( "fmt" - "net" ocmd "git.loafle.net/overflow/commons-go/model/discovery" + ocmm "git.loafle.net/overflow/commons-go/model/meta" "git.loafle.net/overflow/container_discovery/internal/discoverer/ipv4" "git.loafle.net/overflow/container_discovery/internal/discoverer/ipv6" ) @@ -14,20 +14,13 @@ func scanService(port *ocmd.Port, ds *ocmd.DiscoverService, resultChan chan inte doneChan <- struct{}{} }() - _, ipNet, err := net.ParseCIDR(port.Host.Zone.Network) - if nil != err { - errChan <- err - return - } - - switch len(ipNet.IP) { - case net.IPv4len: + switch ocmm.ToMetaIPTypeEnum(port.Host.MetaIPType) { + case ocmm.MetaIPTypeEnumV4: ipv4.ScanService(port, ds, resultChan, errChan, stopChan) - case net.IPv6len: + case ocmm.MetaIPTypeEnumV6: ipv6.ScanService(port, ds, resultChan, errChan, stopChan) - default: - errChan <- fmt.Errorf("Discovery: Not supported ip length") - return + errChan <- fmt.Errorf("Discovery: Not supported MetaIPType") } + } diff --git a/internal/discoverer/zone.go b/internal/discoverer/zone.go index 06db61b..af06696 100644 --- a/internal/discoverer/zone.go +++ b/internal/discoverer/zone.go @@ -3,10 +3,11 @@ package discoverer import ( "net" "regexp" - "strings" + cun "git.loafle.net/commons/util-go/net" occu "git.loafle.net/overflow/commons-go/core/util" ocmd "git.loafle.net/overflow/commons-go/model/discovery" + ocmm "git.loafle.net/overflow/commons-go/model/meta" ) func scanZone(dz *ocmd.DiscoverZone, resultChan chan interface{}, errChan chan error, doneChan chan<- struct{}, stopChan chan struct{}) { @@ -17,8 +18,8 @@ func scanZone(dz *ocmd.DiscoverZone, resultChan chan interface{}, errChan chan e var err error var ifaces []net.Interface var addrs []net.Addr - var ipnet *net.IPNet - var zones []*net.IPNet + var ipnet *cun.IPNet + var zones []*cun.IPNet // var gwIP net.IP // var gwIFace string @@ -32,7 +33,7 @@ func scanZone(dz *ocmd.DiscoverZone, resultChan chan interface{}, errChan chan e return } - zones = make([]*net.IPNet, 0) + zones = make([]*cun.IPNet, 0) for _, i := range ifaces { @@ -43,7 +44,7 @@ func scanZone(dz *ocmd.DiscoverZone, resultChan chan interface{}, errChan chan e for _, addr := range addrs { - if _, ipnet, err = net.ParseCIDR(addr.String()); nil != err { + if _, ipnet, err = cun.ParseCIDR(addr.String()); nil != err { errChan <- err continue } @@ -57,10 +58,17 @@ func scanZone(dz *ocmd.DiscoverZone, resultChan chan interface{}, errChan chan e Network: ipnet.String(), Iface: i.Name, Mac: i.HardwareAddr.String(), - IPV4: strings.Split(addr.String(), "/")[0], + Address: addr.String(), DiscoveredDate: occu.NowPtr(), } + switch ipnet.Version() { + case 6: + z.MetaIPType = ocmm.ToMetaIPType(ocmm.MetaIPTypeEnumV6) + default: + z.MetaIPType = ocmm.ToMetaIPType(ocmm.MetaIPTypeEnumV4) + } + resultChan <- z } } @@ -80,7 +88,7 @@ func checkExclude(ep []string, iface string) bool { return false } -func checkSameZone(zones []*net.IPNet, ipnet *net.IPNet) bool { +func checkSameZone(zones []*cun.IPNet, ipnet *cun.IPNet) bool { for _, i := range zones { if i.Contains(ipnet.IP) { return true diff --git a/service/DiscoveryService.go b/service/DiscoveryService.go index 82bb9cb..c0213c0 100644 --- a/service/DiscoveryService.go +++ b/service/DiscoveryService.go @@ -86,13 +86,17 @@ func (s *DiscoveryService) DiscoverService(requesterID string, port *ocmd.Port, } func (s *DiscoveryService) StopDiscovery(requesterID string) error { + logging.Logger().Debugf("Request DiscoveryService.StopDiscovery") + _stopChan, ok := s.pendingDiscovery.Load(requesterID) if !ok { return fmt.Errorf("discovery request for [%s] is not exist", requesterID) } stopChan := _stopChan.(chan struct{}) - close(stopChan) + stopChan <- struct{}{} + + logging.Logger().Debugf("Close DiscoveryService.StopDiscovery") return nil } @@ -152,8 +156,8 @@ func (s *DiscoveryService) handleDiscovery(requesterID string, discoveryFunc fun } data.Release() case <-stopChan: + logging.Logger().Debugf("DiscoveryService.StopDiscovery") s.discoverer.Stop() - return nil } } }