refactor direct smtp client
This commit is contained in:
21
bot/email.go
21
bot/email.go
@@ -40,16 +40,19 @@ func (b *Bot) SetSendmail(sendmail func(string, string, string) error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bot) shouldQueue(msg string) bool {
|
func (b *Bot) shouldQueue(msg string) bool {
|
||||||
errs := strings.Split(msg, ";")
|
msg = strings.TrimSpace(msg)
|
||||||
for _, err := range errs {
|
if strings.HasPrefix(msg, "4") { // any temporary issue (4xx SMTP code)
|
||||||
errParts := strings.Split(strings.TrimSpace(err), ":")
|
return true
|
||||||
if len(errParts) < 2 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if strings.HasPrefix(strings.TrimSpace(errParts[1]), "4") {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if strings.Contains(msg, "450") || strings.Contains(msg, "451") { // greylisting
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.Contains(msg, "greylisted") { // greylisting
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
2
go.mod
2
go.mod
@@ -24,7 +24,6 @@ require (
|
|||||||
gitlab.com/etke.cc/go/healthchecks v1.0.1
|
gitlab.com/etke.cc/go/healthchecks v1.0.1
|
||||||
gitlab.com/etke.cc/go/mxidwc v1.0.0
|
gitlab.com/etke.cc/go/mxidwc v1.0.0
|
||||||
gitlab.com/etke.cc/go/secgen v1.1.1
|
gitlab.com/etke.cc/go/secgen v1.1.1
|
||||||
gitlab.com/etke.cc/go/trysmtp v1.1.3
|
|
||||||
gitlab.com/etke.cc/go/validator v1.0.6
|
gitlab.com/etke.cc/go/validator v1.0.6
|
||||||
gitlab.com/etke.cc/linkpearl v0.0.0-20231007103859-01907e2b75f2
|
gitlab.com/etke.cc/linkpearl v0.0.0-20231007103859-01907e2b75f2
|
||||||
golang.org/x/exp v0.0.0-20230905200255-921286631fa9
|
golang.org/x/exp v0.0.0-20230905200255-921286631fa9
|
||||||
@@ -52,6 +51,7 @@ require (
|
|||||||
github.com/tidwall/pretty v1.2.1 // indirect
|
github.com/tidwall/pretty v1.2.1 // indirect
|
||||||
github.com/tidwall/sjson v1.2.5 // indirect
|
github.com/tidwall/sjson v1.2.5 // indirect
|
||||||
github.com/yuin/goldmark v1.5.6 // indirect
|
github.com/yuin/goldmark v1.5.6 // indirect
|
||||||
|
gitlab.com/etke.cc/go/trysmtp v1.1.3 // indirect
|
||||||
go.mau.fi/util v0.1.0 // indirect
|
go.mau.fi/util v0.1.0 // indirect
|
||||||
golang.org/x/crypto v0.13.0 // indirect
|
golang.org/x/crypto v0.13.0 // indirect
|
||||||
golang.org/x/net v0.15.0 // indirect
|
golang.org/x/net v0.15.0 // indirect
|
||||||
|
|||||||
@@ -2,12 +2,13 @@ package smtp
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"net"
|
||||||
"net/smtp"
|
"net/smtp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
"gitlab.com/etke.cc/go/trysmtp"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type MailSender interface {
|
type MailSender interface {
|
||||||
@@ -20,6 +21,8 @@ type Client struct {
|
|||||||
log *zerolog.Logger
|
log *zerolog.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var errNoSMTP = fmt.Errorf("cannot connect to any SMTP server")
|
||||||
|
|
||||||
func newClient(cfg *RelayConfig, log *zerolog.Logger) *Client {
|
func newClient(cfg *RelayConfig, log *zerolog.Logger) *Client {
|
||||||
return &Client{
|
return &Client{
|
||||||
config: cfg,
|
config: cfg,
|
||||||
@@ -36,10 +39,10 @@ func (c Client) Send(from, to, data string) error {
|
|||||||
var err error
|
var err error
|
||||||
if c.config.Host != "" {
|
if c.config.Host != "" {
|
||||||
log.Debug().Msg("creating relay client...")
|
log.Debug().Msg("creating relay client...")
|
||||||
conn, err = c.createDirectClient(from, to)
|
conn, err = c.createRelayClient(from, to)
|
||||||
} else {
|
} else {
|
||||||
log.Debug().Msg("trying direct SMTP connection...")
|
log.Debug().Msg("trying direct SMTP connection...")
|
||||||
conn, err = trysmtp.Connect(from, to)
|
conn, err = c.createDirectClient(from, to)
|
||||||
}
|
}
|
||||||
|
|
||||||
if conn == nil {
|
if conn == nil {
|
||||||
@@ -58,7 +61,7 @@ func (c Client) Send(from, to, data string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer w.Close()
|
defer w.Close()
|
||||||
c.log.Debug().Str("DATA", data).Msg("sending command")
|
log.Debug().Str("DATA", data).Msg("sending command")
|
||||||
_, err = strings.NewReader(data).WriteTo(w)
|
_, err = strings.NewReader(data).WriteTo(w)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Msg("cannot write DATA")
|
log.Error().Err(err).Msg("cannot write DATA")
|
||||||
@@ -69,8 +72,8 @@ func (c Client) Send(from, to, data string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// createDirectClient connects directly to the provided smtp host
|
// createRelayClientconnects directly to the provided smtp host
|
||||||
func (c *Client) createDirectClient(from, to string) (*smtp.Client, error) {
|
func (c *Client) createRelayClient(from, to string) (*smtp.Client, error) {
|
||||||
localname := strings.SplitN(from, "@", 2)[1]
|
localname := strings.SplitN(from, "@", 2)[1]
|
||||||
target := c.config.Host + ":" + c.config.Port
|
target := c.config.Host + ":" + c.config.Port
|
||||||
conn, err := smtp.Dial(target)
|
conn, err := smtp.Dial(target)
|
||||||
@@ -110,3 +113,76 @@ func (c *Client) createDirectClient(from, to string) (*smtp.Client, error) {
|
|||||||
|
|
||||||
return conn, nil
|
return conn, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Client) createDirectClient(from, to string) (*smtp.Client, error) {
|
||||||
|
localname := strings.SplitN(from, "@", 2)[1]
|
||||||
|
hostname := strings.SplitN(to, "@", 2)[1]
|
||||||
|
client, cerr := c.trySMTP(localname, hostname)
|
||||||
|
if client == nil {
|
||||||
|
c.log.Warn().Err(cerr).Str("from", from).Str("to", to).Msg("cannot create direct SMTP client")
|
||||||
|
return nil, cerr
|
||||||
|
}
|
||||||
|
|
||||||
|
err := client.Mail(from)
|
||||||
|
if err != nil {
|
||||||
|
c.log.Warn().Err(err).Str("from", from).Str("to", to).Msg("cannot send MAIL command")
|
||||||
|
client.Close()
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = client.Rcpt(to)
|
||||||
|
if err != nil {
|
||||||
|
c.log.Warn().Err(err).Str("from", from).Str("to", to).Msg("cannot send RCPT command")
|
||||||
|
client.Close()
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return client, cerr
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) trySMTP(localname, hostname string) (*smtp.Client, error) {
|
||||||
|
mxs, err := net.LookupMX(hostname)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var client *smtp.Client
|
||||||
|
for _, mx := range mxs {
|
||||||
|
if mx.Host == "." {
|
||||||
|
continue // no records case
|
||||||
|
}
|
||||||
|
client = c.connect(localname, hostname, strings.TrimSuffix(mx.Host, "."))
|
||||||
|
if client != nil {
|
||||||
|
return client, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If there are no MX records, according to https://datatracker.ietf.org/doc/html/rfc5321#section-5.1,
|
||||||
|
// we're supposed to try talking directly to the host.
|
||||||
|
client = c.connect(localname, hostname, hostname)
|
||||||
|
if client != nil {
|
||||||
|
return client, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, errNoSMTP
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) connect(localname, serverOf, mxhost string) *smtp.Client {
|
||||||
|
target := mxhost + ":25"
|
||||||
|
conn, err := smtp.Dial(target)
|
||||||
|
if err != nil {
|
||||||
|
c.log.Warn().Err(err).Str("target", serverOf).Str("host", mxhost).Msg("cannot dial SMTP server")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
err = conn.Hello(localname)
|
||||||
|
if err != nil {
|
||||||
|
c.log.Warn().Err(err).Str("target", serverOf).Str("host", mxhost).Msg("cannot hello SMTP server")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if ok, _ := conn.Extension("STARTTLS"); ok {
|
||||||
|
config := &tls.Config{ServerName: mxhost} //nolint:gosec // it's smtp, even that is too strict sometimes
|
||||||
|
conn.StartTLS(config) //nolint:errcheck // if it doesn't work - we can't do anything anyway
|
||||||
|
}
|
||||||
|
|
||||||
|
return conn
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user