config.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. DefaultVisitorEmailLimitBurst = 16
  44. DefaultVisitorEmailLimitReplenish = time.Hour
  45. DefaultVisitorAccountCreateLimitBurst = 3
  46. DefaultVisitorAccountCreateLimitReplenish = 24 * time.Hour
  47. DefaultVisitorAttachmentTotalSizeLimit = 100 * 1024 * 1024 // 100 MB
  48. DefaultVisitorAttachmentDailyBandwidthLimit = 500 * 1024 * 1024 // 500 MB
  49. )
  50. var (
  51. // DefaultVisitorStatsResetTime defines the time at which visitor stats are reset (wall clock only)
  52. DefaultVisitorStatsResetTime = time.Date(0, 0, 0, 0, 0, 0, 0, time.UTC)
  53. )
  54. // Config is the main config struct for the application. Use New to instantiate a default config struct.
  55. type Config struct {
  56. BaseURL string
  57. ListenHTTP string
  58. ListenHTTPS string
  59. ListenUnix string
  60. ListenUnixMode fs.FileMode
  61. KeyFile string
  62. CertFile string
  63. FirebaseKeyFile string
  64. CacheFile string
  65. CacheDuration time.Duration
  66. CacheStartupQueries string
  67. CacheBatchSize int
  68. CacheBatchTimeout time.Duration
  69. AuthFile string
  70. AuthStartupQueries string
  71. AuthDefault user.Permission
  72. AttachmentCacheDir string
  73. AttachmentTotalSizeLimit int64
  74. AttachmentFileSizeLimit int64
  75. AttachmentExpiryDuration time.Duration
  76. KeepaliveInterval time.Duration
  77. ManagerInterval time.Duration
  78. WebRootIsApp bool
  79. DelayedSenderInterval time.Duration
  80. FirebaseKeepaliveInterval time.Duration
  81. FirebasePollInterval time.Duration
  82. FirebaseQuotaExceededPenaltyDuration time.Duration
  83. UpstreamBaseURL string
  84. SMTPSenderAddr string
  85. SMTPSenderUser string
  86. SMTPSenderPass string
  87. SMTPSenderFrom string
  88. SMTPServerListen string
  89. SMTPServerDomain string
  90. SMTPServerAddrPrefix string
  91. MessageLimit int
  92. MinDelay time.Duration
  93. MaxDelay time.Duration
  94. TotalTopicLimit int
  95. TotalAttachmentSizeLimit int64
  96. VisitorSubscriptionLimit int
  97. VisitorAttachmentTotalSizeLimit int64
  98. VisitorAttachmentDailyBandwidthLimit int
  99. VisitorRequestLimitBurst int
  100. VisitorRequestLimitReplenish time.Duration
  101. VisitorRequestExemptIPAddrs []netip.Prefix
  102. VisitorEmailLimitBurst int
  103. VisitorEmailLimitReplenish time.Duration
  104. VisitorAccountCreateLimitBurst int
  105. VisitorAccountCreateLimitReplenish time.Duration
  106. VisitorStatsResetTime time.Time // Time of the day at which to reset visitor stats
  107. BehindProxy bool
  108. StripeSecretKey string
  109. StripeWebhookKey string
  110. StripePriceCacheDuration time.Duration
  111. EnableWeb bool
  112. EnableSignup bool // Enable creation of accounts via API and UI
  113. EnableLogin bool
  114. EnableReservations bool // Allow users with role "user" to own/reserve topics
  115. AccessControlAllowOrigin string // CORS header field to restrict access from web clients
  116. Version string // injected by App
  117. }
  118. // NewConfig instantiates a default new server config
  119. func NewConfig() *Config {
  120. return &Config{
  121. BaseURL: "",
  122. ListenHTTP: DefaultListenHTTP,
  123. ListenHTTPS: "",
  124. ListenUnix: "",
  125. ListenUnixMode: 0,
  126. KeyFile: "",
  127. CertFile: "",
  128. FirebaseKeyFile: "",
  129. CacheFile: "",
  130. CacheDuration: DefaultCacheDuration,
  131. CacheStartupQueries: "",
  132. CacheBatchSize: 0,
  133. CacheBatchTimeout: 0,
  134. AuthFile: "",
  135. AuthStartupQueries: "",
  136. AuthDefault: user.NewPermission(true, true),
  137. AttachmentCacheDir: "",
  138. AttachmentTotalSizeLimit: DefaultAttachmentTotalSizeLimit,
  139. AttachmentFileSizeLimit: DefaultAttachmentFileSizeLimit,
  140. AttachmentExpiryDuration: DefaultAttachmentExpiryDuration,
  141. KeepaliveInterval: DefaultKeepaliveInterval,
  142. ManagerInterval: DefaultManagerInterval,
  143. WebRootIsApp: false,
  144. DelayedSenderInterval: DefaultDelayedSenderInterval,
  145. FirebaseKeepaliveInterval: DefaultFirebaseKeepaliveInterval,
  146. FirebasePollInterval: DefaultFirebasePollInterval,
  147. FirebaseQuotaExceededPenaltyDuration: DefaultFirebaseQuotaExceededPenaltyDuration,
  148. UpstreamBaseURL: "",
  149. SMTPSenderAddr: "",
  150. SMTPSenderUser: "",
  151. SMTPSenderPass: "",
  152. SMTPSenderFrom: "",
  153. SMTPServerListen: "",
  154. SMTPServerDomain: "",
  155. SMTPServerAddrPrefix: "",
  156. MessageLimit: DefaultMessageLengthLimit,
  157. MinDelay: DefaultMinDelay,
  158. MaxDelay: DefaultMaxDelay,
  159. TotalTopicLimit: DefaultTotalTopicLimit,
  160. TotalAttachmentSizeLimit: 0,
  161. VisitorSubscriptionLimit: DefaultVisitorSubscriptionLimit,
  162. VisitorAttachmentTotalSizeLimit: DefaultVisitorAttachmentTotalSizeLimit,
  163. VisitorAttachmentDailyBandwidthLimit: DefaultVisitorAttachmentDailyBandwidthLimit,
  164. VisitorRequestLimitBurst: DefaultVisitorRequestLimitBurst,
  165. VisitorRequestLimitReplenish: DefaultVisitorRequestLimitReplenish,
  166. VisitorRequestExemptIPAddrs: make([]netip.Prefix, 0),
  167. VisitorEmailLimitBurst: DefaultVisitorEmailLimitBurst,
  168. VisitorEmailLimitReplenish: DefaultVisitorEmailLimitReplenish,
  169. VisitorAccountCreateLimitBurst: DefaultVisitorAccountCreateLimitBurst,
  170. VisitorAccountCreateLimitReplenish: DefaultVisitorAccountCreateLimitReplenish,
  171. VisitorStatsResetTime: DefaultVisitorStatsResetTime,
  172. BehindProxy: false,
  173. StripeSecretKey: "",
  174. StripeWebhookKey: "",
  175. StripePriceCacheDuration: DefaultStripePriceCacheDuration,
  176. EnableWeb: true,
  177. EnableSignup: false,
  178. EnableLogin: false,
  179. EnableReservations: false,
  180. AccessControlAllowOrigin: "*",
  181. Version: "",
  182. }
  183. }