This commit is contained in:
crusader 2018-06-13 19:20:14 +09:00
parent 2345777e47
commit c65fd4ab7d
14 changed files with 128 additions and 131 deletions

8
Gopkg.lock generated
View File

@ -87,6 +87,7 @@
packages = [ packages = [
"context", "context",
"encoding/json", "encoding/json",
"net",
"net/cidr", "net/cidr",
"net/converter", "net/converter",
"reflect", "reflect",
@ -95,7 +96,7 @@
"time/scheduler/storage", "time/scheduler/storage",
"time/scheduler/task" "time/scheduler/task"
] ]
revision = "1966a985759721fd451171a78a0b8f4524afc644" revision = "4d4017d214d2a8fdde59d774254f421991fabe7e"
[[projects]] [[projects]]
branch = "master" branch = "master"
@ -110,11 +111,12 @@
"core/util", "core/util",
"model/data", "model/data",
"model/discovery", "model/discovery",
"model/meta",
"model/sensorconfig", "model/sensorconfig",
"service/container/discovery", "service/container/discovery",
"service/probe" "service/probe"
] ]
revision = "078a39712d33c131303cf2705cca185da18cc196" revision = "846c510a1ae54597ef1b1589613dfca7a3f7a3ed"
[[projects]] [[projects]]
branch = "master" branch = "master"
@ -213,6 +215,6 @@
[solve-meta] [solve-meta]
analyzer-name = "dep" analyzer-name = "dep"
analyzer-version = 1 analyzer-version = 1
inputs-digest = "1b6a1dbfca3cfb6cedd6a61fa2e3aac45cae3541a95ea40766264b926a64dd24" inputs-digest = "70618d25eb22451d93f8eb5b0d9a744c3cdc9a8cb83089e70c19f8d4fcff959c"
solver-name = "gps-cdcl" solver-name = "gps-cdcl"
solver-version = 1 solver-version = 1

View File

@ -154,10 +154,12 @@ func (d *defaultDiscoverer) innerDiscoverZone(wg *sync.WaitGroup, dataChan chan
if nil != dz.DiscoverHost { if nil != dz.DiscoverHost {
cr, _ := cidr.NewCIDRRanger(z.Network) cr, _ := cidr.NewCIDRRanger(z.Network)
dh := &ocmd.DiscoverHost{ dh := &ocmd.DiscoverHost{
FirstScanRangeV4: cr.First().String(), MetaIPType: z.MetaIPType,
LastScanRangeV4: cr.Last().String(), FirstScanRange: cr.First().String(),
DiscoverPort: dz.DiscoverHost.DiscoverPort, LastScanRange: cr.Last().String(),
DiscoverPort: dz.DiscoverHost.DiscoverPort,
} }
wg.Add(1) wg.Add(1)
go d.innerDiscoverHost(wg, dataChan, z, dh) go d.innerDiscoverHost(wg, dataChan, z, dh)
} }

View File

@ -2,9 +2,9 @@ package discoverer
import ( import (
"fmt" "fmt"
"net"
ocmd "git.loafle.net/overflow/commons-go/model/discovery" 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/ipv4"
"git.loafle.net/overflow/container_discovery/internal/discoverer/ipv6" "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{}{} doneChan <- struct{}{}
}() }()
_, ipNet, err := net.ParseCIDR(zone.Network) switch ocmm.ToMetaIPTypeEnum(dh.MetaIPType) {
if nil != err { case ocmm.MetaIPTypeEnumV4:
errChan <- err
return
}
switch len(ipNet.IP) {
case net.IPv4len:
ipv4.ScanHost(zone, dh, resultChan, errChan, stopChan) ipv4.ScanHost(zone, dh, resultChan, errChan, stopChan)
case net.IPv6len: case ocmm.MetaIPTypeEnumV6:
ipv6.ScanHost(zone, dh, resultChan, errChan, stopChan) ipv6.ScanHost(zone, dh, resultChan, errChan, stopChan)
default: default:
errChan <- fmt.Errorf("Discovery: Not supported ip length") errChan <- fmt.Errorf("Discovery: Not supported MetaIPType")
return
} }
} }

