1
0

types.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. package server
  2. import (
  3. "net/http"
  4. "net/netip"
  5. "time"
  6. "heckel.io/ntfy/v2/log"
  7. "heckel.io/ntfy/v2/user"
  8. "heckel.io/ntfy/v2/util"
  9. )
  10. // List of possible events
  11. const (
  12. openEvent = "open"
  13. keepaliveEvent = "keepalive"
  14. messageEvent = "message"
  15. messageDeleteEvent = "message_delete"
  16. messageClearEvent = "message_clear"
  17. pollRequestEvent = "poll_request"
  18. )
  19. const (
  20. messageIDLength = 12
  21. )
  22. // message represents a message published to a topic
  23. type message struct {
  24. ID string `json:"id"` // Random message ID
  25. SequenceID string `json:"sequence_id,omitempty"` // Message sequence ID for updating message contents (omitted if same as ID)
  26. Time int64 `json:"time"` // Unix time in seconds
  27. Expires int64 `json:"expires,omitempty"` // Unix time in seconds (not required for open/keepalive)
  28. Event string `json:"event"` // One of the above
  29. Topic string `json:"topic"`
  30. Title string `json:"title,omitempty"`
  31. Message string `json:"message,omitempty"`
  32. Priority int `json:"priority,omitempty"`
  33. Tags []string `json:"tags,omitempty"`
  34. Click string `json:"click,omitempty"`
  35. Icon string `json:"icon,omitempty"`
  36. Actions []*action `json:"actions,omitempty"`
  37. Attachment *attachment `json:"attachment,omitempty"`
  38. PollID string `json:"poll_id,omitempty"`
  39. ContentType string `json:"content_type,omitempty"` // text/plain by default (if empty), or text/markdown
  40. Encoding string `json:"encoding,omitempty"` // Empty for raw UTF-8, or "base64" for encoded bytes
  41. Sender netip.Addr `json:"-"` // IP address of uploader, used for rate limiting
  42. User string `json:"-"` // UserID of the uploader, used to associated attachments
  43. }
  44. func (m *message) Context() log.Context {
  45. fields := map[string]any{
  46. "topic": m.Topic,
  47. "message_id": m.ID,
  48. "message_sequence_id": m.SequenceID,
  49. "message_time": m.Time,
  50. "message_event": m.Event,
  51. "message_body_size": len(m.Message),
  52. }
  53. if m.Sender.IsValid() {
  54. fields["message_sender"] = m.Sender.String()
  55. }
  56. if m.User != "" {
  57. fields["message_user"] = m.User
  58. }
  59. return fields
  60. }
  61. // forJSON returns a copy of the message suitable for JSON output.
  62. // It clears the SequenceID if it equals the ID to reduce redundancy.
  63. func (m *message) forJSON() *message {
  64. if m.SequenceID == m.ID {
  65. clone := *m
  66. clone.SequenceID = ""
  67. return &clone
  68. }
  69. return m
  70. }
  71. type attachment struct {
  72. Name string `json:"name"`
  73. Type string `json:"type,omitempty"`
  74. Size int64 `json:"size,omitempty"`
  75. Expires int64 `json:"expires,omitempty"`
  76. URL string `json:"url"`
  77. }
  78. type action struct {
  79. ID string `json:"id"`
  80. Action string `json:"action"` // "view", "broadcast", "http", or "copy"
  81. Label string `json:"label"` // action button label
  82. Clear bool `json:"clear"` // clear notification after successful execution
  83. URL string `json:"url,omitempty"` // used in "view" and "http" actions
  84. Method string `json:"method,omitempty"` // used in "http" action, default is POST (!)
  85. Headers map[string]string `json:"headers,omitempty"` // used in "http" action
  86. Body string `json:"body,omitempty"` // used in "http" action
  87. Intent string `json:"intent,omitempty"` // used in "broadcast" action
  88. Extras map[string]string `json:"extras,omitempty"` // used in "broadcast" action
  89. Value string `json:"value,omitempty"` // used in "copy" action
  90. }
  91. func newAction() *action {
  92. return &action{
  93. Headers: make(map[string]string),
  94. Extras: make(map[string]string),
  95. }
  96. }
  97. // publishMessage is used as input when publishing as JSON
  98. type publishMessage struct {
  99. Topic string `json:"topic"`
  100. SequenceID string `json:"sequence_id"`
  101. Title string `json:"title"`
  102. Message string `json:"message"`
  103. Priority int `json:"priority"`
  104. Tags []string `json:"tags"`
  105. Click string `json:"click"`
  106. Icon string `json:"icon"`
  107. Actions []action `json:"actions"`
  108. Attach string `json:"attach"`
  109. Markdown bool `json:"markdown"`
  110. Filename string `json:"filename"`
  111. Email string `json:"email"`
  112. Call string `json:"call"`
  113. Cache string `json:"cache"` // use string as it defaults to true (or use &bool instead)
  114. Firebase string `json:"firebase"` // use string as it defaults to true (or use &bool instead)
  115. Delay string `json:"delay"`
  116. }
  117. // messageEncoder is a function that knows how to encode a message
  118. type messageEncoder func(msg *message) (string, error)
  119. // newMessage creates a new message with the current timestamp
  120. func newMessage(event, topic, msg string) *message {
  121. return &message{
  122. ID: util.RandomString(messageIDLength),
  123. Time: time.Now().Unix(),
  124. Event: event,
  125. Topic: topic,
  126. Message: msg,
  127. }
  128. }
  129. // newOpenMessage is a convenience method to create an open message
  130. func newOpenMessage(topic string) *message {
  131. return newMessage(openEvent, topic, "")
  132. }
  133. // newKeepaliveMessage is a convenience method to create a keepalive message
  134. func newKeepaliveMessage(topic string) *message {
  135. return newMessage(keepaliveEvent, topic, "")
  136. }
  137. // newDefaultMessage is a convenience method to create a notification message
  138. func newDefaultMessage(topic, msg string) *message {
  139. return newMessage(messageEvent, topic, msg)
  140. }
  141. // newPollRequestMessage is a convenience method to create a poll request message
  142. func newPollRequestMessage(topic, pollID string) *message {
  143. m := newMessage(pollRequestEvent, topic, newMessageBody)
  144. m.PollID = pollID
  145. return m
  146. }
  147. // newActionMessage creates a new action message (message_delete or message_clear)
  148. func newActionMessage(event, topic, sequenceID string) *message {
  149. m := newMessage(event, topic, "")
  150. m.SequenceID = sequenceID
  151. return m
  152. }
  153. func validMessageID(s string) bool {
  154. return util.ValidRandomString(s, messageIDLength)
  155. }
  156. type sinceMarker struct {
  157. time time.Time
  158. id string
  159. }
  160. func newSinceTime(timestamp int64) sinceMarker {
  161. return sinceMarker{time.Unix(timestamp, 0), ""}
  162. }
  163. func newSinceID(id string) sinceMarker {
  164. return sinceMarker{time.Unix(0, 0), id}
  165. }
  166. func (t sinceMarker) IsAll() bool {
  167. return t == sinceAllMessages
  168. }
  169. func (t sinceMarker) IsNone() bool {
  170. return t == sinceNoMessages
  171. }
  172. func (t sinceMarker) IsLatest() bool {
  173. return t == sinceLatestMessage
  174. }
  175. func (t sinceMarker) IsID() bool {
  176. return t.id != "" && t.id != "latest"
  177. }
  178. func (t sinceMarker) Time() time.Time {
  179. return t.time
  180. }
  181. func (t sinceMarker) ID() string {
  182. return t.id
  183. }
  184. var (
  185. sinceAllMessages = sinceMarker{time.Unix(0, 0), ""}
  186. sinceNoMessages = sinceMarker{time.Unix(1, 0), ""}
  187. sinceLatestMessage = sinceMarker{time.Unix(0, 0), "latest"}
  188. )
  189. type queryFilter struct {
  190. ID string
  191. Message string
  192. Title string
  193. Tags []string
  194. Priority []int
  195. }
  196. func parseQueryFilters(r *http.Request) (*queryFilter, error) {
  197. idFilter := readParam(r, "x-id", "id")
  198. messageFilter := readParam(r, "x-message", "message", "m")
  199. titleFilter := readParam(r, "x-title", "title", "t")
  200. tagsFilter := util.SplitNoEmpty(readParam(r, "x-tags", "tags", "tag", "ta"), ",")
  201. priorityFilter := make([]int, 0)
  202. for _, p := range util.SplitNoEmpty(readParam(r, "x-priority", "priority", "prio", "p"), ",") {
  203. priority, err := util.ParsePriority(p)
  204. if err != nil {
  205. return nil, errHTTPBadRequestPriorityInvalid
  206. }
  207. priorityFilter = append(priorityFilter, priority)
  208. }
  209. return &queryFilter{
  210. ID: idFilter,
  211. Message: messageFilter,
  212. Title: titleFilter,
  213. Tags: tagsFilter,
  214. Priority: priorityFilter,
  215. }, nil
  216. }
  217. func (q *queryFilter) Pass(msg *message) bool {
  218. if msg.Event != messageEvent && msg.Event != messageDeleteEvent && msg.Event != messageClearEvent {
  219. return true // filters only apply to messages
  220. } else if q.ID != "" && msg.ID != q.ID {
  221. return false
  222. } else if q.Message != "" && msg.Message != q.Message {
  223. return false
  224. } else if q.Title != "" && msg.Title != q.Title {
  225. return false
  226. }
  227. messagePriority := msg.Priority
  228. if messagePriority == 0 {
  229. messagePriority = 3 // For query filters, default priority (3) is the same as "not set" (0)
  230. }
  231. if len(q.Priority) > 0 && !util.Contains(q.Priority, messagePriority) {
  232. return false
  233. }
  234. if len(q.Tags) > 0 && !util.ContainsAll(msg.Tags, q.Tags) {
  235. return false
  236. }
  237. return true
  238. }
  239. // templateMode represents the mode in which templates are used
  240. //
  241. // It can be
  242. // - empty: templating is disabled
  243. // - a boolean string (yes/1/true/no/0/false): inline-templating mode
  244. // - a filename (e.g. grafana): template mode with a file
  245. type templateMode string
  246. // Enabled returns true if templating is enabled
  247. func (t templateMode) Enabled() bool {
  248. return t != ""
  249. }
  250. // InlineMode returns true if inline-templating mode is enabled
  251. func (t templateMode) InlineMode() bool {
  252. return t.Enabled() && isBoolValue(string(t))
  253. }
  254. // FileMode returns true if file-templating mode is enabled
  255. func (t templateMode) FileMode() bool {
  256. return t.Enabled() && !isBoolValue(string(t))
  257. }
  258. // FileName returns the filename if file-templating mode is enabled, or an empty string otherwise
  259. func (t templateMode) FileName() string {
  260. if t.FileMode() {
  261. return string(t)
  262. }
  263. return ""
  264. }
  265. // templateFile represents a template file with title, message, and priority
  266. // It is used for file-based templates, e.g. grafana, influxdb, etc.
  267. //
  268. // Example YAML:
  269. //
  270. // title: "Alert: {{ .Title }}"
  271. // message: |
  272. // This is a {{ .Type }} alert.
  273. // It can be multiline.
  274. // priority: '{{ if eq .status "Error" }}5{{ else }}3{{ end }}'
  275. type templateFile struct {
  276. Title *string `yaml:"title"`
  277. Message *string `yaml:"message"`
  278. Priority *string `yaml:"priority"`
  279. }
  280. type apiHealthResponse struct {
  281. Healthy bool `json:"healthy"`
  282. }
  283. type apiStatsResponse struct {
  284. Messages int64 `json:"messages"`
  285. MessagesRate float64 `json:"messages_rate"` // Average number of messages per second
  286. }
  287. type apiUserAddOrUpdateRequest struct {
  288. Username string `json:"username"`
  289. Password string `json:"password"`
  290. Hash string `json:"hash"`
  291. Tier string `json:"tier"`
  292. // Do not add 'role' here. We don't want to add admins via the API.
  293. }
  294. type apiUserResponse struct {
  295. Username string `json:"username"`
  296. Role string `json:"role"`
  297. Tier string `json:"tier,omitempty"`
  298. Grants []*apiUserGrantResponse `json:"grants,omitempty"`
  299. }
  300. type apiUserGrantResponse struct {
  301. Topic string `json:"topic"` // This may be a pattern
  302. Permission string `json:"permission"`
  303. }
  304. type apiUserDeleteRequest struct {
  305. Username string `json:"username"`
  306. }
  307. type apiAccessAllowRequest struct {
  308. Username string `json:"username"`
  309. Topic string `json:"topic"` // This may be a pattern
  310. Permission string `json:"permission"`
  311. }
  312. type apiAccessResetRequest struct {
  313. Username string `json:"username"`
  314. Topic string `json:"topic"`
  315. }
  316. type apiAccountCreateRequest struct {
  317. Username string `json:"username"`
  318. Password string `json:"password"`
  319. }
  320. type apiAccountPasswordChangeRequest struct {
  321. Password string `json:"password"`
  322. NewPassword string `json:"new_password"`
  323. }
  324. type apiAccountDeleteRequest struct {
  325. Password string `json:"password"`
  326. }
  327. type apiAccountTokenIssueRequest struct {
  328. Label *string `json:"label"`
  329. Expires *int64 `json:"expires"` // Unix timestamp
  330. }
  331. type apiAccountTokenUpdateRequest struct {
  332. Token string `json:"token"`
  333. Label *string `json:"label"`
  334. Expires *int64 `json:"expires"` // Unix timestamp
  335. }
  336. type apiAccountTokenResponse struct {
  337. Token string `json:"token"`
  338. Label string `json:"label,omitempty"`
  339. LastAccess int64 `json:"last_access,omitempty"`
  340. LastOrigin string `json:"last_origin,omitempty"`
  341. Expires int64 `json:"expires,omitempty"` // Unix timestamp
  342. Provisioned bool `json:"provisioned,omitempty"` // True if this token was provisioned by the server config
  343. }
  344. type apiAccountPhoneNumberVerifyRequest struct {
  345. Number string `json:"number"`
  346. Channel string `json:"channel"`
  347. }
  348. type apiAccountPhoneNumberAddRequest struct {
  349. Number string `json:"number"`
  350. Code string `json:"code"` // Only set when adding a phone number
  351. }
  352. type apiAccountTier struct {
  353. Code string `json:"code"`
  354. Name string `json:"name"`
  355. }
  356. type apiAccountLimits struct {
  357. Basis string `json:"basis,omitempty"` // "ip" or "tier"
  358. Messages int64 `json:"messages"`
  359. MessagesExpiryDuration int64 `json:"messages_expiry_duration"`
  360. Emails int64 `json:"emails"`
  361. Calls int64 `json:"calls"`
  362. Reservations int64 `json:"reservations"`
  363. AttachmentTotalSize int64 `json:"attachment_total_size"`
  364. AttachmentFileSize int64 `json:"attachment_file_size"`
  365. AttachmentExpiryDuration int64 `json:"attachment_expiry_duration"`
  366. AttachmentBandwidth int64 `json:"attachment_bandwidth"`
  367. }
  368. type apiAccountStats struct {
  369. Messages int64 `json:"messages"`
  370. MessagesRemaining int64 `json:"messages_remaining"`
  371. Emails int64 `json:"emails"`
  372. EmailsRemaining int64 `json:"emails_remaining"`
  373. Calls int64 `json:"calls"`
  374. CallsRemaining int64 `json:"calls_remaining"`
  375. Reservations int64 `json:"reservations"`
  376. ReservationsRemaining int64 `json:"reservations_remaining"`
  377. AttachmentTotalSize int64 `json:"attachment_total_size"`
  378. AttachmentTotalSizeRemaining int64 `json:"attachment_total_size_remaining"`
  379. }
  380. type apiAccountReservation struct {
  381. Topic string `json:"topic"`
  382. Everyone string `json:"everyone"`
  383. }
  384. type apiAccountBilling struct {
  385. Customer bool `json:"customer"`
  386. Subscription bool `json:"subscription"`
  387. Status string `json:"status,omitempty"`
  388. Interval string `json:"interval,omitempty"`
  389. PaidUntil int64 `json:"paid_until,omitempty"`
  390. CancelAt int64 `json:"cancel_at,omitempty"`
  391. }
  392. type apiAccountResponse struct {
  393. Username string `json:"username"`
  394. Role string `json:"role,omitempty"`
  395. SyncTopic string `json:"sync_topic,omitempty"`
  396. Provisioned bool `json:"provisioned,omitempty"`
  397. Language string `json:"language,omitempty"`
  398. Notification *user.NotificationPrefs `json:"notification,omitempty"`
  399. Subscriptions []*user.Subscription `json:"subscriptions,omitempty"`
  400. Reservations []*apiAccountReservation `json:"reservations,omitempty"`
  401. Tokens []*apiAccountTokenResponse `json:"tokens,omitempty"`
  402. PhoneNumbers []string `json:"phone_numbers,omitempty"`
  403. Tier *apiAccountTier `json:"tier,omitempty"`
  404. Limits *apiAccountLimits `json:"limits,omitempty"`
  405. Stats *apiAccountStats `json:"stats,omitempty"`
  406. Billing *apiAccountBilling `json:"billing,omitempty"`
  407. }
  408. type apiAccountReservationRequest struct {
  409. Topic string `json:"topic"`
  410. Everyone string `json:"everyone"`
  411. }
  412. type apiConfigResponse struct {
  413. BaseURL string `json:"base_url"`
  414. AppRoot string `json:"app_root"`
  415. EnableLogin bool `json:"enable_login"`
  416. RequireLogin bool `json:"require_login"`
  417. EnableSignup bool `json:"enable_signup"`
  418. EnablePayments bool `json:"enable_payments"`
  419. EnableCalls bool `json:"enable_calls"`
  420. EnableEmails bool `json:"enable_emails"`
  421. EnableReservations bool `json:"enable_reservations"`
  422. EnableWebPush bool `json:"enable_web_push"`
  423. BillingContact string `json:"billing_contact"`
  424. WebPushPublicKey string `json:"web_push_public_key"`
  425. DisallowedTopics []string `json:"disallowed_topics"`
  426. ConfigHash string `json:"config_hash"`
  427. }
  428. type apiAccountBillingPrices struct {
  429. Month int64 `json:"month"`
  430. Year int64 `json:"year"`
  431. }
  432. type apiAccountBillingTier struct {
  433. Code string `json:"code,omitempty"`
  434. Name string `json:"name,omitempty"`
  435. Prices *apiAccountBillingPrices `json:"prices,omitempty"`
  436. Limits *apiAccountLimits `json:"limits"`
  437. }
  438. type apiAccountBillingSubscriptionCreateResponse struct {
  439. RedirectURL string `json:"redirect_url"`
  440. }
  441. type apiAccountBillingSubscriptionChangeRequest struct {
  442. Tier string `json:"tier"`
  443. Interval string `json:"interval"`
  444. }
  445. type apiAccountBillingPortalRedirectResponse struct {
  446. RedirectURL string `json:"redirect_url"`
  447. }
  448. type apiAccountSyncTopicResponse struct {
  449. Event string `json:"event"`
  450. }
  451. type apiSuccessResponse struct {
  452. Success bool `json:"success"`
  453. }
  454. func newSuccessResponse() *apiSuccessResponse {
  455. return &apiSuccessResponse{
  456. Success: true,
  457. }
  458. }
  459. type apiStripeSubscriptionUpdatedEvent struct {
  460. ID string `json:"id"`
  461. Customer string `json:"customer"`
  462. Status string `json:"status"`
  463. CurrentPeriodEnd int64 `json:"current_period_end"`
  464. CancelAt int64 `json:"cancel_at"`
  465. Items *struct {
  466. Data []*struct {
  467. Price *struct {
  468. ID string `json:"id"`
  469. Recurring *struct {
  470. Interval string `json:"interval"`
  471. } `json:"recurring"`
  472. } `json:"price"`
  473. } `json:"data"`
  474. } `json:"items"`
  475. }
  476. type apiStripeSubscriptionDeletedEvent struct {
  477. ID string `json:"id"`
  478. Customer string `json:"customer"`
  479. }
  480. type apiWebPushUpdateSubscriptionRequest struct {
  481. Endpoint string `json:"endpoint"`
  482. Auth string `json:"auth"`
  483. P256dh string `json:"p256dh"`
  484. Topics []string `json:"topics"`
  485. }
  486. // List of possible Web Push events (see sw.js)
  487. const (
  488. webPushMessageEvent = "message"
  489. webPushExpiringEvent = "subscription_expiring"
  490. )
  491. type webPushPayload struct {
  492. Event string `json:"event"`
  493. SubscriptionID string `json:"subscription_id"`
  494. Message *message `json:"message"`
  495. }
  496. func newWebPushPayload(subscriptionID string, message *message) *webPushPayload {
  497. return &webPushPayload{
  498. Event: webPushMessageEvent,
  499. SubscriptionID: subscriptionID,
  500. Message: message,
  501. }
  502. }
  503. type webPushControlMessagePayload struct {
  504. Event string `json:"event"`
  505. }
  506. func newWebPushSubscriptionExpiringPayload() *webPushControlMessagePayload {
  507. return &webPushControlMessagePayload{
  508. Event: webPushExpiringEvent,
  509. }
  510. }
  511. type webPushSubscription struct {
  512. ID string
  513. Endpoint string
  514. Auth string
  515. P256dh string
  516. UserID string
  517. }
  518. func (w *webPushSubscription) Context() log.Context {
  519. return map[string]any{
  520. "web_push_subscription_id": w.ID,
  521. "web_push_subscription_user_id": w.UserID,
  522. "web_push_subscription_endpoint": w.Endpoint,
  523. }
  524. }
  525. // https://developer.mozilla.org/en-US/docs/Web/Manifest
  526. type webManifestResponse struct {
  527. Name string `json:"name"`
  528. Description string `json:"description"`
  529. ShortName string `json:"short_name"`
  530. Scope string `json:"scope"`
  531. StartURL string `json:"start_url"`
  532. Display string `json:"display"`
  533. BackgroundColor string `json:"background_color"`
  534. ThemeColor string `json:"theme_color"`
  535. Icons []*webManifestIcon `json:"icons"`
  536. }
  537. type webManifestIcon struct {
  538. SRC string `json:"src"`
  539. Sizes string `json:"sizes"`
  540. Type string `json:"type"`
  541. }