config.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. package server
  2. import (
  3. "io/fs"
  4. "net/netip"
  5. "time"
  6. "heckel.io/ntfy/v2/user"
  7. )
  8. // Defines default config settings (excluding limits, see below)
  9. const (
  10. DefaultListenHTTP = ":80"
  11. DefaultCacheDuration = 12 * time.Hour
  12. DefaultCacheBatchTimeout = time.Duration(0)
  13. DefaultKeepaliveInterval = 45 * time.Second // Not too frequently to save battery (Android read timeout used to be 77s!)
  14. DefaultManagerInterval = time.Minute
  15. DefaultDelayedSenderInterval = 10 * time.Second
  16. DefaultMessageDelayMin = 10 * time.Second
  17. DefaultMessageDelayMax = 3 * 24 * time.Hour
  18. DefaultFirebaseKeepaliveInterval = 3 * time.Hour // ~control topic (Android), not too frequently to save battery
  19. DefaultFirebasePollInterval = 20 * time.Minute // ~poll topic (iOS), max. 2-3 times per hour (see docs)
  20. DefaultFirebaseQuotaExceededPenaltyDuration = 10 * time.Minute // Time that over-users are locked out of Firebase if it returns "quota exceeded"
  21. DefaultStripePriceCacheDuration = 3 * time.Hour // Time to keep Stripe prices cached in memory before a refresh is needed
  22. )
  23. // Defines default Web Push settings
  24. const (
  25. DefaultWebPushExpiryWarningDuration = 7 * 24 * time.Hour
  26. DefaultWebPushExpiryDuration = 9 * 24 * time.Hour
  27. )
  28. // Defines all global and per-visitor limits
  29. // - message size limit: the max number of bytes for a message
  30. // - total topic limit: max number of topics overall
  31. // - various attachment limits
  32. const (
  33. DefaultMessageSizeLimit = 4096 // Bytes; note that FCM/APNS have a limit of ~4 KB for the entire message
  34. DefaultTotalTopicLimit = 15000
  35. DefaultAttachmentTotalSizeLimit = int64(5 * 1024 * 1024 * 1024) // 5 GB
  36. DefaultAttachmentFileSizeLimit = int64(15 * 1024 * 1024) // 15 MB
  37. DefaultAttachmentExpiryDuration = 3 * time.Hour
  38. )
  39. // Defines all per-visitor limits
  40. // - per visitor subscription limit: max number of subscriptions (active HTTP connections) per per-visitor/IP
  41. // - per visitor request limit: max number of PUT/GET/.. requests (here: 60 requests bucket, replenished at a rate of one per 5 seconds)
  42. // - per visitor email limit: max number of emails (here: 16 email bucket, replenished at a rate of one per hour)
  43. // - per visitor attachment size limit: total per-visitor attachment size in bytes to be stored on the server
  44. // - per visitor attachment daily bandwidth limit: number of bytes that can be transferred to/from the server
  45. const (
  46. DefaultVisitorSubscriptionLimit = 30
  47. DefaultVisitorRequestLimitBurst = 60
  48. DefaultVisitorRequestLimitReplenish = 5 * time.Second
  49. DefaultVisitorMessageDailyLimit = 0
  50. DefaultVisitorEmailLimitBurst = 16
  51. DefaultVisitorEmailLimitReplenish = time.Hour
  52. DefaultVisitorAccountCreationLimitBurst = 3
  53. DefaultVisitorAccountCreationLimitReplenish = 24 * time.Hour
  54. DefaultVisitorAuthFailureLimitBurst = 30
  55. DefaultVisitorAuthFailureLimitReplenish = time.Minute
  56. DefaultVisitorAttachmentTotalSizeLimit = 100 * 1024 * 1024 // 100 MB
  57. DefaultVisitorAttachmentDailyBandwidthLimit = 500 * 1024 * 1024 // 500 MB
  58. )
  59. var (
  60. // DefaultVisitorStatsResetTime defines the time at which visitor stats are reset (wall clock only)
  61. DefaultVisitorStatsResetTime = time.Date(0, 0, 0, 0, 0, 0, 0, time.UTC)
  62. // DefaultDisallowedTopics defines the topics that are forbidden, because they are used elsewhere. This array can be
  63. // extended using the server.yml config. If updated, also update in Android and web app.
  64. DefaultDisallowedTopics = []string{"docs", "static", "file", "app", "metrics", "account", "settings", "signup", "login", "v1"}
  65. )
  66. // Config is the main config struct for the application. Use New to instantiate a default config struct.
  67. type Config struct {
  68. File string // Config file, only used for testing
  69. BaseURL string
  70. ListenHTTP string
  71. ListenHTTPS string
  72. ListenUnix string
  73. ListenUnixMode fs.FileMode
  74. KeyFile string
  75. CertFile string
  76. FirebaseKeyFile string
  77. CacheFile string
  78. CacheDuration time.Duration
  79. CacheStartupQueries string
  80. CacheBatchSize int
  81. CacheBatchTimeout time.Duration
  82. AuthFile string
  83. AuthStartupQueries string
  84. AuthDefault user.Permission
  85. AuthBcryptCost int
  86. AuthStatsQueueWriterInterval time.Duration
  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. MessageDelayMin time.Duration
  118. MessageDelayMax time.Duration
  119. MessageSizeLimit int
  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. MessageSizeLimit: DefaultMessageSizeLimit,
  206. MessageDelayMin: DefaultMessageDelayMin,
  207. MessageDelayMax: DefaultMessageDelayMax,
  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. }
  242. }