Add some helper for base-url

This commit is contained in:
binwiederhier 2024-03-07 12:44:31 -05:00
parent 243123fd7e
commit f2cd1edc57
3 changed files with 20 additions and 6 deletions

View File

@ -16,6 +16,7 @@ import (
"math" "math"
"net" "net"
"net/netip" "net/netip"
"net/url"
"os" "os"
"os/signal" "os/signal"
"strings" "strings"
@ -126,7 +127,7 @@ func execServe(c *cli.Context) error {
// Read all the options // Read all the options
config := c.String("config") config := c.String("config")
baseURL := c.String("base-url") baseURL := strings.TrimSuffix(c.String("base-url"), "/")
listenHTTP := c.String("listen-http") listenHTTP := c.String("listen-http")
listenHTTPS := c.String("listen-https") listenHTTPS := c.String("listen-https")
listenUnix := c.String("listen-unix") listenUnix := c.String("listen-unix")
@ -273,10 +274,15 @@ func execServe(c *cli.Context) error {
return errors.New("if smtp-server-listen is set, smtp-server-domain must also be set") return errors.New("if smtp-server-listen is set, smtp-server-domain must also be set")
} else if attachmentCacheDir != "" && baseURL == "" { } else if attachmentCacheDir != "" && baseURL == "" {
return errors.New("if attachment-cache-dir is set, base-url must also be set") return errors.New("if attachment-cache-dir is set, base-url must also be set")
} else if baseURL != "" && !strings.HasPrefix(baseURL, "http://") && !strings.HasPrefix(baseURL, "https://") { } else if baseURL != "" {
return errors.New("if set, base-url must start with http:// or https://") u, err := url.Parse(baseURL)
} else if baseURL != "" && strings.HasSuffix(baseURL, "/") { if err != nil {
return errors.New("if set, base-url must not end with a slash (/)") return fmt.Errorf("if set, base-url must be a valid URL, e.g. https://ntfy.mydomain.com: %v", err)
} else if u.Scheme != "http" && u.Scheme != "https" {
return errors.New("if set, base-url must be a valid URL starting with http:// or https://, e.g. https://ntfy.mydomain.com")
} else if u.Path != "" {
return fmt.Errorf("if set, base-url must not have a path (%s), as hosting ntfy on a sub-path is not supported, e.g. https://ntfy.mydomain.com", u.Path)
}
} else if upstreamBaseURL != "" && !strings.HasPrefix(upstreamBaseURL, "http://") && !strings.HasPrefix(upstreamBaseURL, "https://") { } else if upstreamBaseURL != "" && !strings.HasPrefix(upstreamBaseURL, "http://") && !strings.HasPrefix(upstreamBaseURL, "https://") {
return errors.New("if set, upstream-base-url must start with http:// or https://") return errors.New("if set, upstream-base-url must start with http:// or https://")
} else if upstreamBaseURL != "" && strings.HasSuffix(upstreamBaseURL, "/") { } else if upstreamBaseURL != "" && strings.HasSuffix(upstreamBaseURL, "/") {

View File

@ -1315,6 +1315,11 @@ and the [ntfy Android app](https://github.com/binwiederhier/ntfy-android/release
### ntfy server v2.9.0 ### ntfy server v2.9.0
**Features:**
* Support for larger message delays with `message-delay-limit` (see [message limits](config.md#message-limits), [#1050](https://github.com/binwiederhier/ntfy/pull/1050)/[#1019](https://github.com/binwiederhier/ntfy/issues/1019), thanks to [@MrChadMWood](https://github.com/MrChadMWood) for reporting)
* Support for larger message body sizes with `message-size-limit` (use at your own risk, see [message limits](config.md#message-limits), [#1050](https://github.com/binwiederhier/ntfy/pull/1050), thanks to [@zhzy0077](https://github.com/zhzy0077) for implementing this, and to [@nkjshlsqja7331](https://github.com/nkjshlsqja7331) for reporting)
**Bug fixes + maintenance:** **Bug fixes + maintenance:**
* Remove `Rate-Topics` header due to DoS security issue if `visitor-subscriber-rate-limiting: true` ([#1048](https://github.com/binwiederhier/ntfy/issues/1048)) * Remove `Rate-Topics` header due to DoS security issue if `visitor-subscriber-rate-limiting: true` ([#1048](https://github.com/binwiederhier/ntfy/issues/1048))
@ -1326,6 +1331,9 @@ and the [ntfy Android app](https://github.com/binwiederhier/ntfy-android/release
* Update Watchtower example ([#1014](https://github.com/binwiederhier/ntfy/pull/1014), thanks to [@lennart-m](https://github.com/lennart-m)) * Update Watchtower example ([#1014](https://github.com/binwiederhier/ntfy/pull/1014), thanks to [@lennart-m](https://github.com/lennart-m))
* Fix dead links ([#1022](https://github.com/binwiederhier/ntfy/pull/1022), thanks to [@DerRockWolf](https://github.com/DerRockWolf)) * Fix dead links ([#1022](https://github.com/binwiederhier/ntfy/pull/1022), thanks to [@DerRockWolf](https://github.com/DerRockWolf))
!!! warning
**Breaking change**: The `Rate-Topics` header was removed due to a ([DoS issue](https://github.com/binwiederhier/ntfy/issues/1048). This only affects installations with `visitor-subscriber-rate-limiting: true`, which is not the default and likely very rarely used.
### ntfy Android app v1.16.1 (UNRELEASED) ### ntfy Android app v1.16.1 (UNRELEASED)
**Features:** **Features:**

View File

@ -128,7 +128,7 @@ func TestParseSize_10kLowerCaseSuccess(t *testing.T) {
func TestParseSize_FailureInvalid(t *testing.T) { func TestParseSize_FailureInvalid(t *testing.T) {
_, err := ParseSize("not a size") _, err := ParseSize("not a size")
require.Nil(t, err) require.Error(t, err)
} }
func TestFormatSize(t *testing.T) { func TestFormatSize(t *testing.T) {