2021-12-18 22:02:36 -05:00
package server
2021-10-23 21:29:45 -04:00
2021-10-23 22:49:50 -04:00
import (
2022-07-03 15:07:57 -04:00
"io/fs"
2022-10-05 15:42:07 -05:00
"net/netip"
2021-10-23 22:49:50 -04:00
"time"
2023-05-24 21:36:01 +02:00
2023-11-16 20:54:58 -05:00
"heckel.io/ntfy/v2/user"
2021-10-23 22:49:50 -04:00
)
2022-01-12 18:52:07 -05:00
// Defines default config settings (excluding limits, see below)
2021-10-23 21:29:45 -04:00
const (
2022-05-31 23:27:24 -04:00
DefaultListenHTTP = ":80"
DefaultCacheDuration = 12 * time . Hour
2024-03-07 12:22:35 -05:00
DefaultCacheBatchTimeout = time . Duration ( 0 )
2022-05-31 23:27:24 -04:00
DefaultKeepaliveInterval = 45 * time . Second // Not too frequently to save battery (Android read timeout used to be 77s!)
DefaultManagerInterval = time . Minute
DefaultDelayedSenderInterval = 10 * time . Second
2024-03-07 11:53:12 -05:00
DefaultMessageDelayMin = 10 * time . Second
DefaultMessageDelayMax = 3 * 24 * time . Hour
2022-05-31 23:27:24 -04:00
DefaultFirebaseKeepaliveInterval = 3 * time . Hour // ~control topic (Android), not too frequently to save battery
DefaultFirebasePollInterval = 20 * time . Minute // ~poll topic (iOS), max. 2-3 times per hour (see docs)
DefaultFirebaseQuotaExceededPenaltyDuration = 10 * time . Minute // Time that over-users are locked out of Firebase if it returns "quota exceeded"
2023-02-08 22:57:10 -05:00
DefaultStripePriceCacheDuration = 3 * time . Hour // Time to keep Stripe prices cached in memory before a refresh is needed
2021-10-23 22:49:50 -04:00
)
2023-06-09 23:17:48 -04:00
// Defines default Web Push settings
2023-06-02 14:45:05 +02:00
const (
2025-05-31 15:33:21 -04:00
DefaultWebPushExpiryWarningDuration = 55 * 24 * time . Hour
DefaultWebPushExpiryDuration = 60 * 24 * time . Hour
2023-06-02 14:45:05 +02:00
)
2022-01-12 18:52:07 -05:00
// Defines all global and per-visitor limits
// - message size limit: the max number of bytes for a message
2022-01-04 00:55:08 +01:00
// - total topic limit: max number of topics overall
2022-01-12 18:52:07 -05:00
// - various attachment limits
const (
2024-03-07 11:53:12 -05:00
DefaultMessageSizeLimit = 4096 // Bytes; note that FCM/APNS have a limit of ~4 KB for the entire message
2022-01-12 18:52:07 -05:00
DefaultTotalTopicLimit = 15000
2022-01-12 21:24:48 -05:00
DefaultAttachmentTotalSizeLimit = int64 ( 5 * 1024 * 1024 * 1024 ) // 5 GB
DefaultAttachmentFileSizeLimit = int64 ( 15 * 1024 * 1024 ) // 15 MB
2022-01-12 18:52:07 -05:00
DefaultAttachmentExpiryDuration = 3 * time . Hour
)
// Defines all per-visitor limits
2022-01-04 00:55:08 +01:00
// - per visitor subscription limit: max number of subscriptions (active HTTP connections) per per-visitor/IP
2022-02-14 17:07:17 -05:00
// - per visitor request limit: max number of PUT/GET/.. requests (here: 60 requests bucket, replenished at a rate of one per 5 seconds)
2021-12-24 00:03:04 +01:00
// - per visitor email limit: max number of emails (here: 16 email bucket, replenished at a rate of one per hour)
2022-01-12 18:52:07 -05:00
// - per visitor attachment size limit: total per-visitor attachment size in bytes to be stored on the server
2022-01-12 21:24:48 -05:00
// - per visitor attachment daily bandwidth limit: number of bytes that can be transferred to/from the server
2021-11-05 13:46:27 -04:00
const (
2022-01-12 21:24:48 -05:00
DefaultVisitorSubscriptionLimit = 30
DefaultVisitorRequestLimitBurst = 60
2022-02-14 17:07:17 -05:00
DefaultVisitorRequestLimitReplenish = 5 * time . Second
2023-01-26 22:57:18 -05:00
DefaultVisitorMessageDailyLimit = 0
2022-01-12 21:24:48 -05:00
DefaultVisitorEmailLimitBurst = 16
DefaultVisitorEmailLimitReplenish = time . Hour
2023-01-25 22:26:04 -05:00
DefaultVisitorAccountCreationLimitBurst = 3
DefaultVisitorAccountCreationLimitReplenish = 24 * time . Hour
2023-03-08 14:51:47 -05:00
DefaultVisitorAuthFailureLimitBurst = 30
2023-02-08 15:20:44 -05:00
DefaultVisitorAuthFailureLimitReplenish = time . Minute
2022-01-12 21:24:48 -05:00
DefaultVisitorAttachmentTotalSizeLimit = 100 * 1024 * 1024 // 100 MB
DefaultVisitorAttachmentDailyBandwidthLimit = 500 * 1024 * 1024 // 500 MB
2025-07-04 10:16:49 +02:00
DefaultVisitorPrefixBitsIPv4 = 32 // Use the entire IPv4 address for rate limiting
DefaultVisitorPrefixBitsIPv6 = 64 // Use /64 for IPv6 rate limiting
2021-10-23 21:29:45 -04:00
)
2023-01-10 22:51:51 -05:00
var (
// DefaultVisitorStatsResetTime defines the time at which visitor stats are reset (wall clock only)
DefaultVisitorStatsResetTime = time . Date ( 0 , 0 , 0 , 0 , 0 , 0 , 0 , time . UTC )
2023-02-09 08:32:51 -05:00
// DefaultDisallowedTopics defines the topics that are forbidden, because they are used elsewhere. This array can be
// extended using the server.yml config. If updated, also update in Android and web app.
2023-03-15 22:34:06 -04:00
DefaultDisallowedTopics = [ ] string { "docs" , "static" , "file" , "app" , "metrics" , "account" , "settings" , "signup" , "login" , "v1" }
2023-01-10 22:51:51 -05:00
)
2021-10-23 21:29:45 -04:00
// Config is the main config struct for the application. Use New to instantiate a default config struct.
type Config struct {
2023-02-08 15:20:44 -05:00
File string // Config file, only used for testing
2022-01-12 21:24:48 -05:00
BaseURL string
ListenHTTP string
ListenHTTPS string
2022-01-14 20:16:12 -05:00
ListenUnix string
2022-07-03 15:07:57 -04:00
ListenUnixMode fs . FileMode
2022-01-12 21:24:48 -05:00
KeyFile string
CertFile string
FirebaseKeyFile string
CacheFile string
CacheDuration time . Duration
2022-06-23 11:02:45 -04:00
CacheStartupQueries string
2022-11-16 10:28:20 -05:00
CacheBatchSize int
CacheBatchTimeout time . Duration
2022-01-22 23:01:20 -05:00
AuthFile string
2023-01-05 15:20:44 -05:00
AuthStartupQueries string
2023-01-02 21:12:42 -05:00
AuthDefault user . Permission
2023-01-28 09:03:14 -05:00
AuthBcryptCost int
2023-01-28 20:29:06 -05:00
AuthStatsQueueWriterInterval time . Duration
2022-01-12 21:24:48 -05:00
AttachmentCacheDir string
AttachmentTotalSizeLimit int64
AttachmentFileSizeLimit int64
AttachmentExpiryDuration time . Duration
KeepaliveInterval time . Duration
ManagerInterval time . Duration
2023-02-09 08:32:51 -05:00
DisallowedTopics [ ] string
2023-05-01 11:58:49 -04:00
WebRoot string // empty to disable
2022-05-31 21:39:19 -04:00
DelayedSenderInterval time . Duration
2022-01-12 21:24:48 -05:00
FirebaseKeepaliveInterval time . Duration
2022-05-25 21:39:46 -04:00
FirebasePollInterval time . Duration
2022-05-31 23:27:24 -04:00
FirebaseQuotaExceededPenaltyDuration time . Duration
2022-05-27 20:30:20 -04:00
UpstreamBaseURL string
2023-05-18 13:08:10 -04:00
UpstreamAccessToken string
2022-01-12 21:24:48 -05:00
SMTPSenderAddr string
SMTPSenderUser string
SMTPSenderPass string
SMTPSenderFrom string
SMTPServerListen string
SMTPServerDomain string
SMTPServerAddrPrefix string
2023-05-05 16:22:54 -04:00
TwilioAccount string
TwilioAuthToken string
2023-05-18 13:32:27 -04:00
TwilioPhoneNumber string
2023-05-16 14:15:58 -04:00
TwilioCallsBaseURL string
2023-05-11 13:50:10 -04:00
TwilioVerifyBaseURL string
TwilioVerifyService string
2023-03-15 22:34:06 -04:00
MetricsEnable bool
MetricsListenHTTP string
2023-03-28 14:41:16 -04:00
ProfileListenHTTP string
2024-03-07 11:53:12 -05:00
MessageDelayMin time . Duration
MessageDelayMax time . Duration
MessageSizeLimit int
2022-01-12 21:24:48 -05:00
TotalTopicLimit int
TotalAttachmentSizeLimit int64
VisitorSubscriptionLimit int
VisitorAttachmentTotalSizeLimit int64
2023-01-25 10:05:54 -05:00
VisitorAttachmentDailyBandwidthLimit int64
2022-01-12 21:24:48 -05:00
VisitorRequestLimitBurst int
VisitorRequestLimitReplenish time . Duration
2022-10-05 15:42:07 -05:00
VisitorRequestExemptIPAddrs [ ] netip . Prefix
2023-01-26 22:57:18 -05:00
VisitorMessageDailyLimit int
2022-01-12 21:24:48 -05:00
VisitorEmailLimitBurst int
VisitorEmailLimitReplenish time . Duration
2023-01-25 22:26:04 -05:00
VisitorAccountCreationLimitBurst int
VisitorAccountCreationLimitReplenish time . Duration
2023-02-08 15:20:44 -05:00
VisitorAuthFailureLimitBurst int
VisitorAuthFailureLimitReplenish time . Duration
2023-01-10 22:51:51 -05:00
VisitorStatsResetTime time . Time // Time of the day at which to reset visitor stats
2023-03-03 20:23:18 -05:00
VisitorSubscriberRateLimiting bool // Enable subscriber-based rate limiting for UnifiedPush topics
2025-07-04 10:16:49 +02:00
VisitorPrefixBitsIPv4 int // Number of bits for IPv4 rate limiting (default: 32)
VisitorPrefixBitsIPv6 int // Number of bits for IPv6 rate limiting (default: 64)
2025-07-04 07:38:58 +02:00
BehindProxy bool // If true, the server will trust the proxy client IP header to determine the client IP address (IPv4 and IPv6 supported)
ProxyForwardedHeader string // The header field to read the real/client IP address from, if BehindProxy is true, defaults to "X-Forwarded-For" (IPv4 and IPv6 supported)
ProxyTrustedAddresses [ ] string // List of trusted proxy addresses (IPv4 or IPv6) that will be stripped from the Forwarded header if BehindProxy is true
2023-01-16 16:35:37 -05:00
StripeSecretKey string
2023-01-14 06:43:44 -05:00
StripeWebhookKey string
2023-01-18 15:50:06 -05:00
StripePriceCacheDuration time . Duration
2023-02-28 14:38:31 -05:00
BillingContact string
2023-01-05 15:20:44 -05:00
EnableSignup bool // Enable creation of accounts via API and UI
2022-12-14 23:11:22 -05:00
EnableLogin bool
2023-03-15 22:34:06 -04:00
EnableReservations bool // Allow users with role "user" to own/reserve topics
EnableMetrics bool
2023-01-18 15:50:06 -05:00
AccessControlAllowOrigin string // CORS header field to restrict access from web clients
2022-06-12 11:54:58 -04:00
Version string // injected by App
2023-05-24 21:36:01 +02:00
WebPushPrivateKey string
WebPushPublicKey string
2023-06-17 21:57:47 -04:00
WebPushFile string
2023-05-24 21:36:01 +02:00
WebPushEmailAddress string
2023-06-18 14:20:22 -04:00
WebPushStartupQueries string
2023-06-02 14:45:05 +02:00
WebPushExpiryDuration time . Duration
WebPushExpiryWarningDuration time . Duration
2021-10-23 21:29:45 -04:00
}
2021-12-19 14:27:26 -05:00
// NewConfig instantiates a default new server config
2021-12-22 14:17:50 +01:00
func NewConfig ( ) * Config {
2021-10-23 21:29:45 -04:00
return & Config {
2023-02-08 15:20:44 -05:00
File : "" , // Only used for testing
2022-01-12 21:24:48 -05:00
BaseURL : "" ,
ListenHTTP : DefaultListenHTTP ,
ListenHTTPS : "" ,
2022-01-14 20:16:12 -05:00
ListenUnix : "" ,
2022-07-03 19:33:01 -04:00
ListenUnixMode : 0 ,
2022-01-12 21:24:48 -05:00
KeyFile : "" ,
CertFile : "" ,
FirebaseKeyFile : "" ,
CacheFile : "" ,
CacheDuration : DefaultCacheDuration ,
2023-01-18 15:50:06 -05:00
CacheStartupQueries : "" ,
2022-11-16 10:28:20 -05:00
CacheBatchSize : 0 ,
CacheBatchTimeout : 0 ,
2022-01-22 23:01:20 -05:00
AuthFile : "" ,
2023-01-18 15:50:06 -05:00
AuthStartupQueries : "" ,
2023-02-08 22:57:10 -05:00
AuthDefault : user . PermissionReadWrite ,
2023-01-28 09:03:14 -05:00
AuthBcryptCost : user . DefaultUserPasswordBcryptCost ,
2023-01-28 20:29:06 -05:00
AuthStatsQueueWriterInterval : user . DefaultUserStatsQueueWriterInterval ,
2022-01-12 21:24:48 -05:00
AttachmentCacheDir : "" ,
AttachmentTotalSizeLimit : DefaultAttachmentTotalSizeLimit ,
AttachmentFileSizeLimit : DefaultAttachmentFileSizeLimit ,
AttachmentExpiryDuration : DefaultAttachmentExpiryDuration ,
KeepaliveInterval : DefaultKeepaliveInterval ,
ManagerInterval : DefaultManagerInterval ,
2023-02-09 15:24:12 -05:00
DisallowedTopics : DefaultDisallowedTopics ,
2023-05-01 11:58:49 -04:00
WebRoot : "/" ,
2022-05-31 23:27:24 -04:00
DelayedSenderInterval : DefaultDelayedSenderInterval ,
2022-01-12 21:24:48 -05:00
FirebaseKeepaliveInterval : DefaultFirebaseKeepaliveInterval ,
2022-05-25 21:39:46 -04:00
FirebasePollInterval : DefaultFirebasePollInterval ,
2022-05-31 23:27:24 -04:00
FirebaseQuotaExceededPenaltyDuration : DefaultFirebaseQuotaExceededPenaltyDuration ,
2023-01-18 15:50:06 -05:00
UpstreamBaseURL : "" ,
2023-05-18 13:08:10 -04:00
UpstreamAccessToken : "" ,
2023-01-18 15:50:06 -05:00
SMTPSenderAddr : "" ,
SMTPSenderUser : "" ,
SMTPSenderPass : "" ,
SMTPSenderFrom : "" ,
SMTPServerListen : "" ,
SMTPServerDomain : "" ,
SMTPServerAddrPrefix : "" ,
2023-05-16 14:15:58 -04:00
TwilioCallsBaseURL : "https://api.twilio.com" , // Override for tests
2023-05-05 16:22:54 -04:00
TwilioAccount : "" ,
TwilioAuthToken : "" ,
2023-05-18 13:32:27 -04:00
TwilioPhoneNumber : "" ,
2023-05-11 13:50:10 -04:00
TwilioVerifyBaseURL : "https://verify.twilio.com" , // Override for tests
TwilioVerifyService : "" ,
2024-03-07 11:53:12 -05:00
MessageSizeLimit : DefaultMessageSizeLimit ,
MessageDelayMin : DefaultMessageDelayMin ,
MessageDelayMax : DefaultMessageDelayMax ,
2022-01-12 21:24:48 -05:00
TotalTopicLimit : DefaultTotalTopicLimit ,
2023-01-18 15:50:06 -05:00
TotalAttachmentSizeLimit : 0 ,
2022-01-12 21:24:48 -05:00
VisitorSubscriptionLimit : DefaultVisitorSubscriptionLimit ,
2025-07-04 10:19:27 +02:00
VisitorSubscriberRateLimiting : false ,
2022-01-12 21:24:48 -05:00
VisitorAttachmentTotalSizeLimit : DefaultVisitorAttachmentTotalSizeLimit ,
VisitorAttachmentDailyBandwidthLimit : DefaultVisitorAttachmentDailyBandwidthLimit ,
VisitorRequestLimitBurst : DefaultVisitorRequestLimitBurst ,
VisitorRequestLimitReplenish : DefaultVisitorRequestLimitReplenish ,
2022-10-05 15:42:07 -05:00
VisitorRequestExemptIPAddrs : make ( [ ] netip . Prefix , 0 ) ,
2023-01-26 22:57:18 -05:00
VisitorMessageDailyLimit : DefaultVisitorMessageDailyLimit ,
2022-01-12 21:24:48 -05:00
VisitorEmailLimitBurst : DefaultVisitorEmailLimitBurst ,
VisitorEmailLimitReplenish : DefaultVisitorEmailLimitReplenish ,
2023-01-25 22:26:04 -05:00
VisitorAccountCreationLimitBurst : DefaultVisitorAccountCreationLimitBurst ,
VisitorAccountCreationLimitReplenish : DefaultVisitorAccountCreationLimitReplenish ,
2023-02-08 15:20:44 -05:00
VisitorAuthFailureLimitBurst : DefaultVisitorAuthFailureLimitBurst ,
VisitorAuthFailureLimitReplenish : DefaultVisitorAuthFailureLimitReplenish ,
2023-01-10 22:51:51 -05:00
VisitorStatsResetTime : DefaultVisitorStatsResetTime ,
2025-07-04 10:19:27 +02:00
VisitorPrefixBitsIPv4 : DefaultVisitorPrefixBitsIPv4 , // Default: use full IPv4 address
VisitorPrefixBitsIPv6 : DefaultVisitorPrefixBitsIPv6 , // Default: use /64 for IPv6
BehindProxy : false , // If true, the server will trust the proxy client IP header to determine the client IP address
ProxyForwardedHeader : "X-Forwarded-For" , // Default header for reverse proxy client IPs
2023-01-18 15:50:06 -05:00
StripeSecretKey : "" ,
StripeWebhookKey : "" ,
StripePriceCacheDuration : DefaultStripePriceCacheDuration ,
2023-03-03 20:23:18 -05:00
BillingContact : "" ,
2023-01-18 15:50:06 -05:00
EnableSignup : false ,
EnableLogin : false ,
EnableReservations : false ,
AccessControlAllowOrigin : "*" ,
2022-06-12 11:54:58 -04:00
Version : "" ,
2023-05-24 21:36:01 +02:00
WebPushPrivateKey : "" ,
WebPushPublicKey : "" ,
2023-06-17 21:57:47 -04:00
WebPushFile : "" ,
2023-05-29 17:57:21 +02:00
WebPushEmailAddress : "" ,
2023-06-02 14:45:05 +02:00
WebPushExpiryDuration : DefaultWebPushExpiryDuration ,
WebPushExpiryWarningDuration : DefaultWebPushExpiryWarningDuration ,
2021-10-23 21:29:45 -04:00
}
}