2018-10-25 13:51:47 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-08-08 07:41:49 +00:00
|
|
|
"crypto/subtle"
|
2020-12-19 10:55:52 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2019-01-03 11:18:47 +00:00
|
|
|
"net/http"
|
2018-10-25 13:51:47 +00:00
|
|
|
"net/url"
|
2019-07-21 14:41:11 +00:00
|
|
|
"regexp"
|
2018-10-25 13:51:47 +00:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/labstack/echo"
|
2020-08-08 07:41:49 +00:00
|
|
|
"github.com/labstack/echo/middleware"
|
2018-10-25 13:51:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// stdInputMaxLen is the maximum allowed length for a standard input field.
|
|
|
|
stdInputMaxLen = 200
|
2020-10-24 14:30:29 +00:00
|
|
|
|
|
|
|
sortAsc = "asc"
|
|
|
|
sortDesc = "desc"
|
2018-10-25 13:51:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type okResp struct {
|
|
|
|
Data interface{} `json:"data"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// pagination represents a query's pagination (limit, offset) related values.
|
|
|
|
type pagination struct {
|
|
|
|
PerPage int `json:"per_page"`
|
|
|
|
Page int `json:"page"`
|
|
|
|
Offset int `json:"offset"`
|
|
|
|
Limit int `json:"limit"`
|
|
|
|
}
|
|
|
|
|
2020-12-19 10:55:52 +00:00
|
|
|
var (
|
|
|
|
reUUID = regexp.MustCompile("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$")
|
|
|
|
reLangCode = regexp.MustCompile("[^a-zA-Z_0-9]")
|
|
|
|
)
|
2019-07-21 14:41:11 +00:00
|
|
|
|
2019-01-03 11:18:47 +00:00
|
|
|
// registerHandlers registers HTTP handlers.
|
2020-03-07 18:33:22 +00:00
|
|
|
func registerHTTPHandlers(e *echo.Echo) {
|
2020-08-08 07:41:49 +00:00
|
|
|
// Group of private handlers with BasicAuth.
|
|
|
|
g := e.Group("", middleware.BasicAuth(basicAuth))
|
|
|
|
g.GET("/", handleIndexPage)
|
|
|
|
g.GET("/api/health", handleHealthCheck)
|
|
|
|
g.GET("/api/config.js", handleGetConfigScript)
|
2020-12-19 10:55:52 +00:00
|
|
|
g.GET("/api/lang/:lang", handleLoadLanguage)
|
2020-08-08 07:41:49 +00:00
|
|
|
g.GET("/api/dashboard/charts", handleGetDashboardCharts)
|
|
|
|
g.GET("/api/dashboard/counts", handleGetDashboardCounts)
|
|
|
|
|
|
|
|
g.GET("/api/settings", handleGetSettings)
|
|
|
|
g.PUT("/api/settings", handleUpdateSettings)
|
|
|
|
g.POST("/api/admin/reload", handleReloadApp)
|
2020-10-10 18:24:03 +00:00
|
|
|
g.GET("/api/logs", handleGetLogs)
|
2020-08-08 07:41:49 +00:00
|
|
|
|
|
|
|
g.GET("/api/subscribers/:id", handleGetSubscriber)
|
|
|
|
g.GET("/api/subscribers/:id/export", handleExportSubscriberData)
|
|
|
|
g.POST("/api/subscribers", handleCreateSubscriber)
|
|
|
|
g.PUT("/api/subscribers/:id", handleUpdateSubscriber)
|
|
|
|
g.POST("/api/subscribers/:id/optin", handleSubscriberSendOptin)
|
|
|
|
g.PUT("/api/subscribers/blocklist", handleBlocklistSubscribers)
|
|
|
|
g.PUT("/api/subscribers/:id/blocklist", handleBlocklistSubscribers)
|
|
|
|
g.PUT("/api/subscribers/lists/:id", handleManageSubscriberLists)
|
|
|
|
g.PUT("/api/subscribers/lists", handleManageSubscriberLists)
|
|
|
|
g.DELETE("/api/subscribers/:id", handleDeleteSubscribers)
|
|
|
|
g.DELETE("/api/subscribers", handleDeleteSubscribers)
|
2019-01-03 11:18:47 +00:00
|
|
|
|
|
|
|
// Subscriber operations based on arbitrary SQL queries.
|
|
|
|
// These aren't very REST-like.
|
2020-08-08 07:41:49 +00:00
|
|
|
g.POST("/api/subscribers/query/delete", handleDeleteSubscribersByQuery)
|
|
|
|
g.PUT("/api/subscribers/query/blocklist", handleBlocklistSubscribersByQuery)
|
|
|
|
g.PUT("/api/subscribers/query/lists", handleManageSubscriberListsByQuery)
|
|
|
|
g.GET("/api/subscribers", handleQuerySubscribers)
|
2021-01-23 12:53:29 +00:00
|
|
|
g.GET("/api/subscribers/export",
|
|
|
|
middleware.GzipWithConfig(middleware.GzipConfig{Level: 9})(handleExportSubscribers))
|
2020-08-08 07:41:49 +00:00
|
|
|
|
|
|
|
g.GET("/api/import/subscribers", handleGetImportSubscribers)
|
|
|
|
g.GET("/api/import/subscribers/logs", handleGetImportSubscriberStats)
|
|
|
|
g.POST("/api/import/subscribers", handleImportSubscribers)
|
|
|
|
g.DELETE("/api/import/subscribers", handleStopImportSubscribers)
|
|
|
|
|
|
|
|
g.GET("/api/lists", handleGetLists)
|
|
|
|
g.GET("/api/lists/:id", handleGetLists)
|
|
|
|
g.POST("/api/lists", handleCreateList)
|
|
|
|
g.PUT("/api/lists/:id", handleUpdateList)
|
|
|
|
g.DELETE("/api/lists/:id", handleDeleteLists)
|
|
|
|
|
|
|
|
g.GET("/api/campaigns", handleGetCampaigns)
|
|
|
|
g.GET("/api/campaigns/running/stats", handleGetRunningCampaignStats)
|
|
|
|
g.GET("/api/campaigns/:id", handleGetCampaigns)
|
|
|
|
g.GET("/api/campaigns/:id/preview", handlePreviewCampaign)
|
|
|
|
g.POST("/api/campaigns/:id/preview", handlePreviewCampaign)
|
|
|
|
g.POST("/api/campaigns/:id/test", handleTestCampaign)
|
|
|
|
g.POST("/api/campaigns", handleCreateCampaign)
|
|
|
|
g.PUT("/api/campaigns/:id", handleUpdateCampaign)
|
|
|
|
g.PUT("/api/campaigns/:id/status", handleUpdateCampaignStatus)
|
|
|
|
g.DELETE("/api/campaigns/:id", handleDeleteCampaign)
|
|
|
|
|
|
|
|
g.GET("/api/media", handleGetMedia)
|
|
|
|
g.POST("/api/media", handleUploadMedia)
|
|
|
|
g.DELETE("/api/media/:id", handleDeleteMedia)
|
|
|
|
|
|
|
|
g.GET("/api/templates", handleGetTemplates)
|
|
|
|
g.GET("/api/templates/:id", handleGetTemplates)
|
|
|
|
g.GET("/api/templates/:id/preview", handlePreviewTemplate)
|
|
|
|
g.POST("/api/templates/preview", handlePreviewTemplate)
|
|
|
|
g.POST("/api/templates", handleCreateTemplate)
|
|
|
|
g.PUT("/api/templates/:id", handleUpdateTemplate)
|
|
|
|
g.PUT("/api/templates/:id/default", handleTemplateSetDefault)
|
|
|
|
g.DELETE("/api/templates/:id", handleDeleteTemplate)
|
|
|
|
|
|
|
|
// Static admin views.
|
|
|
|
g.GET("/lists", handleIndexPage)
|
|
|
|
g.GET("/lists/forms", handleIndexPage)
|
|
|
|
g.GET("/subscribers", handleIndexPage)
|
|
|
|
g.GET("/subscribers/lists/:listID", handleIndexPage)
|
|
|
|
g.GET("/subscribers/import", handleIndexPage)
|
|
|
|
g.GET("/campaigns", handleIndexPage)
|
|
|
|
g.GET("/campaigns/new", handleIndexPage)
|
|
|
|
g.GET("/campaigns/media", handleIndexPage)
|
|
|
|
g.GET("/campaigns/templates", handleIndexPage)
|
|
|
|
g.GET("/campaigns/:campignID", handleIndexPage)
|
|
|
|
g.GET("/settings", handleIndexPage)
|
2020-10-24 08:06:22 +00:00
|
|
|
g.GET("/settings/logs", handleIndexPage)
|
2020-08-08 07:41:49 +00:00
|
|
|
|
|
|
|
// Public subscriber facing views.
|
2020-03-07 14:49:22 +00:00
|
|
|
e.POST("/subscription/form", handleSubscriptionForm)
|
2019-07-21 16:28:25 +00:00
|
|
|
e.GET("/subscription/:campUUID/:subUUID", validateUUID(subscriberExists(handleSubscriptionPage),
|
2019-07-21 14:41:11 +00:00
|
|
|
"campUUID", "subUUID"))
|
2019-07-21 16:28:25 +00:00
|
|
|
e.POST("/subscription/:campUUID/:subUUID", validateUUID(subscriberExists(handleSubscriptionPage),
|
2019-07-21 14:41:11 +00:00
|
|
|
"campUUID", "subUUID"))
|
2019-12-01 12:18:36 +00:00
|
|
|
e.GET("/subscription/optin/:subUUID", validateUUID(subscriberExists(handleOptinPage), "subUUID"))
|
|
|
|
e.POST("/subscription/optin/:subUUID", validateUUID(subscriberExists(handleOptinPage), "subUUID"))
|
2019-07-21 16:28:25 +00:00
|
|
|
e.POST("/subscription/export/:subUUID", validateUUID(subscriberExists(handleSelfExportSubscriberData),
|
2019-07-21 14:41:11 +00:00
|
|
|
"subUUID"))
|
2019-07-21 16:28:25 +00:00
|
|
|
e.POST("/subscription/wipe/:subUUID", validateUUID(subscriberExists(handleWipeSubscriberData),
|
2019-07-21 14:41:11 +00:00
|
|
|
"subUUID"))
|
|
|
|
e.GET("/link/:linkUUID/:campUUID/:subUUID", validateUUID(handleLinkRedirect,
|
|
|
|
"linkUUID", "campUUID", "subUUID"))
|
2020-04-26 10:21:43 +00:00
|
|
|
e.GET("/campaign/:campUUID/:subUUID", validateUUID(handleViewCampaignMessage,
|
|
|
|
"campUUID", "subUUID"))
|
2019-07-21 14:41:11 +00:00
|
|
|
e.GET("/campaign/:campUUID/:subUUID/px.png", validateUUID(handleRegisterCampaignView,
|
|
|
|
"campUUID", "subUUID"))
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
2020-05-19 17:59:58 +00:00
|
|
|
// handleIndex is the root handler that renders the Javascript frontend.
|
2018-10-25 13:51:47 +00:00
|
|
|
func handleIndexPage(c echo.Context) error {
|
|
|
|
app := c.Get("app").(*App)
|
2019-01-03 11:18:47 +00:00
|
|
|
|
2020-03-07 18:33:22 +00:00
|
|
|
b, err := app.fs.Read("/frontend/index.html")
|
2019-01-03 11:18:47 +00:00
|
|
|
if err != nil {
|
2019-03-09 07:44:53 +00:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
|
2019-01-03 11:18:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c.Response().Header().Set("Content-Type", "text/html")
|
|
|
|
return c.String(http.StatusOK, string(b))
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
2020-07-08 11:00:14 +00:00
|
|
|
// handleHealthCheck is a healthcheck endpoint that returns a 200 response.
|
|
|
|
func handleHealthCheck(c echo.Context) error {
|
|
|
|
return c.JSON(http.StatusOK, okResp{true})
|
|
|
|
}
|
|
|
|
|
2020-12-19 10:55:52 +00:00
|
|
|
// handleLoadLanguage returns the JSON language pack given the language code.
|
|
|
|
func handleLoadLanguage(c echo.Context) error {
|
|
|
|
app := c.Get("app").(*App)
|
|
|
|
|
|
|
|
lang := c.Param("lang")
|
|
|
|
if len(lang) > 6 || reLangCode.MatchString(lang) {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Invalid language code.")
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := app.fs.Read(fmt.Sprintf("/lang/%s.json", lang))
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Unknown language.")
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(http.StatusOK, okResp{json.RawMessage(b)})
|
|
|
|
}
|
|
|
|
|
2020-08-08 07:41:49 +00:00
|
|
|
// basicAuth middleware does an HTTP BasicAuth authentication for admin handlers.
|
|
|
|
func basicAuth(username, password string, c echo.Context) (bool, error) {
|
|
|
|
app := c.Get("app").(*App)
|
|
|
|
|
|
|
|
// Auth is disabled.
|
|
|
|
if len(app.constants.AdminUsername) == 0 &&
|
|
|
|
len(app.constants.AdminPassword) == 0 {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if subtle.ConstantTimeCompare([]byte(username), app.constants.AdminUsername) == 1 &&
|
|
|
|
subtle.ConstantTimeCompare([]byte(password), app.constants.AdminPassword) == 1 {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2019-07-21 16:28:25 +00:00
|
|
|
// validateUUID middleware validates the UUID string format for a given set of params.
|
2019-07-21 14:41:11 +00:00
|
|
|
func validateUUID(next echo.HandlerFunc, params ...string) echo.HandlerFunc {
|
|
|
|
return func(c echo.Context) error {
|
2020-12-19 10:55:52 +00:00
|
|
|
app := c.Get("app").(*App)
|
|
|
|
|
2019-07-21 14:41:11 +00:00
|
|
|
for _, p := range params {
|
|
|
|
if !reUUID.MatchString(c.Param(p)) {
|
2020-03-07 14:54:42 +00:00
|
|
|
return c.Render(http.StatusBadRequest, tplMessage,
|
2020-12-19 10:55:52 +00:00
|
|
|
makeMsgTpl(app.i18n.T("public.errorTitle"), "",
|
|
|
|
app.i18n.T("globals.messages.invalidUUID")))
|
2019-07-21 14:41:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-21 16:28:25 +00:00
|
|
|
// subscriberExists middleware checks if a subscriber exists given the UUID
|
|
|
|
// param in a request.
|
|
|
|
func subscriberExists(next echo.HandlerFunc, params ...string) echo.HandlerFunc {
|
|
|
|
return func(c echo.Context) error {
|
|
|
|
var (
|
|
|
|
app = c.Get("app").(*App)
|
|
|
|
subUUID = c.Param("subUUID")
|
|
|
|
)
|
|
|
|
|
|
|
|
var exists bool
|
2020-03-07 18:33:22 +00:00
|
|
|
if err := app.queries.SubscriberExists.Get(&exists, 0, subUUID); err != nil {
|
|
|
|
app.log.Printf("error checking subscriber existence: %v", err)
|
2020-03-07 14:54:42 +00:00
|
|
|
return c.Render(http.StatusInternalServerError, tplMessage,
|
2020-12-19 10:55:52 +00:00
|
|
|
makeMsgTpl(app.i18n.T("public.errorTitle"), "",
|
|
|
|
app.i18n.T("public.errorProcessingRequest")))
|
2019-07-21 16:28:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !exists {
|
2020-12-19 10:55:52 +00:00
|
|
|
return c.Render(http.StatusNotFound, tplMessage,
|
|
|
|
makeMsgTpl(app.i18n.T("public.notFoundTitle"), "",
|
|
|
|
app.i18n.T("public.subNotFound")))
|
2019-07-21 16:28:25 +00:00
|
|
|
}
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-25 13:51:47 +00:00
|
|
|
// getPagination takes form values and extracts pagination values from it.
|
2020-08-01 13:27:33 +00:00
|
|
|
func getPagination(q url.Values, perPage, maxPerPage int) pagination {
|
|
|
|
page, _ := strconv.Atoi(q.Get("page"))
|
2019-05-14 16:36:14 +00:00
|
|
|
pp := q.Get("per_page")
|
|
|
|
if pp == "all" {
|
|
|
|
// No limit.
|
|
|
|
perPage = 0
|
|
|
|
} else {
|
|
|
|
ppi, _ := strconv.Atoi(pp)
|
2020-01-17 16:47:14 +00:00
|
|
|
if ppi > 0 && ppi <= maxPerPage {
|
|
|
|
perPage = ppi
|
2019-05-14 16:36:14 +00:00
|
|
|
}
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if page < 1 {
|
|
|
|
page = 0
|
|
|
|
} else {
|
|
|
|
page--
|
|
|
|
}
|
|
|
|
|
|
|
|
return pagination{
|
|
|
|
Page: page + 1,
|
|
|
|
PerPage: perPage,
|
|
|
|
Offset: page * perPage,
|
|
|
|
Limit: perPage,
|
|
|
|
}
|
|
|
|
}
|
2020-11-08 13:06:25 +00:00
|
|
|
|
|
|
|
// copyEchoCtx returns a copy of the the current echo.Context in a request
|
|
|
|
// with the given params set for the active handler to proxy the request
|
|
|
|
// to another handler without mutating its context.
|
|
|
|
func copyEchoCtx(c echo.Context, params map[string]string) echo.Context {
|
|
|
|
var (
|
|
|
|
keys = make([]string, 0, len(params))
|
|
|
|
vals = make([]string, 0, len(params))
|
|
|
|
)
|
|
|
|
for k, v := range params {
|
|
|
|
keys = append(keys, k)
|
|
|
|
vals = append(vals, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
b := c.Echo().NewContext(c.Request(), c.Response())
|
|
|
|
b.Set("app", c.Get("app").(*App))
|
|
|
|
b.SetParamNames(keys...)
|
|
|
|
b.SetParamValues(vals...)
|
|
|
|
return b
|
|
|
|
}
|