config.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 = 3 * 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. DefaultVisitorAuthFailureLimitBurst = 30
  49. DefaultVisitorAuthFailureLimitReplenish = time.Minute
  50. DefaultVisitorAttachmentTotalSizeLimit = 100 * 1024 * 1024 // 100 MB
  51. DefaultVisitorAttachmentDailyBandwidthLimit = 500 * 1024 * 1024 // 500 MB
  52. )
  53. var (
  54. // DefaultVisitorStatsResetTime defines the time at which visitor stats are reset (wall clock only)
  55. DefaultVisitorStatsResetTime = time.Date(0, 0, 0, 0, 0, 0, 0, time.UTC)
  56. // DefaultDisallowedTopics defines the topics that are forbidden, because they are used elsewhere. This array can be
  57. // extended using the server.yml config. If updated, also update in Android and web app.
  58. DefaultDisallowedTopics = []string{"docs", "static", "file", "app", "metrics", "account", "settings", "signup", "login", "v1"}
  59. )
  60. // Config is the main config struct for the application. Use New to instantiate a default config struct.
  61. type Config struct {
  62. File string // Config file, only used for testing
  63. BaseURL string
  64. ListenHTTP string
  65. ListenHTTPS string
  66. ListenUnix string
  67. ListenUnixMode fs.FileMode
  68. KeyFile string
  69. CertFile string
  70. FirebaseKeyFile string
  71. CacheFile string
  72. CacheDuration time.Duration
  73. CacheStartupQueries string
  74. CacheBatchSize int
  75. CacheBatchTimeout time.Duration
  76. AuthFile string
  77. AuthStartupQueries string
  78. AuthDefault user.Permission
  79. AuthBcryptCost int
  80. AuthStatsQueueWriterInterval time.Duration
  81. AttachmentCacheDir string
  82. AttachmentTotalSizeLimit int64
  83. AttachmentFileSizeLimit int64
  84. AttachmentExpiryDuration time.Duration
  85. KeepaliveInterval time.Duration
  86. ManagerInterval time.Duration
  87. DisallowedTopics []string
  88. WebRoot string // empty to disable
  89. DelayedSenderInterval time.Duration
  90. FirebaseKeepaliveInterval time.Duration
  91. FirebasePollInterval time.Duration
  92. FirebaseQuotaExceededPenaltyDuration time.Duration
  93. UpstreamBaseURL string
  94. UpstreamAccessToken string
  95. SMTPSenderAddr string
  96. SMTPSenderUser string
  97. SMTPSenderPass string
  98. SMTPSenderFrom string
  99. SMTPServerListen string
  100. SMTPServerDomain string
  101. SMTPServerAddrPrefix string
  102. TwilioAccount string
  103. TwilioAuthToken string
  104. TwilioPhoneNumber string
  105. TwilioCallsBaseURL string
  106. TwilioVerifyBaseURL string
  107. TwilioVerifyService string
  108. MetricsEnable bool
  109. MetricsListenHTTP string
  110. ProfileListenHTTP string
  111. MessageLimit int
  112. MinDelay time.Duration
  113. MaxDelay time.Duration
  114. TotalTopicLimit int
  115. TotalAttachmentSizeLimit int64
  116. VisitorSubscriptionLimit int
  117. VisitorAttachmentTotalSizeLimit int64
  118. VisitorAttachmentDailyBandwidthLimit int64
  119. VisitorRequestLimitBurst int
  120. VisitorRequestLimitReplenish time.Duration
  121. VisitorRequestExemptIPAddrs []netip.Prefix
  122. VisitorMessageDailyLimit int
  123. VisitorEmailLimitBurst int
  124. VisitorEmailLimitReplenish time.Duration
  125. VisitorAccountCreationLimitBurst int
  126. VisitorAccountCreationLimitReplenish time.Duration
  127. VisitorAuthFailureLimitBurst int
  128. VisitorAuthFailureLimitReplenish time.Duration
  129. VisitorStatsResetTime time.Time // Time of the day at which to reset visitor stats
  130. VisitorSubscriberRateLimiting bool // Enable subscriber-based rate limiting for UnifiedPush topics
  131. BehindProxy bool
  132. StripeSecretKey string
  133. StripeWebhookKey string
  134. StripePriceCacheDuration time.Duration
  135. BillingContact string
  136. EnableSignup bool // Enable creation of accounts via API and UI
  137. EnableLogin bool
  138. EnableReservations bool // Allow users with role "user" to own/reserve topics
  139. EnableMetrics bool
  140. AccessControlAllowOrigin string // CORS header field to restrict access from web clients
  141. Version string // injected by App
  142. }
  143. // NewConfig instantiates a default new server config
  144. func NewConfig() *Config {
  145. return &Config{
  146. File: "", // Only used for testing
  147. BaseURL: "",
  148. ListenHTTP: DefaultListenHTTP,
  149. ListenHTTPS: "",
  150. ListenUnix: "",
  151. ListenUnixMode: 0,
  152. KeyFile: "",
  153. CertFile: "",
  154. FirebaseKeyFile: "",
  155. CacheFile: "",
  156. CacheDuration: DefaultCacheDuration,
  157. CacheStartupQueries: "",
  158. CacheBatchSize: 0,
  159. CacheBatchTimeout: 0,
  160. AuthFile: "",
  161. AuthStartupQueries: "",
  162. AuthDefault: user.PermissionReadWrite,
  163. AuthBcryptCost: user.DefaultUserPasswordBcryptCost,
  164. AuthStatsQueueWriterInterval: user.DefaultUserStatsQueueWriterInterval,
  165. AttachmentCacheDir: "",
  166. AttachmentTotalSizeLimit: DefaultAttachmentTotalSizeLimit,
  167. AttachmentFileSizeLimit: DefaultAttachmentFileSizeLimit,
  168. AttachmentExpiryDuration: DefaultAttachmentExpiryDuration,
  169. KeepaliveInterval: DefaultKeepaliveInterval,
  170. ManagerInterval: DefaultManagerInterval,
  171. DisallowedTopics: DefaultDisallowedTopics,
  172. WebRoot: "/",
  173. DelayedSenderInterval: DefaultDelayedSenderInterval,
  174. FirebaseKeepaliveInterval: DefaultFirebaseKeepaliveInterval,
  175. FirebasePollInterval: DefaultFirebasePollInterval,
  176. FirebaseQuotaExceededPenaltyDuration: DefaultFirebaseQuotaExceededPenaltyDuration,
  177. UpstreamBaseURL: "",
  178. UpstreamAccessToken: "",
  179. SMTPSenderAddr: "",
  180. SMTPSenderUser: "",
  181. SMTPSenderPass: "",
  182. SMTPSenderFrom: "",
  183. SMTPServerListen: "",
  184. SMTPServerDomain: "",
  185. SMTPServerAddrPrefix: "",
  186. TwilioCallsBaseURL: "https://api.twilio.com", // Override for tests
  187. TwilioAccount: "",
  188. TwilioAuthToken: "",
  189. TwilioPhoneNumber: "",
  190. TwilioVerifyBaseURL: "https://verify.twilio.com", // Override for tests
  191. TwilioVerifyService: "",
  192. MessageLimit: DefaultMessageLengthLimit,
  193. MinDelay: DefaultMinDelay,
  194. MaxDelay: DefaultMaxDelay,
  195. TotalTopicLimit: DefaultTotalTopicLimit,
  196. TotalAttachmentSizeLimit: 0,
  197. VisitorSubscriptionLimit: DefaultVisitorSubscriptionLimit,
  198. VisitorAttachmentTotalSizeLimit: DefaultVisitorAttachmentTotalSizeLimit,
  199. VisitorAttachmentDailyBandwidthLimit: DefaultVisitorAttachmentDailyBandwidthLimit,
  200. VisitorRequestLimitBurst: DefaultVisitorRequestLimitBurst,
  201. VisitorRequestLimitReplenish: DefaultVisitorRequestLimitReplenish,
  202. VisitorRequestExemptIPAddrs: make([]netip.Prefix, 0),
  203. VisitorMessageDailyLimit: DefaultVisitorMessageDailyLimit,
  204. VisitorEmailLimitBurst: DefaultVisitorEmailLimitBurst,
  205. VisitorEmailLimitReplenish: DefaultVisitorEmailLimitReplenish,
  206. VisitorAccountCreationLimitBurst: DefaultVisitorAccountCreationLimitBurst,
  207. VisitorAccountCreationLimitReplenish: DefaultVisitorAccountCreationLimitReplenish,
  208. VisitorAuthFailureLimitBurst: DefaultVisitorAuthFailureLimitBurst,
  209. VisitorAuthFailureLimitReplenish: DefaultVisitorAuthFailureLimitReplenish,
  210. VisitorStatsResetTime: DefaultVisitorStatsResetTime,
  211. VisitorSubscriberRateLimiting: false,
  212. BehindProxy: false,
  213. StripeSecretKey: "",
  214. StripeWebhookKey: "",
  215. StripePriceCacheDuration: DefaultStripePriceCacheDuration,
  216. BillingContact: "",
  217. EnableSignup: false,
  218. EnableLogin: false,
  219. EnableReservations: false,
  220. AccessControlAllowOrigin: "*",
  221. Version: "",
  222. }
  223. }