2021-10-27 20:56:17 +02:00
|
|
|
package server
|
|
|
|
|
2021-10-29 19:58:14 +02:00
|
|
|
import (
|
2022-12-25 17:41:38 +01:00
|
|
|
"heckel.io/ntfy/user"
|
2022-01-16 05:17:46 +01:00
|
|
|
"net/http"
|
2022-10-05 22:42:07 +02:00
|
|
|
"net/netip"
|
2021-10-29 19:58:14 +02:00
|
|
|
"time"
|
2022-10-05 22:42:07 +02:00
|
|
|
|
|
|
|
"heckel.io/ntfy/util"
|
2021-10-29 19:58:14 +02:00
|
|
|
)
|
2021-10-27 20:56:17 +02:00
|
|
|
|
|
|
|
// List of possible events
|
|
|
|
const (
|
2022-02-01 01:33:22 +01:00
|
|
|
openEvent = "open"
|
|
|
|
keepaliveEvent = "keepalive"
|
|
|
|
messageEvent = "message"
|
|
|
|
pollRequestEvent = "poll_request"
|
2021-10-29 19:58:14 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-02-26 21:57:10 +01:00
|
|
|
messageIDLength = 12
|
2021-10-27 20:56:17 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// message represents a message published to a topic
|
|
|
|
type message struct {
|
2022-01-02 23:56:12 +01:00
|
|
|
ID string `json:"id"` // Random message ID
|
|
|
|
Time int64 `json:"time"` // Unix time in seconds
|
|
|
|
Event string `json:"event"` // One of the above
|
|
|
|
Topic string `json:"topic"`
|
2022-05-27 13:55:57 +02:00
|
|
|
Title string `json:"title,omitempty"`
|
|
|
|
Message string `json:"message,omitempty"`
|
2022-01-02 23:56:12 +01:00
|
|
|
Priority int `json:"priority,omitempty"`
|
|
|
|
Tags []string `json:"tags,omitempty"`
|
2022-01-05 00:25:49 +01:00
|
|
|
Click string `json:"click,omitempty"`
|
2022-09-11 22:31:39 +02:00
|
|
|
Icon string `json:"icon,omitempty"`
|
2022-04-19 15:14:32 +02:00
|
|
|
Actions []*action `json:"actions,omitempty"`
|
2022-01-05 00:25:49 +01:00
|
|
|
Attachment *attachment `json:"attachment,omitempty"`
|
2022-05-27 13:55:57 +02:00
|
|
|
PollID string `json:"poll_id,omitempty"`
|
2022-01-17 19:28:07 +01:00
|
|
|
Encoding string `json:"encoding,omitempty"` // empty for raw UTF-8, or "base64" for encoded bytes
|
2022-12-20 03:42:36 +01:00
|
|
|
Sender netip.Addr `json:"-"` // IP address of uploader, used for rate limiting
|
|
|
|
User string `json:"-"` // Username of the uploader, used to associated attachments
|
2022-01-02 23:56:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type attachment struct {
|
2022-01-07 14:49:28 +01:00
|
|
|
Name string `json:"name"`
|
|
|
|
Type string `json:"type,omitempty"`
|
|
|
|
Size int64 `json:"size,omitempty"`
|
|
|
|
Expires int64 `json:"expires,omitempty"`
|
|
|
|
URL string `json:"url"`
|
2021-10-27 20:56:17 +02:00
|
|
|
}
|
|
|
|
|
2022-04-16 22:17:58 +02:00
|
|
|
type action struct {
|
2022-04-17 20:29:43 +02:00
|
|
|
ID string `json:"id"`
|
2022-04-23 05:07:35 +02:00
|
|
|
Action string `json:"action"` // "view", "broadcast", or "http"
|
|
|
|
Label string `json:"label"` // action button label
|
|
|
|
Clear bool `json:"clear"` // clear notification after successful execution
|
2022-04-19 15:14:32 +02:00
|
|
|
URL string `json:"url,omitempty"` // used in "view" and "http" actions
|
|
|
|
Method string `json:"method,omitempty"` // used in "http" action, default is POST (!)
|
|
|
|
Headers map[string]string `json:"headers,omitempty"` // used in "http" action
|
|
|
|
Body string `json:"body,omitempty"` // used in "http" action
|
2022-04-20 01:22:19 +02:00
|
|
|
Intent string `json:"intent,omitempty"` // used in "broadcast" action
|
2022-04-19 15:14:32 +02:00
|
|
|
Extras map[string]string `json:"extras,omitempty"` // used in "broadcast" action
|
2022-04-16 22:17:58 +02:00
|
|
|
}
|
|
|
|
|
2022-04-27 15:51:23 +02:00
|
|
|
func newAction() *action {
|
|
|
|
return &action{
|
|
|
|
Headers: make(map[string]string),
|
|
|
|
Extras: make(map[string]string),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-15 21:00:59 +01:00
|
|
|
// publishMessage is used as input when publishing as JSON
|
|
|
|
type publishMessage struct {
|
2022-03-16 19:16:54 +01:00
|
|
|
Topic string `json:"topic"`
|
|
|
|
Title string `json:"title"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
Priority int `json:"priority"`
|
|
|
|
Tags []string `json:"tags"`
|
|
|
|
Click string `json:"click"`
|
2022-09-11 22:31:39 +02:00
|
|
|
Icon string `json:"icon"`
|
2022-04-16 22:17:58 +02:00
|
|
|
Actions []action `json:"actions"`
|
2022-03-16 19:16:54 +01:00
|
|
|
Attach string `json:"attach"`
|
|
|
|
Filename string `json:"filename"`
|
2022-03-29 21:40:26 +02:00
|
|
|
Email string `json:"email"`
|
|
|
|
Delay string `json:"delay"`
|
2022-03-15 21:00:59 +01:00
|
|
|
}
|
|
|
|
|
2021-10-27 20:56:17 +02:00
|
|
|
// messageEncoder is a function that knows how to encode a message
|
|
|
|
type messageEncoder func(msg *message) (string, error)
|
|
|
|
|
|
|
|
// newMessage creates a new message with the current timestamp
|
2021-10-29 19:58:14 +02:00
|
|
|
func newMessage(event, topic, msg string) *message {
|
2021-10-27 20:56:17 +02:00
|
|
|
return &message{
|
2022-05-27 13:55:57 +02:00
|
|
|
ID: util.RandomString(messageIDLength),
|
|
|
|
Time: time.Now().Unix(),
|
|
|
|
Event: event,
|
|
|
|
Topic: topic,
|
|
|
|
Message: msg,
|
2021-10-27 20:56:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// newOpenMessage is a convenience method to create an open message
|
2021-10-29 19:58:14 +02:00
|
|
|
func newOpenMessage(topic string) *message {
|
|
|
|
return newMessage(openEvent, topic, "")
|
2021-10-27 20:56:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// newKeepaliveMessage is a convenience method to create a keepalive message
|
2021-10-29 19:58:14 +02:00
|
|
|
func newKeepaliveMessage(topic string) *message {
|
|
|
|
return newMessage(keepaliveEvent, topic, "")
|
2021-10-27 20:56:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// newDefaultMessage is a convenience method to create a notification message
|
2021-10-29 19:58:14 +02:00
|
|
|
func newDefaultMessage(topic, msg string) *message {
|
|
|
|
return newMessage(messageEvent, topic, msg)
|
2021-10-27 20:56:17 +02:00
|
|
|
}
|
2022-01-16 05:17:46 +01:00
|
|
|
|
2022-05-27 13:55:57 +02:00
|
|
|
// newPollRequestMessage is a convenience method to create a poll request message
|
|
|
|
func newPollRequestMessage(topic, pollID string) *message {
|
|
|
|
m := newMessage(pollRequestEvent, topic, newMessageBody)
|
|
|
|
m.PollID = pollID
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2022-02-26 21:57:10 +01:00
|
|
|
func validMessageID(s string) bool {
|
|
|
|
return util.ValidRandomString(s, messageIDLength)
|
|
|
|
}
|
|
|
|
|
|
|
|
type sinceMarker struct {
|
|
|
|
time time.Time
|
|
|
|
id string
|
|
|
|
}
|
2022-01-16 05:17:46 +01:00
|
|
|
|
2022-02-26 21:57:10 +01:00
|
|
|
func newSinceTime(timestamp int64) sinceMarker {
|
|
|
|
return sinceMarker{time.Unix(timestamp, 0), ""}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newSinceID(id string) sinceMarker {
|
|
|
|
return sinceMarker{time.Unix(0, 0), id}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t sinceMarker) IsAll() bool {
|
2022-01-16 05:17:46 +01:00
|
|
|
return t == sinceAllMessages
|
|
|
|
}
|
|
|
|
|
2022-02-26 21:57:10 +01:00
|
|
|
func (t sinceMarker) IsNone() bool {
|
2022-01-16 05:17:46 +01:00
|
|
|
return t == sinceNoMessages
|
|
|
|
}
|
|
|
|
|
2022-02-26 21:57:10 +01:00
|
|
|
func (t sinceMarker) IsID() bool {
|
|
|
|
return t.id != ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t sinceMarker) Time() time.Time {
|
|
|
|
return t.time
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t sinceMarker) ID() string {
|
|
|
|
return t.id
|
2022-01-16 05:17:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2022-02-26 21:57:10 +01:00
|
|
|
sinceAllMessages = sinceMarker{time.Unix(0, 0), ""}
|
|
|
|
sinceNoMessages = sinceMarker{time.Unix(1, 0), ""}
|
2022-01-16 05:17:46 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type queryFilter struct {
|
2022-05-27 00:52:55 +02:00
|
|
|
ID string
|
2022-01-16 05:17:46 +01:00
|
|
|
Message string
|
|
|
|
Title string
|
|
|
|
Tags []string
|
|
|
|
Priority []int
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseQueryFilters(r *http.Request) (*queryFilter, error) {
|
2022-05-27 00:52:55 +02:00
|
|
|
idFilter := readParam(r, "x-id", "id")
|
2022-01-16 05:17:46 +01:00
|
|
|
messageFilter := readParam(r, "x-message", "message", "m")
|
|
|
|
titleFilter := readParam(r, "x-title", "title", "t")
|
|
|
|
tagsFilter := util.SplitNoEmpty(readParam(r, "x-tags", "tags", "tag", "ta"), ",")
|
|
|
|
priorityFilter := make([]int, 0)
|
|
|
|
for _, p := range util.SplitNoEmpty(readParam(r, "x-priority", "priority", "prio", "p"), ",") {
|
|
|
|
priority, err := util.ParsePriority(p)
|
|
|
|
if err != nil {
|
2022-07-01 15:28:42 +02:00
|
|
|
return nil, errHTTPBadRequestPriorityInvalid
|
2022-01-16 05:17:46 +01:00
|
|
|
}
|
|
|
|
priorityFilter = append(priorityFilter, priority)
|
|
|
|
}
|
|
|
|
return &queryFilter{
|
2022-05-27 00:52:55 +02:00
|
|
|
ID: idFilter,
|
2022-01-16 05:17:46 +01:00
|
|
|
Message: messageFilter,
|
|
|
|
Title: titleFilter,
|
|
|
|
Tags: tagsFilter,
|
|
|
|
Priority: priorityFilter,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *queryFilter) Pass(msg *message) bool {
|
|
|
|
if msg.Event != messageEvent {
|
|
|
|
return true // filters only apply to messages
|
2022-05-27 00:52:55 +02:00
|
|
|
} else if q.ID != "" && msg.ID != q.ID {
|
2022-01-16 05:17:46 +01:00
|
|
|
return false
|
2022-05-27 00:52:55 +02:00
|
|
|
} else if q.Message != "" && msg.Message != q.Message {
|
|
|
|
return false
|
|
|
|
} else if q.Title != "" && msg.Title != q.Title {
|
2022-01-16 05:17:46 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
messagePriority := msg.Priority
|
|
|
|
if messagePriority == 0 {
|
|
|
|
messagePriority = 3 // For query filters, default priority (3) is the same as "not set" (0)
|
|
|
|
}
|
2022-10-01 21:50:48 +02:00
|
|
|
if len(q.Priority) > 0 && !util.Contains(q.Priority, messagePriority) {
|
2022-01-16 05:17:46 +01:00
|
|
|
return false
|
|
|
|
}
|
2022-10-01 21:50:48 +02:00
|
|
|
if len(q.Tags) > 0 && !util.ContainsAll(msg.Tags, q.Tags) {
|
2022-01-16 05:17:46 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2022-12-15 05:11:22 +01:00
|
|
|
|
2022-12-24 18:22:54 +01:00
|
|
|
type apiHealthResponse struct {
|
|
|
|
Healthy bool `json:"healthy"`
|
|
|
|
}
|
2022-12-24 18:26:56 +01:00
|
|
|
|
2022-12-15 05:11:22 +01:00
|
|
|
type apiAccountCreateRequest struct {
|
|
|
|
Username string `json:"username"`
|
|
|
|
Password string `json:"password"`
|
|
|
|
}
|
|
|
|
|
2022-12-29 04:16:11 +01:00
|
|
|
type apiAccountPasswordChangeRequest struct {
|
|
|
|
Password string `json:"password"`
|
|
|
|
}
|
|
|
|
|
2022-12-15 05:11:22 +01:00
|
|
|
type apiAccountTokenResponse struct {
|
2022-12-25 17:41:38 +01:00
|
|
|
Token string `json:"token"`
|
|
|
|
Expires int64 `json:"expires"`
|
2022-12-15 05:11:22 +01:00
|
|
|
}
|
|
|
|
|
2022-12-19 22:22:13 +01:00
|
|
|
type apiAccountPlan struct {
|
2022-12-19 15:59:32 +01:00
|
|
|
Code string `json:"code"`
|
|
|
|
Upgradable bool `json:"upgradable"`
|
2022-12-17 21:17:52 +01:00
|
|
|
}
|
|
|
|
|
2022-12-19 15:59:32 +01:00
|
|
|
type apiAccountLimits struct {
|
2022-12-19 22:22:13 +01:00
|
|
|
Basis string `json:"basis"` // "ip", "role" or "plan"
|
|
|
|
Messages int64 `json:"messages"`
|
|
|
|
Emails int64 `json:"emails"`
|
|
|
|
AttachmentTotalSize int64 `json:"attachment_total_size"`
|
|
|
|
AttachmentFileSize int64 `json:"attachment_file_size"`
|
2022-12-19 15:59:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type apiAccountStats struct {
|
2022-12-19 22:22:13 +01:00
|
|
|
Messages int64 `json:"messages"`
|
|
|
|
MessagesRemaining int64 `json:"messages_remaining"`
|
|
|
|
Emails int64 `json:"emails"`
|
|
|
|
EmailsRemaining int64 `json:"emails_remaining"`
|
|
|
|
AttachmentTotalSize int64 `json:"attachment_total_size"`
|
|
|
|
AttachmentTotalSizeRemaining int64 `json:"attachment_total_size_remaining"`
|
2022-12-15 05:11:22 +01:00
|
|
|
}
|
|
|
|
|
2022-12-28 04:14:14 +01:00
|
|
|
type apiAccountResponse struct {
|
2022-12-25 17:41:38 +01:00
|
|
|
Username string `json:"username"`
|
|
|
|
Role string `json:"role,omitempty"`
|
|
|
|
Language string `json:"language,omitempty"`
|
|
|
|
Notification *user.NotificationPrefs `json:"notification,omitempty"`
|
|
|
|
Subscriptions []*user.Subscription `json:"subscriptions,omitempty"`
|
|
|
|
Plan *apiAccountPlan `json:"plan,omitempty"`
|
|
|
|
Limits *apiAccountLimits `json:"limits,omitempty"`
|
|
|
|
Stats *apiAccountStats `json:"stats,omitempty"`
|
2022-12-15 05:11:22 +01:00
|
|
|
}
|