2022-01-16 04:17:46 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2022-06-02 03:24:44 +00:00
|
|
|
"fmt"
|
|
|
|
"github.com/emersion/go-smtp"
|
2022-12-22 02:55:39 +00:00
|
|
|
"heckel.io/ntfy/log"
|
2022-06-20 01:25:35 +00:00
|
|
|
"heckel.io/ntfy/util"
|
2022-12-29 14:57:42 +00:00
|
|
|
"io"
|
2022-01-16 04:17:46 +00:00
|
|
|
"net/http"
|
2022-12-22 02:55:39 +00:00
|
|
|
"net/netip"
|
2022-01-16 04:17:46 +00:00
|
|
|
"strings"
|
2022-06-20 01:25:35 +00:00
|
|
|
"unicode/utf8"
|
2022-01-16 04:17:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func readBoolParam(r *http.Request, defaultValue bool, names ...string) bool {
|
|
|
|
value := strings.ToLower(readParam(r, names...))
|
|
|
|
if value == "" {
|
|
|
|
return defaultValue
|
|
|
|
}
|
|
|
|
return value == "1" || value == "yes" || value == "true"
|
|
|
|
}
|
|
|
|
|
|
|
|
func readParam(r *http.Request, names ...string) string {
|
2022-02-26 04:25:04 +00:00
|
|
|
value := readHeaderParam(r, names...)
|
|
|
|
if value != "" {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
return readQueryParam(r, names...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func readHeaderParam(r *http.Request, names ...string) string {
|
2022-01-16 04:17:46 +00:00
|
|
|
for _, name := range names {
|
|
|
|
value := r.Header.Get(name)
|
|
|
|
if value != "" {
|
|
|
|
return strings.TrimSpace(value)
|
|
|
|
}
|
|
|
|
}
|
2022-02-26 04:25:04 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func readQueryParam(r *http.Request, names ...string) string {
|
2022-01-16 04:17:46 +00:00
|
|
|
for _, name := range names {
|
|
|
|
value := r.URL.Query().Get(strings.ToLower(name))
|
|
|
|
if value != "" {
|
|
|
|
return strings.TrimSpace(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
2022-06-02 03:24:44 +00:00
|
|
|
|
|
|
|
func logMessagePrefix(v *visitor, m *message) string {
|
2023-01-23 03:21:30 +00:00
|
|
|
return fmt.Sprintf("%s/%s/%s", v.String(), m.Topic, m.ID)
|
2022-06-02 03:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func logHTTPPrefix(v *visitor, r *http.Request) string {
|
|
|
|
requestURI := r.RequestURI
|
|
|
|
if requestURI == "" {
|
|
|
|
requestURI = r.URL.Path
|
|
|
|
}
|
2023-01-23 15:58:39 +00:00
|
|
|
return fmt.Sprintf("HTTP %s %s %s", v.String(), r.Method, requestURI)
|
2023-01-23 03:21:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func logStripePrefix(customerID, subscriptionID string) string {
|
|
|
|
if subscriptionID != "" {
|
2023-01-23 15:58:39 +00:00
|
|
|
return fmt.Sprintf("STRIPE %s/%s", customerID, subscriptionID)
|
2023-01-23 03:21:30 +00:00
|
|
|
}
|
2023-01-23 15:58:39 +00:00
|
|
|
return fmt.Sprintf("STRIPE %s", customerID)
|
2022-06-02 03:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func logSMTPPrefix(state *smtp.ConnectionState) string {
|
2023-01-23 15:58:39 +00:00
|
|
|
return fmt.Sprintf("SMTP %s/%s", state.Hostname, state.RemoteAddr.String())
|
2022-06-02 03:24:44 +00:00
|
|
|
}
|
2022-06-20 01:25:35 +00:00
|
|
|
|
|
|
|
func renderHTTPRequest(r *http.Request) string {
|
|
|
|
peekLimit := 4096
|
|
|
|
lines := fmt.Sprintf("%s %s %s\n", r.Method, r.URL.RequestURI(), r.Proto)
|
|
|
|
for key, values := range r.Header {
|
|
|
|
for _, value := range values {
|
|
|
|
lines += fmt.Sprintf("%s: %s\n", key, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lines += "\n"
|
|
|
|
body, err := util.Peek(r.Body, peekLimit)
|
|
|
|
if err != nil {
|
|
|
|
lines = fmt.Sprintf("(could not read body: %s)\n", err.Error())
|
|
|
|
} else if utf8.Valid(body.PeekedBytes) {
|
|
|
|
lines += string(body.PeekedBytes)
|
|
|
|
if body.LimitReached {
|
|
|
|
lines += fmt.Sprintf(" ... (peeked %d bytes)", peekLimit)
|
|
|
|
}
|
|
|
|
lines += "\n"
|
|
|
|
} else {
|
|
|
|
if body.LimitReached {
|
|
|
|
lines += fmt.Sprintf("(peeked bytes not UTF-8, peek limit of %d bytes reached, hex: %x ...)\n", peekLimit, body.PeekedBytes)
|
|
|
|
} else {
|
|
|
|
lines += fmt.Sprintf("(peeked bytes not UTF-8, %d bytes, hex: %x)\n", len(body.PeekedBytes), body.PeekedBytes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r.Body = body // Important: Reset body, so it can be re-read
|
|
|
|
return strings.TrimSpace(lines)
|
|
|
|
}
|
2022-12-22 02:55:39 +00:00
|
|
|
|
|
|
|
func extractIPAddress(r *http.Request, behindProxy bool) netip.Addr {
|
|
|
|
remoteAddr := r.RemoteAddr
|
|
|
|
addrPort, err := netip.ParseAddrPort(remoteAddr)
|
|
|
|
ip := addrPort.Addr()
|
|
|
|
if err != nil {
|
|
|
|
// This should not happen in real life; only in tests. So, using falling back to 0.0.0.0 if address unspecified
|
|
|
|
ip, err = netip.ParseAddr(remoteAddr)
|
|
|
|
if err != nil {
|
|
|
|
ip = netip.IPv4Unspecified()
|
2022-12-23 14:37:47 +00:00
|
|
|
if remoteAddr != "@" || !behindProxy { // RemoteAddr is @ when unix socket is used
|
|
|
|
log.Warn("unable to parse IP (%s), new visitor with unspecified IP (0.0.0.0) created %s", remoteAddr, err)
|
|
|
|
}
|
2022-12-22 02:55:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if behindProxy && strings.TrimSpace(r.Header.Get("X-Forwarded-For")) != "" {
|
|
|
|
// X-Forwarded-For can contain multiple addresses (see #328). If we are behind a proxy,
|
|
|
|
// only the right-most address can be trusted (as this is the one added by our proxy server).
|
|
|
|
// See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For for details.
|
|
|
|
ips := util.SplitNoEmpty(r.Header.Get("X-Forwarded-For"), ",")
|
|
|
|
realIP, err := netip.ParseAddr(strings.TrimSpace(util.LastString(ips, remoteAddr)))
|
|
|
|
if err != nil {
|
|
|
|
log.Error("invalid IP address %s received in X-Forwarded-For header: %s", ip, err.Error())
|
|
|
|
// Fall back to regular remote address if X-Forwarded-For is damaged
|
|
|
|
} else {
|
|
|
|
ip = realIP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ip
|
|
|
|
}
|
2022-12-29 14:57:42 +00:00
|
|
|
|
2023-01-28 04:10:59 +00:00
|
|
|
func readJSONWithLimit[T any](r io.ReadCloser, limit int, allowEmpty bool) (*T, error) {
|
|
|
|
obj, err := util.UnmarshalJSONWithLimit[T](r, limit, allowEmpty)
|
2022-12-31 21:08:49 +00:00
|
|
|
if err == util.ErrUnmarshalJSON {
|
2022-12-29 14:57:42 +00:00
|
|
|
return nil, errHTTPBadRequestJSONInvalid
|
|
|
|
} else if err == util.ErrTooLargeJSON {
|
|
|
|
return nil, errHTTPEntityTooLargeJSONBody
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return obj, nil
|
|
|
|
}
|