config.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package server
  2. import (
  3. "heckel.io/ntfy/user"
  4. "io/fs"
  5. "net/netip"
  6. "time"
  7. )
  8. // Defines default config settings (excluding limits, see below)
  9. const (
  10. DefaultListenHTTP = ":80"
  11. DefaultCacheDuration = 12 * time.Hour
  12. DefaultKeepaliveInterval = 45 * time.Second // Not too frequently to save battery (Android read timeout used to be 77s!)
  13. DefaultManagerInterval = time.Minute
  14. DefaultDelayedSenderInterval = 10 * time.Second
  15. DefaultMinDelay = 10 * time.Second
  16. DefaultMaxDelay = 3 * 24 * time.Hour
  17. DefaultFirebaseKeepaliveInterval = 3 * time.Hour // ~control topic (Android), not too frequently to save battery
  18. DefaultFirebasePollInterval = 20 * time.Minute // ~poll topic (iOS), max. 2-3 times per hour (see docs)
  19. DefaultFirebaseQuotaExceededPenaltyDuration = 10 * time.Minute // Time that over-users are locked out of Firebase if it returns "quota exceeded"
  20. )
  21. // Defines all global and per-visitor limits
  22. // - message size limit: the max number of bytes for a message
  23. // - total topic limit: max number of topics overall
  24. // - various attachment limits
  25. const (
  26. DefaultMessageLengthLimit = 4096 // Bytes
  27. DefaultTotalTopicLimit = 15000
  28. DefaultAttachmentTotalSizeLimit = int64(5 * 1024 * 1024 * 1024) // 5 GB
  29. DefaultAttachmentFileSizeLimit = int64(15 * 1024 * 1024) // 15 MB
  30. DefaultAttachmentExpiryDuration = 3 * time.Hour
  31. )
  32. // Defines all per-visitor limits
  33. // - per visitor subscription limit: max number of subscriptions (active HTTP connections) per per-visitor/IP
  34. // - per visitor request limit: max number of PUT/GET/.. requests (here: 60 requests bucket, replenished at a rate of one per 5 seconds)
  35. // - per visitor email limit: max number of emails (here: 16 email bucket, replenished at a rate of one per hour)
  36. // - per visitor attachment size limit: total per-visitor attachment size in bytes to be stored on the server
  37. // - per visitor attachment daily bandwidth limit: number of bytes that can be transferred to/from the server
  38. const (
  39. DefaultVisitorSubscriptionLimit = 30
  40. DefaultVisitorRequestLimitBurst = 60
  41. DefaultVisitorRequestLimitReplenish = 5 * time.Second
  42. DefaultVisitorEmailLimitBurst = 16
  43. DefaultVisitorEmailLimitReplenish = time.Hour
  44. DefaultVisitorAccountCreateLimitBurst = 3
  45. DefaultVisitorAccountCreateLimitReplenish = 24 * time.Hour
  46. DefaultVisitorAttachmentTotalSizeLimit = 100 * 1024 * 1024 // 100 MB
  47. DefaultVisitorAttachmentDailyBandwidthLimit = 500 * 1024 * 1024 // 500 MB
  48. )
  49. var (
  50. // DefaultVisitorStatsResetTime defines the time at which visitor stats are reset (wall clock only)
  51. DefaultVisitorStatsResetTime = time.Date(0, 0, 0, 0, 0, 0, 0, time.UTC)
  52. )
  53. // Config is the main config struct for the application. Use New to instantiate a default config struct.
  54. type Config struct {
  55. BaseURL string
  56. ListenHTTP string
  57. ListenHTTPS string
  58. ListenUnix string
  59. ListenUnixMode fs.FileMode
  60. KeyFile string
  61. CertFile string
  62. FirebaseKeyFile string
  63. CacheFile string
  64. CacheDuration time.Duration
  65. CacheStartupQueries string
  66. CacheBatchSize int
  67. CacheBatchTimeout time.Duration
  68. AuthFile string
  69. AuthStartupQueries string
  70. AuthDefault user.Permission
  71. AttachmentCacheDir string
  72. AttachmentTotalSizeLimit int64
  73. AttachmentFileSizeLimit int64
  74. AttachmentExpiryDuration time.Duration
  75. KeepaliveInterval time.Duration
  76. ManagerInterval time.Duration
  77. WebRootIsApp bool
  78. DelayedSenderInterval time.Duration
  79. FirebaseKeepaliveInterval time.Duration
  80. FirebasePollInterval time.Duration
  81. FirebaseQuotaExceededPenaltyDuration time.Duration
  82. UpstreamBaseURL string
  83. SMTPSenderAddr string
  84. SMTPSenderUser string
  85. SMTPSenderPass string
  86. SMTPSenderFrom string
  87. SMTPServerListen string
  88. SMTPServerDomain string
  89. SMTPServerAddrPrefix string
  90. MessageLimit int
  91. MinDelay time.Duration
  92. MaxDelay time.Duration
  93. TotalTopicLimit int
  94. TotalAttachmentSizeLimit int64
  95. VisitorSubscriptionLimit int
  96. VisitorAttachmentTotalSizeLimit int64
  97. VisitorAttachmentDailyBandwidthLimit int
  98. VisitorRequestLimitBurst int
  99. VisitorRequestLimitReplenish time.Duration
  100. VisitorRequestExemptIPAddrs []netip.Prefix
  101. VisitorEmailLimitBurst int
  102. VisitorEmailLimitReplenish time.Duration
  103. VisitorAccountCreateLimitBurst int
  104. VisitorAccountCreateLimitReplenish time.Duration
  105. VisitorStatsResetTime time.Time // Time of the day at which to reset visitor stats
  106. BehindProxy bool
  107. StripeSecretKey string
  108. StripeWebhookKey string
  109. EnableWeb bool
  110. EnableSignup bool // Enable creation of accounts via API and UI
  111. EnableLogin bool
  112. EnablePayments bool
  113. EnableReservations bool // Allow users with role "user" to own/reserve topics
  114. Version string // injected by App
  115. }
  116. // NewConfig instantiates a default new server config
  117. func NewConfig() *Config {
  118. return &Config{
  119. BaseURL: "",
  120. ListenHTTP: DefaultListenHTTP,
  121. ListenHTTPS: "",
  122. ListenUnix: "",
  123. ListenUnixMode: 0,
  124. KeyFile: "",
  125. CertFile: "",
  126. FirebaseKeyFile: "",
  127. CacheFile: "",
  128. CacheDuration: DefaultCacheDuration,
  129. CacheBatchSize: 0,
  130. CacheBatchTimeout: 0,
  131. AuthFile: "",
  132. AuthDefault: user.NewPermission(true, true),
  133. AttachmentCacheDir: "",
  134. AttachmentTotalSizeLimit: DefaultAttachmentTotalSizeLimit,
  135. AttachmentFileSizeLimit: DefaultAttachmentFileSizeLimit,
  136. AttachmentExpiryDuration: DefaultAttachmentExpiryDuration,
  137. KeepaliveInterval: DefaultKeepaliveInterval,
  138. ManagerInterval: DefaultManagerInterval,
  139. MessageLimit: DefaultMessageLengthLimit,
  140. MinDelay: DefaultMinDelay,
  141. MaxDelay: DefaultMaxDelay,
  142. DelayedSenderInterval: DefaultDelayedSenderInterval,
  143. FirebaseKeepaliveInterval: DefaultFirebaseKeepaliveInterval,
  144. FirebasePollInterval: DefaultFirebasePollInterval,
  145. FirebaseQuotaExceededPenaltyDuration: DefaultFirebaseQuotaExceededPenaltyDuration,
  146. TotalTopicLimit: DefaultTotalTopicLimit,
  147. VisitorSubscriptionLimit: DefaultVisitorSubscriptionLimit,
  148. VisitorAttachmentTotalSizeLimit: DefaultVisitorAttachmentTotalSizeLimit,
  149. VisitorAttachmentDailyBandwidthLimit: DefaultVisitorAttachmentDailyBandwidthLimit,
  150. VisitorRequestLimitBurst: DefaultVisitorRequestLimitBurst,
  151. VisitorRequestLimitReplenish: DefaultVisitorRequestLimitReplenish,
  152. VisitorRequestExemptIPAddrs: make([]netip.Prefix, 0),
  153. VisitorEmailLimitBurst: DefaultVisitorEmailLimitBurst,
  154. VisitorEmailLimitReplenish: DefaultVisitorEmailLimitReplenish,
  155. VisitorAccountCreateLimitBurst: DefaultVisitorAccountCreateLimitBurst,
  156. VisitorAccountCreateLimitReplenish: DefaultVisitorAccountCreateLimitReplenish,
  157. VisitorStatsResetTime: DefaultVisitorStatsResetTime,
  158. BehindProxy: false,
  159. EnableWeb: true,
  160. Version: "",
  161. }
  162. }