Skip to content
Permalink
Browse files

misc: small fix or general refactoring i did not bother commenting

  • Loading branch information
evilsocket committed Apr 22, 2019
1 parent 2bd768f commit b868408c17a5d6fe1d2a456d1570c2e894e26fce
Showing with 31 additions and 41 deletions.
  1. +31 −41 modules/net_probe/net_probe_mdns.go
@@ -5,8 +5,6 @@ import (
"io/ioutil"
"log"
"net"
"sync"
"time"

"github.com/bettercap/bettercap/packets"

@@ -38,58 +36,50 @@ func (mod *Prober) sendProbeMDNS(from net.IP, from_hw net.HardwareAddr) {
}
}

func (mod *Prober) mdnsProber() {
mod.waitGroup.Add(1)
defer mod.waitGroup.Done()
func (mod *Prober) mdnsListener(c chan *mdns.ServiceEntry) {
mod.Debug("mdns listener started")
defer mod.Debug("mdns listener stopped")

mod.Debug("mdns prober started")
defer mod.Debug("mdns.prober stopped")
for entry := range c {
if host := mod.Session.Lan.GetByIp(entry.AddrV4.String()); host != nil {
meta := make(map[string]string)

log.SetOutput(ioutil.Discard)
meta["mdns:name"] = entry.Name
meta["mdns:hostname"] = entry.Host
meta["mdns:ipv4"] = entry.AddrV4.String()

ch := make(chan *mdns.ServiceEntry)
wait := sync.WaitGroup{}
if entry.AddrV6 != nil {
meta["mdns:ipv6"] = entry.AddrV6.String()
}

defer close(ch)
meta["mdns:port"] = fmt.Sprintf("%d", entry.Port)

go func(c chan *mdns.ServiceEntry) {
mod.Debug("mdns channel read started")
defer mod.Debug("mdns channel read stopped")
host.OnMeta(meta)
} else {
mod.Debug("got mdns entry for known ip %s", entry.AddrV4)
}
}
}

for entry := range c {
if host := mod.Session.Lan.GetByIp(entry.AddrV4.String()); host != nil {
meta := make(map[string]string)
func (mod *Prober) mdnsProber() {
mod.Debug("mdns prober started")
defer mod.Debug("mdns.prober stopped")

meta["mdns:name"] = entry.Name
meta["mdns:hostname"] = entry.Host
meta["mdns:ipv4"] = entry.AddrV4.String()
mod.waitGroup.Add(1)
defer mod.waitGroup.Done()

if entry.AddrV6 != nil {
meta["mdns:ipv6"] = entry.AddrV6.String()
}
log.SetOutput(ioutil.Discard)

meta["mdns:port"] = fmt.Sprintf("%d", entry.Port)
ch := make(chan *mdns.ServiceEntry)
defer close(ch)

host.OnMeta(meta)
} else {
mod.Debug("got mdns entry for known ip %s", entry.AddrV4)
}
}
}(ch)
go mod.mdnsListener(ch)

for mod.Running() {
for _, svc := range services {
go func(svc string, w *sync.WaitGroup) {
w.Add(1)
defer w.Done()

params := mdns.DefaultParams(svc)
params.Entries = ch
params.Timeout = time.Duration(5) * time.Second

mdns.Query(params)
}(svc, &wait)
if mod.Running() {
mdns.Lookup(svc, ch)
}
}
wait.Wait()
}
}

0 comments on commit b868408

Please sign in to comment.
You can’t perform that action at this time.