View File

@ -86,7 +86,6 @@ func ScanHost(zone *ocmd.Zone, dh *ocmd.DiscoverHost, resultChan chan interface{
case <-timerStopped: case <-timerStopped:
return return
} }
} }
func sendARP(ps pcap.PCapScanner, zone *ocmd.Zone, hostRanges []net.IP, stopChan chan struct{}) error { 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 { if nil != err {
return err return err
} }
ip := net.ParseIP(zone.IPV4) ip := net.ParseIP(zone.Address)
if nil == ip { 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) ethPacket := makePacketEthernet(hwAddr)
@ -146,7 +145,7 @@ func handlePacketARP(zone *ocmd.Zone, hostRanges []net.IP, hosts map[string]*ocm
} }
h := &ocmd.Host{ h := &ocmd.Host{
IPV4: ip.String(), Address: ip.String(),
Mac: net.HardwareAddr(packet.SourceHwAddress).String(), Mac: net.HardwareAddr(packet.SourceHwAddress).String(),
Zone: zone, Zone: zone,
DiscoveredDate: occu.NowPtr(), 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) { func getTargetHostRange(dh *ocmd.DiscoverHost, cr cidr.CIDRRanger) ([]net.IP, error) {
var firstIP net.IP var firstIP net.IP
if "" != dh.FirstScanRangeV4 { if "" != dh.FirstScanRange {
firstIP = net.ParseIP(dh.FirstScanRangeV4) firstIP = net.ParseIP(dh.FirstScanRange)
if nil == firstIP { if nil == firstIP {
return nil, fmt.Errorf("Discovery: IP(%v) of FirstScanRange host is not valid", firstIP) return nil, fmt.Errorf("Discovery: IP(%v) of FirstScanRange host is not valid", firstIP)
} }
} }
var lastIP net.IP var lastIP net.IP
if "" != dh.LastScanRangeV4 { if "" != dh.LastScanRange {
lastIP = net.ParseIP(dh.LastScanRangeV4) lastIP = net.ParseIP(dh.LastScanRange)
if nil == lastIP { if nil == lastIP {
return nil, fmt.Errorf("Discovery: IP(%v) of LastScanRange host is not valid", lastIP) return nil, fmt.Errorf("Discovery: IP(%v) of LastScanRange host is not valid", lastIP)
} }
} }
includeIPs := make([]net.IP, 0) includeIPs := make([]net.IP, 0)
for _, iHost := range dh.IncludeHostsV4 { for _, iHost := range dh.IncludeHosts {
iIP := net.ParseIP(iHost) iIP := net.ParseIP(iHost)
if nil == iIP { if nil == iIP {
return nil, fmt.Errorf("Discovery: IP(%v) of include host is not valid", iHost) 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) excludeIPs := make([]net.IP, 0)
for _, eHost := range dh.ExcludeHostsV4 { for _, eHost := range dh.ExcludeHosts {
eIP := net.ParseIP(eHost) eIP := net.ParseIP(eHost)
if nil == eIP { if nil == eIP {
return nil, fmt.Errorf("Discovery: IP(%v) of exclude host is not valid", eHost) return nil, fmt.Errorf("Discovery: IP(%v) of exclude host is not valid", eHost)

View File

@ -10,9 +10,9 @@ import (
"time" "time"
"git.loafle.net/commons/logging-go" "git.loafle.net/commons/logging-go"
occc "git.loafle.net/overflow/commons-go/core/constants"
occu "git.loafle.net/overflow/commons-go/core/util" occu "git.loafle.net/overflow/commons-go/core/util"
ocmd "git.loafle.net/overflow/commons-go/model/discovery" 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/pcap"
"github.com/google/gopacket" "github.com/google/gopacket"
@ -33,9 +33,9 @@ func scanPortTCP(host *ocmd.Host, dp *ocmd.DiscoverPort, resultChan chan interfa
go pcap.ReleaseScanner(host.Zone) go pcap.ReleaseScanner(host.Zone)
}() }()
tcpChan := ps.OpenTCP(host.IPV4) tcpChan := ps.OpenTCP(host.Address)
defer func() { defer func() {
go ps.CloseTCP(host.IPV4, tcpChan) go ps.CloseTCP(host.Address, tcpChan)
}() }()
timerStopped := make(chan struct{}) timerStopped := make(chan struct{})
@ -134,14 +134,14 @@ func handlePacketTCP(host *ocmd.Host, dp *ocmd.DiscoverPort, ports map[int]*ocmd
} }
port := int(packet.SrcPort) 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) { if _, ok := ports[port]; ok || !dp.Contains(port) {
return nil return nil
} }
p := &ocmd.Port{ p := &ocmd.Port{
PortType: occc.PortTypeTCP, MetaPortType: ocmm.ToMetaPortType(ocmm.MetaPortTypeEnumTCP),
PortNumber: json.Number(strconv.Itoa(port)), PortNumber: json.Number(strconv.Itoa(port)),
DiscoveredDate: occu.NowPtr(), DiscoveredDate: occu.NowPtr(),
} }
@ -162,13 +162,13 @@ type PortPacketTCP struct {
func makePacketPortTCP(host *ocmd.Host) (*PortPacketTCP, error) { func makePacketPortTCP(host *ocmd.Host) (*PortPacketTCP, error) {
packetTCP := &PortPacketTCP{} packetTCP := &PortPacketTCP{}
srcIP := net.ParseIP(host.Zone.IPV4) srcIP := net.ParseIP(host.Zone.Address)
if nil == srcIP { 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 { 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{ packetTCP.IP = &layers.IPv4{

View File

@ -10,9 +10,9 @@ import (
"time" "time"
"git.loafle.net/commons/logging-go" "git.loafle.net/commons/logging-go"
occc "git.loafle.net/overflow/commons-go/core/constants"
occu "git.loafle.net/overflow/commons-go/core/util" occu "git.loafle.net/overflow/commons-go/core/util"
ocmd "git.loafle.net/overflow/commons-go/model/discovery" 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/pcap"
"git.loafle.net/overflow/container_discovery/internal/matcher" "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) pcap.ReleaseScanner(host.Zone)
}() }()
udpChan := ps.OpenUDP(host.IPV4) udpChan := ps.OpenUDP(host.Address)
defer func() { defer func() {
ps.CloseUDP(host.IPV4, udpChan) ps.CloseUDP(host.Address, udpChan)
}() }()
timerStopped := make(chan struct{}) 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 { 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 { 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() 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 { func handlePacketUDP(host *ocmd.Host, dp *ocmd.DiscoverPort, ports map[int]*ocmd.Port, packet gopacket.Packet) *ocmd.Port {
ipLayer := packet.Layer(layers.LayerTypeIPv4) 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 return nil
} }
@ -157,7 +157,7 @@ func handlePacketUDP(host *ocmd.Host, dp *ocmd.DiscoverPort, ports map[int]*ocmd
} }
p := &ocmd.Port{ p := &ocmd.Port{
PortType: occc.PortTypeUDP, MetaPortType: ocmm.ToMetaPortType(ocmm.MetaPortTypeEnumUDP),
PortNumber: json.Number(strconv.Itoa(port)), PortNumber: json.Number(strconv.Itoa(port)),
UDPLayer: udpLayer, UDPLayer: udpLayer,
DiscoveredDate: occu.NowPtr(), DiscoveredDate: occu.NowPtr(),

View File

@ -4,9 +4,9 @@ import (
"fmt" "fmt"
cuej "git.loafle.net/commons/util-go/encoding/json" 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" occu "git.loafle.net/overflow/commons-go/core/util"
ocmd "git.loafle.net/overflow/commons-go/model/discovery" ocmd "git.loafle.net/overflow/commons-go/model/discovery"
ocmm "git.loafle.net/overflow/commons-go/model/meta"
"github.com/google/gopacket/layers" "github.com/google/gopacket/layers"
) )
@ -17,14 +17,13 @@ func ScanService(port *ocmd.Port, ds *ocmd.DiscoverService, resultChan chan inte
return return
} }
switch port.PortType { switch ocmm.ToMetaPortTypeEnum(port.MetaPortType) {
case occc.PortTypeTCP: case ocmm.MetaPortTypeEnumTCP:
if !scanServiceTCP(port, ds, resultChan, errChan, stopChan) { if !scanServiceTCP(port, ds, resultChan, errChan, stopChan) {
if dName, ok := layers.TCPPortNames[layers.TCPPort(portNumber)]; ok { if dName, ok := layers.TCPPortNames[layers.TCPPort(portNumber)]; ok {
sName := fmt.Sprintf("Not Supported Service. Perhaps %s[%d]", dName, portNumber) sName := fmt.Sprintf("Not Supported Service. Perhaps %s[%d]", dName, portNumber)
s := &ocmd.Service{ s := &ocmd.Service{
ServiceName: sName, Name: sName,
DiscoveredDate: occu.NowPtr(), DiscoveredDate: occu.NowPtr(),
Port: port, 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 !scanServiceUDP(port, ds, resultChan, errChan, stopChan) {
if dName, ok := layers.UDPPortNames[layers.UDPPort(portNumber)]; ok { if dName, ok := layers.UDPPortNames[layers.UDPPort(portNumber)]; ok {
sName := fmt.Sprintf("Not Supported Service. Perhaps %s[%d]", dName, portNumber) sName := fmt.Sprintf("Not Supported Service. Perhaps %s[%d]", dName, portNumber)
s := &ocmd.Service{ s := &ocmd.Service{
ServiceName: sName, Name: sName,
DiscoveredDate: occu.NowPtr(), DiscoveredDate: occu.NowPtr(),
Port: port, Port: port,
} }

View File

@ -5,19 +5,21 @@ import (
"fmt" "fmt"
"net" "net"
"time" "time"
ocmm "git.loafle.net/overflow/commons-go/model/meta"
) )
type serviceConnector interface { type serviceConnector interface {
CryptoType() string MetaCryptoType() *ocmm.MetaCryptoType
Dial(ip string, port int) (net.Conn, error) Dial(ip string, port int) (net.Conn, error)
} }
type normalServiceConn struct { type normalServiceConn struct {
cryptoType string metaCryptoType *ocmm.MetaCryptoType
} }
func (nsc *normalServiceConn) CryptoType() string { func (nsc *normalServiceConn) MetaCryptoType() *ocmm.MetaCryptoType {
return nsc.cryptoType return nsc.metaCryptoType
} }
func (nsc *normalServiceConn) Dial(ip string, port int) (net.Conn, error) { 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 { type tlsServiceConn struct {
cryptoType string metaCryptoType *ocmm.MetaCryptoType
} }
func (tsc *tlsServiceConn) CryptoType() string { func (tsc *tlsServiceConn) MetaCryptoType() *ocmm.MetaCryptoType {
return tsc.cryptoType return tsc.metaCryptoType
} }
func (tsc *tlsServiceConn) Dial(ip string, port int) (net.Conn, error) { func (tsc *tlsServiceConn) Dial(ip string, port int) (net.Conn, error) {

View File

@ -8,14 +8,14 @@ import (
"git.loafle.net/commons/logging-go" "git.loafle.net/commons/logging-go"
csm "git.loafle.net/commons/service_matcher-go" csm "git.loafle.net/commons/service_matcher-go"
cuej "git.loafle.net/commons/util-go/encoding/json" 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" occu "git.loafle.net/overflow/commons-go/core/util"
ocmd "git.loafle.net/overflow/commons-go/model/discovery" 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" "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 { 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) portNumber, err := cuej.NumberToInt(port.PortNumber)
if err != nil { if err != nil {
errChan <- fmt.Errorf("Discovery: Service scan on %s:%s error has occurred %v ", hostIP, port.PortNumber, err) 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{ scs := []serviceConnector{
&normalServiceConn{ &normalServiceConn{
cryptoType: occc.CryptoTypeNONE.String(), metaCryptoType: ocmm.ToMetaCryptoType(ocmm.MetaCryptoTypeEnumNONE),
}, },
&tlsServiceConn{ &tlsServiceConn{
cryptoType: occc.CryptoTypeTLS.String(), metaCryptoType: ocmm.ToMetaCryptoType(ocmm.MetaCryptoTypeEnumTLS),
}, },
} }
@ -40,15 +40,15 @@ LOOP:
conn, err := sc.Dial(hostIP, portNumber) conn, err := sc.Dial(hostIP, portNumber)
if err != nil { 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 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) buf := make([]byte, 1024)
if err := conn.SetReadDeadline(time.Now().Add(1 * time.Second)); nil != err { 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 return false
} }
rn, err := conn.Read(buf) 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 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) ms := matcher.GetTCPMatchers(true)
buf := make([]byte, 1024) buf := make([]byte, 1024)
@ -103,9 +103,9 @@ LOOP:
if 0 == packetCount { if 0 == packetCount {
s = &ocmd.Service{ s = &ocmd.Service{
ServiceName: m.Name(), Name: m.Name(),
CryptoType: occc.ToCryptoType(sc.CryptoType()), MetaCryptoTypeKey: sc.MetaCryptoType(),
DiscoveredDate: occu.NowPtr(), DiscoveredDate: occu.NowPtr(),
} }
break LOOP break LOOP
} }
@ -115,39 +115,39 @@ LOOP:
for j := 0; j < packetCount; j++ { for j := 0; j < packetCount; j++ {
tPacket := m.Packet(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 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 { 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 break LOOP
} }
wn, err := conn.Write(tPacket.Buffer) wn, err := conn.Write(tPacket.Buffer)
if nil != err { 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 continue LOOP
} }
if wn != tPacket.Len { 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 continue LOOP
} }
if err := conn.SetReadDeadline(time.Now().Add(1 * time.Second)); nil != err { 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 break LOOP
} }
rn, err := conn.Read(buf) rn, err := conn.Read(buf)
if nil != err { 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 break LOOP
} }
if m.Match(info, j+1, csm.NewPacket(buf, rn)) { 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 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 found = true
} else { } else {
// logging.Logger().Debugf("Discovery: Service scan receive not match length[%d], buf[%v]", rn, buf) // 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 found = false
continue LOOP continue LOOP
} }
@ -155,9 +155,9 @@ LOOP:
if found { if found {
s = &ocmd.Service{ s = &ocmd.Service{
ServiceName: m.Name(), Name: m.Name(),
CryptoType: occc.ToCryptoType(sc.CryptoType()), MetaCryptoTypeKey: sc.MetaCryptoType(),
DiscoveredDate: occu.NowPtr(), DiscoveredDate: occu.NowPtr(),
} }
break LOOP break LOOP
} }
@ -181,7 +181,7 @@ LOOP:
conn, err := sc.Dial(info.IP(), info.Port()) conn, err := sc.Dial(info.IP(), info.Port())
if err != nil { 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 break LOOP
} }
@ -189,57 +189,57 @@ LOOP:
for j := 0; j < packetCount; j++ { for j := 0; j < packetCount; j++ {
tPacket := m.Packet(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 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 { 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 break
} }
wn, err := conn.Write(tPacket.Buffer) wn, err := conn.Write(tPacket.Buffer)
if nil != err { 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 break
} }
if wn != tPacket.Len { 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 break
} }
if err := conn.SetReadDeadline(time.Now().Add(1 * time.Second)); nil != err { 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 break
} }
rn, err := conn.Read(buf) rn, err := conn.Read(buf)
if nil != err { if nil != err {
if !m.HasResponse(j) { if !m.HasResponse(j) {
s = &ocmd.Service{ s = &ocmd.Service{
ServiceName: m.Name(), Name: m.Name(),
CryptoType: occc.ToCryptoType(sc.CryptoType()), MetaCryptoTypeKey: sc.MetaCryptoType(),
DiscoveredDate: occu.NowPtr(), DiscoveredDate: occu.NowPtr(),
} }
break 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 break
} }
if m.Match(info, j, csm.NewPacket(buf, rn)) { if m.Match(info, j, csm.NewPacket(buf, rn)) {
if packetCount-1 == j { if packetCount-1 == j {
s = &ocmd.Service{ s = &ocmd.Service{
ServiceName: m.Name(), Name: m.Name(),
CryptoType: occc.ToCryptoType(sc.CryptoType()), MetaCryptoTypeKey: sc.MetaCryptoType(),
DiscoveredDate: occu.NowPtr(), DiscoveredDate: occu.NowPtr(),
} }
break break
} }
// logging.Logger().Debugf("Discovery: Service scan receive match length[%d], buf[%v]", rn, buf) // 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 continue
} else { } else {
// logging.Logger().Debugf("Discovery: Service scan receive not match length[%d], buf[%v]", rn, buf) // 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 break
} }
} }

View File

@ -18,7 +18,7 @@ func scanServiceUDP(port *ocmd.Port, ds *ocmd.DiscoverService, resultChan chan i
} }
ms := matcher.GetUDPMatchers() ms := matcher.GetUDPMatchers()
mi := csm.NewMatchInfo(port.Host.IPV4, portNumber) mi := csm.NewMatchInfo(port.Host.Address, portNumber)
for i := 0; i < len(ms); i++ { for i := 0; i < len(ms); i++ {
m := 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) { if m.Match(mi, 0, p) {
s := &ocmd.Service{ s := &ocmd.Service{
ServiceName: m.Name(), Name: m.Name(),
Port: port, Port: port,
DiscoveredDate: occu.NowPtr(), DiscoveredDate: occu.NowPtr(),
} }

View File

@ -2,9 +2,9 @@ package discoverer
import ( import (
"fmt" "fmt"
"net"
ocmd "git.loafle.net/overflow/commons-go/model/discovery" 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/ipv4"
"git.loafle.net/overflow/container_discovery/internal/discoverer/ipv6" "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{}{} doneChan <- struct{}{}
}() }()
_, ipNet, err := net.ParseCIDR(host.Zone.Network) switch ocmm.ToMetaIPTypeEnum(host.MetaIPType) {
if nil != err { case ocmm.MetaIPTypeEnumV4:
errChan <- err
return
}
switch len(ipNet.IP) {
case net.IPv4len:
ipv4.ScanPort(host, dp, resultChan, errChan, stopChan) ipv4.ScanPort(host, dp, resultChan, errChan, stopChan)
case net.IPv6len: case ocmm.MetaIPTypeEnumV6:
ipv6.ScanPort(host, dp, resultChan, errChan, stopChan) ipv6.ScanPort(host, dp, resultChan, errChan, stopChan)
default: default:
errChan <- fmt.Errorf("Discovery: Not supported ip length") errChan <- fmt.Errorf("Discovery: Not supported MetaIPType")
return
} }
} }

View File

@ -2,9 +2,9 @@ package discoverer
import ( import (
"fmt" "fmt"
"net"
ocmd "git.loafle.net/overflow/commons-go/model/discovery" 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/ipv4"
"git.loafle.net/overflow/container_discovery/internal/discoverer/ipv6" "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{}{} doneChan <- struct{}{}
}() }()
_, ipNet, err := net.ParseCIDR(port.Host.Zone.Network) switch ocmm.ToMetaIPTypeEnum(port.Host.MetaIPType) {
if nil != err { case ocmm.MetaIPTypeEnumV4:
errChan <- err
return
}
switch len(ipNet.IP) {
case net.IPv4len:
ipv4.ScanService(port, ds, resultChan, errChan, stopChan) ipv4.ScanService(port, ds, resultChan, errChan, stopChan)
case net.IPv6len: case ocmm.MetaIPTypeEnumV6:
ipv6.ScanService(port, ds, resultChan, errChan, stopChan) ipv6.ScanService(port, ds, resultChan, errChan, stopChan)
default: default:
errChan <- fmt.Errorf("Discovery: Not supported ip length") errChan <- fmt.Errorf("Discovery: Not supported MetaIPType")
return
} }
} }

View File

@ -3,10 +3,11 @@ package discoverer
import ( import (
"net" "net"
"regexp" "regexp"
"strings"
cun "git.loafle.net/commons/util-go/net"
occu "git.loafle.net/overflow/commons-go/core/util" occu "git.loafle.net/overflow/commons-go/core/util"
ocmd "git.loafle.net/overflow/commons-go/model/discovery" 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{}) { 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 err error
var ifaces []net.Interface var ifaces []net.Interface
var addrs []net.Addr var addrs []net.Addr
var ipnet *net.IPNet var ipnet *cun.IPNet
var zones []*net.IPNet var zones []*cun.IPNet
// var gwIP net.IP // var gwIP net.IP
// var gwIFace string // var gwIFace string
@ -32,7 +33,7 @@ func scanZone(dz *ocmd.DiscoverZone, resultChan chan interface{}, errChan chan e
return return
} }
zones = make([]*net.IPNet, 0) zones = make([]*cun.IPNet, 0)
for _, i := range ifaces { for _, i := range ifaces {
@ -43,7 +44,7 @@ func scanZone(dz *ocmd.DiscoverZone, resultChan chan interface{}, errChan chan e
for _, addr := range addrs { for _, addr := range addrs {
if _, ipnet, err = net.ParseCIDR(addr.String()); nil != err { if _, ipnet, err = cun.ParseCIDR(addr.String()); nil != err {
errChan <- err errChan <- err
continue continue
} }
@ -57,10 +58,17 @@ func scanZone(dz *ocmd.DiscoverZone, resultChan chan interface{}, errChan chan e
Network: ipnet.String(), Network: ipnet.String(),
Iface: i.Name, Iface: i.Name,
Mac: i.HardwareAddr.String(), Mac: i.HardwareAddr.String(),
IPV4: strings.Split(addr.String(), "/")[0], Address: addr.String(),
DiscoveredDate: occu.NowPtr(), DiscoveredDate: occu.NowPtr(),
} }
switch ipnet.Version() {
case 6:
z.MetaIPType = ocmm.ToMetaIPType(ocmm.MetaIPTypeEnumV6)
default:
z.MetaIPType = ocmm.ToMetaIPType(ocmm.MetaIPTypeEnumV4)
}
resultChan <- z resultChan <- z
} }
} }
@ -80,7 +88,7 @@ func checkExclude(ep []string, iface string) bool {
return false return false
} }
func checkSameZone(zones []*net.IPNet, ipnet *net.IPNet) bool { func checkSameZone(zones []*cun.IPNet, ipnet *cun.IPNet) bool {
for _, i := range zones { for _, i := range zones {
if i.Contains(ipnet.IP) { if i.Contains(ipnet.IP) {
return true return true

View File

@ -86,13 +86,17 @@ func (s *DiscoveryService) DiscoverService(requesterID string, port *ocmd.Port,
} }
func (s *DiscoveryService) StopDiscovery(requesterID string) error { func (s *DiscoveryService) StopDiscovery(requesterID string) error {
logging.Logger().Debugf("Request DiscoveryService.StopDiscovery")
_stopChan, ok := s.pendingDiscovery.Load(requesterID) _stopChan, ok := s.pendingDiscovery.Load(requesterID)
if !ok { if !ok {
return fmt.Errorf("discovery request for [%s] is not exist", requesterID) return fmt.Errorf("discovery request for [%s] is not exist", requesterID)
} }
stopChan := _stopChan.(chan struct{}) stopChan := _stopChan.(chan struct{})
close(stopChan) stopChan <- struct{}{}
logging.Logger().Debugf("Close DiscoveryService.StopDiscovery")
return nil return nil
} }
@ -152,8 +156,8 @@ func (s *DiscoveryService) handleDiscovery(requesterID string, discoveryFunc fun
} }
data.Release() data.Release()
case <-stopChan: case <-stopChan:
logging.Logger().Debugf("DiscoveryService.StopDiscovery")
s.discoverer.Stop() s.discoverer.Stop()
return nil
} }
} }
} }