2020-07-08 11:00:14 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-07-11 13:24:38 +00:00
|
|
|
"bytes"
|
|
|
|
"io/ioutil"
|
2020-07-08 11:00:14 +00:00
|
|
|
"net/http"
|
2020-09-20 11:01:24 +00:00
|
|
|
"regexp"
|
2023-06-24 07:37:13 +00:00
|
|
|
"runtime"
|
2020-09-20 11:01:24 +00:00
|
|
|
"strings"
|
2020-07-08 11:00:14 +00:00
|
|
|
"syscall"
|
|
|
|
"time"
|
2023-07-21 18:16:46 +00:00
|
|
|
"unicode/utf8"
|
2020-07-08 11:00:14 +00:00
|
|
|
|
2020-10-10 15:55:59 +00:00
|
|
|
"github.com/gofrs/uuid"
|
2023-06-24 07:37:13 +00:00
|
|
|
"github.com/jmoiron/sqlx/types"
|
2022-07-11 13:24:38 +00:00
|
|
|
"github.com/knadh/koanf/parsers/json"
|
|
|
|
"github.com/knadh/koanf/providers/rawbytes"
|
2023-04-10 07:15:25 +00:00
|
|
|
"github.com/knadh/koanf/v2"
|
2022-07-11 13:24:38 +00:00
|
|
|
"github.com/knadh/listmonk/internal/messenger/email"
|
2022-04-03 15:24:40 +00:00
|
|
|
"github.com/knadh/listmonk/models"
|
2021-12-09 15:21:07 +00:00
|
|
|
"github.com/labstack/echo/v4"
|
2020-07-08 11:00:14 +00:00
|
|
|
)
|
|
|
|
|
2023-07-21 18:16:46 +00:00
|
|
|
const pwdMask = "•"
|
|
|
|
|
2023-06-24 07:37:13 +00:00
|
|
|
type aboutHost struct {
|
2023-08-06 05:19:12 +00:00
|
|
|
OS string `json:"os"`
|
|
|
|
Machine string `json:"arch"`
|
|
|
|
Hostname string `json:"hostname"`
|
2023-06-24 07:37:13 +00:00
|
|
|
}
|
|
|
|
type aboutSystem struct {
|
|
|
|
NumCPU int `json:"num_cpu"`
|
|
|
|
AllocMB uint64 `json:"memory_alloc_mb"`
|
|
|
|
OSMB uint64 `json:"memory_from_os_mb"`
|
|
|
|
}
|
|
|
|
type about struct {
|
|
|
|
Version string `json:"version"`
|
|
|
|
Build string `json:"build"`
|
|
|
|
GoVersion string `json:"go_version"`
|
|
|
|
GoArch string `json:"go_arch"`
|
|
|
|
Database types.JSONText `json:"database"`
|
|
|
|
System aboutSystem `json:"system"`
|
|
|
|
Host aboutHost `json:"host"`
|
|
|
|
}
|
|
|
|
|
2020-09-20 11:01:24 +00:00
|
|
|
var (
|
|
|
|
reAlphaNum = regexp.MustCompile(`[^a-z0-9\-]`)
|
|
|
|
)
|
|
|
|
|
2020-07-08 11:00:14 +00:00
|
|
|
// handleGetSettings returns settings from the DB.
|
|
|
|
func handleGetSettings(c echo.Context) error {
|
|
|
|
app := c.Get("app").(*App)
|
|
|
|
|
2022-04-03 15:24:40 +00:00
|
|
|
s, err := app.core.GetSettings()
|
2020-07-08 11:00:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Empty out passwords.
|
|
|
|
for i := 0; i < len(s.SMTP); i++ {
|
2023-07-21 18:16:46 +00:00
|
|
|
s.SMTP[i].Password = strings.Repeat(pwdMask, utf8.RuneCountInString(s.SMTP[i].Password))
|
2020-07-08 11:00:14 +00:00
|
|
|
}
|
2021-05-24 17:11:48 +00:00
|
|
|
for i := 0; i < len(s.BounceBoxes); i++ {
|
2023-07-21 18:16:46 +00:00
|
|
|
s.BounceBoxes[i].Password = strings.Repeat(pwdMask, utf8.RuneCountInString(s.BounceBoxes[i].Password))
|
2021-05-24 17:11:48 +00:00
|
|
|
}
|
2020-09-20 11:01:24 +00:00
|
|
|
for i := 0; i < len(s.Messengers); i++ {
|
2023-07-21 18:16:46 +00:00
|
|
|
s.Messengers[i].Password = strings.Repeat(pwdMask, utf8.RuneCountInString(s.Messengers[i].Password))
|
2020-09-20 11:01:24 +00:00
|
|
|
}
|
2023-07-21 18:16:46 +00:00
|
|
|
s.UploadS3AwsSecretAccessKey = strings.Repeat(pwdMask, utf8.RuneCountInString(s.UploadS3AwsSecretAccessKey))
|
|
|
|
s.SendgridKey = strings.Repeat(pwdMask, utf8.RuneCountInString(s.SendgridKey))
|
|
|
|
s.SecurityCaptchaSecret = strings.Repeat(pwdMask, utf8.RuneCountInString(s.SecurityCaptchaSecret))
|
2023-08-31 15:57:34 +00:00
|
|
|
s.BouncePostmark.Password = strings.Repeat(pwdMask, utf8.RuneCountInString(s.BouncePostmark.Password))
|
2020-07-08 11:00:14 +00:00
|
|
|
|
|
|
|
return c.JSON(http.StatusOK, okResp{s})
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleUpdateSettings returns settings from the DB.
|
|
|
|
func handleUpdateSettings(c echo.Context) error {
|
|
|
|
var (
|
|
|
|
app = c.Get("app").(*App)
|
2022-04-03 15:24:40 +00:00
|
|
|
set models.Settings
|
2020-07-08 11:00:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Unmarshal and marshal the fields once to sanitize the settings blob.
|
|
|
|
if err := c.Bind(&set); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the existing settings.
|
2022-04-03 15:24:40 +00:00
|
|
|
cur, err := app.core.GetSettings()
|
2020-07-08 11:00:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// There should be at least one SMTP block that's enabled.
|
|
|
|
has := false
|
|
|
|
for i, s := range set.SMTP {
|
|
|
|
if s.Enabled {
|
|
|
|
has = true
|
|
|
|
}
|
|
|
|
|
2022-02-13 07:37:00 +00:00
|
|
|
// Assign a UUID. The frontend only sends a password when the user explicitly
|
2020-10-10 15:55:59 +00:00
|
|
|
// changes the password. In other cases, the existing password in the DB
|
|
|
|
// is copied while updating the settings and the UUID is used to match
|
|
|
|
// the incoming array of SMTP blocks with the array in the DB.
|
|
|
|
if s.UUID == "" {
|
|
|
|
set.SMTP[i].UUID = uuid.Must(uuid.NewV4()).String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there's no password coming in from the frontend, copy the existing
|
|
|
|
// password by matching the UUID.
|
|
|
|
if s.Password == "" {
|
|
|
|
for _, c := range cur.SMTP {
|
|
|
|
if s.UUID == c.UUID {
|
|
|
|
set.SMTP[i].Password = c.Password
|
|
|
|
}
|
2020-07-08 11:00:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !has {
|
2020-12-19 10:55:52 +00:00
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("settings.errorNoSMTP"))
|
2020-09-20 11:01:24 +00:00
|
|
|
}
|
|
|
|
|
2023-09-19 09:53:32 +00:00
|
|
|
set.AppRootURL = strings.TrimRight(set.AppRootURL, "/")
|
|
|
|
|
2021-05-24 17:11:48 +00:00
|
|
|
// Bounce boxes.
|
|
|
|
for i, s := range set.BounceBoxes {
|
2022-02-13 07:37:00 +00:00
|
|
|
// Assign a UUID. The frontend only sends a password when the user explicitly
|
2021-05-24 17:11:48 +00:00
|
|
|
// changes the password. In other cases, the existing password in the DB
|
|
|
|
// is copied while updating the settings and the UUID is used to match
|
|
|
|
// the incoming array of blocks with the array in the DB.
|
|
|
|
if s.UUID == "" {
|
|
|
|
set.BounceBoxes[i].UUID = uuid.Must(uuid.NewV4()).String()
|
|
|
|
}
|
|
|
|
|
|
|
|
if d, _ := time.ParseDuration(s.ScanInterval); d.Minutes() < 1 {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("settings.bounces.invalidScanInterval"))
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there's no password coming in from the frontend, copy the existing
|
|
|
|
// password by matching the UUID.
|
|
|
|
if s.Password == "" {
|
|
|
|
for _, c := range cur.BounceBoxes {
|
|
|
|
if s.UUID == c.UUID {
|
|
|
|
set.BounceBoxes[i].Password = c.Password
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-20 11:01:24 +00:00
|
|
|
// Validate and sanitize postback Messenger names. Duplicates are disallowed
|
|
|
|
// and "email" is a reserved name.
|
|
|
|
names := map[string]bool{emailMsgr: true}
|
|
|
|
|
2020-10-10 15:55:59 +00:00
|
|
|
for i, m := range set.Messengers {
|
|
|
|
// UUID to keep track of password changes similar to the SMTP logic above.
|
|
|
|
if m.UUID == "" {
|
|
|
|
set.Messengers[i].UUID = uuid.Must(uuid.NewV4()).String()
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.Password == "" {
|
|
|
|
for _, c := range cur.Messengers {
|
|
|
|
if m.UUID == c.UUID {
|
|
|
|
set.Messengers[i].Password = c.Password
|
|
|
|
}
|
2020-09-20 11:01:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-10 15:55:59 +00:00
|
|
|
name := reAlphaNum.ReplaceAllString(strings.ToLower(m.Name), "")
|
2020-09-20 11:01:24 +00:00
|
|
|
if _, ok := names[name]; ok {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest,
|
2021-01-23 13:24:33 +00:00
|
|
|
app.i18n.Ts("settings.duplicateMessengerName", "name", name))
|
2020-09-20 11:01:24 +00:00
|
|
|
}
|
|
|
|
if len(name) == 0 {
|
2020-12-19 10:55:52 +00:00
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("settings.invalidMessengerName"))
|
2020-09-20 11:01:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
set.Messengers[i].Name = name
|
|
|
|
names[name] = true
|
2020-07-08 11:00:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// S3 password?
|
|
|
|
if set.UploadS3AwsSecretAccessKey == "" {
|
|
|
|
set.UploadS3AwsSecretAccessKey = cur.UploadS3AwsSecretAccessKey
|
|
|
|
}
|
2021-05-24 17:11:48 +00:00
|
|
|
if set.SendgridKey == "" {
|
|
|
|
set.SendgridKey = cur.SendgridKey
|
|
|
|
}
|
2023-08-31 15:57:34 +00:00
|
|
|
if set.BouncePostmark.Password == "" {
|
|
|
|
set.BouncePostmark.Password = cur.BouncePostmark.Password
|
|
|
|
}
|
2023-01-23 16:20:10 +00:00
|
|
|
if set.SecurityCaptchaSecret == "" {
|
|
|
|
set.SecurityCaptchaSecret = cur.SecurityCaptchaSecret
|
|
|
|
}
|
2020-07-08 11:00:14 +00:00
|
|
|
|
2023-05-18 11:25:59 +00:00
|
|
|
for n, v := range set.UploadExtensions {
|
|
|
|
set.UploadExtensions[n] = strings.ToLower(strings.TrimPrefix(strings.TrimSpace(v), "."))
|
|
|
|
}
|
|
|
|
|
2021-09-25 07:27:55 +00:00
|
|
|
// Domain blocklist.
|
|
|
|
doms := make([]string, 0)
|
|
|
|
for _, d := range set.DomainBlocklist {
|
|
|
|
d = strings.TrimSpace(strings.ToLower(d))
|
|
|
|
if d != "" {
|
|
|
|
doms = append(doms, d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
set.DomainBlocklist = doms
|
|
|
|
|
2020-07-08 11:00:14 +00:00
|
|
|
// Update the settings in the DB.
|
2022-04-03 15:24:40 +00:00
|
|
|
if err := app.core.UpdateSettings(set); err != nil {
|
|
|
|
return err
|
2020-07-08 11:00:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If there are any active campaigns, don't do an auto reload and
|
|
|
|
// warn the user on the frontend.
|
|
|
|
if app.manager.HasRunningCampaigns() {
|
|
|
|
app.Lock()
|
|
|
|
app.needsRestart = true
|
|
|
|
app.Unlock()
|
|
|
|
|
|
|
|
return c.JSON(http.StatusOK, okResp{struct {
|
|
|
|
NeedsRestart bool `json:"needs_restart"`
|
|
|
|
}{true}})
|
|
|
|
}
|
|
|
|
|
|
|
|
// No running campaigns. Reload the app.
|
|
|
|
go func() {
|
|
|
|
<-time.After(time.Millisecond * 500)
|
2023-05-26 16:37:58 +00:00
|
|
|
app.chReload <- syscall.SIGHUP
|
2020-07-08 11:00:14 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
return c.JSON(http.StatusOK, okResp{true})
|
|
|
|
}
|
|
|
|
|
2020-10-10 18:24:03 +00:00
|
|
|
// handleGetLogs returns the log entries stored in the log buffer.
|
|
|
|
func handleGetLogs(c echo.Context) error {
|
|
|
|
app := c.Get("app").(*App)
|
|
|
|
return c.JSON(http.StatusOK, okResp{app.bufLog.Lines()})
|
|
|
|
}
|
2022-07-11 13:24:38 +00:00
|
|
|
|
|
|
|
// handleTestSMTPSettings returns the log entries stored in the log buffer.
|
|
|
|
func handleTestSMTPSettings(c echo.Context) error {
|
|
|
|
app := c.Get("app").(*App)
|
|
|
|
|
|
|
|
// Copy the raw JSON post body.
|
|
|
|
reqBody, err := ioutil.ReadAll(c.Request().Body)
|
|
|
|
if err != nil {
|
|
|
|
app.log.Printf("error reading SMTP test: %v", err)
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.Ts("globals.messages.internalError"))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the JSON into koanf to parse SMTP settings properly including timestrings.
|
|
|
|
ko := koanf.New(".")
|
|
|
|
if err := ko.Load(rawbytes.Provider(reqBody), json.Parser()); err != nil {
|
|
|
|
app.log.Printf("error unmarshalling SMTP test request: %v", err)
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.Ts("globals.messages.internalError"))
|
|
|
|
}
|
|
|
|
|
|
|
|
req := email.Server{}
|
|
|
|
if err := ko.UnmarshalWithConf("", &req, koanf.UnmarshalConf{Tag: "json"}); err != nil {
|
|
|
|
app.log.Printf("error scanning SMTP test request: %v", err)
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.Ts("globals.messages.internalError"))
|
|
|
|
}
|
|
|
|
|
|
|
|
to := ko.String("email")
|
|
|
|
if to == "" {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.Ts("globals.messages.missingFields", "name", "email"))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize a new SMTP pool.
|
|
|
|
req.MaxConns = 1
|
|
|
|
req.IdleTimeout = time.Second * 2
|
|
|
|
req.PoolWaitTimeout = time.Second * 2
|
|
|
|
msgr, err := email.New(req)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest,
|
|
|
|
app.i18n.Ts("globals.messages.errorCreating", "name", "SMTP", "error", err.Error()))
|
|
|
|
}
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
if err := app.notifTpls.tpls.ExecuteTemplate(&b, "smtp-test", nil); err != nil {
|
|
|
|
app.log.Printf("error compiling notification template '%s': %v", "smtp-test", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-08 17:13:25 +00:00
|
|
|
m := models.Message{}
|
2022-07-11 13:24:38 +00:00
|
|
|
m.ContentType = app.notifTpls.contentType
|
|
|
|
m.From = app.constants.FromEmail
|
|
|
|
m.To = []string{to}
|
|
|
|
m.Subject = app.i18n.T("settings.smtp.testConnection")
|
|
|
|
m.Body = b.Bytes()
|
|
|
|
if err := msgr.Push(m); err != nil {
|
|
|
|
app.log.Printf("error sending SMTP test (%s): %v", m.Subject, err)
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(http.StatusOK, okResp{app.bufLog.Lines()})
|
|
|
|
}
|
2023-06-24 07:37:13 +00:00
|
|
|
|
|
|
|
func handleGetAboutInfo(c echo.Context) error {
|
|
|
|
var (
|
2023-08-03 18:16:46 +00:00
|
|
|
app = c.Get("app").(*App)
|
|
|
|
mem runtime.MemStats
|
2023-06-24 07:37:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
runtime.ReadMemStats(&mem)
|
|
|
|
|
|
|
|
out := app.about
|
|
|
|
out.System.AllocMB = mem.Alloc / 1024 / 1024
|
|
|
|
out.System.OSMB = mem.Sys / 1024 / 1024
|
|
|
|
|
|
|
|
return c.JSON(http.StatusOK, out)
|
|
|
|
}
|