config.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. package server
  2. import (
  3. "io/fs"
  4. "net/netip"
  5. "time"
  6. "heckel.io/ntfy/user"
  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 default Web Push settings
  23. const (
  24. DefaultWebPushExpiryWarningDuration = 7 * 24 * time.Hour
  25. DefaultWebPushExpiryDuration = 9 * 24 * time.Hour
  26. )
  27. // Defines all global and per-visitor limits
  28. // - message size limit: the max number of bytes for a message
  29. // - total topic limit: max number of topics overall
  30. // - various attachment limits
  31. const (
  32. DefaultMessageLengthLimit = 4096 // Bytes
  33. DefaultTotalTopicLimit = 15000
  34. DefaultAttachmentTotalSizeLimit = int64(5 * 1024 * 1024 * 1024) // 5 GB
  35. DefaultAttachmentFileSizeLimit = int64(15 * 1024 * 1024) // 15 MB
  36. DefaultAttachmentExpiryDuration = 3 * time.Hour
  37. )
  38. // Defines all per-visitor limits
  39. // - per visitor subscription limit: max number of subscriptions (active HTTP connections) per per-visitor/IP
  40. // - per visitor request limit: max number of PUT/GET/.. requests (here: 60 requests bucket, replenished at a rate of one per 5 seconds)
  41. // - per visitor email limit: max number of emails (here: 16 email bucket, replenished at a rate of one per hour)
  42. // - per visitor attachment size limit: total per-visitor attachment size in bytes to be stored on the server
  43. // - per visitor attachment daily bandwidth limit: number of bytes that can be transferred to/from the server
  44. const (
  45. DefaultVisitorSubscriptionLimit = 30
  46. DefaultVisitorRequestLimitBurst = 60
  47. DefaultVisitorRequestLimitReplenish = 5 * time.Second
  48. DefaultVisitorMessageDailyLimit = 0
  49. DefaultVisitorEmailLimitBurst = 16
  50. DefaultVisitorEmailLimitReplenish = time.Hour
  51. DefaultVisitorAccountCreationLimitBurst = 3
  52. DefaultVisitorAccountCreationLimitReplenish = 24 * time.Hour
  53. DefaultVisitorAuthFailureLimitBurst = 30
  54. DefaultVisitorAuthFailureLimitReplenish = time.Minute
  55. DefaultVisitorAttachmentTotalSizeLimit = 100 * 1024 * 1024 // 100 MB
  56. DefaultVisitorAttachmentDailyBandwidthLimit = 500 * 1024 * 1024 // 500 MB
  57. )
  58. var (
  59. // DefaultVisitorStatsResetTime defines the time at which visitor stats are reset (wall clock only)
  60. DefaultVisitorStatsResetTime = time.Date(0, 0, 0, 0, 0, 0, 0, time.UTC)
  61. // DefaultDisallowedTopics defines the topics that are forbidden, because they are used elsewhere. This array can be
  62. // extended using the server.yml config. If updated, also update in Android and web app.
  63. DefaultDisallowedTopics = []string{"docs", "static", "file", "app", "metrics", "account", "settings", "signup", "login", "v1"}
  64. )
  65. // Config is the main config struct for the application. Use New to instantiate a default config struct.
  66. type Config struct {
  67. File string // Config file, only used for testing
  68. BaseURL string
  69. ListenHTTP string
  70. ListenHTTPS string
  71. ListenUnix string
  72. ListenUnixMode fs.FileMode
  73. KeyFile string
  74. CertFile string
  75. FirebaseKeyFile string
  76. CacheFile string
  77. CacheDuration time.Duration
  78. CacheStartupQueries string
  79. CacheBatchSize int
  80. CacheBatchTimeout time.Duration
  81. AuthFile string
  82. AuthStartupQueries string
  83. AuthDefault user.Permission
  84. AuthBcryptCost int
  85. AuthStatsQueueWriterInterval time.Duration
  86. AuthUserHeader string
  87. AttachmentCacheDir string
  88. AttachmentTotalSizeLimit int64
  89. AttachmentFileSizeLimit int64
  90. AttachmentExpiryDuration time.Duration
  91. KeepaliveInterval time.Duration
  92. ManagerInterval time.Duration
  93. DisallowedTopics []string
  94. WebRoot string // empty to disable
  95. DelayedSenderInterval time.Duration
  96. FirebaseKeepaliveInterval time.Duration
  97. FirebasePollInterval time.Duration
  98. FirebaseQuotaExceededPenaltyDuration time.Duration
  99. UpstreamBaseURL string
  100. UpstreamAccessToken string
  101. SMTPSenderAddr string
  102. SMTPSenderUser string
  103. SMTPSenderPass string
  104. SMTPSenderFrom string
  105. SMTPServerListen string
  106. SMTPServerDomain string
  107. SMTPServerAddrPrefix string
  108. TwilioAccount string
  109. TwilioAuthToken string
  110. TwilioPhoneNumber string
  111. TwilioCallsBaseURL string
  112. TwilioVerifyBaseURL string
  113. TwilioVerifyService string
  114. MetricsEnable bool
  115. MetricsListenHTTP string
  116. ProfileListenHTTP string
  117. MessageLimit int
  118. MinDelay time.Duration
  119. MaxDelay time.Duration
  120. TotalTopicLimit int
  121. TotalAttachmentSizeLimit int64
  122. VisitorSubscriptionLimit int
  123. VisitorAttachmentTotalSizeLimit int64
  124. VisitorAttachmentDailyBandwidthLimit int64
  125. VisitorRequestLimitBurst int
  126. VisitorRequestLimitReplenish time.Duration
  127. VisitorRequestExemptIPAddrs []netip.Prefix
  128. VisitorMessageDailyLimit int
  129. VisitorEmailLimitBurst int
  130. VisitorEmailLimitReplenish time.Duration
  131. VisitorAccountCreationLimitBurst int
  132. VisitorAccountCreationLimitReplenish time.Duration
  133. VisitorAuthFailureLimitBurst int
  134. VisitorAuthFailureLimitReplenish time.Duration
  135. VisitorStatsResetTime time.Time // Time of the day at which to reset visitor stats
  136. VisitorSubscriberRateLimiting bool // Enable subscriber-based rate limiting for UnifiedPush topics
  137. BehindProxy bool
  138. StripeSecretKey string
  139. StripeWebhookKey string
  140. StripePriceCacheDuration time.Duration
  141. BillingContact string
  142. EnableSignup bool // Enable creation of accounts via API and UI
  143. EnableLogin bool
  144. EnableReservations bool // Allow users with role "user" to own/reserve topics
  145. EnableMetrics bool
  146. AccessControlAllowOrigin string // CORS header field to restrict access from web clients
  147. Version string // injected by App
  148. WebPushPrivateKey string
  149. WebPushPublicKey string
  150. WebPushFile string
  151. WebPushEmailAddress string
  152. WebPushStartupQueries string
  153. WebPushExpiryDuration time.Duration
  154. WebPushExpiryWarningDuration time.Duration
  155. }
  156. // NewConfig instantiates a default new server config
  157. func NewConfig() *Config {
  158. return &Config{
  159. File: "", // Only used for testing
  160. BaseURL: "",
  161. ListenHTTP: DefaultListenHTTP,
  162. ListenHTTPS: "",
  163. ListenUnix: "",
  164. ListenUnixMode: 0,
  165. KeyFile: "",
  166. CertFile: "",
  167. FirebaseKeyFile: "",
  168. CacheFile: "",
  169. CacheDuration: DefaultCacheDuration,
  170. CacheStartupQueries: "",
  171. CacheBatchSize: 0,
  172. CacheBatchTimeout: 0,
  173. AuthFile: "",
  174. AuthStartupQueries: "",
  175. AuthDefault: user.PermissionReadWrite,
  176. AuthBcryptCost: user.DefaultUserPasswordBcryptCost,
  177. AuthStatsQueueWriterInterval: user.DefaultUserStatsQueueWriterInterval,
  178. AttachmentCacheDir: "",
  179. AttachmentTotalSizeLimit: DefaultAttachmentTotalSizeLimit,
  180. AttachmentFileSizeLimit: DefaultAttachmentFileSizeLimit,
  181. AttachmentExpiryDuration: DefaultAttachmentExpiryDuration,
  182. KeepaliveInterval: DefaultKeepaliveInterval,
  183. ManagerInterval: DefaultManagerInterval,
  184. DisallowedTopics: DefaultDisallowedTopics,
  185. WebRoot: "/",
  186. DelayedSenderInterval: DefaultDelayedSenderInterval,
  187. FirebaseKeepaliveInterval: DefaultFirebaseKeepaliveInterval,
  188. FirebasePollInterval: DefaultFirebasePollInterval,
  189. FirebaseQuotaExceededPenaltyDuration: DefaultFirebaseQuotaExceededPenaltyDuration,
  190. UpstreamBaseURL: "",
  191. UpstreamAccessToken: "",
  192. SMTPSenderAddr: "",
  193. SMTPSenderUser: "",
  194. SMTPSenderPass: "",
  195. SMTPSenderFrom: "",
  196. SMTPServerListen: "",
  197. SMTPServerDomain: "",
  198. SMTPServerAddrPrefix: "",
  199. TwilioCallsBaseURL: "https://api.twilio.com", // Override for tests
  200. TwilioAccount: "",
  201. TwilioAuthToken: "",
  202. TwilioPhoneNumber: "",
  203. TwilioVerifyBaseURL: "https://verify.twilio.com", // Override for tests
  204. TwilioVerifyService: "",
  205. MessageLimit: DefaultMessageLengthLimit,
  206. MinDelay: DefaultMinDelay,
  207. MaxDelay: DefaultMaxDelay,
  208. TotalTopicLimit: DefaultTotalTopicLimit,
  209. TotalAttachmentSizeLimit: 0,
  210. VisitorSubscriptionLimit: DefaultVisitorSubscriptionLimit,
  211. VisitorAttachmentTotalSizeLimit: DefaultVisitorAttachmentTotalSizeLimit,
  212. VisitorAttachmentDailyBandwidthLimit: DefaultVisitorAttachmentDailyBandwidthLimit,
  213. VisitorRequestLimitBurst: DefaultVisitorRequestLimitBurst,
  214. VisitorRequestLimitReplenish: DefaultVisitorRequestLimitReplenish,
  215. VisitorRequestExemptIPAddrs: make([]netip.Prefix, 0),
  216. VisitorMessageDailyLimit: DefaultVisitorMessageDailyLimit,
  217. VisitorEmailLimitBurst: DefaultVisitorEmailLimitBurst,
  218. VisitorEmailLimitReplenish: DefaultVisitorEmailLimitReplenish,
  219. VisitorAccountCreationLimitBurst: DefaultVisitorAccountCreationLimitBurst,
  220. VisitorAccountCreationLimitReplenish: DefaultVisitorAccountCreationLimitReplenish,
  221. VisitorAuthFailureLimitBurst: DefaultVisitorAuthFailureLimitBurst,
  222. VisitorAuthFailureLimitReplenish: DefaultVisitorAuthFailureLimitReplenish,
  223. VisitorStatsResetTime: DefaultVisitorStatsResetTime,
  224. VisitorSubscriberRateLimiting: false,
  225. BehindProxy: false,
  226. StripeSecretKey: "",
  227. StripeWebhookKey: "",
  228. StripePriceCacheDuration: DefaultStripePriceCacheDuration,
  229. BillingContact: "",
  230. EnableSignup: false,
  231. EnableLogin: false,
  232. EnableReservations: false,
  233. AccessControlAllowOrigin: "*",
  234. Version: "",
  235. WebPushPrivateKey: "",
  236. WebPushPublicKey: "",
  237. WebPushFile: "",
  238. WebPushEmailAddress: "",
  239. WebPushExpiryDuration: DefaultWebPushExpiryDuration,
  240. WebPushExpiryWarningDuration: DefaultWebPushExpiryWarningDuration,
  241. AuthUserHeader: "",
  242. }
  243. }