2021-10-23 04:26:01 +03:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-10-29 06:50:38 +03:00
|
|
|
"context"
|
2021-12-24 02:03:04 +03:00
|
|
|
"embed"
|
2022-01-17 21:28:07 +03:00
|
|
|
"encoding/base64"
|
2021-10-23 04:26:01 +03:00
|
|
|
"encoding/json"
|
2021-12-27 18:39:28 +03:00
|
|
|
"errors"
|
2021-10-23 20:21:33 +03:00
|
|
|
"fmt"
|
2021-12-27 17:48:09 +03:00
|
|
|
"github.com/emersion/go-smtp"
|
2022-01-15 21:23:35 +03:00
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
"golang.org/x/sync/errgroup"
|
2022-01-23 08:02:16 +03:00
|
|
|
"heckel.io/ntfy/auth"
|
2021-12-26 00:07:55 +03:00
|
|
|
"heckel.io/ntfy/util"
|
2021-11-08 17:24:34 +03:00
|
|
|
"html/template"
|
2021-10-23 04:26:01 +03:00
|
|
|
"io"
|
|
|
|
"log"
|
2021-10-24 05:49:50 +03:00
|
|
|
"net"
|
2021-10-23 04:26:01 +03:00
|
|
|
"net/http"
|
2021-12-27 18:39:28 +03:00
|
|
|
"net/http/httptest"
|
2022-01-14 20:13:14 +03:00
|
|
|
"net/url"
|
2022-01-11 00:28:13 +03:00
|
|
|
"os"
|
2022-01-14 20:13:14 +03:00
|
|
|
"path"
|
2022-01-03 01:56:12 +03:00
|
|
|
"path/filepath"
|
2021-10-23 04:26:01 +03:00
|
|
|
"regexp"
|
2021-10-29 20:58:14 +03:00
|
|
|
"strconv"
|
2021-10-23 04:26:01 +03:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2022-01-03 01:56:12 +03:00
|
|
|
"unicode/utf8"
|
2021-10-23 04:26:01 +03:00
|
|
|
)
|
|
|
|
|
2021-12-07 19:45:15 +03:00
|
|
|
// Server is the main server, providing the UI and API for ntfy
|
2021-10-23 04:26:01 +03:00
|
|
|
type Server struct {
|
2022-01-11 00:28:13 +03:00
|
|
|
config *Config
|
|
|
|
httpServer *http.Server
|
|
|
|
httpsServer *http.Server
|
|
|
|
unixListener net.Listener
|
|
|
|
smtpServer *smtp.Server
|
|
|
|
smtpBackend *smtpBackend
|
|
|
|
topics map[string]*topic
|
|
|
|
visitors map[string]*visitor
|
|
|
|
firebase subscriber
|
|
|
|
mailer mailer
|
|
|
|
messages int64
|
2022-01-23 08:54:18 +03:00
|
|
|
auth auth.Auther
|
2022-01-11 00:28:13 +03:00
|
|
|
cache cache
|
2022-01-15 04:16:12 +03:00
|
|
|
fileCache *fileCache
|
2022-01-11 00:28:13 +03:00
|
|
|
closeChan chan bool
|
|
|
|
mu sync.Mutex
|
2021-10-23 04:26:01 +03:00
|
|
|
}
|
|
|
|
|
2021-11-08 17:24:34 +03:00
|
|
|
type indexPage struct {
|
|
|
|
Topic string
|
2021-12-09 18:23:17 +03:00
|
|
|
CacheDuration time.Duration
|
2021-11-08 17:24:34 +03:00
|
|
|
}
|
|
|
|
|
2022-01-22 06:22:27 +03:00
|
|
|
// handleFunc extends the normal http.HandlerFunc to be able to easily return errors
|
|
|
|
type handleFunc func(http.ResponseWriter, *http.Request, *visitor) error
|
|
|
|
|
2021-10-23 04:26:01 +03:00
|
|
|
var (
|
2022-01-31 19:44:58 +03:00
|
|
|
// If changed, don't forget to update Android App and auth_sqlite.go
|
2021-12-28 00:06:40 +03:00
|
|
|
topicRegex = regexp.MustCompile(`^[-_A-Za-z0-9]{1,64}$`) // No /!
|
|
|
|
topicPathRegex = regexp.MustCompile(`^/[-_A-Za-z0-9]{1,64}$`) // Regex must match JS & Android app!
|
|
|
|
jsonPathRegex = regexp.MustCompile(`^/[-_A-Za-z0-9]{1,64}(,[-_A-Za-z0-9]{1,64})*/json$`)
|
|
|
|
ssePathRegex = regexp.MustCompile(`^/[-_A-Za-z0-9]{1,64}(,[-_A-Za-z0-9]{1,64})*/sse$`)
|
|
|
|
rawPathRegex = regexp.MustCompile(`^/[-_A-Za-z0-9]{1,64}(,[-_A-Za-z0-9]{1,64})*/raw$`)
|
2022-01-15 21:23:35 +03:00
|
|
|
wsPathRegex = regexp.MustCompile(`^/[-_A-Za-z0-9]{1,64}(,[-_A-Za-z0-9]{1,64})*/ws$`)
|
2022-01-26 07:04:09 +03:00
|
|
|
authPathRegex = regexp.MustCompile(`^/[-_A-Za-z0-9]{1,64}(,[-_A-Za-z0-9]{1,64})*/auth$`)
|
2021-12-28 00:06:40 +03:00
|
|
|
publishPathRegex = regexp.MustCompile(`^/[-_A-Za-z0-9]{1,64}(,[-_A-Za-z0-9]{1,64})*/(publish|send|trigger)$`)
|
2021-10-29 20:58:14 +03:00
|
|
|
|
2021-12-09 06:13:59 +03:00
|
|
|
staticRegex = regexp.MustCompile(`^/static/.+`)
|
|
|
|
docsRegex = regexp.MustCompile(`^/docs(|/.*)$`)
|
2022-01-03 01:56:12 +03:00
|
|
|
fileRegex = regexp.MustCompile(`^/file/([-_A-Za-z0-9]{1,64})(?:\.[A-Za-z0-9]{1,16})?$`)
|
2022-01-31 19:44:58 +03:00
|
|
|
disallowedTopics = []string{"docs", "static", "file"} // If updated, also update in Android app
|
2022-01-08 23:47:08 +03:00
|
|
|
attachURLRegex = regexp.MustCompile(`^https?://`)
|
2021-10-23 04:26:01 +03:00
|
|
|
|
2021-12-09 18:23:17 +03:00
|
|
|
templateFnMap = template.FuncMap{
|
|
|
|
"durationToHuman": util.DurationToHuman,
|
|
|
|
}
|
|
|
|
|
2021-11-08 17:24:34 +03:00
|
|
|
//go:embed "index.gohtml"
|
|
|
|
indexSource string
|
2021-12-09 18:23:17 +03:00
|
|
|
indexTemplate = template.Must(template.New("index").Funcs(templateFnMap).Parse(indexSource))
|
2021-10-24 04:29:45 +03:00
|
|
|
|
2021-11-18 17:22:33 +03:00
|
|
|
//go:embed "example.html"
|
2021-11-28 00:12:08 +03:00
|
|
|
exampleSource string
|
2021-11-18 17:22:33 +03:00
|
|
|
|
2021-10-24 21:22:53 +03:00
|
|
|
//go:embed static
|
2021-11-29 17:34:43 +03:00
|
|
|
webStaticFs embed.FS
|
|
|
|
webStaticFsCached = &util.CachingEmbedFS{ModTime: time.Now(), FS: webStaticFs}
|
2021-10-24 21:22:53 +03:00
|
|
|
|
2021-12-03 01:27:31 +03:00
|
|
|
//go:embed docs
|
2021-12-07 18:38:58 +03:00
|
|
|
docsStaticFs embed.FS
|
2021-12-03 01:27:31 +03:00
|
|
|
docsStaticCached = &util.CachingEmbedFS{ModTime: time.Now(), FS: docsStaticFs}
|
2021-10-23 04:26:01 +03:00
|
|
|
)
|
|
|
|
|
2021-12-14 06:30:28 +03:00
|
|
|
const (
|
2022-01-13 05:24:48 +03:00
|
|
|
firebaseControlTopic = "~control" // See Android if changed
|
|
|
|
emptyMessageBody = "triggered" // Used if message body is empty
|
|
|
|
defaultAttachmentMessage = "You received a file: %s" // Used if message body is empty, and there is an attachment
|
2022-01-17 21:28:07 +03:00
|
|
|
encodingBase64 = "base64"
|
2022-01-16 06:33:35 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// WebSocket constants
|
|
|
|
const (
|
|
|
|
wsWriteWait = 2 * time.Second
|
|
|
|
wsBufferSize = 1024
|
|
|
|
wsReadLimit = 64 // We only ever receive PINGs
|
|
|
|
wsPongWait = 15 * time.Second
|
2021-12-14 06:30:28 +03:00
|
|
|
)
|
|
|
|
|
2021-12-07 19:45:15 +03:00
|
|
|
// New instantiates a new Server. It creates the cache and adds a Firebase
|
|
|
|
// subscriber (if configured).
|
2021-12-19 06:02:36 +03:00
|
|
|
func New(conf *Config) (*Server, error) {
|
2021-12-24 02:03:04 +03:00
|
|
|
var mailer mailer
|
2021-12-27 18:39:28 +03:00
|
|
|
if conf.SMTPSenderAddr != "" {
|
|
|
|
mailer = &smtpSender{config: conf}
|
2021-12-24 02:03:04 +03:00
|
|
|
}
|
2021-11-03 04:09:49 +03:00
|
|
|
cache, err := createCache(conf)
|
2021-11-02 21:08:21 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-03 04:09:49 +03:00
|
|
|
topics, err := cache.Topics()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-11-02 21:08:21 +03:00
|
|
|
}
|
2022-01-07 16:49:28 +03:00
|
|
|
var fileCache *fileCache
|
2022-01-03 01:56:12 +03:00
|
|
|
if conf.AttachmentCacheDir != "" {
|
2022-01-07 16:49:28 +03:00
|
|
|
fileCache, err = newFileCache(conf.AttachmentCacheDir, conf.AttachmentTotalSizeLimit, conf.AttachmentFileSizeLimit)
|
|
|
|
if err != nil {
|
2022-01-03 01:56:12 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2022-01-23 08:54:18 +03:00
|
|
|
var auther auth.Auther
|
2022-01-23 07:01:20 +03:00
|
|
|
if conf.AuthFile != "" {
|
2022-01-23 08:02:16 +03:00
|
|
|
auther, err = auth.NewSQLiteAuth(conf.AuthFile, conf.AuthDefaultRead, conf.AuthDefaultWrite)
|
2022-01-23 07:01:20 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-22 22:47:27 +03:00
|
|
|
}
|
2022-02-01 03:33:22 +03:00
|
|
|
var firebaseSubscriber subscriber
|
|
|
|
if conf.FirebaseKeyFile != "" {
|
2022-02-01 04:12:47 +03:00
|
|
|
var err error
|
|
|
|
firebaseSubscriber, err = createFirebaseSubscriber(conf.FirebaseKeyFile, auther)
|
2022-02-01 03:33:22 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2021-10-23 04:26:01 +03:00
|
|
|
return &Server{
|
2022-01-07 16:49:28 +03:00
|
|
|
config: conf,
|
|
|
|
cache: cache,
|
|
|
|
fileCache: fileCache,
|
|
|
|
firebase: firebaseSubscriber,
|
|
|
|
mailer: mailer,
|
|
|
|
topics: topics,
|
2022-01-23 08:02:16 +03:00
|
|
|
auth: auther,
|
2022-01-07 16:49:28 +03:00
|
|
|
visitors: make(map[string]*visitor),
|
2021-10-29 06:50:38 +03:00
|
|
|
}, nil
|
2021-10-23 04:26:01 +03:00
|
|
|
}
|
|
|
|
|
2021-12-19 06:02:36 +03:00
|
|
|
func createCache(conf *Config) (cache, error) {
|
2021-12-09 18:23:17 +03:00
|
|
|
if conf.CacheDuration == 0 {
|
|
|
|
return newNopCache(), nil
|
|
|
|
} else if conf.CacheFile != "" {
|
2021-11-03 04:09:49 +03:00
|
|
|
return newSqliteCache(conf.CacheFile)
|
2021-11-02 21:08:21 +03:00
|
|
|
}
|
2021-11-03 04:09:49 +03:00
|
|
|
return newMemCache(), nil
|
2021-11-02 21:08:21 +03:00
|
|
|
}
|
|
|
|
|
2021-12-07 19:45:15 +03:00
|
|
|
// Run executes the main server. It listens on HTTP (+ HTTPS, if configured), and starts
|
|
|
|
// a manager go routine to print stats and prune messages.
|
2021-10-23 04:26:01 +03:00
|
|
|
func (s *Server) Run() error {
|
2022-01-11 00:28:13 +03:00
|
|
|
var listenStr string
|
|
|
|
if s.config.ListenHTTP != "" {
|
|
|
|
listenStr += fmt.Sprintf(" %s[http]", s.config.ListenHTTP)
|
|
|
|
}
|
2021-12-02 16:52:48 +03:00
|
|
|
if s.config.ListenHTTPS != "" {
|
2022-01-11 00:28:13 +03:00
|
|
|
listenStr += fmt.Sprintf(" %s[https]", s.config.ListenHTTPS)
|
|
|
|
}
|
|
|
|
if s.config.ListenUnix != "" {
|
|
|
|
listenStr += fmt.Sprintf(" %s[unix]", s.config.ListenUnix)
|
2021-12-02 16:52:48 +03:00
|
|
|
}
|
2021-12-28 00:06:40 +03:00
|
|
|
if s.config.SMTPServerListen != "" {
|
2022-01-11 00:28:13 +03:00
|
|
|
listenStr += fmt.Sprintf(" %s[smtp]", s.config.SMTPServerListen)
|
2021-12-28 00:06:40 +03:00
|
|
|
}
|
2022-01-11 00:28:13 +03:00
|
|
|
log.Printf("Listening on%s", listenStr)
|
2021-12-23 01:45:19 +03:00
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("/", s.handle)
|
2021-12-02 16:52:48 +03:00
|
|
|
errChan := make(chan error)
|
2021-12-22 16:17:50 +03:00
|
|
|
s.mu.Lock()
|
|
|
|
s.closeChan = make(chan bool)
|
2022-01-11 00:28:13 +03:00
|
|
|
if s.config.ListenHTTP != "" {
|
|
|
|
s.httpServer = &http.Server{Addr: s.config.ListenHTTP, Handler: mux}
|
|
|
|
go func() {
|
|
|
|
errChan <- s.httpServer.ListenAndServe()
|
|
|
|
}()
|
|
|
|
}
|
2021-12-02 16:52:48 +03:00
|
|
|
if s.config.ListenHTTPS != "" {
|
2022-01-06 16:45:23 +03:00
|
|
|
s.httpsServer = &http.Server{Addr: s.config.ListenHTTPS, Handler: mux}
|
2021-12-02 16:52:48 +03:00
|
|
|
go func() {
|
2021-12-22 16:17:50 +03:00
|
|
|
errChan <- s.httpsServer.ListenAndServeTLS(s.config.CertFile, s.config.KeyFile)
|
2021-12-02 16:52:48 +03:00
|
|
|
}()
|
|
|
|
}
|
2022-01-11 00:28:13 +03:00
|
|
|
if s.config.ListenUnix != "" {
|
|
|
|
go func() {
|
|
|
|
var err error
|
|
|
|
s.mu.Lock()
|
|
|
|
os.Remove(s.config.ListenUnix)
|
|
|
|
s.unixListener, err = net.Listen("unix", s.config.ListenUnix)
|
|
|
|
if err != nil {
|
|
|
|
errChan <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s.mu.Unlock()
|
|
|
|
httpServer := &http.Server{Handler: mux}
|
|
|
|
errChan <- httpServer.Serve(s.unixListener)
|
|
|
|
}()
|
|
|
|
}
|
2021-12-27 18:39:28 +03:00
|
|
|
if s.config.SMTPServerListen != "" {
|
2021-12-27 17:48:09 +03:00
|
|
|
go func() {
|
2021-12-28 00:06:40 +03:00
|
|
|
errChan <- s.runSMTPServer()
|
2021-12-27 17:48:09 +03:00
|
|
|
}()
|
|
|
|
}
|
2021-12-22 16:17:50 +03:00
|
|
|
s.mu.Unlock()
|
2021-12-23 01:20:43 +03:00
|
|
|
go s.runManager()
|
|
|
|
go s.runAtSender()
|
2022-01-21 22:17:59 +03:00
|
|
|
go s.runFirebaseKeepaliver()
|
2021-12-27 17:48:09 +03:00
|
|
|
|
2021-12-02 16:52:48 +03:00
|
|
|
return <-errChan
|
2021-10-23 04:26:01 +03:00
|
|
|
}
|
|
|
|
|
2021-12-22 16:17:50 +03:00
|
|
|
// Stop stops HTTP (+HTTPS) server and all managers
|
|
|
|
func (s *Server) Stop() {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
if s.httpServer != nil {
|
|
|
|
s.httpServer.Close()
|
|
|
|
}
|
|
|
|
if s.httpsServer != nil {
|
|
|
|
s.httpsServer.Close()
|
|
|
|
}
|
2022-01-11 00:28:13 +03:00
|
|
|
if s.unixListener != nil {
|
|
|
|
s.unixListener.Close()
|
|
|
|
}
|
2021-12-28 00:06:40 +03:00
|
|
|
if s.smtpServer != nil {
|
|
|
|
s.smtpServer.Close()
|
|
|
|
}
|
2021-12-22 16:17:50 +03:00
|
|
|
close(s.closeChan)
|
|
|
|
}
|
|
|
|
|
2021-10-23 04:26:01 +03:00
|
|
|
func (s *Server) handle(w http.ResponseWriter, r *http.Request) {
|
2022-02-15 00:09:59 +03:00
|
|
|
v := s.visitor(r)
|
|
|
|
if err := s.handleInternal(w, r, v); err != nil {
|
2022-01-16 06:33:35 +03:00
|
|
|
if websocket.IsWebSocketUpgrade(r) {
|
2022-02-15 00:09:59 +03:00
|
|
|
log.Printf("[%s] WS %s %s - %s", v.ip, r.Method, r.URL.Path, err.Error())
|
2022-01-16 06:33:35 +03:00
|
|
|
return // Do not attempt to write to upgraded connection
|
2021-10-24 05:49:50 +03:00
|
|
|
}
|
2022-01-16 06:33:35 +03:00
|
|
|
httpErr, ok := err.(*errHTTP)
|
|
|
|
if !ok {
|
|
|
|
httpErr = errHTTPInternalError
|
|
|
|
}
|
2022-02-15 00:09:59 +03:00
|
|
|
log.Printf("[%s] HTTP %s %s - %d - %d - %s", v.ip, r.Method, r.URL.Path, httpErr.HTTPCode, httpErr.Code, err.Error())
|
2021-12-25 17:15:05 +03:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Header().Set("Access-Control-Allow-Origin", "*") // CORS, allow cross-origin requests
|
2022-01-16 06:33:35 +03:00
|
|
|
w.WriteHeader(httpErr.HTTPCode)
|
|
|
|
io.WriteString(w, httpErr.JSON()+"\n")
|
2021-10-23 04:26:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-15 00:09:59 +03:00
|
|
|
func (s *Server) handleInternal(w http.ResponseWriter, r *http.Request, v *visitor) error {
|
2021-12-03 01:27:31 +03:00
|
|
|
if r.Method == http.MethodGet && r.URL.Path == "/" {
|
2021-10-23 04:26:01 +03:00
|
|
|
return s.handleHome(w, r)
|
2021-11-18 17:22:33 +03:00
|
|
|
} else if r.Method == http.MethodGet && r.URL.Path == "/example.html" {
|
|
|
|
return s.handleExample(w, r)
|
2021-11-05 20:46:27 +03:00
|
|
|
} else if r.Method == http.MethodHead && r.URL.Path == "/" {
|
2022-01-26 07:04:09 +03:00
|
|
|
return s.handleEmpty(w, r, v)
|
2021-10-24 21:22:53 +03:00
|
|
|
} else if r.Method == http.MethodGet && staticRegex.MatchString(r.URL.Path) {
|
|
|
|
return s.handleStatic(w, r)
|
2021-12-03 01:27:31 +03:00
|
|
|
} else if r.Method == http.MethodGet && docsRegex.MatchString(r.URL.Path) {
|
|
|
|
return s.handleDocs(w, r)
|
2022-01-04 02:55:08 +03:00
|
|
|
} else if r.Method == http.MethodGet && fileRegex.MatchString(r.URL.Path) && s.config.AttachmentCacheDir != "" {
|
2022-01-22 06:22:27 +03:00
|
|
|
return s.limitRequests(s.handleFile)(w, r, v)
|
2021-11-05 20:46:27 +03:00
|
|
|
} else if r.Method == http.MethodOptions {
|
|
|
|
return s.handleOptions(w, r)
|
2021-12-28 00:06:40 +03:00
|
|
|
} else if r.Method == http.MethodGet && topicPathRegex.MatchString(r.URL.Path) {
|
2021-12-26 00:07:55 +03:00
|
|
|
return s.handleTopic(w, r)
|
2021-12-28 00:06:40 +03:00
|
|
|
} else if (r.Method == http.MethodPut || r.Method == http.MethodPost) && topicPathRegex.MatchString(r.URL.Path) {
|
2022-01-22 06:22:27 +03:00
|
|
|
return s.limitRequests(s.authWrite(s.handlePublish))(w, r, v)
|
2021-12-28 00:06:40 +03:00
|
|
|
} else if r.Method == http.MethodGet && publishPathRegex.MatchString(r.URL.Path) {
|
2022-01-22 06:22:27 +03:00
|
|
|
return s.limitRequests(s.authWrite(s.handlePublish))(w, r, v)
|
2021-12-28 00:06:40 +03:00
|
|
|
} else if r.Method == http.MethodGet && jsonPathRegex.MatchString(r.URL.Path) {
|
2022-01-22 06:22:27 +03:00
|
|
|
return s.limitRequests(s.authRead(s.handleSubscribeJSON))(w, r, v)
|
2021-12-28 00:06:40 +03:00
|
|
|
} else if r.Method == http.MethodGet && ssePathRegex.MatchString(r.URL.Path) {
|
2022-01-22 06:22:27 +03:00
|
|
|
return s.limitRequests(s.authRead(s.handleSubscribeSSE))(w, r, v)
|
2021-12-28 00:06:40 +03:00
|
|
|
} else if r.Method == http.MethodGet && rawPathRegex.MatchString(r.URL.Path) {
|
2022-01-22 06:22:27 +03:00
|
|
|
return s.limitRequests(s.authRead(s.handleSubscribeRaw))(w, r, v)
|
2022-01-15 21:23:35 +03:00
|
|
|
} else if r.Method == http.MethodGet && wsPathRegex.MatchString(r.URL.Path) {
|
2022-01-22 06:22:27 +03:00
|
|
|
return s.limitRequests(s.authRead(s.handleSubscribeWS))(w, r, v)
|
2022-01-26 07:04:09 +03:00
|
|
|
} else if r.Method == http.MethodGet && authPathRegex.MatchString(r.URL.Path) {
|
|
|
|
return s.limitRequests(s.authRead(s.handleTopicAuth))(w, r, v)
|
2021-10-23 04:26:01 +03:00
|
|
|
}
|
2021-10-24 05:49:50 +03:00
|
|
|
return errHTTPNotFound
|
2021-10-23 04:26:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) handleHome(w http.ResponseWriter, r *http.Request) error {
|
2021-11-08 17:24:34 +03:00
|
|
|
return indexTemplate.Execute(w, &indexPage{
|
|
|
|
Topic: r.URL.Path[1:],
|
2021-12-09 18:23:17 +03:00
|
|
|
CacheDuration: s.config.CacheDuration,
|
2021-11-08 17:24:34 +03:00
|
|
|
})
|
2021-10-23 04:26:01 +03:00
|
|
|
}
|
|
|
|
|
2021-12-26 00:07:55 +03:00
|
|
|
func (s *Server) handleTopic(w http.ResponseWriter, r *http.Request) error {
|
2022-01-16 07:17:46 +03:00
|
|
|
unifiedpush := readBoolParam(r, false, "x-unifiedpush", "unifiedpush", "up") // see PUT/POST too!
|
2021-12-26 00:07:55 +03:00
|
|
|
if unifiedpush {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Header().Set("Access-Control-Allow-Origin", "*") // CORS, allow cross-origin requests
|
|
|
|
_, err := io.WriteString(w, `{"unifiedpush":{"version":1}}`+"\n")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return s.handleHome(w, r)
|
|
|
|
}
|
|
|
|
|
2022-01-26 07:04:09 +03:00
|
|
|
func (s *Server) handleEmpty(_ http.ResponseWriter, _ *http.Request, _ *visitor) error {
|
2021-11-05 20:46:27 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-26 07:04:09 +03:00
|
|
|
func (s *Server) handleTopicAuth(w http.ResponseWriter, _ *http.Request, _ *visitor) error {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Header().Set("Access-Control-Allow-Origin", "*") // CORS, allow cross-origin requests
|
|
|
|
_, err := io.WriteString(w, `{"success":true}`+"\n")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-07 19:45:15 +03:00
|
|
|
func (s *Server) handleExample(w http.ResponseWriter, _ *http.Request) error {
|
2021-11-18 17:22:33 +03:00
|
|
|
_, err := io.WriteString(w, exampleSource)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-29 20:58:14 +03:00
|
|
|
func (s *Server) handleStatic(w http.ResponseWriter, r *http.Request) error {
|
2021-11-29 17:34:43 +03:00
|
|
|
http.FileServer(http.FS(webStaticFsCached)).ServeHTTP(w, r)
|
2021-10-29 20:58:14 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-03 01:27:31 +03:00
|
|
|
func (s *Server) handleDocs(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
http.FileServer(http.FS(docsStaticCached)).ServeHTTP(w, r)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-13 02:52:07 +03:00
|
|
|
func (s *Server) handleFile(w http.ResponseWriter, r *http.Request, v *visitor) error {
|
2022-01-03 01:56:12 +03:00
|
|
|
if s.config.AttachmentCacheDir == "" {
|
2022-01-04 02:55:08 +03:00
|
|
|
return errHTTPInternalError
|
2022-01-03 01:56:12 +03:00
|
|
|
}
|
|
|
|
matches := fileRegex.FindStringSubmatch(r.URL.Path)
|
|
|
|
if len(matches) != 2 {
|
|
|
|
return errHTTPInternalErrorInvalidFilePath
|
|
|
|
}
|
|
|
|
messageID := matches[1]
|
|
|
|
file := filepath.Join(s.config.AttachmentCacheDir, messageID)
|
|
|
|
stat, err := os.Stat(file)
|
|
|
|
if err != nil {
|
|
|
|
return errHTTPNotFound
|
|
|
|
}
|
2022-01-13 05:24:48 +03:00
|
|
|
if err := v.BandwidthLimiter().Allow(stat.Size()); err != nil {
|
|
|
|
return errHTTPTooManyRequestsAttachmentBandwidthLimit
|
2022-01-13 02:52:07 +03:00
|
|
|
}
|
2022-01-12 19:05:04 +03:00
|
|
|
w.Header().Set("Content-Length", fmt.Sprintf("%d", stat.Size()))
|
2022-01-03 01:56:12 +03:00
|
|
|
f, err := os.Open(file)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2022-01-10 21:38:51 +03:00
|
|
|
_, err = io.Copy(util.NewContentTypeWriter(w, r.URL.Path), f)
|
2022-01-03 01:56:12 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-24 02:03:04 +03:00
|
|
|
func (s *Server) handlePublish(w http.ResponseWriter, r *http.Request, v *visitor) error {
|
2021-12-15 17:41:55 +03:00
|
|
|
t, err := s.topicFromPath(r.URL.Path)
|
2021-11-01 23:39:40 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-03 01:56:12 +03:00
|
|
|
body, err := util.Peak(r.Body, s.config.MessageLimit)
|
2021-10-23 04:26:01 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-03 01:56:12 +03:00
|
|
|
m := newDefaultMessage(t.ID, "")
|
2022-01-17 21:28:07 +03:00
|
|
|
cache, firebase, email, unifiedpush, err := s.parsePublishParams(r, v, m)
|
2021-12-10 19:31:42 +03:00
|
|
|
if err != nil {
|
2021-10-29 06:50:38 +03:00
|
|
|
return err
|
|
|
|
}
|
2022-01-17 21:28:07 +03:00
|
|
|
if err := s.handlePublishBody(r, v, m, body, unifiedpush); err != nil {
|
2022-01-08 23:47:08 +03:00
|
|
|
return err
|
2021-12-24 02:03:04 +03:00
|
|
|
}
|
2021-12-15 17:41:55 +03:00
|
|
|
if m.Message == "" {
|
2021-12-24 02:03:04 +03:00
|
|
|
m.Message = emptyMessageBody
|
2021-12-15 17:41:55 +03:00
|
|
|
}
|
2021-12-10 19:31:42 +03:00
|
|
|
delayed := m.Time > time.Now().Unix()
|
|
|
|
if !delayed {
|
|
|
|
if err := t.Publish(m); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-12-26 00:07:55 +03:00
|
|
|
if s.firebase != nil && firebase && !delayed {
|
2021-12-09 20:15:17 +03:00
|
|
|
go func() {
|
|
|
|
if err := s.firebase(m); err != nil {
|
2022-02-15 00:09:59 +03:00
|
|
|
log.Printf("[%s] FB - Unable to publish to Firebase: %v", v.ip, err.Error())
|
2021-12-09 20:15:17 +03:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2021-12-26 00:07:55 +03:00
|
|
|
if s.mailer != nil && email != "" && !delayed {
|
2021-12-23 23:04:17 +03:00
|
|
|
go func() {
|
2021-12-24 17:01:29 +03:00
|
|
|
if err := s.mailer.Send(v.ip, email, m); err != nil {
|
2022-02-15 00:09:59 +03:00
|
|
|
log.Printf("[%s] MAIL - Unable to send email: %v", v.ip, err.Error())
|
2021-12-23 23:04:17 +03:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2021-12-09 18:23:17 +03:00
|
|
|
if cache {
|
|
|
|
if err := s.cache.AddMessage(m); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-11-02 21:08:21 +03:00
|
|
|
}
|
2021-12-16 00:12:40 +03:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2021-10-24 20:34:15 +03:00
|
|
|
w.Header().Set("Access-Control-Allow-Origin", "*") // CORS, allow cross-origin requests
|
2021-11-03 18:33:34 +03:00
|
|
|
if err := json.NewEncoder(w).Encode(m); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-16 07:17:46 +03:00
|
|
|
s.mu.Lock()
|
|
|
|
s.messages++
|
|
|
|
s.mu.Unlock()
|
2021-10-24 20:34:15 +03:00
|
|
|
return nil
|
2021-10-23 04:26:01 +03:00
|
|
|
}
|
|
|
|
|
2022-01-17 21:28:07 +03:00
|
|
|
func (s *Server) parsePublishParams(r *http.Request, v *visitor, m *message) (cache bool, firebase bool, email string, unifiedpush bool, err error) {
|
2022-01-16 07:17:46 +03:00
|
|
|
cache = readBoolParam(r, true, "x-cache", "cache")
|
|
|
|
firebase = readBoolParam(r, true, "x-firebase", "firebase")
|
2021-12-22 11:44:16 +03:00
|
|
|
m.Title = readParam(r, "x-title", "title", "t")
|
2022-01-05 01:40:41 +03:00
|
|
|
m.Click = readParam(r, "x-click", "click")
|
2022-01-08 23:47:08 +03:00
|
|
|
filename := readParam(r, "x-filename", "filename", "file", "f")
|
2022-01-14 20:13:14 +03:00
|
|
|
attach := readParam(r, "x-attach", "attach", "a")
|
2022-01-08 23:47:08 +03:00
|
|
|
if attach != "" || filename != "" {
|
|
|
|
m.Attachment = &attachment{}
|
|
|
|
}
|
2022-01-14 20:13:14 +03:00
|
|
|
if filename != "" {
|
|
|
|
m.Attachment.Name = filename
|
|
|
|
}
|
2022-01-08 23:47:08 +03:00
|
|
|
if attach != "" {
|
|
|
|
if !attachURLRegex.MatchString(attach) {
|
2022-01-17 21:28:07 +03:00
|
|
|
return false, false, "", false, errHTTPBadRequestAttachmentURLInvalid
|
2022-01-08 23:47:08 +03:00
|
|
|
}
|
|
|
|
m.Attachment.URL = attach
|
2022-01-14 20:13:14 +03:00
|
|
|
if m.Attachment.Name == "" {
|
|
|
|
u, err := url.Parse(m.Attachment.URL)
|
|
|
|
if err == nil {
|
|
|
|
m.Attachment.Name = path.Base(u.Path)
|
|
|
|
if m.Attachment.Name == "." || m.Attachment.Name == "/" {
|
|
|
|
m.Attachment.Name = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if m.Attachment.Name == "" {
|
|
|
|
m.Attachment.Name = "attachment"
|
|
|
|
}
|
2022-01-08 23:47:08 +03:00
|
|
|
}
|
|
|
|
email = readParam(r, "x-email", "x-e-mail", "email", "e-mail", "mail", "e")
|
|
|
|
if email != "" {
|
|
|
|
if err := v.EmailAllowed(); err != nil {
|
2022-01-17 21:28:07 +03:00
|
|
|
return false, false, "", false, errHTTPTooManyRequestsLimitEmails
|
2022-01-08 23:47:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if s.mailer == nil && email != "" {
|
2022-01-17 21:28:07 +03:00
|
|
|
return false, false, "", false, errHTTPBadRequestEmailDisabled
|
2022-01-08 23:47:08 +03:00
|
|
|
}
|
2022-02-13 17:23:55 +03:00
|
|
|
messageStr := strings.ReplaceAll(readParam(r, "x-message", "message", "m"), "\\n", "\n")
|
2021-12-15 17:41:55 +03:00
|
|
|
if messageStr != "" {
|
|
|
|
m.Message = messageStr
|
|
|
|
}
|
2021-12-17 04:33:01 +03:00
|
|
|
m.Priority, err = util.ParsePriority(readParam(r, "x-priority", "priority", "prio", "p"))
|
|
|
|
if err != nil {
|
2022-01-17 21:28:07 +03:00
|
|
|
return false, false, "", false, errHTTPBadRequestPriorityInvalid
|
2021-11-28 00:12:08 +03:00
|
|
|
}
|
2021-12-22 11:44:16 +03:00
|
|
|
tagsStr := readParam(r, "x-tags", "tags", "tag", "ta")
|
2021-11-28 00:12:08 +03:00
|
|
|
if tagsStr != "" {
|
2021-12-10 19:31:42 +03:00
|
|
|
m.Tags = make([]string, 0)
|
2021-12-21 23:22:27 +03:00
|
|
|
for _, s := range util.SplitNoEmpty(tagsStr, ",") {
|
2021-12-10 19:31:42 +03:00
|
|
|
m.Tags = append(m.Tags, strings.TrimSpace(s))
|
2021-12-07 23:39:42 +03:00
|
|
|
}
|
2021-11-28 00:12:08 +03:00
|
|
|
}
|
2021-12-15 17:41:55 +03:00
|
|
|
delayStr := readParam(r, "x-delay", "delay", "x-at", "at", "x-in", "in")
|
2021-12-11 08:06:25 +03:00
|
|
|
if delayStr != "" {
|
2021-12-10 19:31:42 +03:00
|
|
|
if !cache {
|
2022-01-17 21:28:07 +03:00
|
|
|
return false, false, "", false, errHTTPBadRequestDelayNoCache
|
2021-12-10 19:31:42 +03:00
|
|
|
}
|
2021-12-24 02:03:04 +03:00
|
|
|
if email != "" {
|
2022-01-17 21:28:07 +03:00
|
|
|
return false, false, "", false, errHTTPBadRequestDelayNoEmail // we cannot store the email address (yet)
|
2021-12-24 02:03:04 +03:00
|
|
|
}
|
2021-12-11 08:06:25 +03:00
|
|
|
delay, err := util.ParseFutureTime(delayStr, time.Now())
|
2021-12-10 19:31:42 +03:00
|
|
|
if err != nil {
|
2022-01-17 21:28:07 +03:00
|
|
|
return false, false, "", false, errHTTPBadRequestDelayCannotParse
|
2021-12-11 08:06:25 +03:00
|
|
|
} else if delay.Unix() < time.Now().Add(s.config.MinDelay).Unix() {
|
2022-01-17 21:28:07 +03:00
|
|
|
return false, false, "", false, errHTTPBadRequestDelayTooSmall
|
2021-12-11 08:06:25 +03:00
|
|
|
} else if delay.Unix() > time.Now().Add(s.config.MaxDelay).Unix() {
|
2022-01-17 21:28:07 +03:00
|
|
|
return false, false, "", false, errHTTPBadRequestDelayTooLarge
|
2021-12-10 19:31:42 +03:00
|
|
|
}
|
2021-12-11 08:06:25 +03:00
|
|
|
m.Time = delay.Unix()
|
2021-12-10 19:31:42 +03:00
|
|
|
}
|
2022-01-17 21:28:07 +03:00
|
|
|
unifiedpush = readBoolParam(r, false, "x-unifiedpush", "unifiedpush", "up") // see GET too!
|
2021-12-26 00:07:55 +03:00
|
|
|
if unifiedpush {
|
|
|
|
firebase = false
|
2022-01-17 21:28:07 +03:00
|
|
|
unifiedpush = true
|
2021-12-26 00:07:55 +03:00
|
|
|
}
|
2022-01-17 21:28:07 +03:00
|
|
|
return cache, firebase, email, unifiedpush, nil
|
2021-11-28 00:12:08 +03:00
|
|
|
}
|
|
|
|
|
2022-01-08 23:47:08 +03:00
|
|
|
// handlePublishBody consumes the PUT/POST body and decides whether the body is an attachment or the message.
|
|
|
|
//
|
2022-01-17 21:28:07 +03:00
|
|
|
// 1. curl -T somebinarydata.bin "ntfy.sh/mytopic?up=1"
|
|
|
|
// If body is binary, encode as base64, if not do not encode
|
|
|
|
// 2. curl -H "Attach: http://example.com/file.jpg" ntfy.sh/mytopic
|
2022-01-08 23:47:08 +03:00
|
|
|
// Body must be a message, because we attached an external URL
|
2022-01-17 21:28:07 +03:00
|
|
|
// 3. curl -T short.txt -H "Filename: short.txt" ntfy.sh/mytopic
|
2022-01-08 23:47:08 +03:00
|
|
|
// Body must be attachment, because we passed a filename
|
|
|
|
// 4. curl -T file.txt ntfy.sh/mytopic
|
2022-01-17 21:28:07 +03:00
|
|
|
// If file.txt is <= 4096 (message limit) and valid UTF-8, treat it as a message
|
|
|
|
// 5. curl -T file.txt ntfy.sh/mytopic
|
2022-01-08 23:47:08 +03:00
|
|
|
// If file.txt is > message limit, treat it as an attachment
|
2022-01-17 21:28:07 +03:00
|
|
|
func (s *Server) handlePublishBody(r *http.Request, v *visitor, m *message, body *util.PeakedReadCloser, unifiedpush bool) error {
|
|
|
|
if unifiedpush {
|
|
|
|
return s.handleBodyAsMessageAutoDetect(m, body) // Case 1
|
|
|
|
} else if m.Attachment != nil && m.Attachment.URL != "" {
|
|
|
|
return s.handleBodyAsTextMessage(m, body) // Case 2
|
2022-01-08 23:47:08 +03:00
|
|
|
} else if m.Attachment != nil && m.Attachment.Name != "" {
|
2022-01-17 21:28:07 +03:00
|
|
|
return s.handleBodyAsAttachment(r, v, m, body) // Case 3
|
2022-01-08 23:47:08 +03:00
|
|
|
} else if !body.LimitReached && utf8.Valid(body.PeakedBytes) {
|
2022-01-17 21:28:07 +03:00
|
|
|
return s.handleBodyAsTextMessage(m, body) // Case 4
|
2022-01-08 23:47:08 +03:00
|
|
|
}
|
2022-01-17 21:28:07 +03:00
|
|
|
return s.handleBodyAsAttachment(r, v, m, body) // Case 5
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) handleBodyAsMessageAutoDetect(m *message, body *util.PeakedReadCloser) error {
|
|
|
|
if utf8.Valid(body.PeakedBytes) {
|
|
|
|
m.Message = string(body.PeakedBytes) // Do not trim
|
|
|
|
} else {
|
|
|
|
m.Message = base64.StdEncoding.EncodeToString(body.PeakedBytes)
|
|
|
|
m.Encoding = encodingBase64
|
|
|
|
}
|
|
|
|
return nil
|
2022-01-08 23:47:08 +03:00
|
|
|
}
|
|
|
|
|
2022-01-17 21:28:07 +03:00
|
|
|
func (s *Server) handleBodyAsTextMessage(m *message, body *util.PeakedReadCloser) error {
|
2022-01-08 23:47:08 +03:00
|
|
|
if !utf8.Valid(body.PeakedBytes) {
|
|
|
|
return errHTTPBadRequestMessageNotUTF8
|
|
|
|
}
|
|
|
|
if len(body.PeakedBytes) > 0 { // Empty body should not override message (publish via GET!)
|
|
|
|
m.Message = strings.TrimSpace(string(body.PeakedBytes)) // Truncates the message to the peak limit if required
|
|
|
|
}
|
2022-01-10 21:38:51 +03:00
|
|
|
if m.Attachment != nil && m.Attachment.Name != "" && m.Message == "" {
|
|
|
|
m.Message = fmt.Sprintf(defaultAttachmentMessage, m.Attachment.Name)
|
|
|
|
}
|
2022-01-08 23:47:08 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-11 20:58:11 +03:00
|
|
|
func (s *Server) handleBodyAsAttachment(r *http.Request, v *visitor, m *message, body *util.PeakedReadCloser) error {
|
2022-01-12 19:05:04 +03:00
|
|
|
if s.fileCache == nil || s.config.BaseURL == "" || s.config.AttachmentCacheDir == "" {
|
2022-01-08 23:47:08 +03:00
|
|
|
return errHTTPBadRequestAttachmentsDisallowed
|
|
|
|
} else if m.Time > time.Now().Add(s.config.AttachmentExpiryDuration).Unix() {
|
|
|
|
return errHTTPBadRequestAttachmentsExpiryBeforeDelivery
|
|
|
|
}
|
2022-01-11 20:58:11 +03:00
|
|
|
visitorAttachmentsSize, err := s.cache.AttachmentsSize(v.ip)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
remainingVisitorAttachmentSize := s.config.VisitorAttachmentTotalSizeLimit - visitorAttachmentsSize
|
|
|
|
contentLengthStr := r.Header.Get("Content-Length")
|
|
|
|
if contentLengthStr != "" { // Early "do-not-trust" check, hard limit see below
|
|
|
|
contentLength, err := strconv.ParseInt(contentLengthStr, 10, 64)
|
|
|
|
if err == nil && (contentLength > remainingVisitorAttachmentSize || contentLength > s.config.AttachmentFileSizeLimit) {
|
|
|
|
return errHTTPBadRequestAttachmentTooLarge
|
|
|
|
}
|
|
|
|
}
|
2022-01-08 23:47:08 +03:00
|
|
|
if m.Attachment == nil {
|
|
|
|
m.Attachment = &attachment{}
|
|
|
|
}
|
2022-01-10 21:38:51 +03:00
|
|
|
var ext string
|
2022-01-08 23:47:08 +03:00
|
|
|
m.Attachment.Owner = v.ip // Important for attachment rate limiting
|
|
|
|
m.Attachment.Expires = time.Now().Add(s.config.AttachmentExpiryDuration).Unix()
|
2022-01-10 21:38:51 +03:00
|
|
|
m.Attachment.Type, ext = util.DetectContentType(body.PeakedBytes, m.Attachment.Name)
|
2022-01-08 23:47:08 +03:00
|
|
|
m.Attachment.URL = fmt.Sprintf("%s/file/%s%s", s.config.BaseURL, m.ID, ext)
|
|
|
|
if m.Attachment.Name == "" {
|
|
|
|
m.Attachment.Name = fmt.Sprintf("attachment%s", ext)
|
|
|
|
}
|
|
|
|
if m.Message == "" {
|
2022-01-10 21:38:51 +03:00
|
|
|
m.Message = fmt.Sprintf(defaultAttachmentMessage, m.Attachment.Name)
|
2022-01-04 02:55:08 +03:00
|
|
|
}
|
2022-01-13 05:24:48 +03:00
|
|
|
m.Attachment.Size, err = s.fileCache.Write(m.ID, body, v.BandwidthLimiter(), util.NewFixedLimiter(remainingVisitorAttachmentSize))
|
2022-01-07 16:49:28 +03:00
|
|
|
if err == util.ErrLimitReached {
|
2022-01-11 20:58:11 +03:00
|
|
|
return errHTTPBadRequestAttachmentTooLarge
|
2022-01-07 16:49:28 +03:00
|
|
|
} else if err != nil {
|
2022-01-03 01:56:12 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-01 22:21:38 +03:00
|
|
|
func (s *Server) handleSubscribeJSON(w http.ResponseWriter, r *http.Request, v *visitor) error {
|
2021-10-27 21:56:17 +03:00
|
|
|
encoder := func(msg *message) (string, error) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
if err := json.NewEncoder(&buf).Encode(&msg); err != nil {
|
|
|
|
return "", err
|
2021-10-23 04:26:01 +03:00
|
|
|
}
|
2021-10-27 21:56:17 +03:00
|
|
|
return buf.String(), nil
|
2021-10-23 04:26:01 +03:00
|
|
|
}
|
2022-01-16 07:17:46 +03:00
|
|
|
return s.handleSubscribeHTTP(w, r, v, "application/x-ndjson", encoder)
|
2021-10-23 04:26:01 +03:00
|
|
|
}
|
|
|
|
|
2021-11-01 22:21:38 +03:00
|
|
|
func (s *Server) handleSubscribeSSE(w http.ResponseWriter, r *http.Request, v *visitor) error {
|
2021-10-27 21:56:17 +03:00
|
|
|
encoder := func(msg *message) (string, error) {
|
2021-10-23 20:21:33 +03:00
|
|
|
var buf bytes.Buffer
|
|
|
|
if err := json.NewEncoder(&buf).Encode(&msg); err != nil {
|
2021-10-27 21:56:17 +03:00
|
|
|
return "", err
|
2021-10-23 20:21:33 +03:00
|
|
|
}
|
2021-10-29 15:29:27 +03:00
|
|
|
if msg.Event != messageEvent {
|
2021-10-27 21:56:17 +03:00
|
|
|
return fmt.Sprintf("event: %s\ndata: %s\n", msg.Event, buf.String()), nil // Browser's .onmessage() does not fire on this!
|
2021-10-23 20:21:33 +03:00
|
|
|
}
|
2021-10-27 21:56:17 +03:00
|
|
|
return fmt.Sprintf("data: %s\n", buf.String()), nil
|
2021-10-23 22:22:17 +03:00
|
|
|
}
|
2022-01-16 07:17:46 +03:00
|
|
|
return s.handleSubscribeHTTP(w, r, v, "text/event-stream", encoder)
|
2021-10-23 20:21:33 +03:00
|
|
|
}
|
|
|
|
|
2021-11-01 22:21:38 +03:00
|
|
|
func (s *Server) handleSubscribeRaw(w http.ResponseWriter, r *http.Request, v *visitor) error {
|
2021-10-27 21:56:17 +03:00
|
|
|
encoder := func(msg *message) (string, error) {
|
2021-11-02 21:10:56 +03:00
|
|
|
if msg.Event == messageEvent { // only handle default events
|
2021-10-27 21:56:17 +03:00
|
|
|
return strings.ReplaceAll(msg.Message, "\n", " ") + "\n", nil
|
|
|
|
}
|
|
|
|
return "\n", nil // "keepalive" and "open" events just send an empty line
|
|
|
|
}
|
2022-01-16 07:17:46 +03:00
|
|
|
return s.handleSubscribeHTTP(w, r, v, "text/plain", encoder)
|
2021-10-27 21:56:17 +03:00
|
|
|
}
|
|
|
|
|
2022-01-16 07:17:46 +03:00
|
|
|
func (s *Server) handleSubscribeHTTP(w http.ResponseWriter, r *http.Request, v *visitor, contentType string, encoder messageEncoder) error {
|
2021-12-25 17:15:05 +03:00
|
|
|
if err := v.SubscriptionAllowed(); err != nil {
|
|
|
|
return errHTTPTooManyRequestsLimitSubscriptions
|
2021-11-01 22:21:38 +03:00
|
|
|
}
|
|
|
|
defer v.RemoveSubscription()
|
2022-01-16 07:17:46 +03:00
|
|
|
topics, topicsStr, err := s.topicsFromPath(r.URL.Path)
|
2021-11-01 23:39:40 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-16 07:17:46 +03:00
|
|
|
poll, since, scheduled, filters, err := parseSubscribeParams(r)
|
2021-12-21 23:22:27 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-22 11:44:16 +03:00
|
|
|
var wlock sync.Mutex
|
2021-10-27 21:56:17 +03:00
|
|
|
sub := func(msg *message) error {
|
2022-01-16 07:17:46 +03:00
|
|
|
if !filters.Pass(msg) {
|
2021-12-21 23:22:27 +03:00
|
|
|
return nil
|
|
|
|
}
|
2021-10-27 21:56:17 +03:00
|
|
|
m, err := encoder(msg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-21 23:22:27 +03:00
|
|
|
wlock.Lock()
|
|
|
|
defer wlock.Unlock()
|
2021-10-27 21:56:17 +03:00
|
|
|
if _, err := w.Write([]byte(m)); err != nil {
|
2021-10-24 04:29:45 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if fl, ok := w.(http.Flusher); ok {
|
|
|
|
fl.Flush()
|
|
|
|
}
|
|
|
|
return nil
|
2021-10-27 21:56:17 +03:00
|
|
|
}
|
2021-11-07 21:08:03 +03:00
|
|
|
w.Header().Set("Access-Control-Allow-Origin", "*") // CORS, allow cross-origin requests
|
|
|
|
w.Header().Set("Content-Type", contentType+"; charset=utf-8") // Android/Volley client needs charset!
|
2021-10-29 20:58:14 +03:00
|
|
|
if poll {
|
2021-12-10 19:31:42 +03:00
|
|
|
return s.sendOldMessages(topics, since, scheduled, sub)
|
2021-10-29 20:58:14 +03:00
|
|
|
}
|
2021-11-15 15:56:58 +03:00
|
|
|
subscriberIDs := make([]int, 0)
|
|
|
|
for _, t := range topics {
|
|
|
|
subscriberIDs = append(subscriberIDs, t.Subscribe(sub))
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
for i, subscriberID := range subscriberIDs {
|
|
|
|
topics[i].Unsubscribe(subscriberID) // Order!
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if err := sub(newOpenMessage(topicsStr)); err != nil { // Send out open message
|
2021-10-29 20:58:14 +03:00
|
|
|
return err
|
|
|
|
}
|
2021-12-10 19:31:42 +03:00
|
|
|
if err := s.sendOldMessages(topics, since, scheduled, sub); err != nil {
|
2021-10-27 21:56:17 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-r.Context().Done():
|
|
|
|
return nil
|
|
|
|
case <-time.After(s.config.KeepaliveInterval):
|
2021-11-01 22:21:38 +03:00
|
|
|
v.Keepalive()
|
2021-11-15 15:56:58 +03:00
|
|
|
if err := sub(newKeepaliveMessage(topicsStr)); err != nil { // Send keepalive message
|
2021-10-27 21:56:17 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-10-24 04:29:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-15 21:23:35 +03:00
|
|
|
func (s *Server) handleSubscribeWS(w http.ResponseWriter, r *http.Request, v *visitor) error {
|
2022-01-17 01:54:15 +03:00
|
|
|
if r.Header.Get("Upgrade") != "websocket" {
|
|
|
|
return errHTTPBadRequestWebSocketsUpgradeHeaderMissing
|
|
|
|
}
|
2022-01-15 21:23:35 +03:00
|
|
|
if err := v.SubscriptionAllowed(); err != nil {
|
|
|
|
return errHTTPTooManyRequestsLimitSubscriptions
|
|
|
|
}
|
|
|
|
defer v.RemoveSubscription()
|
2022-01-16 07:17:46 +03:00
|
|
|
topics, topicsStr, err := s.topicsFromPath(r.URL.Path)
|
2022-01-15 21:23:35 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-16 07:17:46 +03:00
|
|
|
poll, since, scheduled, filters, err := parseSubscribeParams(r)
|
2022-01-15 21:23:35 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
upgrader := &websocket.Upgrader{
|
|
|
|
ReadBufferSize: wsBufferSize,
|
|
|
|
WriteBufferSize: wsBufferSize,
|
|
|
|
CheckOrigin: func(r *http.Request) bool {
|
|
|
|
return true // We're open for business!
|
|
|
|
},
|
|
|
|
}
|
|
|
|
conn, err := upgrader.Upgrade(w, r, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
2022-01-16 08:07:32 +03:00
|
|
|
var wlock sync.Mutex
|
2022-01-15 21:23:35 +03:00
|
|
|
g, ctx := errgroup.WithContext(context.Background())
|
|
|
|
g.Go(func() error {
|
|
|
|
pongWait := s.config.KeepaliveInterval + wsPongWait
|
|
|
|
conn.SetReadLimit(wsReadLimit)
|
|
|
|
if err := conn.SetReadDeadline(time.Now().Add(pongWait)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
conn.SetPongHandler(func(appData string) error {
|
|
|
|
return conn.SetReadDeadline(time.Now().Add(pongWait))
|
|
|
|
})
|
|
|
|
for {
|
|
|
|
_, _, err := conn.NextReader()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
g.Go(func() error {
|
|
|
|
ping := func() error {
|
2022-01-16 08:07:32 +03:00
|
|
|
wlock.Lock()
|
|
|
|
defer wlock.Unlock()
|
2022-01-15 21:23:35 +03:00
|
|
|
if err := conn.SetWriteDeadline(time.Now().Add(wsWriteWait)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return conn.WriteMessage(websocket.PingMessage, nil)
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
|
|
|
case <-time.After(s.config.KeepaliveInterval):
|
|
|
|
v.Keepalive()
|
|
|
|
if err := ping(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
sub := func(msg *message) error {
|
2022-01-16 07:17:46 +03:00
|
|
|
if !filters.Pass(msg) {
|
2022-01-15 21:23:35 +03:00
|
|
|
return nil
|
|
|
|
}
|
2022-01-16 08:07:32 +03:00
|
|
|
wlock.Lock()
|
|
|
|
defer wlock.Unlock()
|
2022-01-15 21:23:35 +03:00
|
|
|
if err := conn.SetWriteDeadline(time.Now().Add(wsWriteWait)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return conn.WriteJSON(msg)
|
|
|
|
}
|
|
|
|
w.Header().Set("Access-Control-Allow-Origin", "*") // CORS, allow cross-origin requests
|
|
|
|
if poll {
|
|
|
|
return s.sendOldMessages(topics, since, scheduled, sub)
|
|
|
|
}
|
|
|
|
subscriberIDs := make([]int, 0)
|
|
|
|
for _, t := range topics {
|
|
|
|
subscriberIDs = append(subscriberIDs, t.Subscribe(sub))
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
for i, subscriberID := range subscriberIDs {
|
|
|
|
topics[i].Unsubscribe(subscriberID) // Order!
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if err := sub(newOpenMessage(topicsStr)); err != nil { // Send out open message
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := s.sendOldMessages(topics, since, scheduled, sub); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-16 06:33:35 +03:00
|
|
|
err = g.Wait()
|
|
|
|
if err != nil && websocket.IsCloseError(err, websocket.CloseNormalClosure, websocket.CloseGoingAway) {
|
|
|
|
return nil // Normal closures are not errors
|
|
|
|
}
|
|
|
|
return err
|
2022-01-15 21:23:35 +03:00
|
|
|
}
|
|
|
|
|
2022-01-16 07:17:46 +03:00
|
|
|
func parseSubscribeParams(r *http.Request) (poll bool, since sinceTime, scheduled bool, filters *queryFilter, err error) {
|
|
|
|
poll = readBoolParam(r, false, "x-poll", "poll", "po")
|
|
|
|
scheduled = readBoolParam(r, false, "x-scheduled", "scheduled", "sched")
|
|
|
|
since, err = parseSince(r, poll)
|
|
|
|
if err != nil {
|
|
|
|
return
|
2021-12-21 23:22:27 +03:00
|
|
|
}
|
2022-01-16 07:17:46 +03:00
|
|
|
filters, err = parseQueryFilters(r)
|
|
|
|
if err != nil {
|
|
|
|
return
|
2021-12-21 23:22:27 +03:00
|
|
|
}
|
2022-01-16 07:17:46 +03:00
|
|
|
return
|
2021-12-21 23:22:27 +03:00
|
|
|
}
|
|
|
|
|
2021-12-10 19:31:42 +03:00
|
|
|
func (s *Server) sendOldMessages(topics []*topic, since sinceTime, scheduled bool, sub subscriber) error {
|
2021-11-08 17:46:31 +03:00
|
|
|
if since.IsNone() {
|
2021-10-29 20:58:14 +03:00
|
|
|
return nil
|
|
|
|
}
|
2021-11-15 15:56:58 +03:00
|
|
|
for _, t := range topics {
|
2021-12-10 19:31:42 +03:00
|
|
|
messages, err := s.cache.Messages(t.ID, since, scheduled)
|
2021-11-15 15:56:58 +03:00
|
|
|
if err != nil {
|
2021-10-29 20:58:14 +03:00
|
|
|
return err
|
|
|
|
}
|
2021-11-15 15:56:58 +03:00
|
|
|
for _, m := range messages {
|
|
|
|
if err := sub(m); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-10-29 20:58:14 +03:00
|
|
|
}
|
2021-10-24 20:34:15 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-08 17:46:31 +03:00
|
|
|
// parseSince returns a timestamp identifying the time span from which cached messages should be received.
|
|
|
|
//
|
|
|
|
// Values in the "since=..." parameter can be either a unix timestamp or a duration (e.g. 12h), or
|
|
|
|
// "all" for all messages.
|
2021-12-22 11:44:16 +03:00
|
|
|
func parseSince(r *http.Request, poll bool) (sinceTime, error) {
|
|
|
|
since := readParam(r, "x-since", "since", "si")
|
|
|
|
if since == "" {
|
|
|
|
if poll {
|
2021-11-08 17:46:31 +03:00
|
|
|
return sinceAllMessages, nil
|
|
|
|
}
|
|
|
|
return sinceNoMessages, nil
|
|
|
|
}
|
2021-12-22 11:44:16 +03:00
|
|
|
if since == "all" {
|
2021-11-08 17:46:31 +03:00
|
|
|
return sinceAllMessages, nil
|
2021-12-22 11:44:16 +03:00
|
|
|
} else if s, err := strconv.ParseInt(since, 10, 64); err == nil {
|
2021-11-08 17:46:31 +03:00
|
|
|
return sinceTime(time.Unix(s, 0)), nil
|
2021-12-22 11:44:16 +03:00
|
|
|
} else if d, err := time.ParseDuration(since); err == nil {
|
2021-11-08 17:46:31 +03:00
|
|
|
return sinceTime(time.Now().Add(-1 * d)), nil
|
2021-10-29 20:58:14 +03:00
|
|
|
}
|
2021-12-25 17:15:05 +03:00
|
|
|
return sinceNoMessages, errHTTPBadRequestSinceInvalid
|
2021-10-29 20:58:14 +03:00
|
|
|
}
|
|
|
|
|
2021-12-07 19:45:15 +03:00
|
|
|
func (s *Server) handleOptions(w http.ResponseWriter, _ *http.Request) error {
|
2021-10-29 20:58:14 +03:00
|
|
|
w.Header().Set("Access-Control-Allow-Methods", "GET, PUT, POST")
|
2022-02-26 00:07:25 +03:00
|
|
|
w.Header().Set("Access-Control-Allow-Origin", "*") // CORS, allow cross-origin requests
|
|
|
|
w.Header().Set("Access-Control-Allow-Headers", "Authorization") // CORS, allow auth
|
2021-10-24 21:22:53 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-15 17:41:55 +03:00
|
|
|
func (s *Server) topicFromPath(path string) (*topic, error) {
|
|
|
|
parts := strings.Split(path, "/")
|
|
|
|
if len(parts) < 2 {
|
2021-12-25 17:15:05 +03:00
|
|
|
return nil, errHTTPBadRequestTopicInvalid
|
2021-12-15 17:41:55 +03:00
|
|
|
}
|
|
|
|
topics, err := s.topicsFromIDs(parts[1])
|
2021-11-15 15:56:58 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return topics[0], nil
|
|
|
|
}
|
|
|
|
|
2022-01-16 07:17:46 +03:00
|
|
|
func (s *Server) topicsFromPath(path string) ([]*topic, string, error) {
|
|
|
|
parts := strings.Split(path, "/")
|
|
|
|
if len(parts) < 2 {
|
|
|
|
return nil, "", errHTTPBadRequestTopicInvalid
|
|
|
|
}
|
|
|
|
topicIDs := util.SplitNoEmpty(parts[1], ",")
|
|
|
|
topics, err := s.topicsFromIDs(topicIDs...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", errHTTPBadRequestTopicInvalid
|
|
|
|
}
|
|
|
|
return topics, parts[1], nil
|
|
|
|
}
|
|
|
|
|
2021-11-28 00:12:08 +03:00
|
|
|
func (s *Server) topicsFromIDs(ids ...string) ([]*topic, error) {
|
2021-10-23 04:26:01 +03:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
2021-11-15 15:56:58 +03:00
|
|
|
topics := make([]*topic, 0)
|
2021-11-28 00:12:08 +03:00
|
|
|
for _, id := range ids {
|
2021-12-09 06:13:59 +03:00
|
|
|
if util.InStringList(disallowedTopics, id) {
|
2021-12-25 17:15:05 +03:00
|
|
|
return nil, errHTTPBadRequestTopicDisallowed
|
2021-12-09 06:13:59 +03:00
|
|
|
}
|
2021-11-15 15:56:58 +03:00
|
|
|
if _, ok := s.topics[id]; !ok {
|
2022-01-03 01:56:12 +03:00
|
|
|
if len(s.topics) >= s.config.TotalTopicLimit {
|
2022-01-12 19:05:04 +03:00
|
|
|
return nil, errHTTPTooManyRequestsLimitTotalTopics
|
2021-11-15 15:56:58 +03:00
|
|
|
}
|
2021-12-09 06:57:31 +03:00
|
|
|
s.topics[id] = newTopic(id)
|
2021-10-29 20:58:14 +03:00
|
|
|
}
|
2021-11-15 15:56:58 +03:00
|
|
|
topics = append(topics, s.topics[id])
|
2021-10-23 04:26:01 +03:00
|
|
|
}
|
2021-11-15 15:56:58 +03:00
|
|
|
return topics, nil
|
2021-10-23 04:26:01 +03:00
|
|
|
}
|
|
|
|
|
2021-12-11 06:57:01 +03:00
|
|
|
func (s *Server) updateStatsAndPrune() {
|
2021-10-23 04:26:01 +03:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
2021-10-29 20:58:14 +03:00
|
|
|
|
|
|
|
// Expire visitors from rate visitors map
|
|
|
|
for ip, v := range s.visitors {
|
2021-11-01 22:21:38 +03:00
|
|
|
if v.Stale() {
|
2021-10-29 20:58:14 +03:00
|
|
|
delete(s.visitors, ip)
|
|
|
|
}
|
2021-10-23 04:26:01 +03:00
|
|
|
}
|
2021-10-24 04:29:45 +03:00
|
|
|
|
2022-01-07 17:15:33 +03:00
|
|
|
// Delete expired attachments
|
2022-01-08 20:14:43 +03:00
|
|
|
if s.fileCache != nil {
|
|
|
|
ids, err := s.cache.AttachmentsExpired()
|
|
|
|
if err == nil {
|
|
|
|
if err := s.fileCache.Remove(ids...); err != nil {
|
|
|
|
log.Printf("error while deleting attachments: %s", err.Error())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Printf("error retrieving expired attachments: %s", err.Error())
|
2022-01-07 17:15:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-11 06:57:01 +03:00
|
|
|
// Prune message cache
|
2021-12-09 06:57:31 +03:00
|
|
|
olderThan := time.Now().Add(-1 * s.config.CacheDuration)
|
|
|
|
if err := s.cache.Prune(olderThan); err != nil {
|
2021-11-03 04:09:49 +03:00
|
|
|
log.Printf("error pruning cache: %s", err.Error())
|
2021-11-02 21:08:21 +03:00
|
|
|
}
|
|
|
|
|
2021-12-11 06:57:01 +03:00
|
|
|
// Prune old topics, remove subscriptions without subscribers
|
2021-11-03 04:09:49 +03:00
|
|
|
var subscribers, messages int
|
2021-10-29 20:58:14 +03:00
|
|
|
for _, t := range s.topics {
|
2021-11-03 04:09:49 +03:00
|
|
|
subs := t.Subscribers()
|
2021-12-09 06:57:31 +03:00
|
|
|
msgs, err := s.cache.MessageCount(t.ID)
|
2021-11-03 04:09:49 +03:00
|
|
|
if err != nil {
|
2021-12-09 06:57:31 +03:00
|
|
|
log.Printf("cannot get stats for topic %s: %s", t.ID, err.Error())
|
2021-11-03 04:09:49 +03:00
|
|
|
continue
|
|
|
|
}
|
2021-12-09 20:15:17 +03:00
|
|
|
if msgs == 0 && subs == 0 {
|
2021-12-09 06:57:31 +03:00
|
|
|
delete(s.topics, t.ID)
|
2021-11-03 04:09:49 +03:00
|
|
|
continue
|
2021-10-29 20:58:14 +03:00
|
|
|
}
|
|
|
|
subscribers += subs
|
|
|
|
messages += msgs
|
2021-10-24 04:29:45 +03:00
|
|
|
}
|
2021-11-03 04:09:49 +03:00
|
|
|
|
2021-12-28 00:18:15 +03:00
|
|
|
// Mail stats
|
|
|
|
var mailSuccess, mailFailure int64
|
|
|
|
if s.smtpBackend != nil {
|
|
|
|
mailSuccess, mailFailure = s.smtpBackend.Counts()
|
|
|
|
}
|
2021-12-28 00:06:40 +03:00
|
|
|
|
2021-11-03 04:09:49 +03:00
|
|
|
// Print stats
|
2021-12-28 00:06:40 +03:00
|
|
|
log.Printf("Stats: %d message(s) published, %d in cache, %d successful mails, %d failed, %d topic(s) active, %d subscriber(s), %d visitor(s)",
|
|
|
|
s.messages, messages, mailSuccess, mailFailure, len(s.topics), subscribers, len(s.visitors))
|
2021-10-24 04:29:45 +03:00
|
|
|
}
|
2021-10-24 05:49:50 +03:00
|
|
|
|
2021-12-28 00:06:40 +03:00
|
|
|
func (s *Server) runSMTPServer() error {
|
2021-12-27 18:39:28 +03:00
|
|
|
sub := func(m *message) error {
|
|
|
|
url := fmt.Sprintf("%s/%s", s.config.BaseURL, m.Topic)
|
|
|
|
req, err := http.NewRequest("PUT", url, strings.NewReader(m.Message))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if m.Title != "" {
|
|
|
|
req.Header.Set("Title", m.Title)
|
|
|
|
}
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
s.handle(rr, req)
|
|
|
|
if rr.Code != http.StatusOK {
|
|
|
|
return errors.New("error: " + rr.Body.String())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2021-12-28 00:06:40 +03:00
|
|
|
s.smtpBackend = newMailBackend(s.config, sub)
|
|
|
|
s.smtpServer = smtp.NewServer(s.smtpBackend)
|
|
|
|
s.smtpServer.Addr = s.config.SMTPServerListen
|
|
|
|
s.smtpServer.Domain = s.config.SMTPServerDomain
|
|
|
|
s.smtpServer.ReadTimeout = 10 * time.Second
|
|
|
|
s.smtpServer.WriteTimeout = 10 * time.Second
|
2021-12-28 03:26:20 +03:00
|
|
|
s.smtpServer.MaxMessageBytes = 1024 * 1024 // Must be much larger than message size (headers, multipart, etc.)
|
2021-12-28 00:06:40 +03:00
|
|
|
s.smtpServer.MaxRecipients = 1
|
|
|
|
s.smtpServer.AllowInsecureAuth = true
|
|
|
|
return s.smtpServer.ListenAndServe()
|
2021-12-27 17:48:09 +03:00
|
|
|
}
|
|
|
|
|
2021-12-15 17:13:16 +03:00
|
|
|
func (s *Server) runManager() {
|
2021-12-22 16:17:50 +03:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-time.After(s.config.ManagerInterval):
|
2021-12-15 17:13:16 +03:00
|
|
|
s.updateStatsAndPrune()
|
2021-12-22 16:17:50 +03:00
|
|
|
case <-s.closeChan:
|
|
|
|
return
|
2021-12-15 17:13:16 +03:00
|
|
|
}
|
2021-12-22 16:17:50 +03:00
|
|
|
}
|
2021-12-15 17:13:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) runAtSender() {
|
|
|
|
for {
|
2021-12-22 16:17:50 +03:00
|
|
|
select {
|
|
|
|
case <-time.After(s.config.AtSenderInterval):
|
|
|
|
if err := s.sendDelayedMessages(); err != nil {
|
|
|
|
log.Printf("error sending scheduled messages: %s", err.Error())
|
|
|
|
}
|
|
|
|
case <-s.closeChan:
|
|
|
|
return
|
2021-12-15 17:13:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-21 22:17:59 +03:00
|
|
|
func (s *Server) runFirebaseKeepaliver() {
|
2021-12-15 17:13:16 +03:00
|
|
|
if s.firebase == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for {
|
2021-12-22 16:17:50 +03:00
|
|
|
select {
|
|
|
|
case <-time.After(s.config.FirebaseKeepaliveInterval):
|
|
|
|
if err := s.firebase(newKeepaliveMessage(firebaseControlTopic)); err != nil {
|
|
|
|
log.Printf("error sending Firebase keepalive message: %s", err.Error())
|
|
|
|
}
|
|
|
|
case <-s.closeChan:
|
|
|
|
return
|
2021-12-15 17:13:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-22 16:17:50 +03:00
|
|
|
|
2021-12-10 19:31:42 +03:00
|
|
|
func (s *Server) sendDelayedMessages() error {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
messages, err := s.cache.MessagesDue()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, m := range messages {
|
|
|
|
t, ok := s.topics[m.Topic] // If no subscribers, just mark message as published
|
|
|
|
if ok {
|
|
|
|
if err := t.Publish(m); err != nil {
|
|
|
|
log.Printf("unable to publish message %s to topic %s: %v", m.ID, m.Topic, err.Error())
|
|
|
|
}
|
2022-01-10 23:36:12 +03:00
|
|
|
}
|
|
|
|
if s.firebase != nil { // Firebase subscribers may not show up in topics map
|
|
|
|
if err := s.firebase(m); err != nil {
|
|
|
|
log.Printf("unable to publish to Firebase: %v", err.Error())
|
2021-12-10 19:31:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := s.cache.MarkPublished(m); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-22 06:22:27 +03:00
|
|
|
func (s *Server) limitRequests(next handleFunc) handleFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request, v *visitor) error {
|
2022-02-15 00:09:59 +03:00
|
|
|
if util.InStringList(s.config.VisitorRequestExemptIPAddrs, v.ip) {
|
|
|
|
return next(w, r, v)
|
|
|
|
} else if err := v.RequestAllowed(); err != nil {
|
2022-01-22 06:22:27 +03:00
|
|
|
return errHTTPTooManyRequestsLimitRequests
|
|
|
|
}
|
|
|
|
return next(w, r, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) authWrite(next handleFunc) handleFunc {
|
2022-01-23 08:02:16 +03:00
|
|
|
return s.withAuth(next, auth.PermissionWrite)
|
2022-01-22 06:22:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) authRead(next handleFunc) handleFunc {
|
2022-01-23 08:02:16 +03:00
|
|
|
return s.withAuth(next, auth.PermissionRead)
|
2022-01-22 06:22:27 +03:00
|
|
|
}
|
|
|
|
|
2022-01-23 08:02:16 +03:00
|
|
|
func (s *Server) withAuth(next handleFunc, perm auth.Permission) handleFunc {
|
2022-01-22 06:22:27 +03:00
|
|
|
return func(w http.ResponseWriter, r *http.Request, v *visitor) error {
|
2022-01-23 07:01:20 +03:00
|
|
|
if s.auth == nil {
|
2022-01-22 06:22:27 +03:00
|
|
|
return next(w, r, v)
|
|
|
|
}
|
2022-01-27 20:49:05 +03:00
|
|
|
topics, _, err := s.topicsFromPath(r.URL.Path)
|
2022-01-22 06:22:27 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-24 07:02:39 +03:00
|
|
|
var user *auth.User // may stay nil if no auth header!
|
2022-01-23 07:01:20 +03:00
|
|
|
username, password, ok := r.BasicAuth()
|
2022-01-22 06:22:27 +03:00
|
|
|
if ok {
|
2022-01-23 07:01:20 +03:00
|
|
|
if user, err = s.auth.Authenticate(username, password); err != nil {
|
2022-01-22 22:47:27 +03:00
|
|
|
log.Printf("authentication failed: %s", err.Error())
|
2022-01-22 06:22:27 +03:00
|
|
|
return errHTTPUnauthorized
|
|
|
|
}
|
|
|
|
}
|
2022-01-27 20:49:05 +03:00
|
|
|
for _, t := range topics {
|
|
|
|
if err := s.auth.Authorize(user, t.ID, perm); err != nil {
|
|
|
|
log.Printf("unauthorized: %s", err.Error())
|
|
|
|
return errHTTPForbidden
|
|
|
|
}
|
2022-01-22 06:22:27 +03:00
|
|
|
}
|
|
|
|
return next(w, r, v)
|
2021-11-05 20:46:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-24 05:49:50 +03:00
|
|
|
// visitor creates or retrieves a rate.Limiter for the given visitor.
|
|
|
|
// This function was taken from https://www.alexedwards.net/blog/how-to-rate-limit-http-requests (MIT).
|
2021-11-05 20:46:27 +03:00
|
|
|
func (s *Server) visitor(r *http.Request) *visitor {
|
2021-10-24 05:49:50 +03:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
2021-11-05 20:46:27 +03:00
|
|
|
remoteAddr := r.RemoteAddr
|
2021-10-24 05:49:50 +03:00
|
|
|
ip, _, err := net.SplitHostPort(remoteAddr)
|
|
|
|
if err != nil {
|
|
|
|
ip = remoteAddr // This should not happen in real life; only in tests.
|
|
|
|
}
|
2021-11-05 20:46:27 +03:00
|
|
|
if s.config.BehindProxy && r.Header.Get("X-Forwarded-For") != "" {
|
|
|
|
ip = r.Header.Get("X-Forwarded-For")
|
|
|
|
}
|
2021-10-24 05:49:50 +03:00
|
|
|
v, exists := s.visitors[ip]
|
|
|
|
if !exists {
|
2021-12-24 17:01:29 +03:00
|
|
|
s.visitors[ip] = newVisitor(s.config, ip)
|
2021-11-01 22:21:38 +03:00
|
|
|
return s.visitors[ip]
|
2021-10-24 05:49:50 +03:00
|
|
|
}
|
2021-12-22 12:04:59 +03:00
|
|
|
v.Keepalive()
|
2021-10-24 05:49:50 +03:00
|
|
|
return v
|
|
|
|
}
|