config.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. DefaultStripePriceCacheDuration = time.Hour // Time to keep Stripe prices cached in memory before a refresh is needed
  21. )
  22. // Defines all global and per-visitor limits
  23. // - message size limit: the max number of bytes for a message
  24. // - total topic limit: max number of topics overall
  25. // - various attachment limits
  26. const (
  27. DefaultMessageLengthLimit = 4096 // Bytes
  28. DefaultTotalTopicLimit = 15000
  29. DefaultAttachmentTotalSizeLimit = int64(5 * 1024 * 1024 * 1024) // 5 GB
  30. DefaultAttachmentFileSizeLimit = int64(15 * 1024 * 1024) // 15 MB
  31. DefaultAttachmentExpiryDuration = 3 * time.Hour
  32. )
  33. // Defines all per-visitor limits
  34. // - per visitor subscription limit: max number of subscriptions (active HTTP connections) per per-visitor/IP
  35. // - per visitor request limit: max number of PUT/GET/.. requests (here: 60 requests bucket, replenished at a rate of one per 5 seconds)
  36. // - per visitor email limit: max number of emails (here: 16 email bucket, replenished at a rate of one per hour)
  37. // - per visitor attachment size limit: total per-visitor attachment size in bytes to be stored on the server
  38. // - per visitor attachment daily bandwidth limit: number of bytes that can be transferred to/from the server
  39. const (
  40. DefaultVisitorSubscriptionLimit = 30
  41. DefaultVisitorRequestLimitBurst = 60
  42. DefaultVisitorRequestLimitReplenish = 5 * time.Second
  43. DefaultVisitorMessageDailyLimit = 0
  44. DefaultVisitorEmailLimitBurst = 16
  45. DefaultVisitorEmailLimitReplenish = time.Hour
  46. DefaultVisitorAccountCreationLimitBurst = 3
  47. DefaultVisitorAccountCreationLimitReplenish = 24 * time.Hour
  48. DefaultVisitorAttachmentTotalSizeLimit = 100 * 1024 * 1024 // 100 MB
  49. DefaultVisitorAttachmentDailyBandwidthLimit = 500 * 1024 * 1024 // 500 MB
  50. )
  51. var (
  52. // DefaultVisitorStatsResetTime defines the time at which visitor stats are reset (wall clock only)
  53. DefaultVisitorStatsResetTime = time.Date(0, 0, 0, 0, 0, 0, 0, time.UTC)
  54. )
  55. // Config is the main config struct for the application. Use New to instantiate a default config struct.
  56. type Config struct {
  57. BaseURL string
  58. ListenHTTP string
  59. ListenHTTPS string
  60. ListenUnix string
  61. ListenUnixMode fs.FileMode
  62. KeyFile string
  63. CertFile string
  64. FirebaseKeyFile string
  65. CacheFile string
  66. CacheDuration time.Duration
  67. CacheStartupQueries string
  68. CacheBatchSize int
  69. CacheBatchTimeout time.Duration
  70. AuthFile string
  71. AuthStartupQueries string
  72. AuthDefault user.Permission
  73. AuthBcryptCost int
  74. AttachmentCacheDir string
  75. AttachmentTotalSizeLimit int64
  76. AttachmentFileSizeLimit int64
  77. AttachmentExpiryDuration time.Duration
  78. KeepaliveInterval time.Duration
  79. ManagerInterval time.Duration
  80. WebRootIsApp bool
  81. DelayedSenderInterval time.Duration
  82. FirebaseKeepaliveInterval time.Duration
  83. FirebasePollInterval time.Duration
  84. FirebaseQuotaExceededPenaltyDuration time.Duration
  85. UpstreamBaseURL string
  86. SMTPSenderAddr string
  87. SMTPSenderUser string
  88. SMTPSenderPass string
  89. SMTPSenderFrom string
  90. SMTPServerListen string
  91. SMTPServerDomain string
  92. SMTPServerAddrPrefix string
  93. MessageLimit int
  94. MinDelay time.Duration
  95. MaxDelay time.Duration
  96. TotalTopicLimit int
  97. TotalAttachmentSizeLimit int64
  98. VisitorSubscriptionLimit int
  99. VisitorAttachmentTotalSizeLimit int64
  100. VisitorAttachmentDailyBandwidthLimit int64
  101. VisitorRequestLimitBurst int
  102. VisitorRequestLimitReplenish time.Duration
  103. VisitorRequestExemptIPAddrs []netip.Prefix
  104. VisitorMessageDailyLimit int
  105. VisitorEmailLimitBurst int
  106. VisitorEmailLimitReplenish time.Duration
  107. VisitorAccountCreationLimitBurst int
  108. VisitorAccountCreationLimitReplenish time.Duration
  109. VisitorStatsResetTime time.Time // Time of the day at which to reset visitor stats
  110. BehindProxy bool
  111. StripeSecretKey string
  112. StripeWebhookKey string
  113. StripePriceCacheDuration time.Duration
  114. EnableWeb bool
  115. EnableSignup bool // Enable creation of accounts via API and UI
  116. EnableLogin bool
  117. EnableReservations bool // Allow users with role "user" to own/reserve topics
  118. AccessControlAllowOrigin string // CORS header field to restrict access from web clients
  119. Version string // injected by App
  120. }
  121. // NewConfig instantiates a default new server config
  122. func NewConfig() *Config {
  123. return &Config{
  124. BaseURL: "",
  125. ListenHTTP: DefaultListenHTTP,
  126. ListenHTTPS: "",
  127. ListenUnix: "",
  128. ListenUnixMode: 0,
  129. KeyFile: "",
  130. CertFile: "",
  131. FirebaseKeyFile: "",
  132. CacheFile: "",
  133. CacheDuration: DefaultCacheDuration,
  134. CacheStartupQueries: "",
  135. CacheBatchSize: 0,
  136. CacheBatchTimeout: 0,
  137. AuthFile: "",
  138. AuthStartupQueries: "",
  139. AuthDefault: user.NewPermission(true, true),
  140. AuthBcryptCost: user.DefaultUserPasswordBcryptCost,
  141. AttachmentCacheDir: "",
  142. AttachmentTotalSizeLimit: DefaultAttachmentTotalSizeLimit,
  143. AttachmentFileSizeLimit: DefaultAttachmentFileSizeLimit,
  144. AttachmentExpiryDuration: DefaultAttachmentExpiryDuration,
  145. KeepaliveInterval: DefaultKeepaliveInterval,
  146. ManagerInterval: DefaultManagerInterval,
  147. WebRootIsApp: false,
  148. DelayedSenderInterval: DefaultDelayedSenderInterval,
  149. FirebaseKeepaliveInterval: DefaultFirebaseKeepaliveInterval,
  150. FirebasePollInterval: DefaultFirebasePollInterval,
  151. FirebaseQuotaExceededPenaltyDuration: DefaultFirebaseQuotaExceededPenaltyDuration,
  152. UpstreamBaseURL: "",
  153. SMTPSenderAddr: "",
  154. SMTPSenderUser: "",
  155. SMTPSenderPass: "",
  156. SMTPSenderFrom: "",
  157. SMTPServerListen: "",
  158. SMTPServerDomain: "",
  159. SMTPServerAddrPrefix: "",
  160. MessageLimit: DefaultMessageLengthLimit,
  161. MinDelay: DefaultMinDelay,
  162. MaxDelay: DefaultMaxDelay,
  163. TotalTopicLimit: DefaultTotalTopicLimit,
  164. TotalAttachmentSizeLimit: 0,
  165. VisitorSubscriptionLimit: DefaultVisitorSubscriptionLimit,
  166. VisitorAttachmentTotalSizeLimit: DefaultVisitorAttachmentTotalSizeLimit,
  167. VisitorAttachmentDailyBandwidthLimit: DefaultVisitorAttachmentDailyBandwidthLimit,
  168. VisitorRequestLimitBurst: DefaultVisitorRequestLimitBurst,
  169. VisitorRequestLimitReplenish: DefaultVisitorRequestLimitReplenish,
  170. VisitorRequestExemptIPAddrs: make([]netip.Prefix, 0),
  171. VisitorMessageDailyLimit: DefaultVisitorMessageDailyLimit,
  172. VisitorEmailLimitBurst: DefaultVisitorEmailLimitBurst,
  173. VisitorEmailLimitReplenish: DefaultVisitorEmailLimitReplenish,
  174. VisitorAccountCreationLimitBurst: DefaultVisitorAccountCreationLimitBurst,
  175. VisitorAccountCreationLimitReplenish: DefaultVisitorAccountCreationLimitReplenish,
  176. VisitorStatsResetTime: DefaultVisitorStatsResetTime,
  177. BehindProxy: false,
  178. StripeSecretKey: "",
  179. StripeWebhookKey: "",
  180. StripePriceCacheDuration: DefaultStripePriceCacheDuration,
  181. EnableWeb: true,
  182. EnableSignup: false,
  183. EnableLogin: false,
  184. EnableReservations: false,
  185. AccessControlAllowOrigin: "*",
  186. Version: "",
  187. }
  188. }