main.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package smtp_server
  2. import (
  3. "crypto/tls"
  4. "github.com/emersion/go-smtp"
  5. log "github.com/sirupsen/logrus"
  6. "net"
  7. "pmail/config"
  8. "time"
  9. )
  10. // The Backend implements SMTP server methods.
  11. type Backend struct{}
  12. func (bkd *Backend) NewSession(conn *smtp.Conn) (smtp.Session, error) {
  13. remoteAddress := conn.Conn().RemoteAddr()
  14. return &Session{
  15. RemoteAddress: remoteAddress,
  16. }, nil
  17. }
  18. // A Session is returned after EHLO.
  19. type Session struct {
  20. RemoteAddress net.Addr
  21. }
  22. func (s *Session) AuthPlain(username, password string) error {
  23. return nil
  24. }
  25. func (s *Session) Mail(from string, opts *smtp.MailOptions) error {
  26. return nil
  27. }
  28. func (s *Session) Rcpt(to string) error {
  29. return nil
  30. }
  31. func (s *Session) Reset() {}
  32. func (s *Session) Logout() error {
  33. return nil
  34. }
  35. var instance *smtp.Server
  36. func Start() {
  37. be := &Backend{}
  38. instance = smtp.NewServer(be)
  39. instance.Addr = ":25"
  40. instance.Domain = config.Instance.Domain
  41. instance.ReadTimeout = 10 * time.Second
  42. instance.WriteTimeout = 10 * time.Second
  43. instance.MaxMessageBytes = 1024 * 1024
  44. instance.MaxRecipients = 50
  45. // force TLS for auth
  46. instance.AllowInsecureAuth = false
  47. // Load the certificate and key
  48. cer, err := tls.LoadX509KeyPair(config.Instance.SSLPublicKeyPath, config.Instance.SSLPrivateKeyPath)
  49. if err != nil {
  50. log.Fatal(err)
  51. return
  52. }
  53. // Configure the TLS support
  54. instance.TLSConfig = &tls.Config{Certificates: []tls.Certificate{cer}}
  55. log.Println("Starting server at", instance.Addr)
  56. if err := instance.ListenAndServe(); err != nil {
  57. log.Fatal(err)
  58. }
  59. }
  60. func Stop() {
  61. if instance != nil {
  62. instance.Close()
  63. }
  64. }