| 1 |
package server |
| 2 |
|
| 3 |
import ( |
| 4 |
"net/http" |
| 5 |
|
| 6 |
"heckel.io/ntfy/v2/model" |
| 7 |
"heckel.io/ntfy/v2/user" |
| 8 |
"heckel.io/ntfy/v2/util" |
| 9 |
) |
| 10 |
|
| 11 |
// publishMessage is used as input when publishing as JSON |
| 12 |
type publishMessage struct { |
| 13 |
Topic string `json:"topic"` |
| 14 |
SequenceID string `json:"sequence_id"` |
| 15 |
Title string `json:"title"` |
| 16 |
Message string `json:"message"` |
| 17 |
Priority int `json:"priority"` |
| 18 |
Tags []string `json:"tags"` |
| 19 |
Click string `json:"click"` |
| 20 |
Icon string `json:"icon"` |
| 21 |
Actions []model.Action `json:"actions"` |
| 22 |
Attach string `json:"attach"` |
| 23 |
Markdown bool `json:"markdown"` |
| 24 |
Filename string `json:"filename"` |
| 25 |
Email string `json:"email"` |
| 26 |
Call string `json:"call"` |
| 27 |
Cache string `json:"cache"` // use string as it defaults to true (or use &bool instead) |
| 28 |
Firebase string `json:"firebase"` // use string as it defaults to true (or use &bool instead) |
| 29 |
Delay string `json:"delay"` |
| 30 |
} |
| 31 |
|
| 32 |
// dispatchOpts selects which delivery targets fire for a published message, beyond delivery |
| 33 |
// to local subscribers (see Server.dispatch) |
| 34 |
type dispatchOpts struct { |
| 35 |
firebase bool // Send to Firebase (if configured) |
| 36 |
email string // Send an email to this address (if a mailer is configured) |
| 37 |
call string // Call this phone number (if Twilio is configured) |
| 38 |
upstream bool // Forward a poll request to the upstream server (if configured) |
| 39 |
webPush bool // Publish to web push endpoints (if configured) |
| 40 |
async bool // Deliver to local subscribers in a goroutine, logging errors instead of returning them |
| 41 |
} |
| 42 |
|
| 43 |
// messageEncoder is a function that knows how to encode a message |
| 44 |
type messageEncoder func(msg *model.Message) (string, error) |
| 45 |
|
| 46 |
type queryFilter struct { |
| 47 |
ID string |
| 48 |
Message string |
| 49 |
Title string |
| 50 |
Tags []string |
| 51 |
Priority []int |
| 52 |
} |
| 53 |
|
| 54 |
func parseQueryFilters(r *http.Request) (*queryFilter, error) { |
| 55 |
idFilter := readParam(r, "x-id", "id") |
| 56 |
messageFilter := readParam(r, "x-message", "message", "m") |
| 57 |
titleFilter := readParam(r, "x-title", "title", "t") |
| 58 |
tagsFilter := util.SplitNoEmpty(readParam(r, "x-tags", "tags", "tag", "ta"), ",") |
| 59 |
priorityFilter := make([]int, 0) |
| 60 |
for _, p := range util.SplitNoEmpty(readParam(r, "x-priority", "priority", "prio", "p"), ",") { |
| 61 |
priority, err := util.ParsePriority(p) |
| 62 |
if err != nil { |
| 63 |
return nil, errHTTPBadRequestPriorityInvalid |
| 64 |
} |
| 65 |
priorityFilter = append(priorityFilter, priority) |
| 66 |
} |
| 67 |
return &queryFilter{ |
| 68 |
ID: idFilter, |
| 69 |
Message: messageFilter, |
| 70 |
Title: titleFilter, |
| 71 |
Tags: tagsFilter, |
| 72 |
Priority: priorityFilter, |
| 73 |
}, nil |
| 74 |
} |
| 75 |
|
| 76 |
func (q *queryFilter) Pass(msg *model.Message) bool { |
| 77 |
if msg.Event != model.MessageEvent && msg.Event != model.MessageDeleteEvent && msg.Event != model.MessageClearEvent { |
| 78 |
return true // filters only apply to messages |
| 79 |
} else if q.ID != "" && msg.ID != q.ID { |
| 80 |
return false |
| 81 |
} else if q.Message != "" && msg.Message != q.Message { |
| 82 |
return false |
| 83 |
} else if q.Title != "" && msg.Title != q.Title { |
| 84 |
return false |
| 85 |
} |
| 86 |
messagePriority := msg.Priority |
| 87 |
if messagePriority == 0 { |
| 88 |
messagePriority = 3 // For query filters, default priority (3) is the same as "not set" (0) |
| 89 |
} |
| 90 |
if len(q.Priority) > 0 && !util.Contains(q.Priority, messagePriority) { |
| 91 |
return false |
| 92 |
} |
| 93 |
if len(q.Tags) > 0 && !util.ContainsAll(msg.Tags, q.Tags) { |
| 94 |
return false |
| 95 |
} |
| 96 |
return true |
| 97 |
} |
| 98 |
|
| 99 |
// templateMode represents the mode in which templates are used |
| 100 |
// |
| 101 |
// It can be |
| 102 |
// - empty: templating is disabled |
| 103 |
// - a boolean string (yes/1/true/no/0/false): inline-templating mode |
| 104 |
// - a filename (e.g. grafana): template mode with a file |
| 105 |
type templateMode string |
| 106 |
|
| 107 |
// Enabled returns true if templating is enabled |
| 108 |
func (t templateMode) Enabled() bool { |
| 109 |
return t != "" |
| 110 |
} |
| 111 |
|
| 112 |
// InlineMode returns true if inline-templating mode is enabled |
| 113 |
func (t templateMode) InlineMode() bool { |
| 114 |
return t.Enabled() && isBoolValue(string(t)) |
| 115 |
} |
| 116 |
|
| 117 |
// FileMode returns true if file-templating mode is enabled |
| 118 |
func (t templateMode) FileMode() bool { |
| 119 |
return t.Enabled() && !isBoolValue(string(t)) |
| 120 |
} |
| 121 |
|
| 122 |
// FileName returns the filename if file-templating mode is enabled, or an empty string otherwise |
| 123 |
func (t templateMode) FileName() string { |
| 124 |
if t.FileMode() { |
| 125 |
return string(t) |
| 126 |
} |
| 127 |
return "" |
| 128 |
} |
| 129 |
|
| 130 |
// templateFile represents a template file with title, message, and priority |
| 131 |
// It is used for file-based templates, e.g. grafana, influxdb, etc. |
| 132 |
// |
| 133 |
// Example YAML: |
| 134 |
// |
| 135 |
// title: "Alert: {{ .Title }}" |
| 136 |
// message: | |
| 137 |
// This is a {{ .Type }} alert. |
| 138 |
// It can be multiline. |
| 139 |
// priority: '{{ if eq .status "Error" }}5{{ else }}3{{ end }}' |
| 140 |
type templateFile struct { |
| 141 |
Title *string `yaml:"title"` |
| 142 |
Message *string `yaml:"message"` |
| 143 |
Priority *string `yaml:"priority"` |
| 144 |
} |
| 145 |
|
| 146 |
type apiHealthResponse struct { |
| 147 |
Healthy bool `json:"healthy"` |
| 148 |
} |
| 149 |
|
| 150 |
type apiVersionResponse struct { |
| 151 |
Version string `json:"version"` |
| 152 |
Commit string `json:"commit"` |
| 153 |
Date string `json:"date"` |
| 154 |
} |
| 155 |
|
| 156 |
type apiStatsResponse struct { |
| 157 |
Messages int64 `json:"messages"` |
| 158 |
MessagesRate float64 `json:"messages_rate"` // Average number of messages per second |
| 159 |
} |
| 160 |
|
| 161 |
type apiUserAddOrUpdateRequest struct { |
| 162 |
Username string `json:"username"` |
| 163 |
Password string `json:"password"` |
| 164 |
Hash string `json:"hash"` |
| 165 |
Tier string `json:"tier"` |
| 166 |
// Do not add 'role' here. We don't want to add admins via the API. |
| 167 |
} |
| 168 |
|
| 169 |
type apiUserResponse struct { |
| 170 |
Username string `json:"username"` |
| 171 |
Role string `json:"role"` |
| 172 |
Tier string `json:"tier,omitempty"` |
| 173 |
Grants []*apiUserGrantResponse `json:"grants,omitempty"` |
| 174 |
} |
| 175 |
|
| 176 |
type apiUserGrantResponse struct { |
| 177 |
Topic string `json:"topic"` // This may be a pattern |
| 178 |
Permission string `json:"permission"` |
| 179 |
} |
| 180 |
|
| 181 |
type apiUserDeleteRequest struct { |
| 182 |
Username string `json:"username"` |
| 183 |
} |
| 184 |
|
| 185 |
type apiAccessAllowRequest struct { |
| 186 |
Username string `json:"username"` |
| 187 |
Topic string `json:"topic"` // This may be a pattern |
| 188 |
Permission string `json:"permission"` |
| 189 |
} |
| 190 |
|
| 191 |
type apiAccessResetRequest struct { |
| 192 |
Username string `json:"username"` |
| 193 |
Topic string `json:"topic"` |
| 194 |
} |
| 195 |
|
| 196 |
type apiAccountCreateRequest struct { |
| 197 |
Username string `json:"username"` |
| 198 |
Password string `json:"password"` |
| 199 |
Email string `json:"email"` // Optional; if set (and SMTP configured), a verification link is sent |
| 200 |
} |
| 201 |
|
| 202 |
type apiAccountPasswordChangeRequest struct { |
| 203 |
Password string `json:"password"` |
| 204 |
NewPassword string `json:"new_password"` |
| 205 |
} |
| 206 |
|
| 207 |
type apiAccountDeleteRequest struct { |
| 208 |
Password string `json:"password"` |
| 209 |
} |
| 210 |
|
| 211 |
type apiAccountTokenIssueRequest struct { |
| 212 |
Label *string `json:"label"` |
| 213 |
Expires *int64 `json:"expires"` // Unix timestamp |
| 214 |
} |
| 215 |
|
| 216 |
type apiAccountTokenUpdateRequest struct { |
| 217 |
Token string `json:"token"` |
| 218 |
Label *string `json:"label"` |
| 219 |
Expires *int64 `json:"expires"` // Unix timestamp |
| 220 |
} |
| 221 |
|
| 222 |
type apiAccountTokenResponse struct { |
| 223 |
Token string `json:"token"` |
| 224 |
Label string `json:"label,omitempty"` |
| 225 |
LastAccess int64 `json:"last_access,omitempty"` |
| 226 |
LastOrigin string `json:"last_origin,omitempty"` |
| 227 |
Expires int64 `json:"expires,omitempty"` // Unix timestamp |
| 228 |
Provisioned bool `json:"provisioned,omitempty"` // True if this token was provisioned by the server config |
| 229 |
} |
| 230 |
|
| 231 |
// apiAccountLoginResponse is the body of POST /v1/account/login: it authenticates a |
| 232 |
// username-or-email + password, mints a session token, and returns the token together with the |
| 233 |
// canonical username (which may differ from the identifier the user typed, e.g. a primary email). |
| 234 |
type apiAccountLoginResponse struct { |
| 235 |
Token string `json:"token"` |
| 236 |
Username string `json:"username"` |
| 237 |
} |
| 238 |
|
| 239 |
type apiAccountPhoneNumberVerifyRequest struct { |
| 240 |
Number string `json:"number"` |
| 241 |
Channel string `json:"channel"` |
| 242 |
} |
| 243 |
|
| 244 |
type apiAccountPhoneNumberAddRequest struct { |
| 245 |
Number string `json:"number"` |
| 246 |
Code string `json:"code"` // Only set when adding a phone number |
| 247 |
} |
| 248 |
|
| 249 |
// apiAccountEmailRequest carries an email address for the add/delete/set-primary/resend |
| 250 |
// endpoints (all of which identify an email by address in the JSON body). |
| 251 |
type apiAccountEmailRequest struct { |
| 252 |
Email string `json:"email"` |
| 253 |
} |
| 254 |
|
| 255 |
// apiAccountEmailVerifyRequest carries the raw magic-link token submitted (unauthenticated) |
| 256 |
// from the verification landing page. |
| 257 |
type apiAccountEmailVerifyRequest struct { |
| 258 |
Token string `json:"token"` |
| 259 |
} |
| 260 |
|
| 261 |
// apiAccountPasswordResetRequest is the body of the (unauthenticated) reset-request endpoint. |
| 262 |
// The identifier is a username or a primary email address. |
| 263 |
type apiAccountPasswordResetRequest struct { |
| 264 |
Identifier string `json:"identifier"` |
| 265 |
} |
| 266 |
|
| 267 |
// apiAccountPasswordResetConfirmRequest is the body of the (unauthenticated) reset-confirm |
| 268 |
// endpoint, submitted from the set-new-password landing page. |
| 269 |
type apiAccountPasswordResetConfirmRequest struct { |
| 270 |
Token string `json:"token"` |
| 271 |
Password string `json:"password"` |
| 272 |
} |
| 273 |
|
| 274 |
type apiAccountTier struct { |
| 275 |
Code string `json:"code"` |
| 276 |
Name string `json:"name"` |
| 277 |
} |
| 278 |
|
| 279 |
type apiAccountLimits struct { |
| 280 |
Basis string `json:"basis,omitempty"` // "ip" or "tier" |
| 281 |
Messages int64 `json:"messages"` |
| 282 |
MessagesExpiryDuration int64 `json:"messages_expiry_duration"` |
| 283 |
Emails int64 `json:"emails"` |
| 284 |
Calls int64 `json:"calls"` |
| 285 |
Reservations int64 `json:"reservations"` |
| 286 |
AttachmentTotalSize int64 `json:"attachment_total_size"` |
| 287 |
AttachmentFileSize int64 `json:"attachment_file_size"` |
| 288 |
AttachmentExpiryDuration int64 `json:"attachment_expiry_duration"` |
| 289 |
AttachmentBandwidth int64 `json:"attachment_bandwidth"` |
| 290 |
} |
| 291 |
|
| 292 |
type apiAccountStats struct { |
| 293 |
Messages int64 `json:"messages"` |
| 294 |
MessagesRemaining int64 `json:"messages_remaining"` |
| 295 |
Emails int64 `json:"emails"` |
| 296 |
EmailsRemaining int64 `json:"emails_remaining"` |
| 297 |
Calls int64 `json:"calls"` |
| 298 |
CallsRemaining int64 `json:"calls_remaining"` |
| 299 |
Reservations int64 `json:"reservations"` |
| 300 |
ReservationsRemaining int64 `json:"reservations_remaining"` |
| 301 |
AttachmentTotalSize int64 `json:"attachment_total_size"` |
| 302 |
AttachmentTotalSizeRemaining int64 `json:"attachment_total_size_remaining"` |
| 303 |
} |
| 304 |
|
| 305 |
type apiAccountReservation struct { |
| 306 |
Topic string `json:"topic"` |
| 307 |
Everyone string `json:"everyone"` |
| 308 |
} |
| 309 |
|
| 310 |
// apiAccountEmailInfo describes one email address on the account, as returned by GET /v1/account. |
| 311 |
// Verified addresses have pending=false; exactly one verified address may be primary (the |
| 312 |
// recovery email). Pending addresses are awaiting a magic-link click and are never primary. |
| 313 |
type apiAccountEmailInfo struct { |
| 314 |
Address string `json:"address"` |
| 315 |
Primary bool `json:"primary,omitempty"` |
| 316 |
Pending bool `json:"pending,omitempty"` |
| 317 |
} |
| 318 |
|
| 319 |
type apiAccountBilling struct { |
| 320 |
Customer bool `json:"customer"` |
| 321 |
Subscription bool `json:"subscription"` |
| 322 |
Status string `json:"status,omitempty"` |
| 323 |
Interval string `json:"interval,omitempty"` |
| 324 |
PaidUntil int64 `json:"paid_until,omitempty"` |
| 325 |
CancelAt int64 `json:"cancel_at,omitempty"` |
| 326 |
} |
| 327 |
|
| 328 |
type apiAccountResponse struct { |
| 329 |
Username string `json:"username"` |
| 330 |
Role string `json:"role,omitempty"` |
| 331 |
SyncTopic string `json:"sync_topic,omitempty"` |
| 332 |
Provisioned bool `json:"provisioned,omitempty"` |
| 333 |
Language string `json:"language,omitempty"` |
| 334 |
DateFormat string `json:"date_format,omitempty"` |
| 335 |
TimeFormat string `json:"time_format,omitempty"` |
| 336 |
Notification *user.NotificationPrefs `json:"notification,omitempty"` |
| 337 |
Subscriptions []*user.Subscription `json:"subscriptions,omitempty"` |
| 338 |
Reservations []*apiAccountReservation `json:"reservations,omitempty"` |
| 339 |
Tokens []*apiAccountTokenResponse `json:"tokens,omitempty"` |
| 340 |
PhoneNumbers []string `json:"phone_numbers,omitempty"` |
| 341 |
Emails []*apiAccountEmailInfo `json:"emails,omitempty"` |
| 342 |
Tier *apiAccountTier `json:"tier,omitempty"` |
| 343 |
Limits *apiAccountLimits `json:"limits,omitempty"` |
| 344 |
Stats *apiAccountStats `json:"stats,omitempty"` |
| 345 |
Billing *apiAccountBilling `json:"billing,omitempty"` |
| 346 |
} |
| 347 |
|
| 348 |
type apiAccountReservationRequest struct { |
| 349 |
Topic string `json:"topic"` |
| 350 |
Everyone string `json:"everyone"` |
| 351 |
} |
| 352 |
|
| 353 |
type apiConfigResponse struct { |
| 354 |
BaseURL string `json:"base_url"` |
| 355 |
AppRoot string `json:"app_root"` |
| 356 |
EnableLogin bool `json:"enable_login"` |
| 357 |
RequireLogin bool `json:"require_login"` |
| 358 |
EnableSignup bool `json:"enable_signup"` |
| 359 |
EnablePayments bool `json:"enable_payments"` |
| 360 |
EnableCalls bool `json:"enable_calls"` |
| 361 |
EnableEmails bool `json:"enable_emails"` |
| 362 |
EnableResetPassword bool `json:"enable_reset_password"` |
| 363 |
EnableReservations bool `json:"enable_reservations"` |
| 364 |
EnableWebPush bool `json:"enable_web_push"` |
| 365 |
BillingContact string `json:"billing_contact"` |
| 366 |
WebPushPublicKey string `json:"web_push_public_key"` |
| 367 |
DisallowedTopics []string `json:"disallowed_topics"` |
| 368 |
ConfigHash string `json:"config_hash"` |
| 369 |
} |
| 370 |
|
| 371 |
type apiAccountBillingPrices struct { |
| 372 |
Month int64 `json:"month"` |
| 373 |
Year int64 `json:"year"` |
| 374 |
} |
| 375 |
|
| 376 |
type apiAccountBillingTier struct { |
| 377 |
Code string `json:"code,omitempty"` |
| 378 |
Name string `json:"name,omitempty"` |
| 379 |
Prices *apiAccountBillingPrices `json:"prices,omitempty"` |
| 380 |
Limits *apiAccountLimits `json:"limits"` |
| 381 |
} |
| 382 |
|
| 383 |
type apiAccountBillingSubscriptionCreateResponse struct { |
| 384 |
RedirectURL string `json:"redirect_url"` |
| 385 |
} |
| 386 |
|
| 387 |
type apiAccountBillingSubscriptionChangeRequest struct { |
| 388 |
Tier string `json:"tier"` |
| 389 |
Interval string `json:"interval"` |
| 390 |
} |
| 391 |
|
| 392 |
type apiAccountBillingPortalRedirectResponse struct { |
| 393 |
RedirectURL string `json:"redirect_url"` |
| 394 |
} |
| 395 |
|
| 396 |
type apiAccountSyncTopicResponse struct { |
| 397 |
Event string `json:"event"` |
| 398 |
} |
| 399 |
|
| 400 |
type apiSuccessResponse struct { |
| 401 |
Success bool `json:"success"` |
| 402 |
} |
| 403 |
|
| 404 |
func newSuccessResponse() *apiSuccessResponse { |
| 405 |
return &apiSuccessResponse{ |
| 406 |
Success: true, |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
type apiStripeSubscriptionUpdatedEvent struct { |
| 411 |
ID string `json:"id"` |
| 412 |
Customer string `json:"customer"` |
| 413 |
Status string `json:"status"` |
| 414 |
CurrentPeriodEnd int64 `json:"current_period_end"` |
| 415 |
CancelAt int64 `json:"cancel_at"` |
| 416 |
Items *struct { |
| 417 |
Data []*struct { |
| 418 |
Price *struct { |
| 419 |
ID string `json:"id"` |
| 420 |
Recurring *struct { |
| 421 |
Interval string `json:"interval"` |
| 422 |
} `json:"recurring"` |
| 423 |
} `json:"price"` |
| 424 |
} `json:"data"` |
| 425 |
} `json:"items"` |
| 426 |
} |
| 427 |
|
| 428 |
type apiStripeSubscriptionDeletedEvent struct { |
| 429 |
ID string `json:"id"` |
| 430 |
Customer string `json:"customer"` |
| 431 |
} |
| 432 |
|
| 433 |
type apiWebPushUpdateSubscriptionRequest struct { |
| 434 |
Endpoint string `json:"endpoint"` |
| 435 |
Auth string `json:"auth"` |
| 436 |
P256dh string `json:"p256dh"` |
| 437 |
Topics []string `json:"topics"` |
| 438 |
} |
| 439 |
|
| 440 |
// List of possible Web Push events (see sw.js) |
| 441 |
const ( |
| 442 |
webPushMessageEvent = "message" |
| 443 |
webPushExpiringEvent = "subscription_expiring" |
| 444 |
) |
| 445 |
|
| 446 |
type webPushPayload struct { |
| 447 |
Event string `json:"event"` |
| 448 |
SubscriptionID string `json:"subscription_id"` |
| 449 |
Message *model.Message `json:"message"` |
| 450 |
} |
| 451 |
|
| 452 |
func newWebPushPayload(subscriptionID string, message *model.Message) *webPushPayload { |
| 453 |
return &webPushPayload{ |
| 454 |
Event: webPushMessageEvent, |
| 455 |
SubscriptionID: subscriptionID, |
| 456 |
Message: message, |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
type webPushControlMessagePayload struct { |
| 461 |
Event string `json:"event"` |
| 462 |
} |
| 463 |
|
| 464 |
func newWebPushSubscriptionExpiringPayload() *webPushControlMessagePayload { |
| 465 |
return &webPushControlMessagePayload{ |
| 466 |
Event: webPushExpiringEvent, |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
// https://developer.mozilla.org/en-US/docs/Web/Manifest |
| 471 |
type webManifestResponse struct { |
| 472 |
Name string `json:"name"` |
| 473 |
Description string `json:"description"` |
| 474 |
ShortName string `json:"short_name"` |
| 475 |
Scope string `json:"scope"` |
| 476 |
StartURL string `json:"start_url"` |
| 477 |
Display string `json:"display"` |
| 478 |
BackgroundColor string `json:"background_color"` |
| 479 |
ThemeColor string `json:"theme_color"` |
| 480 |
Icons []*webManifestIcon `json:"icons"` |
| 481 |
} |
| 482 |
|
| 483 |
type webManifestIcon struct { |
| 484 |
SRC string `json:"src"` |
| 485 |
Sizes string `json:"sizes"` |
| 486 |
Type string `json:"type"` |
| 487 |
} |