mirror of
https://github.com/bettercap/bettercap
synced 2025-08-20 05:23:19 -07:00
external resolver
This commit is contained in:
parent
e656a6cbfa
commit
b0a197b377
15 changed files with 1768 additions and 70 deletions
|
@ -3,10 +3,60 @@ package zerogod
|
|||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
|
||||
"github.com/bettercap/bettercap/v2/zeroconf"
|
||||
"github.com/evilsocket/islazy/str"
|
||||
yaml "gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type ServiceData struct {
|
||||
Name string `yaml:"name"` // Instance name (e.g. "My web page")
|
||||
Service string `yaml:"service"` // Service name (e.g. _http._tcp.)
|
||||
Domain string `yaml:"domain"` // If blank, assumes "local"
|
||||
Port int `yaml:"port"` // Service port
|
||||
Records []string `yaml:"records,omitempty"` // Service DNS text records
|
||||
Responder string `yaml:"responder,omitempty"` // Optional IP to use instead of our tcp acceptor
|
||||
}
|
||||
|
||||
func (svc ServiceData) FullName() string {
|
||||
return fmt.Sprintf("%s.%s.%s",
|
||||
strings.Trim(svc.Name, "."),
|
||||
strings.Trim(svc.Service, "."),
|
||||
strings.Trim(svc.Domain, "."))
|
||||
}
|
||||
|
||||
func (svc ServiceData) Unregister() error {
|
||||
if server, err := zeroconf.Register(svc.Name, svc.Service, svc.Domain, svc.Port, svc.Records, nil); err != nil {
|
||||
return err
|
||||
} else {
|
||||
server.Shutdown()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func svcEntriesToData(services map[string]*zeroconf.ServiceEntry) []ServiceData {
|
||||
data := make([]ServiceData, 0)
|
||||
for _, svc := range services {
|
||||
// filter out empty DNS records
|
||||
records := ([]string)(nil)
|
||||
for _, txt := range svc.Text {
|
||||
if txt = str.Trim(txt); len(txt) > 0 {
|
||||
records = append(records, txt)
|
||||
}
|
||||
}
|
||||
|
||||
data = append(data, ServiceData{
|
||||
Name: svc.Instance,
|
||||
Service: svc.Service,
|
||||
Domain: svc.Domain,
|
||||
Port: svc.Port,
|
||||
Records: records,
|
||||
})
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
func (mod *ZeroGod) save(address, filename string) error {
|
||||
if address == "" {
|
||||
return fmt.Errorf("address cannot be empty")
|
||||
|
@ -16,7 +66,8 @@ func (mod *ZeroGod) save(address, filename string) error {
|
|||
}
|
||||
|
||||
if ipServices, found := mod.mapping[address]; found {
|
||||
data, err := yaml.Marshal(ipServices)
|
||||
services := svcEntriesToData(ipServices)
|
||||
data, err := yaml.Marshal(services)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -26,7 +77,7 @@ func (mod *ZeroGod) save(address, filename string) error {
|
|||
return err
|
||||
}
|
||||
|
||||
mod.Info("mDNS information saved to %s", filename)
|
||||
mod.Info("zeroconf information saved to %s", filename)
|
||||
} else {
|
||||
return fmt.Errorf("no mDNS information found for address %s", address)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue