diff --git a/go.mod b/go.mod index beae7f79..75528133 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/brutella/hap v0.0.17 github.com/deepch/vdk v0.0.19 github.com/gorilla/websocket v1.5.0 - github.com/hashicorp/mdns v1.0.5 + github.com/miekg/dns v1.1.52 github.com/pion/ice/v2 v2.3.1 github.com/pion/interceptor v0.1.12 github.com/pion/rtcp v1.2.10 @@ -31,7 +31,6 @@ require ( github.com/kr/pretty v0.2.1 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.17 // indirect - github.com/miekg/dns v1.1.52 // indirect github.com/pion/datachannel v1.5.5 // indirect github.com/pion/dtls/v2 v2.2.6 // indirect github.com/pion/logging v0.2.2 // indirect @@ -52,8 +51,6 @@ require ( ) replace ( - // windows support: https://github.com/brutella/dnssd/pull/35 - github.com/brutella/dnssd v1.2.2 => github.com/rblenkinsopp/dnssd v1.2.3-0.20220516082132-0923f3c787a1 // RTP tlv8 fix github.com/brutella/hap v0.0.17 => github.com/AlexxIT/hap v0.0.15-0.20221108133010-d8a45b7a7045 // fix reading AAC config bytes diff --git a/go.sum b/go.sum index 20a2e4ee..9f9bebe4 100644 --- a/go.sum +++ b/go.sum @@ -32,8 +32,6 @@ github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= -github.com/hashicorp/mdns v1.0.5 h1:1M5hW1cunYeoXOqHwEb/GBDDHAFo0Yqb/uz/beC6LbE= -github.com/hashicorp/mdns v1.0.5/go.mod h1:mtBihi+LeNXGtG8L9dX59gAEa12BDtBQSp4v/YAJqrc= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= @@ -48,7 +46,6 @@ github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27k github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng= github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= -github.com/miekg/dns v1.1.41/go.mod h1:p6aan82bvRIyn+zDIv9xYNUpwa73JcSh9BKwknJysuI= github.com/miekg/dns v1.1.50/go.mod h1:e3IlAVfNqAllflbibAZEWOXOQ+Ynzk/dDozDxY7XnME= github.com/miekg/dns v1.1.52 h1:Bmlc/qsNNULOe6bpXcUTsuOajd0DzRHwup6D9k1An0c= github.com/miekg/dns v1.1.52/go.mod h1:uInx36IzPl7FYnDcMeVWxj9byh7DutNykX4G9Sj60FY= @@ -148,7 +145,6 @@ golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/ golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= -golang.org/x/net v0.0.0-20210410081132-afb366fc7cd1/go.mod h1:9tjilg8BloeKEkVJvy7fQ90B1CfIiPueXVOjqfkSzI8= golang.org/x/net v0.0.0-20210428140749-89ef3d95e781/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk= golang.org/x/net v0.0.0-20210726213435-c6fcb2dbf985/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= @@ -175,7 +171,6 @@ golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210303074136-134d130e1a04/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/internal/homekit/api.go b/internal/homekit/api.go index 7a83ee2f..8e62d154 100644 --- a/internal/homekit/api.go +++ b/internal/homekit/api.go @@ -1,12 +1,11 @@ package homekit import ( - "fmt" "github.com/AlexxIT/go2rtc/internal/api" "github.com/AlexxIT/go2rtc/internal/app/store" "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/pkg/hap" - "github.com/AlexxIT/go2rtc/pkg/hap/mdns" + "github.com/AlexxIT/go2rtc/pkg/mdns" "net/http" "net/url" "strings" @@ -32,26 +31,23 @@ func apiHandler(w http.ResponseWriter, r *http.Request) { } } - for info := range mdns.GetAll() { - if !strings.HasSuffix(info.Name, mdns.Suffix) { - continue - } - name := info.Name[:len(info.Name)-len(mdns.Suffix)] - device := Device{ - Name: strings.ReplaceAll(name, "\\", ""), - Addr: fmt.Sprintf("%s:%d", info.AddrV4, info.Port), - } - for _, field := range info.InfoFields { - switch field[:2] { - case "id": - device.ID = field[3:] - case "md": - device.Model = field[3:] - case "sf": - device.Paired = field[3] == '0' + err := mdns.Discovery(mdns.ServiceHAP, func(entry *mdns.ServiceEntry) bool { + if entry.Complete() { + device := Device{ + Name: entry.Name, + Addr: entry.Addr(), + ID: entry.Info["id"], + Model: entry.Info["md"], + Paired: entry.Info["sf"] == "0", } + items = append(items, device) } - items = append(items, device) + return false + }) + + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return } api.ResponseJSON(w, items) diff --git a/pkg/hap/conn.go b/pkg/hap/conn.go index 48fb50b8..a35afe9e 100644 --- a/pkg/hap/conn.go +++ b/pkg/hap/conn.go @@ -8,7 +8,7 @@ import ( "errors" "fmt" "github.com/AlexxIT/go2rtc/pkg/core" - "github.com/AlexxIT/go2rtc/pkg/hap/mdns" + "github.com/AlexxIT/go2rtc/pkg/mdns" "github.com/brutella/hap" "github.com/brutella/hap/chacha20poly1305" "github.com/brutella/hap/curve25519" @@ -61,28 +61,29 @@ func NewConn(rawURL string) (*Conn, error) { } func Pair(deviceID, pin string) (*Conn, error) { - entry := mdns.GetEntry(deviceID) - if entry == nil { + var addr string + var mfi bool + + _ = mdns.Discovery(mdns.ServiceHAP, func(entry *mdns.ServiceEntry) bool { + if entry.Complete() && entry.Info["id"] == deviceID { + addr = entry.Addr() + mfi = entry.Info["ff"] == "1" + return true + } + return false + }) + + if addr == "" { return nil, errors.New("can't find device via mDNS") } c := &Conn{ - DeviceAddress: fmt.Sprintf("%s:%d", entry.AddrV4.String(), entry.Port), + DeviceAddress: addr, DeviceID: deviceID, ClientID: GenerateUUID(), ClientPrivate: GenerateKey(), } - var mfi bool - for _, field := range entry.InfoFields { - if field[:2] == "ff" { - if field[3] == '1' { - mfi = true - } - break - } - } - return c, c.Pair(mfi, pin) } @@ -106,9 +107,13 @@ func (c *Conn) DialAndServe() error { func (c *Conn) Dial() error { // update device host before dial - if host := mdns.GetAddress(c.DeviceID); host != "" { - c.DeviceAddress = host - } + _ = mdns.Discovery(mdns.ServiceHAP, func(entry *mdns.ServiceEntry) bool { + if entry.Complete() && entry.Info["id"] == c.DeviceID { + c.DeviceAddress = entry.Addr() + return true + } + return false + }) var err error c.conn, err = net.DialTimeout("tcp", c.DeviceAddress, time.Second*5) diff --git a/pkg/hap/mdns/client.go b/pkg/hap/mdns/client.go deleted file mode 100644 index c5befa45..00000000 --- a/pkg/hap/mdns/client.go +++ /dev/null @@ -1,42 +0,0 @@ -package mdns - -import ( - "fmt" - "github.com/hashicorp/mdns" - "strings" -) - -const Suffix = "._hap._tcp.local." - -func GetAll() chan *mdns.ServiceEntry { - entries := make(chan *mdns.ServiceEntry) - params := &mdns.QueryParam{ - Service: "_hap._tcp", Entries: entries, DisableIPv6: true, - } - - go func() { - _ = mdns.Query(params) - close(entries) - }() - - return entries -} - -func GetAddress(deviceID string) string { - for entry := range GetAll() { - if strings.Contains(entry.Info, deviceID) { - return fmt.Sprintf("%s:%d", entry.AddrV4.String(), entry.Port) - } - } - - return "" -} - -func GetEntry(deviceID string) *mdns.ServiceEntry { - for entry := range GetAll() { - if strings.Contains(entry.Info, deviceID) { - return entry - } - } - return nil -} diff --git a/pkg/hap/mdns/server.go b/pkg/hap/mdns/server.go deleted file mode 100644 index ec78390a..00000000 --- a/pkg/hap/mdns/server.go +++ /dev/null @@ -1,53 +0,0 @@ -package mdns - -import ( - "github.com/hashicorp/mdns" - "net" -) - -const HostHeaderTail = "._hap._tcp.local" - -func NewServer(name string, port int, ips []net.IP, txt []string) (*mdns.Server, error) { - if ips == nil || ips[0] == nil { - ips = LocalIPs() - } - - // important to set hostName manually with any value and `.local.` tail - // important to set ips manually - service, _ := mdns.NewMDNSService( - name, "_hap._tcp", "", name+".local.", port, ips, txt, - ) - - return mdns.NewServer(&mdns.Config{Zone: service}) -} - -func LocalIPs() []net.IP { - ifaces, err := net.Interfaces() - if err != nil { - return nil - } - - var ips []net.IP - for _, iface := range ifaces { - if iface.Flags&net.FlagUp == 0 { - continue // interface down - } - if iface.Flags&net.FlagLoopback != 0 { - continue // loopback interface - } - - var addrs []net.Addr - if addrs, err = iface.Addrs(); err != nil { - continue - } - for _, addr := range addrs { - switch addr := addr.(type) { - case *net.IPNet: - ips = append(ips, addr.IP) - case *net.IPAddr: - ips = append(ips, addr.IP) - } - } - } - return ips -} diff --git a/pkg/mdns/mdns.go b/pkg/mdns/mdns.go new file mode 100644 index 00000000..e1a93925 --- /dev/null +++ b/pkg/mdns/mdns.go @@ -0,0 +1,139 @@ +package mdns + +import ( + "fmt" + "github.com/miekg/dns" + "net" + "strings" + "time" +) + +const ServiceHAP = "_hap._tcp.local." // HomeKit Accessory Protocol + +const requestTimeout = time.Millisecond * 505 +const responseTimeout = time.Second * 2 + +type ServiceEntry struct { + Name string + IP net.IP + Port uint16 + Info map[string]string +} + +func (e *ServiceEntry) Complete() bool { + return e.IP != nil && e.Port > 0 && e.Info != nil +} + +func (e *ServiceEntry) Addr() string { + return fmt.Sprintf("%s:%d", e.IP, e.Port) +} + +func Discovery(service string, onentry func(*ServiceEntry) bool) error { + addr := &net.UDPAddr{ + IP: net.IP{224, 0, 0, 251}, + Port: 5353, + } + + conn, err := net.ListenMulticastUDP("udp4", nil, addr) + if err != nil { + return err + } + + defer conn.Close() + + if err = conn.SetDeadline(time.Now().Add(responseTimeout)); err != nil { + return err + } + + msg := &dns.Msg{ + Question: []dns.Question{ + {service, dns.TypePTR, dns.ClassINET}, + }, + } + + b1, err := msg.Pack() + if err != nil { + return err + } + + go func() { + for { + if _, err := conn.WriteToUDP(b1, addr); err != nil { + return + } + time.Sleep(requestTimeout) + } + }() + + var skipIPs []net.IP + + b2 := make([]byte, 1500) +loop: + for { + n, addr, err := conn.ReadFromUDP(b2) + if err != nil { + break + } + + for _, ip := range skipIPs { + if ip.Equal(addr.IP) { + continue loop + } + } + + if err = msg.Unpack(b2[:n]); err != nil { + continue + } + + if !EqualService(msg, service) { + continue + } + + if entry := NewServiceEntry(msg); onentry(entry) { + break + } + + skipIPs = append(skipIPs, addr.IP) + } + + return nil +} + +func EqualService(msg *dns.Msg, service string) bool { + for _, rr := range msg.Answer { + if rr, ok := rr.(*dns.PTR); ok { + return strings.HasSuffix(rr.Ptr, service) + } + } + + return false +} + +func NewServiceEntry(msg *dns.Msg) *ServiceEntry { + entry := &ServiceEntry{} + + records := make([]dns.RR, 0, len(msg.Answer)+len(msg.Ns)+len(msg.Extra)) + records = append(records, msg.Answer...) + records = append(records, msg.Ns...) + records = append(records, msg.Extra...) + for _, record := range records { + switch record := record.(type) { + case *dns.PTR: + if i := strings.IndexByte(record.Ptr, '.'); i > 0 { + entry.Name = record.Ptr[:i] + } + case *dns.A: + entry.IP = record.A + case *dns.SRV: + entry.Port = record.Port + case *dns.TXT: + entry.Info = make(map[string]string, len(record.Txt)) + for _, txt := range record.Txt { + k, v, _ := strings.Cut(txt, "=") + entry.Info[k] = v + } + } + } + + return entry +} diff --git a/pkg/mdns/mdns_test.go b/pkg/mdns/mdns_test.go new file mode 100644 index 00000000..956c9807 --- /dev/null +++ b/pkg/mdns/mdns_test.go @@ -0,0 +1,16 @@ +package mdns + +import ( + "github.com/stretchr/testify/require" + "testing" +) + +func TestDiscovery(t *testing.T) { + onentry := func(entry *ServiceEntry) bool { + return true + } + err := Discovery(ServiceHAP, onentry) + //err := Discovery("_ewelink._tcp.local.", time.Second, onentry) + // err := Discovery("_googlecast._tcp.local.", time.Second, onentry) + require.Nil(t, err) +}