2018-08-29 12:04:23 +00:00
|
|
|
package mdns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
omd "git.loafle.net/overflow/model/discovery"
|
|
|
|
omm "git.loafle.net/overflow/model/meta"
|
|
|
|
omu "git.loafle.net/overflow/model/util"
|
|
|
|
"git.loafle.net/overflow_scanner/probe/discovery/session"
|
|
|
|
"github.com/grandcat/zeroconf"
|
|
|
|
)
|
|
|
|
|
2018-08-31 05:15:46 +00:00
|
|
|
func Scan(discoverySession session.DiscoverySession) error {
|
2018-09-03 10:33:20 +00:00
|
|
|
|
2018-08-29 12:04:23 +00:00
|
|
|
serviceEntries, err := browse("_services._dns-sd._udp", "local")
|
|
|
|
if nil != err {
|
|
|
|
log.Print("Cannot find service ", err)
|
2018-08-31 05:15:46 +00:00
|
|
|
return nil
|
2018-08-29 12:04:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
metaIPTypeEnum := omm.ToMetaIPTypeEnum(discoverySession.Zone().MetaIPType)
|
|
|
|
|
2018-08-31 05:15:46 +00:00
|
|
|
SERVICE_LOOP:
|
2018-08-29 12:04:23 +00:00
|
|
|
for _, serviceEntry := range serviceEntries {
|
|
|
|
name := removeDomainName(serviceEntry.Instance, serviceEntry.Domain)
|
|
|
|
entries, _err := browse(name, serviceEntry.Domain)
|
|
|
|
if nil != _err {
|
|
|
|
log.Print("Cannot find entry ", _err)
|
2018-08-31 05:15:46 +00:00
|
|
|
continue SERVICE_LOOP
|
2018-08-29 12:04:23 +00:00
|
|
|
}
|
|
|
|
|
2018-08-31 05:15:46 +00:00
|
|
|
ENTRY_LOOP:
|
2018-08-29 12:04:23 +00:00
|
|
|
for _, entry := range entries {
|
2018-09-01 13:09:33 +00:00
|
|
|
// log.Print("serviceEntry ", entry)
|
2018-08-29 12:04:23 +00:00
|
|
|
|
2018-09-12 06:06:50 +00:00
|
|
|
// name := entry.Instance // HP\ LaserJet\ P1505n
|
2018-08-29 12:04:23 +00:00
|
|
|
service := entry.Service // _pdl-datastream._tcp
|
|
|
|
port := entry.Port // 9100
|
|
|
|
meta := toMeta(entry.Text)
|
|
|
|
hostName := removeDomainName(entry.HostName, entry.Domain) // NPIFACA9B
|
|
|
|
serviceName, portType, cryptoType := parseService(service)
|
|
|
|
|
|
|
|
var metaPortType *omm.MetaPortType
|
|
|
|
switch portType {
|
|
|
|
case "tcp":
|
|
|
|
metaPortType = omm.ToMetaPortType(omm.MetaPortTypeEnumTCP)
|
|
|
|
case "udp":
|
|
|
|
metaPortType = omm.ToMetaPortType(omm.MetaPortTypeEnumUDP)
|
|
|
|
}
|
|
|
|
|
|
|
|
metaCryptoType := omm.ToMetaCryptoType(omm.MetaCryptoTypeEnumUNKNOWN)
|
|
|
|
switch cryptoType {
|
|
|
|
case "NONE":
|
|
|
|
metaCryptoType = omm.ToMetaCryptoType(omm.MetaCryptoTypeEnumNONE)
|
|
|
|
case "TLS":
|
|
|
|
metaCryptoType = omm.ToMetaCryptoType(omm.MetaCryptoTypeEnumTLS)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch metaIPTypeEnum {
|
|
|
|
case omm.MetaIPTypeEnumV4:
|
|
|
|
for _, ipv4 := range entry.AddrIPv4 {
|
2018-09-12 04:55:51 +00:00
|
|
|
h := omd.NewHost(
|
|
|
|
discoverySession.Zone(),
|
|
|
|
omm.ToMetaIPType(metaIPTypeEnum),
|
|
|
|
ipv4.String(),
|
|
|
|
)
|
|
|
|
h.Name = hostName
|
|
|
|
h.DiscoveredDate = omu.NowPtr()
|
|
|
|
|
|
|
|
h = discoverySession.AddHost(
|
2018-09-11 07:59:21 +00:00
|
|
|
"mDNS",
|
2018-09-12 04:55:51 +00:00
|
|
|
h,
|
2018-09-11 07:59:21 +00:00
|
|
|
meta,
|
|
|
|
)
|
2018-08-29 12:04:23 +00:00
|
|
|
|
|
|
|
if 1 > port {
|
2018-08-31 05:15:46 +00:00
|
|
|
continue ENTRY_LOOP
|
2018-08-29 12:04:23 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 04:55:51 +00:00
|
|
|
p := omd.NewPort(
|
|
|
|
h,
|
|
|
|
metaPortType,
|
|
|
|
port,
|
|
|
|
)
|
|
|
|
p.DiscoveredDate = omu.NowPtr()
|
|
|
|
|
|
|
|
p = discoverySession.AddPort(
|
2018-09-11 07:59:21 +00:00
|
|
|
"mDNS",
|
2018-09-12 04:55:51 +00:00
|
|
|
p,
|
2018-09-11 07:59:21 +00:00
|
|
|
meta,
|
|
|
|
)
|
|
|
|
|
2018-09-12 04:55:51 +00:00
|
|
|
s := omd.NewService(
|
|
|
|
p,
|
|
|
|
metaCryptoType,
|
2018-09-12 06:06:15 +00:00
|
|
|
omm.MetaServiceTypeEnumUNKNOWN.String(),
|
2018-09-12 04:55:51 +00:00
|
|
|
)
|
2018-09-12 06:06:15 +00:00
|
|
|
s.Name = serviceName
|
2018-09-12 04:55:51 +00:00
|
|
|
s.DiscoveredDate = omu.NowPtr()
|
|
|
|
|
2018-09-11 07:59:21 +00:00
|
|
|
discoverySession.AddService(
|
|
|
|
"mDNS",
|
2018-09-12 04:55:51 +00:00
|
|
|
s,
|
2018-09-11 07:59:21 +00:00
|
|
|
meta,
|
|
|
|
)
|
2018-08-29 12:04:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case omm.MetaIPTypeEnumV6:
|
|
|
|
for _, ipv6 := range entry.AddrIPv6 {
|
2018-09-12 04:55:51 +00:00
|
|
|
h := omd.NewHost(
|
|
|
|
discoverySession.Zone(),
|
|
|
|
omm.ToMetaIPType(metaIPTypeEnum),
|
|
|
|
ipv6.String(),
|
|
|
|
)
|
|
|
|
h.Name = hostName
|
|
|
|
h.DiscoveredDate = omu.NowPtr()
|
|
|
|
|
|
|
|
h = discoverySession.AddHost(
|
2018-09-11 07:59:21 +00:00
|
|
|
"mDNS",
|
2018-09-12 04:55:51 +00:00
|
|
|
h,
|
2018-09-11 07:59:21 +00:00
|
|
|
meta,
|
|
|
|
)
|
2018-08-29 12:04:23 +00:00
|
|
|
|
|
|
|
if 1 > port {
|
2018-08-31 05:15:46 +00:00
|
|
|
continue ENTRY_LOOP
|
2018-08-29 12:04:23 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 04:55:51 +00:00
|
|
|
p := omd.NewPort(
|
|
|
|
h,
|
|
|
|
metaPortType,
|
|
|
|
port,
|
|
|
|
)
|
|
|
|
p.DiscoveredDate = omu.NowPtr()
|
|
|
|
|
|
|
|
p = discoverySession.AddPort(
|
2018-09-11 07:59:21 +00:00
|
|
|
"mDNS",
|
2018-09-12 04:55:51 +00:00
|
|
|
p,
|
2018-09-11 07:59:21 +00:00
|
|
|
meta,
|
|
|
|
)
|
|
|
|
|
2018-09-12 04:55:51 +00:00
|
|
|
s := omd.NewService(
|
|
|
|
p,
|
|
|
|
metaCryptoType,
|
2018-09-12 06:06:15 +00:00
|
|
|
omm.MetaServiceTypeEnumUNKNOWN.String(),
|
2018-09-12 04:55:51 +00:00
|
|
|
)
|
2018-09-12 06:06:15 +00:00
|
|
|
s.Name = serviceName
|
2018-09-12 04:55:51 +00:00
|
|
|
s.DiscoveredDate = omu.NowPtr()
|
|
|
|
|
2018-09-11 07:59:21 +00:00
|
|
|
discoverySession.AddService(
|
|
|
|
"mDNS",
|
2018-09-12 04:55:51 +00:00
|
|
|
s,
|
2018-09-11 07:59:21 +00:00
|
|
|
meta,
|
|
|
|
)
|
2018-08-29 12:04:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-09-03 10:33:20 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-discoverySession.StopChan():
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
}
|
2018-08-29 12:04:23 +00:00
|
|
|
}
|
2018-08-31 05:15:46 +00:00
|
|
|
|
|
|
|
return nil
|
2018-08-29 12:04:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func removeDomainName(instance string, domain string) (service string) {
|
|
|
|
_instance := strings.TrimRight(instance, ".")
|
|
|
|
return strings.TrimRight(_instance, fmt.Sprintf(".%s", domain))
|
|
|
|
}
|
|
|
|
|
|
|
|
func toMeta(text []string) map[string]string {
|
|
|
|
if nil == text || 0 == len(text) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
meta := make(map[string]string)
|
|
|
|
for _, v := range text {
|
|
|
|
ss := strings.SplitN(v, "=", 2)
|
|
|
|
if 2 != len(ss) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if nil != ss {
|
|
|
|
meta[ss[0]] = ss[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return meta
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseService(service string) (name string, portType string, cryptoType string) {
|
|
|
|
ss := strings.SplitN(service, ".", 2)
|
|
|
|
if nil == ss {
|
|
|
|
return "UNKNOWN", "UNKNOWN", "UNKNOWN"
|
|
|
|
}
|
|
|
|
|
|
|
|
_name := strings.TrimLeft(ss[0], "_")
|
|
|
|
_portType := strings.TrimLeft(ss[1], "_")
|
|
|
|
|
|
|
|
name = _name
|
|
|
|
portType = _portType
|
|
|
|
cryptoType = "NONE"
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func browse(service string, domain string) ([]*zeroconf.ServiceEntry, error) {
|
|
|
|
resolver, err := zeroconf.NewResolver(nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to initialize resolver %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
entryChan := make(chan *zeroconf.ServiceEntry)
|
|
|
|
timerStopped := make(chan struct{})
|
|
|
|
|
|
|
|
serviceEntries := make([]*zeroconf.ServiceEntry, 0)
|
|
|
|
|
2018-09-01 06:07:15 +00:00
|
|
|
go func(entryChan <-chan *zeroconf.ServiceEntry) {
|
2018-08-29 12:04:23 +00:00
|
|
|
var delay atomic.Value
|
|
|
|
delay.Store(false)
|
|
|
|
ticker := time.NewTicker(time.Second * 1)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
2018-09-01 06:07:15 +00:00
|
|
|
case entry, ok := <-entryChan:
|
2018-08-29 12:04:23 +00:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
delay.Store(true)
|
|
|
|
serviceEntries = append(serviceEntries, entry)
|
|
|
|
case <-ticker.C:
|
|
|
|
if false == delay.Load().(bool) {
|
|
|
|
ticker.Stop()
|
|
|
|
timerStopped <- struct{}{}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
delay.Store(false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}(entryChan)
|
|
|
|
|
2018-09-01 06:07:15 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(3))
|
2018-08-29 12:04:23 +00:00
|
|
|
defer cancel()
|
|
|
|
err = resolver.Browse(ctx, service, domain, entryChan)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to browse %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
case <-timerStopped:
|
|
|
|
}
|
|
|
|
|
|
|
|
return serviceEntries, nil
|
|
|
|
}
|