Improve HomeKit source start time
This commit is contained in:
+16
-8
@@ -7,6 +7,13 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/AlexxIT/go2rtc/pkg/core"
|
"github.com/AlexxIT/go2rtc/pkg/core"
|
||||||
"github.com/AlexxIT/go2rtc/pkg/mdns"
|
"github.com/AlexxIT/go2rtc/pkg/mdns"
|
||||||
"github.com/brutella/hap"
|
"github.com/brutella/hap"
|
||||||
@@ -16,12 +23,6 @@ import (
|
|||||||
"github.com/brutella/hap/hkdf"
|
"github.com/brutella/hap/hkdf"
|
||||||
"github.com/brutella/hap/tlv8"
|
"github.com/brutella/hap/tlv8"
|
||||||
"github.com/tadglines/go-pkgs/crypto/srp"
|
"github.com/tadglines/go-pkgs/crypto/srp"
|
||||||
"io"
|
|
||||||
"net"
|
|
||||||
"net/http"
|
|
||||||
"net/url"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Conn for HomeKit. DevicePublic can be null.
|
// Conn for HomeKit. DevicePublic can be null.
|
||||||
@@ -105,9 +106,16 @@ func (c *Conn) DialAndServe() error {
|
|||||||
return c.Handle()
|
return c.Handle()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Conn) DeviceHost() string {
|
||||||
|
if i := strings.IndexByte(c.DeviceAddress, ':'); i > 0 {
|
||||||
|
return c.DeviceAddress[:i]
|
||||||
|
}
|
||||||
|
return c.DeviceAddress
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Conn) Dial() error {
|
func (c *Conn) Dial() error {
|
||||||
// update device host before dial
|
// update device address (host and/or port) before dial
|
||||||
_ = mdns.Discovery(mdns.ServiceHAP, func(entry *mdns.ServiceEntry) bool {
|
_ = mdns.QueryOrDiscovery(c.DeviceHost(), mdns.ServiceHAP, func(entry *mdns.ServiceEntry) bool {
|
||||||
if entry.Complete() && entry.Info["id"] == c.DeviceID {
|
if entry.Complete() && entry.Info["id"] == c.DeviceID {
|
||||||
c.DeviceAddress = entry.Addr()
|
c.DeviceAddress = entry.Addr()
|
||||||
return true
|
return true
|
||||||
|
|||||||
@@ -106,6 +106,8 @@ func Query(host, service string) (entry *ServiceEntry, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// QueryOrDiscovery - useful if we know previous device host and want
|
||||||
|
// to update port or any other information. Will work even over VPN.
|
||||||
func QueryOrDiscovery(host, service string, onentry func(*ServiceEntry) bool) error {
|
func QueryOrDiscovery(host, service string, onentry func(*ServiceEntry) bool) error {
|
||||||
entry, _ := Query(host, service)
|
entry, _ := Query(host, service)
|
||||||
if entry != nil && onentry(entry) {
|
if entry != nil && onentry(entry) {
|
||||||
|
|||||||
Reference in New Issue
Block a user