list

server

This is the main dovel repository, it has the Go code to run dovel SMTP server.

curl -O https://dovel.email/server.tar.gz tar.gz

main.go

  1. package main
  2. import (
  3. "crypto/tls"
  4. "encoding/json"
  5. "log/slog"
  6. "os"
  7. "path"
  8. "time"
  9. "github.com/emersion/go-smtp"
  10. )
  11. var (
  12. cfg = Config{}
  13. configPath string
  14. v Vault
  15. )
  16. func main() {
  17. if os.Getenv("DEBUG") != "" {
  18. hand := slog.NewTextHandler(
  19. os.Stdout,
  20. &slog.HandlerOptions{Level: slog.LevelDebug},
  21. )
  22. slog.SetDefault(slog.New(hand))
  23. }
  24. var err error
  25. configPath, err = os.UserConfigDir()
  26. if err != nil {
  27. slog.Warn(err.Error(), "details", "using ~/.config/dovel/config.json")
  28. configPath = "~/.config"
  29. }
  30. configPath = path.Join(configPath, "dovel")
  31. configFile, err := os.Open(path.Join(configPath, "config.json"))
  32. if err != nil {
  33. panic(err)
  34. }
  35. json.NewDecoder(configFile).Decode(&cfg)
  36. slog.Debug("config loaded", "config", cfg)
  37. if cfg.VaultFile != "" {
  38. slog.Info("loading users", "file", cfg.VaultFile)
  39. v, err = NewStore(cfg.VaultFile)
  40. if err != nil {
  41. slog.Warn("failed to load users", "error", err.Error())
  42. }
  43. }
  44. s := smtp.NewServer(backend{})
  45. s.Addr = ":" + cfg.Port
  46. s.Domain = cfg.Domain
  47. s.ReadTimeout = time.Duration(cfg.ReadTimeout) * time.Second
  48. s.WriteTimeout = time.Duration(cfg.WriteTimeout) * time.Second
  49. s.MaxMessageBytes = cfg.MaxMessageBytes
  50. s.MaxRecipients = cfg.MaxRecipients
  51. s.AllowInsecureAuth = cfg.AllowInsecureAuth
  52. if cfg.Certificate != "" {
  53. slog.Debug("loading certs", "cert", cfg.Certificate, "key", cfg.PrivateKey)
  54. c, err := tls.LoadX509KeyPair(cfg.Certificate, cfg.PrivateKey)
  55. if err != nil {
  56. panic(err)
  57. }
  58. s.TLSConfig = &tls.Config{Certificates: []tls.Certificate{c}}
  59. }
  60. err = s.ListenAndServe()
  61. if err != nil {
  62. panic(err)
  63. }
  64. }