Skip to content

Commit

Permalink
Add ability to send additional headers to Frigate
Browse files Browse the repository at this point in the history
  • Loading branch information
0x2142 committed May 13, 2024
1 parent e9c547b commit 5b52d45
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 12 deletions.
11 changes: 6 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 8 additions & 2 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -372,10 +379,8 @@ monitor:
ignoressl:
```


---


## Sample Config { data-search-exclude }

A full config file template has been provided below:
Expand All @@ -384,6 +389,7 @@ A full config file template has been provided below:
frigate:
server:
ignoressl:
headers:
webapi:
enabled:
Expand Down
5 changes: 3 additions & 2 deletions events/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
5 changes: 5 additions & 0 deletions example-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
21 changes: 18 additions & 3 deletions util/httpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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 {
Expand All @@ -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
}

Expand All @@ -58,16 +74,15 @@ 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 {
req.Header.Add(k, v)
}

}

}

// Send HTTP POST
Expand Down

0 comments on commit 5b52d45

Please sign in to comment.