From 5b52d456294ea60be7a5f7469bd42cde7e5f75d0 Mon Sep 17 00:00:00 2001 From: Matt Schmitz Date: Mon, 13 May 2024 14:28:03 -0400 Subject: [PATCH] Add ability to send additional headers to Frigate --- config/config.go | 11 ++++++----- docs/changelog.md | 1 + docs/config.md | 10 ++++++++-- events/api.go | 5 +++-- example-config.yml | 5 +++++ util/httpclient.go | 21 ++++++++++++++++++--- 6 files changed, 41 insertions(+), 12 deletions(-) diff --git a/config/config.go b/config/config.go index b71c376..3cb0c74 100644 --- a/config/config.go +++ b/config/config.go @@ -17,11 +17,12 @@ type Config struct { } type Frigate struct { - Server string `fig:"server" validate:"required"` - Insecure bool `fig:"ignoressl" default:false` - WebAPI WebAPI `fig:"webapi"` - MQTT MQTT `fig:"mqtt"` - Cameras Cameras `fig:"cameras"` + Server string `fig:"server" validate:"required"` + Insecure bool `fig:"ignoressl" default:false` + Headers []map[string]string `fig:"headers"` + WebAPI WebAPI `fig:"webapi"` + MQTT MQTT `fig:"mqtt"` + Cameras Cameras `fig:"cameras"` } type WebAPI struct { diff --git a/docs/changelog.md b/docs/changelog.md index 4e0d87d..6c69b88 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -3,6 +3,7 @@ ## [v0.2.8](https://github.com/0x2142/frigate-notify/releases/tag/v0.2.8) - Upcoming Release - Add support for notifications via [Nfty](https://frigate-notify.0x2142.com/config/#nfty) +- Add ability to send additional HTTP [headers](https://frigate-notify.0x2142.com/config/#frigate) to Frigate ## [v0.2.7](https://github.com/0x2142/frigate-notify/releases/tag/v0.2.7) - May 06 2024 diff --git a/docs/config.md b/docs/config.md index ede8364..59d6a19 100644 --- a/docs/config.md +++ b/docs/config.md @@ -12,11 +12,18 @@ Configuration snippets will be provided throughout this page. Feel free to copy - IP or hostname of the Frigate NVR - **ignoressl** (Optional - Default: `false`) - Set to `true` to allow self-signed certificates +- **headers** (Optional) + - Send additional HTTP headers to Frigate + - Useful for things like authentication + - Header format: `Header: Value` + - Example: `Authorization: Basic abcd1234` ```yaml title="Config File Snippet" frigate: server: nvr.your.domain.tld ignoressl: true + headers: + - Authorization: Basic abcd1234 ``` ### WebAPI @@ -372,10 +379,8 @@ monitor: ignoressl: ``` - --- - ## Sample Config { data-search-exclude } A full config file template has been provided below: @@ -384,6 +389,7 @@ A full config file template has been provided below: frigate: server: ignoressl: + headers: webapi: enabled: diff --git a/events/api.go b/events/api.go index 2475066..a084745 100644 --- a/events/api.go +++ b/events/api.go @@ -35,9 +35,10 @@ func CheckForEvents() { log.Println("Checking for new events...") // Query events - response, err := util.HTTPGet(url, config.ConfigData.Frigate.Insecure) + response, err := util.HTTPGet(url, config.ConfigData.Frigate.Insecure, config.ConfigData.Frigate.Headers...) if err != nil { log.Printf("Cannot get events from %s", url) + log.Printf("Error received: %s", err) } var events []Event @@ -87,7 +88,7 @@ func CheckForEvents() { // GetSnapshot downloads a snapshot from Frigate func GetSnapshot(snapshotURL, eventID string) io.Reader { - response, err := util.HTTPGet(snapshotURL, config.ConfigData.Frigate.Insecure) + response, err := util.HTTPGet(snapshotURL, config.ConfigData.Frigate.Insecure, config.ConfigData.Frigate.Headers...) if err != nil { log.Println("Could not access snaphot. Error: ", err) } diff --git a/example-config.yml b/example-config.yml index 7670ec5..3d4de73 100644 --- a/example-config.yml +++ b/example-config.yml @@ -10,6 +10,11 @@ frigate: # Set to true if using SSL & a self-signed certificate ignoressl: false + # List of HTTP headers to send to Frigate, in format Header: Value + headers: + # Example: + # - Authorization: Basic abcd1234 + webapi: # Set to true to enable event collection via the web API enabled: diff --git a/util/httpclient.go b/util/httpclient.go index 00ca99e..f8d2f3b 100644 --- a/util/httpclient.go +++ b/util/httpclient.go @@ -3,13 +3,15 @@ package util import ( "bytes" "crypto/tls" + "errors" "io" "net/http" + "strconv" "time" ) // HTTPGet is a simple HTTP client function to return page body -func HTTPGet(url string, insecure bool) ([]byte, error) { +func HTTPGet(url string, insecure bool, headers ...map[string]string) ([]byte, error) { // New HTTP Client client := http.Client{Timeout: 10 * time.Second} @@ -25,6 +27,16 @@ func HTTPGet(url string, insecure bool) ([]byte, error) { return nil, err } + // Add headers + if len(headers) > 0 { + for _, h := range headers { + for k, v := range h { + req.Header.Add(k, v) + } + + } + } + // Send HTTP GET response, err := client.Do(req) if err != nil { @@ -38,6 +50,10 @@ func HTTPGet(url string, insecure bool) ([]byte, error) { return nil, err } + if response.StatusCode != 200 { + return nil, errors.New(strconv.Itoa(response.StatusCode)) + } + return body, nil } @@ -58,8 +74,8 @@ func HTTPPost(url string, insecure bool, payload []byte, headers ...map[string]s if err != nil { return nil, err } - //req.Header.Set("Content-Type", "application/json") + // Add headers if len(headers) > 0 { for _, h := range headers { for k, v := range h { @@ -67,7 +83,6 @@ func HTTPPost(url string, insecure bool, payload []byte, headers ...map[string]s } } - } // Send HTTP POST