settings.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. package main
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "regexp"
  6. "strings"
  7. "syscall"
  8. "time"
  9. "github.com/gofrs/uuid"
  10. "github.com/jmoiron/sqlx/types"
  11. "github.com/labstack/echo"
  12. )
  13. type settings struct {
  14. AppRootURL string `json:"app.root_url"`
  15. AppLogoURL string `json:"app.logo_url"`
  16. AppFaviconURL string `json:"app.favicon_url"`
  17. AppFromEmail string `json:"app.from_email"`
  18. AppNotifyEmails []string `json:"app.notify_emails"`
  19. EnablePublicSubPage bool `json:"app.enable_public_subscription_page"`
  20. SendOptinConfirmation bool `json:"app.send_optin_confirmation"`
  21. CheckUpdates bool `json:"app.check_updates"`
  22. AppLang string `json:"app.lang"`
  23. AppBatchSize int `json:"app.batch_size"`
  24. AppConcurrency int `json:"app.concurrency"`
  25. AppMaxSendErrors int `json:"app.max_send_errors"`
  26. AppMessageRate int `json:"app.message_rate"`
  27. AppMessageSlidingWindow bool `json:"app.message_sliding_window"`
  28. AppMessageSlidingWindowDuration string `json:"app.message_sliding_window_duration"`
  29. AppMessageSlidingWindowRate int `json:"app.message_sliding_window_rate"`
  30. PrivacyIndividualTracking bool `json:"privacy.individual_tracking"`
  31. PrivacyUnsubHeader bool `json:"privacy.unsubscribe_header"`
  32. PrivacyAllowBlocklist bool `json:"privacy.allow_blocklist"`
  33. PrivacyAllowExport bool `json:"privacy.allow_export"`
  34. PrivacyAllowWipe bool `json:"privacy.allow_wipe"`
  35. PrivacyExportable []string `json:"privacy.exportable"`
  36. DomainBlocklist []string `json:"privacy.domain_blocklist"`
  37. UploadProvider string `json:"upload.provider"`
  38. UploadFilesystemUploadPath string `json:"upload.filesystem.upload_path"`
  39. UploadFilesystemUploadURI string `json:"upload.filesystem.upload_uri"`
  40. UploadS3URL string `json:"upload.s3.url"`
  41. UploadS3AwsAccessKeyID string `json:"upload.s3.aws_access_key_id"`
  42. UploadS3AwsDefaultRegion string `json:"upload.s3.aws_default_region"`
  43. UploadS3AwsSecretAccessKey string `json:"upload.s3.aws_secret_access_key,omitempty"`
  44. UploadS3Bucket string `json:"upload.s3.bucket"`
  45. UploadS3BucketDomain string `json:"upload.s3.bucket_domain"`
  46. UploadS3BucketPath string `json:"upload.s3.bucket_path"`
  47. UploadS3BucketType string `json:"upload.s3.bucket_type"`
  48. UploadS3Expiry string `json:"upload.s3.expiry"`
  49. SMTP []struct {
  50. UUID string `json:"uuid"`
  51. Enabled bool `json:"enabled"`
  52. Host string `json:"host"`
  53. HelloHostname string `json:"hello_hostname"`
  54. Port int `json:"port"`
  55. AuthProtocol string `json:"auth_protocol"`
  56. Username string `json:"username"`
  57. Password string `json:"password,omitempty"`
  58. EmailHeaders []map[string]string `json:"email_headers"`
  59. MaxConns int `json:"max_conns"`
  60. MaxMsgRetries int `json:"max_msg_retries"`
  61. IdleTimeout string `json:"idle_timeout"`
  62. WaitTimeout string `json:"wait_timeout"`
  63. TLSEnabled bool `json:"tls_enabled"`
  64. TLSSkipVerify bool `json:"tls_skip_verify"`
  65. } `json:"smtp"`
  66. Messengers []struct {
  67. UUID string `json:"uuid"`
  68. Enabled bool `json:"enabled"`
  69. Name string `json:"name"`
  70. RootURL string `json:"root_url"`
  71. Username string `json:"username"`
  72. Password string `json:"password,omitempty"`
  73. MaxConns int `json:"max_conns"`
  74. Timeout string `json:"timeout"`
  75. MaxMsgRetries int `json:"max_msg_retries"`
  76. } `json:"messengers"`
  77. BounceEnabled bool `json:"bounce.enabled"`
  78. BounceEnableWebhooks bool `json:"bounce.webhooks_enabled"`
  79. BounceCount int `json:"bounce.count"`
  80. BounceAction string `json:"bounce.action"`
  81. SESEnabled bool `json:"bounce.ses_enabled"`
  82. SendgridEnabled bool `json:"bounce.sendgrid_enabled"`
  83. SendgridKey string `json:"bounce.sendgrid_key"`
  84. BounceBoxes []struct {
  85. UUID string `json:"uuid"`
  86. Enabled bool `json:"enabled"`
  87. Type string `json:"type"`
  88. Host string `json:"host"`
  89. Port int `json:"port"`
  90. AuthProtocol string `json:"auth_protocol"`
  91. ReturnPath string `json:"return_path"`
  92. Username string `json:"username"`
  93. Password string `json:"password,omitempty"`
  94. TLSEnabled bool `json:"tls_enabled"`
  95. TLSSkipVerify bool `json:"tls_skip_verify"`
  96. ScanInterval string `json:"scan_interval"`
  97. } `json:"bounce.mailboxes"`
  98. }
  99. var (
  100. reAlphaNum = regexp.MustCompile(`[^a-z0-9\-]`)
  101. )
  102. // handleGetSettings returns settings from the DB.
  103. func handleGetSettings(c echo.Context) error {
  104. app := c.Get("app").(*App)
  105. s, err := getSettings(app)
  106. if err != nil {
  107. return err
  108. }
  109. // Empty out passwords.
  110. for i := 0; i < len(s.SMTP); i++ {
  111. s.SMTP[i].Password = ""
  112. }
  113. for i := 0; i < len(s.BounceBoxes); i++ {
  114. s.BounceBoxes[i].Password = ""
  115. }
  116. for i := 0; i < len(s.Messengers); i++ {
  117. s.Messengers[i].Password = ""
  118. }
  119. s.UploadS3AwsSecretAccessKey = ""
  120. s.SendgridKey = ""
  121. return c.JSON(http.StatusOK, okResp{s})
  122. }
  123. // handleUpdateSettings returns settings from the DB.
  124. func handleUpdateSettings(c echo.Context) error {
  125. var (
  126. app = c.Get("app").(*App)
  127. set settings
  128. )
  129. // Unmarshal and marshal the fields once to sanitize the settings blob.
  130. if err := c.Bind(&set); err != nil {
  131. return err
  132. }
  133. // Get the existing settings.
  134. cur, err := getSettings(app)
  135. if err != nil {
  136. return err
  137. }
  138. // There should be at least one SMTP block that's enabled.
  139. has := false
  140. for i, s := range set.SMTP {
  141. if s.Enabled {
  142. has = true
  143. }
  144. // Assign a UUID. The frontend only sends a password when the user explictly
  145. // changes the password. In other cases, the existing password in the DB
  146. // is copied while updating the settings and the UUID is used to match
  147. // the incoming array of SMTP blocks with the array in the DB.
  148. if s.UUID == "" {
  149. set.SMTP[i].UUID = uuid.Must(uuid.NewV4()).String()
  150. }
  151. // If there's no password coming in from the frontend, copy the existing
  152. // password by matching the UUID.
  153. if s.Password == "" {
  154. for _, c := range cur.SMTP {
  155. if s.UUID == c.UUID {
  156. set.SMTP[i].Password = c.Password
  157. }
  158. }
  159. }
  160. }
  161. if !has {
  162. return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("settings.errorNoSMTP"))
  163. }
  164. // Bounce boxes.
  165. for i, s := range set.BounceBoxes {
  166. // Assign a UUID. The frontend only sends a password when the user explictly
  167. // changes the password. In other cases, the existing password in the DB
  168. // is copied while updating the settings and the UUID is used to match
  169. // the incoming array of blocks with the array in the DB.
  170. if s.UUID == "" {
  171. set.BounceBoxes[i].UUID = uuid.Must(uuid.NewV4()).String()
  172. }
  173. if d, _ := time.ParseDuration(s.ScanInterval); d.Minutes() < 1 {
  174. return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("settings.bounces.invalidScanInterval"))
  175. }
  176. // If there's no password coming in from the frontend, copy the existing
  177. // password by matching the UUID.
  178. if s.Password == "" {
  179. for _, c := range cur.BounceBoxes {
  180. if s.UUID == c.UUID {
  181. set.BounceBoxes[i].Password = c.Password
  182. }
  183. }
  184. }
  185. }
  186. // Validate and sanitize postback Messenger names. Duplicates are disallowed
  187. // and "email" is a reserved name.
  188. names := map[string]bool{emailMsgr: true}
  189. for i, m := range set.Messengers {
  190. // UUID to keep track of password changes similar to the SMTP logic above.
  191. if m.UUID == "" {
  192. set.Messengers[i].UUID = uuid.Must(uuid.NewV4()).String()
  193. }
  194. if m.Password == "" {
  195. for _, c := range cur.Messengers {
  196. if m.UUID == c.UUID {
  197. set.Messengers[i].Password = c.Password
  198. }
  199. }
  200. }
  201. name := reAlphaNum.ReplaceAllString(strings.ToLower(m.Name), "")
  202. if _, ok := names[name]; ok {
  203. return echo.NewHTTPError(http.StatusBadRequest,
  204. app.i18n.Ts("settings.duplicateMessengerName", "name", name))
  205. }
  206. if len(name) == 0 {
  207. return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("settings.invalidMessengerName"))
  208. }
  209. set.Messengers[i].Name = name
  210. names[name] = true
  211. }
  212. // S3 password?
  213. if set.UploadS3AwsSecretAccessKey == "" {
  214. set.UploadS3AwsSecretAccessKey = cur.UploadS3AwsSecretAccessKey
  215. }
  216. if set.SendgridKey == "" {
  217. set.SendgridKey = cur.SendgridKey
  218. }
  219. // Domain blocklist.
  220. doms := make([]string, 0)
  221. for _, d := range set.DomainBlocklist {
  222. d = strings.TrimSpace(strings.ToLower(d))
  223. if d != "" {
  224. doms = append(doms, d)
  225. }
  226. }
  227. set.DomainBlocklist = doms
  228. // Marshal settings.
  229. b, err := json.Marshal(set)
  230. if err != nil {
  231. return echo.NewHTTPError(http.StatusInternalServerError,
  232. app.i18n.Ts("settings.errorEncoding", "error", err.Error()))
  233. }
  234. // Update the settings in the DB.
  235. if _, err := app.queries.UpdateSettings.Exec(b); err != nil {
  236. return echo.NewHTTPError(http.StatusInternalServerError,
  237. app.i18n.Ts("globals.messages.errorUpdating",
  238. "name", "{globals.terms.settings}", "error", pqErrMsg(err)))
  239. }
  240. // If there are any active campaigns, don't do an auto reload and
  241. // warn the user on the frontend.
  242. if app.manager.HasRunningCampaigns() {
  243. app.Lock()
  244. app.needsRestart = true
  245. app.Unlock()
  246. return c.JSON(http.StatusOK, okResp{struct {
  247. NeedsRestart bool `json:"needs_restart"`
  248. }{true}})
  249. }
  250. // No running campaigns. Reload the app.
  251. go func() {
  252. <-time.After(time.Millisecond * 500)
  253. app.sigChan <- syscall.SIGHUP
  254. }()
  255. return c.JSON(http.StatusOK, okResp{true})
  256. }
  257. // handleGetLogs returns the log entries stored in the log buffer.
  258. func handleGetLogs(c echo.Context) error {
  259. app := c.Get("app").(*App)
  260. return c.JSON(http.StatusOK, okResp{app.bufLog.Lines()})
  261. }
  262. func getSettings(app *App) (settings, error) {
  263. var (
  264. b types.JSONText
  265. out settings
  266. )
  267. if err := app.queries.GetSettings.Get(&b); err != nil {
  268. return out, echo.NewHTTPError(http.StatusInternalServerError,
  269. app.i18n.Ts("globals.messages.errorFetching",
  270. "name", "{globals.terms.settings}", "error", pqErrMsg(err)))
  271. }
  272. // Unmarshall the settings and filter out sensitive fields.
  273. if err := json.Unmarshal([]byte(b), &out); err != nil {
  274. return out, echo.NewHTTPError(http.StatusInternalServerError,
  275. app.i18n.Ts("settings.errorEncoding", "error", err.Error()))
  276. }
  277. return out, nil
  278. }