From b927c2751a3b003206f5ebb7e5208a83e78d5873 Mon Sep 17 00:00:00 2001 From: seydx Date: Sun, 16 Mar 2025 01:44:21 +0100 Subject: [PATCH] Remove HomeKit pairing when deleting a stream --- internal/homekit/api.go | 17 ++--------------- internal/homekit/homekit.go | 22 +--------------------- internal/streams/api.go | 8 ++++++++ internal/streams/helpers.go | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 36 deletions(-) diff --git a/internal/homekit/api.go b/internal/homekit/api.go index 9f76c2d6b..58fdec3fc 100644 --- a/internal/homekit/api.go +++ b/internal/homekit/api.go @@ -4,7 +4,6 @@ import ( "errors" "fmt" "net/http" - "net/url" "strings" "github.com/AlexxIT/go2rtc/internal/api" @@ -23,7 +22,7 @@ func apiHandler(w http.ResponseWriter, r *http.Request) { return } - urls := findHomeKitURLs() + urls := streams.FindPrefixURLs("homekit") for id, u := range urls { deviceID := u.Query().Get("device_id") for _, source := range sources { @@ -112,7 +111,7 @@ func apiUnpair(id string) error { return errors.New(api.StreamNotFound) } - rawURL := findHomeKitURL(stream.Sources()) + rawURL := streams.FindPrefixURL("homekit", stream.Sources()) if rawURL == "" { return errors.New("not homekit source") } @@ -125,15 +124,3 @@ func apiUnpair(id string) error { return app.PatchConfig([]string{"streams", id}, nil) } - -func findHomeKitURLs() map[string]*url.URL { - urls := map[string]*url.URL{} - for name, sources := range streams.GetAllSources() { - if rawURL := findHomeKitURL(sources); rawURL != "" { - if u, err := url.Parse(rawURL); err == nil { - urls[name] = u - } - } - } - return urls -} diff --git a/internal/homekit/homekit.go b/internal/homekit/homekit.go index b42372119..9dcb43ab8 100644 --- a/internal/homekit/homekit.go +++ b/internal/homekit/homekit.go @@ -79,7 +79,7 @@ func Init() { Handler: homekit.ServerHandler(srv), } - if url := findHomeKitURL(stream.Sources()); url != "" { + if url := streams.FindPrefixURL("homekit", stream.Sources()); url != "" { // 1. Act as transparent proxy for HomeKit camera dial := func() (net.Conn, error) { client, err := homekit.Dial(url, srtp.Server) @@ -190,26 +190,6 @@ func hapHandler(w http.ResponseWriter, r *http.Request) { } } -func findHomeKitURL(sources []string) string { - if len(sources) == 0 { - return "" - } - - url := sources[0] - if strings.HasPrefix(url, "homekit") { - return url - } - - if strings.HasPrefix(url, "hass") { - location, _ := streams.Location(url) - if strings.HasPrefix(location, "homekit") { - return url - } - } - - return "" -} - func parseBitrate(s string) int { n := len(s) if n == 0 { diff --git a/internal/streams/api.go b/internal/streams/api.go index 061e61c2b..051dba5c1 100644 --- a/internal/streams/api.go +++ b/internal/streams/api.go @@ -5,6 +5,7 @@ import ( "github.com/AlexxIT/go2rtc/internal/api" "github.com/AlexxIT/go2rtc/internal/app" + "github.com/AlexxIT/go2rtc/pkg/hap" "github.com/AlexxIT/go2rtc/pkg/probe" ) @@ -94,6 +95,13 @@ func apiStreams(w http.ResponseWriter, r *http.Request) { } case "DELETE": + stream := Get(src) + if stream != nil { + if rawURL := FindPrefixURL("homekit", stream.Sources()); rawURL != "" { + _ = hap.Unpair(rawURL) + } + } + delete(streams, src) if err := app.PatchConfig([]string{"streams", src}, nil); err != nil { diff --git a/internal/streams/helpers.go b/internal/streams/helpers.go index 2ead1aa3e..0caca72ea 100644 --- a/internal/streams/helpers.go +++ b/internal/streams/helpers.go @@ -20,3 +20,37 @@ func ParseQuery(s string) url.Values { } return params } + +func FindPrefixURL(prefix string, sources []string) string { + if len(sources) == 0 { + return "" + } + + url := sources[0] + if strings.HasPrefix(url, prefix) { + return url + } + + if prefix == "homekit" { + if strings.HasPrefix(url, "hass") { + location, _ := Location(url) + if strings.HasPrefix(location, prefix) { + return url + } + } + } + + return "" +} + +func FindPrefixURLs(prefix string) map[string]*url.URL { + urls := map[string]*url.URL{} + for name, sources := range GetAllSources() { + if rawURL := FindPrefixURL(prefix, sources); rawURL != "" { + if u, err := url.Parse(rawURL); err == nil { + urls[name] = u + } + } + } + return urls +} \ No newline at end of file