140 lines
3.2 KiB
Go
140 lines
3.2 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"log"
|
|
"log/slog"
|
|
"net/http"
|
|
"time"
|
|
|
|
"git.nakama.town/fmartingr/gotoolkit/model"
|
|
"git.nakama.town/fmartingr/gotoolkit/service"
|
|
"github.com/emersion/go-sasl"
|
|
"github.com/emersion/go-smtp"
|
|
)
|
|
|
|
var _ model.Server = (*smtpServer)(nil)
|
|
|
|
type smtpServer struct {
|
|
backend *smtp.Server
|
|
}
|
|
|
|
func (s *smtpServer) IsEnabled() bool {
|
|
return true
|
|
}
|
|
|
|
func (s *smtpServer) Start(_ context.Context) error {
|
|
slog.Info("Started SMTP server", slog.String("addr", s.backend.Addr))
|
|
return s.backend.ListenAndServe()
|
|
}
|
|
|
|
func (s *smtpServer) Stop(ctx context.Context) error {
|
|
slog.Info("Stopping SMTP server")
|
|
return s.backend.Shutdown(ctx)
|
|
}
|
|
|
|
func NewSMTPServer(backend *smtp.Server) model.Server {
|
|
return &smtpServer{
|
|
backend: backend,
|
|
}
|
|
}
|
|
|
|
// The Backend implements SMTP server methods.
|
|
type Backend struct{}
|
|
|
|
// NewSession is called after client greeting (EHLO, HELO).
|
|
func (bkd *Backend) NewSession(c *smtp.Conn) (smtp.Session, error) {
|
|
return &Session{}, nil
|
|
}
|
|
|
|
// A Session is returned after successful login.
|
|
type Session struct{}
|
|
|
|
// AuthMechanisms returns a slice of available auth mechanisms; only PLAIN is
|
|
// supported in this example.
|
|
func (s *Session) AuthMechanisms() []string {
|
|
return []string{sasl.Plain}
|
|
}
|
|
|
|
// Auth is the handler for supported authenticators.
|
|
func (s *Session) Auth(mech string) (sasl.Server, error) {
|
|
return sasl.NewPlainServer(func(identity, username, password string) error {
|
|
if username != "username" || password != "password" {
|
|
return errors.New("Invalid username or password")
|
|
}
|
|
return nil
|
|
}), nil
|
|
}
|
|
|
|
func (s *Session) Mail(from string, opts *smtp.MailOptions) error {
|
|
log.Println("Mail from:", from, opts)
|
|
return nil
|
|
}
|
|
|
|
func (s *Session) Rcpt(to string, opts *smtp.RcptOptions) error {
|
|
log.Println("Rcpt to:", to)
|
|
return nil
|
|
}
|
|
|
|
func (s *Session) Data(r io.Reader) error {
|
|
_, err := http.Post("https://ntfy.sh/fmartingr-dev", "text/plain", r)
|
|
if err != nil {
|
|
slog.Error("Error sending message:", slog.String("err", err.Error()))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *Session) Reset() {}
|
|
|
|
func (s *Session) Logout() error {
|
|
return nil
|
|
}
|
|
|
|
// ExampleServer runs an example SMTP server.
|
|
//
|
|
// It can be tested manually with e.g. netcat:
|
|
//
|
|
// > netcat -C localhost 1025
|
|
// EHLO localhost
|
|
// AUTH PLAIN
|
|
// AHVzZXJuYW1lAHBhc3N3b3Jk
|
|
// MAIL FROM:<root@nsa.gov>
|
|
// RCPT TO:<root@gchq.gov.uk>
|
|
// DATA
|
|
// Hey <3
|
|
// .
|
|
func main() {
|
|
be := &Backend{}
|
|
|
|
smtpBackend := smtp.NewServer(be)
|
|
|
|
smtpBackend.Addr = "localhost:11025"
|
|
smtpBackend.Domain = "localhost"
|
|
smtpBackend.WriteTimeout = 10 * time.Second
|
|
smtpBackend.ReadTimeout = 10 * time.Second
|
|
smtpBackend.MaxMessageBytes = 1024 * 1024
|
|
smtpBackend.MaxRecipients = 50
|
|
smtpBackend.AllowInsecureAuth = true
|
|
|
|
ctx := context.Background()
|
|
smtpServer := NewSMTPServer(smtpBackend)
|
|
|
|
svc, err := service.NewService([]model.Server{
|
|
smtpServer,
|
|
})
|
|
if err != nil {
|
|
slog.Error("Error creating service:", slog.String("err", err.Error()))
|
|
return
|
|
}
|
|
|
|
if err := svc.Start(ctx); err != nil {
|
|
slog.Error("Error starting service:", slog.String("err", err.Error()))
|
|
return
|
|
}
|
|
|
|
if err := svc.WaitStop(ctx); err != nil {
|
|
slog.Error("Error waiting for service interruption:", slog.String("err", err.Error()))
|
|
}
|
|
}
|