ntfy/server/util.go

106 lines
3 KiB
Go
Raw Normal View History

2022-01-16 17:17:46 +13:00
package server
import (
"heckel.io/ntfy/util"
2022-12-30 03:57:42 +13:00
"io"
2022-01-16 17:17:46 +13:00
"net/http"
2022-12-22 15:55:39 +13:00
"net/netip"
2022-01-16 17:17:46 +13:00
"strings"
)
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"
}
2023-02-23 15:33:18 +13:00
func readCommaSeparatedParam(r *http.Request, names ...string) (params []string) {
2023-02-22 15:04:56 +13:00
paramStr := readParam(r, names...)
if paramStr != "" {
params = make([]string, 0)
for _, s := range util.SplitNoEmpty(paramStr, ",") {
params = append(params, strings.TrimSpace(s))
}
}
return params
}
2022-01-16 17:17:46 +13:00
func readParam(r *http.Request, names ...string) string {
value := readHeaderParam(r, names...)
if value != "" {
return value
}
return readQueryParam(r, names...)
}
func readHeaderParam(r *http.Request, names ...string) string {
2022-01-16 17:17:46 +13:00
for _, name := range names {
value := r.Header.Get(name)
if value != "" {
return strings.TrimSpace(value)
}
}
return ""
}
2023-02-22 15:04:56 +13:00
func readHeaderParamValues(r *http.Request, names ...string) (values []string) {
for _, name := range names {
values = append(values, r.Header.Values(name)...)
}
return
}
func readQueryParam(r *http.Request, names ...string) string {
2022-01-16 17:17:46 +13:00
for _, name := range names {
value := r.URL.Query().Get(strings.ToLower(name))
if value != "" {
return strings.TrimSpace(value)
}
}
return ""
}
2022-06-02 15:24:44 +12:00
2022-12-22 15:55:39 +13: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-24 03:37:47 +13:00
if remoteAddr != "@" || !behindProxy { // RemoteAddr is @ when unix socket is used
2023-02-16 04:55:01 +13:00
logr(r).Err(err).Warn("unable to parse IP (%s), new visitor with unspecified IP (0.0.0.0) created", remoteAddr)
2022-12-24 03:37:47 +13:00
}
2022-12-22 15:55:39 +13: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 {
2023-02-16 04:55:01 +13:00
logr(r).Err(err).Error("invalid IP address %s received in X-Forwarded-For header", ip)
2022-12-22 15:55:39 +13:00
// Fall back to regular remote address if X-Forwarded-For is damaged
} else {
ip = realIP
}
}
return ip
}
2022-12-30 03:57:42 +13:00
2023-01-28 17:10:59 +13:00
func readJSONWithLimit[T any](r io.ReadCloser, limit int, allowEmpty bool) (*T, error) {
obj, err := util.UnmarshalJSONWithLimit[T](r, limit, allowEmpty)
2023-01-01 10:08:49 +13:00
if err == util.ErrUnmarshalJSON {
2022-12-30 03:57:42 +13:00
return nil, errHTTPBadRequestJSONInvalid
} else if err == util.ErrTooLargeJSON {
return nil, errHTTPEntityTooLargeJSONBody
} else if err != nil {
return nil, err
}
return obj, nil
}