| 1 |
package ntfy |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto/tls" |
| 5 |
"encoding/base64" |
| 6 |
"fmt" |
| 7 |
"net/http" |
| 8 |
"net/url" |
| 9 |
"strings" |
| 10 |
"time" |
| 11 |
|
| 12 |
"github.com/nicholas-fedor/shoutrrr/internal/meta" |
| 13 |
"github.com/nicholas-fedor/shoutrrr/pkg/format" |
| 14 |
"github.com/nicholas-fedor/shoutrrr/pkg/services/standard" |
| 15 |
"github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 16 |
"github.com/nicholas-fedor/shoutrrr/pkg/util/jsonclient" |
| 17 |
) |
| 18 |
|
| 19 |
// Service sends notifications to ntfy. |
| 20 |
type Service struct { |
| 21 |
standard.Standard |
| 22 |
|
| 23 |
Config *Config |
| 24 |
pkr format.PropKeyResolver |
| 25 |
httpClient *http.Client |
| 26 |
client jsonclient.Client |
| 27 |
} |
| 28 |
|
| 29 |
// HTTPTimeout defines the HTTP client timeout in seconds. |
| 30 |
const HTTPTimeout = 10 |
| 31 |
|
| 32 |
// GetID returns the service identifier. |
| 33 |
func (s *Service) GetID() string { |
| 34 |
return Scheme |
| 35 |
} |
| 36 |
|
| 37 |
// Initialize configures the service with a URL and logger. |
| 38 |
func (s *Service) Initialize(serviceURL *url.URL, logger types.StdLogger) error { |
| 39 |
s.SetLogger(logger) |
| 40 |
s.Config = &Config{} |
| 41 |
s.pkr = format.NewPropKeyResolver(s.Config) |
| 42 |
|
| 43 |
if err := s.pkr.SetDefaultProps(s.Config); err != nil { |
| 44 |
return fmt.Errorf("setting default props: %w", err) |
| 45 |
} |
| 46 |
|
| 47 |
err := s.Config.setURL(&s.pkr, serviceURL) |
| 48 |
if err != nil { |
| 49 |
return err |
| 50 |
} |
| 51 |
|
| 52 |
// Force HTTP scheme when DisableTLS is true |
| 53 |
if s.Config.DisableTLS { |
| 54 |
s.Config.Scheme = "http" |
| 55 |
} |
| 56 |
|
| 57 |
s.httpClient = &http.Client{ |
| 58 |
Timeout: HTTPTimeout * time.Second, |
| 59 |
} |
| 60 |
|
| 61 |
// Configure HTTP transport: skip TLS verification if disabled, enforce TLS 1.2 minimum |
| 62 |
if s.Config.DisableTLSVerification { |
| 63 |
s.httpClient.Transport = &http.Transport{ |
| 64 |
TLSClientConfig: &tls.Config{ |
| 65 |
InsecureSkipVerify: true, |
| 66 |
MinVersion: tls.VersionTLS12, |
| 67 |
}, |
| 68 |
} |
| 69 |
s.Log("Warning: TLS verification is disabled, making connections insecure") |
| 70 |
} else { |
| 71 |
s.httpClient.Transport = &http.Transport{ |
| 72 |
TLSClientConfig: &tls.Config{MinVersion: tls.VersionTLS12}, |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
s.client = jsonclient.NewWithHTTPClient(s.httpClient) |
| 77 |
|
| 78 |
return nil |
| 79 |
} |
| 80 |
|
| 81 |
// Send delivers a notification message to ntfy. |
| 82 |
func (s *Service) Send(message string, params *types.Params) error { |
| 83 |
config := s.Config |
| 84 |
|
| 85 |
// Update config with runtime parameters |
| 86 |
if err := s.pkr.UpdateConfigFromParams(config, params); err != nil { |
| 87 |
return fmt.Errorf("updating config from params: %w", err) |
| 88 |
} |
| 89 |
|
| 90 |
// Execute the API request to send the notification |
| 91 |
if err := s.sendAPI(config, message); err != nil { |
| 92 |
return fmt.Errorf("failed to send ntfy notification: %w", err) |
| 93 |
} |
| 94 |
|
| 95 |
return nil |
| 96 |
} |
| 97 |
|
| 98 |
// SetHTTPClient sets a custom HTTP client for the service. |
| 99 |
func (s *Service) SetHTTPClient(httpClient *http.Client) { |
| 100 |
s.httpClient = httpClient |
| 101 |
s.client = jsonclient.NewWithHTTPClient(s.httpClient) |
| 102 |
} |
| 103 |
|
| 104 |
// sendAPI sends a notification to the ntfy API. |
| 105 |
func (s *Service) sendAPI(config *Config, message string) error { |
| 106 |
response := apiResponseError{} |
| 107 |
request := message |
| 108 |
|
| 109 |
// Prepare request headers |
| 110 |
headers := s.client.Headers() |
| 111 |
headers.Del("Content-Type") |
| 112 |
headers.Set("Content-Type", "text/plain; charset=utf-8") |
| 113 |
headers.Set("User-Agent", "shoutrrr/"+meta.Version) |
| 114 |
addHeaderIfNotEmpty(&headers, "Title", config.Title) |
| 115 |
addHeaderIfNotEmpty(&headers, "Priority", config.Priority.String()) |
| 116 |
addHeaderIfNotEmpty(&headers, "Tags", strings.Join(config.Tags, ",")) |
| 117 |
addHeaderIfNotEmpty(&headers, "Delay", config.Delay) |
| 118 |
addHeaderIfNotEmpty(&headers, "Actions", strings.Join(config.Actions, ";")) |
| 119 |
addHeaderIfNotEmpty(&headers, "Click", config.Click) |
| 120 |
addHeaderIfNotEmpty(&headers, "Attach", config.Attach) |
| 121 |
addHeaderIfNotEmpty(&headers, "X-Icon", config.Icon) |
| 122 |
addHeaderIfNotEmpty(&headers, "Filename", config.Filename) |
| 123 |
addHeaderIfNotEmpty(&headers, "Email", config.Email) |
| 124 |
|
| 125 |
if !config.Cache { |
| 126 |
headers.Add("Cache", "no") |
| 127 |
} |
| 128 |
|
| 129 |
if !config.Firebase { |
| 130 |
headers.Add("Firebase", "no") |
| 131 |
} |
| 132 |
|
| 133 |
// Add Basic Auth header if username or password is provided |
| 134 |
if config.Username != "" || config.Password != "" { |
| 135 |
headers.Set( |
| 136 |
"Authorization", |
| 137 |
"Basic "+base64.StdEncoding.EncodeToString([]byte(config.Username+":"+config.Password)), |
| 138 |
) |
| 139 |
} |
| 140 |
|
| 141 |
// Send the HTTP request |
| 142 |
if err := s.client.Post(config.GetAPIURL(), request, &response); err != nil { |
| 143 |
s.Logf("NTFY API request failed with error: %v", err) |
| 144 |
// Attempt to parse structured error response from API |
| 145 |
if s.client.ErrorResponse(err, &response) { |
| 146 |
return &response |
| 147 |
} |
| 148 |
|
| 149 |
return fmt.Errorf("posting to ntfy API: %w", err) |
| 150 |
} |
| 151 |
|
| 152 |
s.Logf("NTFY API request succeeded") |
| 153 |
|
| 154 |
return nil |
| 155 |
} |
| 156 |
|
| 157 |
// addHeaderIfNotEmpty adds a header to the request if the value is non-empty. |
| 158 |
func addHeaderIfNotEmpty(headers *http.Header, key, value string) { |
| 159 |
if value != "" { |
| 160 |
headers.Add(key, value) |
| 161 |
} |
| 162 |
} |