2021-12-27 15:39:28 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
2022-06-01 00:38:56 +00:00
|
|
|
"fmt"
|
2021-12-27 15:39:28 +00:00
|
|
|
"github.com/emersion/go-smtp"
|
|
|
|
"io"
|
2021-12-28 00:26:20 +00:00
|
|
|
"mime"
|
|
|
|
"mime/multipart"
|
2022-06-01 00:38:56 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2021-12-27 15:39:28 +00:00
|
|
|
"net/mail"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2021-12-27 21:06:40 +00:00
|
|
|
var (
|
2021-12-28 00:26:20 +00:00
|
|
|
errInvalidDomain = errors.New("invalid domain")
|
|
|
|
errInvalidAddress = errors.New("invalid address")
|
|
|
|
errInvalidTopic = errors.New("invalid topic")
|
|
|
|
errTooManyRecipients = errors.New("too many recipients")
|
|
|
|
errUnsupportedContentType = errors.New("unsupported content type")
|
2021-12-27 21:06:40 +00:00
|
|
|
)
|
|
|
|
|
2021-12-27 15:39:28 +00:00
|
|
|
// smtpBackend implements SMTP server methods.
|
|
|
|
type smtpBackend struct {
|
2021-12-27 21:06:40 +00:00
|
|
|
config *Config
|
2022-06-01 00:38:56 +00:00
|
|
|
handler func(http.ResponseWriter, *http.Request)
|
2021-12-27 21:06:40 +00:00
|
|
|
success int64
|
|
|
|
failure int64
|
|
|
|
mu sync.Mutex
|
2021-12-27 15:39:28 +00:00
|
|
|
}
|
|
|
|
|
2022-06-01 00:38:56 +00:00
|
|
|
func newMailBackend(conf *Config, handler func(http.ResponseWriter, *http.Request)) *smtpBackend {
|
2021-12-27 15:39:28 +00:00
|
|
|
return &smtpBackend{
|
2022-06-01 00:38:56 +00:00
|
|
|
config: conf,
|
|
|
|
handler: handler,
|
2021-12-27 15:39:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *smtpBackend) Login(state *smtp.ConnectionState, username, password string) (smtp.Session, error) {
|
2022-06-01 00:38:56 +00:00
|
|
|
return &smtpSession{backend: b, remoteAddr: state.RemoteAddr.String()}, nil
|
2021-12-27 15:39:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *smtpBackend) AnonymousLogin(state *smtp.ConnectionState) (smtp.Session, error) {
|
2022-06-01 00:38:56 +00:00
|
|
|
return &smtpSession{backend: b, remoteAddr: state.RemoteAddr.String()}, nil
|
2021-12-27 21:06:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *smtpBackend) Counts() (success int64, failure int64) {
|
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
|
|
|
return b.success, b.failure
|
2021-12-27 15:39:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// smtpSession is returned after EHLO.
|
|
|
|
type smtpSession struct {
|
2022-06-01 00:38:56 +00:00
|
|
|
backend *smtpBackend
|
|
|
|
remoteAddr string
|
|
|
|
topic string
|
|
|
|
mu sync.Mutex
|
2021-12-27 15:39:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *smtpSession) AuthPlain(username, password string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *smtpSession) Mail(from string, opts smtp.MailOptions) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *smtpSession) Rcpt(to string) error {
|
2021-12-27 21:06:40 +00:00
|
|
|
return s.withFailCount(func() error {
|
|
|
|
conf := s.backend.config
|
|
|
|
addressList, err := mail.ParseAddressList(to)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if len(addressList) != 1 {
|
|
|
|
return errTooManyRecipients
|
|
|
|
}
|
|
|
|
to = addressList[0].Address
|
|
|
|
if !strings.HasSuffix(to, "@"+conf.SMTPServerDomain) {
|
|
|
|
return errInvalidDomain
|
|
|
|
}
|
|
|
|
to = strings.TrimSuffix(to, "@"+conf.SMTPServerDomain)
|
|
|
|
if conf.SMTPServerAddrPrefix != "" {
|
|
|
|
if !strings.HasPrefix(to, conf.SMTPServerAddrPrefix) {
|
|
|
|
return errInvalidAddress
|
|
|
|
}
|
|
|
|
to = strings.TrimPrefix(to, conf.SMTPServerAddrPrefix)
|
|
|
|
}
|
|
|
|
if !topicRegex.MatchString(to) {
|
|
|
|
return errInvalidTopic
|
|
|
|
}
|
|
|
|
s.mu.Lock()
|
|
|
|
s.topic = to
|
|
|
|
s.mu.Unlock()
|
|
|
|
return nil
|
|
|
|
})
|
2021-12-27 15:39:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *smtpSession) Data(r io.Reader) error {
|
2021-12-27 21:06:40 +00:00
|
|
|
return s.withFailCount(func() error {
|
2021-12-28 00:26:20 +00:00
|
|
|
conf := s.backend.config
|
2021-12-27 21:06:40 +00:00
|
|
|
b, err := io.ReadAll(r) // Protected by MaxMessageBytes
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
msg, err := mail.ReadMessage(bytes.NewReader(b))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-28 00:26:20 +00:00
|
|
|
body, err := readMailBody(msg)
|
2021-12-27 21:06:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-28 00:41:00 +00:00
|
|
|
body = strings.TrimSpace(body)
|
2021-12-28 00:26:20 +00:00
|
|
|
if len(body) > conf.MessageLimit {
|
|
|
|
body = body[:conf.MessageLimit]
|
|
|
|
}
|
|
|
|
m := newDefaultMessage(s.topic, body)
|
2021-12-28 00:41:00 +00:00
|
|
|
subject := strings.TrimSpace(msg.Header.Get("Subject"))
|
2021-12-27 21:06:40 +00:00
|
|
|
if subject != "" {
|
2021-12-28 00:26:20 +00:00
|
|
|
dec := mime.WordDecoder{}
|
|
|
|
subject, err := dec.DecodeHeader(subject)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-27 21:06:40 +00:00
|
|
|
m.Title = subject
|
|
|
|
}
|
2021-12-28 00:41:00 +00:00
|
|
|
if m.Title != "" && m.Message == "" {
|
|
|
|
m.Message = m.Title // Flip them, this makes more sense
|
|
|
|
m.Title = ""
|
|
|
|
}
|
2022-06-01 00:38:56 +00:00
|
|
|
if err := s.publishMessage(m); err != nil {
|
2021-12-27 21:06:40 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.backend.mu.Lock()
|
|
|
|
s.backend.success++
|
|
|
|
s.backend.mu.Unlock()
|
|
|
|
return nil
|
|
|
|
})
|
2021-12-27 15:39:28 +00:00
|
|
|
}
|
|
|
|
|
2022-06-01 00:38:56 +00:00
|
|
|
func (s *smtpSession) publishMessage(m *message) error {
|
|
|
|
url := fmt.Sprintf("%s/%s", s.backend.config.BaseURL, m.Topic)
|
|
|
|
req, err := http.NewRequest("PUT", url, strings.NewReader(m.Message))
|
|
|
|
req.RemoteAddr = s.remoteAddr // rate limiting!!
|
2022-06-01 03:27:24 +00:00
|
|
|
req.Header.Set("X-Forwarded-For", s.remoteAddr)
|
2022-06-01 00:38:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if m.Title != "" {
|
|
|
|
req.Header.Set("Title", m.Title)
|
|
|
|
}
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
s.backend.handler(rr, req)
|
|
|
|
if rr.Code != http.StatusOK {
|
|
|
|
return errors.New("error: " + rr.Body.String())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-27 15:39:28 +00:00
|
|
|
func (s *smtpSession) Reset() {
|
|
|
|
s.mu.Lock()
|
2021-12-27 21:06:40 +00:00
|
|
|
s.topic = ""
|
2021-12-27 15:39:28 +00:00
|
|
|
s.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *smtpSession) Logout() error {
|
|
|
|
return nil
|
|
|
|
}
|
2021-12-27 21:06:40 +00:00
|
|
|
|
|
|
|
func (s *smtpSession) withFailCount(fn func() error) error {
|
|
|
|
err := fn()
|
|
|
|
s.backend.mu.Lock()
|
|
|
|
defer s.backend.mu.Unlock()
|
|
|
|
if err != nil {
|
|
|
|
s.backend.failure++
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2021-12-28 00:26:20 +00:00
|
|
|
|
|
|
|
func readMailBody(msg *mail.Message) (string, error) {
|
2022-05-22 00:20:44 +00:00
|
|
|
if msg.Header.Get("Content-Type") == "" {
|
|
|
|
return readPlainTextMailBody(msg)
|
|
|
|
}
|
2021-12-28 00:26:20 +00:00
|
|
|
contentType, params, err := mime.ParseMediaType(msg.Header.Get("Content-Type"))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if contentType == "text/plain" {
|
2022-05-22 00:20:44 +00:00
|
|
|
return readPlainTextMailBody(msg)
|
|
|
|
} else if strings.HasPrefix(contentType, "multipart/") {
|
|
|
|
return readMultipartMailBody(msg, params)
|
|
|
|
}
|
|
|
|
return "", errUnsupportedContentType
|
|
|
|
}
|
|
|
|
|
|
|
|
func readPlainTextMailBody(msg *mail.Message) (string, error) {
|
|
|
|
body, err := io.ReadAll(msg.Body)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(body), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func readMultipartMailBody(msg *mail.Message, params map[string]string) (string, error) {
|
|
|
|
mr := multipart.NewReader(msg.Body, params["boundary"])
|
|
|
|
for {
|
|
|
|
part, err := mr.NextPart()
|
|
|
|
if err != nil { // may be io.EOF
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
partContentType, _, err := mime.ParseMediaType(part.Header.Get("Content-Type"))
|
2021-12-28 00:26:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-05-22 00:20:44 +00:00
|
|
|
if partContentType != "text/plain" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
body, err := io.ReadAll(part)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2021-12-28 00:26:20 +00:00
|
|
|
}
|
2022-05-22 00:20:44 +00:00
|
|
|
return string(body), nil
|
2021-12-28 00:26:20 +00:00
|
|
|
}
|
|
|
|
}
|