3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-22 03:49:27 +01:00

add support for email timeouts

This commit is contained in:
Shivaram Lingamneni 2021-07-07 07:12:15 -04:00
parent 46572b871f
commit 032ca175e4
4 changed files with 29 additions and 6 deletions

View File

@ -413,6 +413,7 @@ accounts:
# password: "hunter2" # password: "hunter2"
blacklist-regexes: blacklist-regexes:
# - ".*@mailinator.com" # - ".*@mailinator.com"
timeout: 60s
# throttle account login attempts (to prevent either password guessing, or DoS # throttle account login attempts (to prevent either password guessing, or DoS
# attacks on the server aimed at forcing repeated expensive bcrypt computations) # attacks on the server aimed at forcing repeated expensive bcrypt computations)

View File

@ -9,6 +9,7 @@ import (
"net" "net"
"regexp" "regexp"
"strings" "strings"
"time"
"github.com/ergochat/ergo/irc/smtp" "github.com/ergochat/ergo/irc/smtp"
) )
@ -40,6 +41,7 @@ type MailtoConfig struct {
MTAReal MTAConfig `yaml:"mta"` MTAReal MTAConfig `yaml:"mta"`
BlacklistRegexes []string `yaml:"blacklist-regexes"` BlacklistRegexes []string `yaml:"blacklist-regexes"`
blacklistRegexes []*regexp.Regexp blacklistRegexes []*regexp.Regexp
Timeout time.Duration
} }
func (config *MailtoConfig) Postprocess(heloDomain string) (err error) { func (config *MailtoConfig) Postprocess(heloDomain string) (err error) {
@ -126,5 +128,5 @@ func SendMail(config MailtoConfig, recipient string, msg []byte) (err error) {
addr = fmt.Sprintf("%s:smtp", mx) addr = fmt.Sprintf("%s:smtp", mx)
} }
return smtp.SendMail(addr, auth, config.HeloDomain, config.Sender, []string{recipient}, msg, config.RequireTLS) return smtp.SendMail(addr, auth, config.HeloDomain, config.Sender, []string{recipient}, msg, config.RequireTLS, config.Timeout)
} }

View File

@ -24,6 +24,11 @@ import (
"net" "net"
"net/textproto" "net/textproto"
"strings" "strings"
"time"
)
var (
ErrTimedOut = errors.New("Timed out")
) )
// A Client represents a client connection to an SMTP server. // A Client represents a client connection to an SMTP server.
@ -48,11 +53,25 @@ type Client struct {
// Dial returns a new Client connected to an SMTP server at addr. // Dial returns a new Client connected to an SMTP server at addr.
// The addr must include a port, as in "mail.example.com:smtp". // The addr must include a port, as in "mail.example.com:smtp".
func Dial(addr string) (*Client, error) { func Dial(addr string, timeout time.Duration) (*Client, error) {
conn, err := net.Dial("tcp", addr) var conn net.Conn
var err error
start := time.Now()
if timeout == 0 {
conn, err = net.Dial("tcp", addr)
} else {
conn, err = net.DialTimeout("tcp", addr, timeout)
}
if err != nil { if err != nil {
return nil, err return nil, err
} }
if timeout != 0 {
remaining := timeout - time.Since(start)
if remaining <= 0 {
return nil, ErrTimedOut
}
conn.SetDeadline(time.Now().Add(remaining))
}
host, _, _ := net.SplitHostPort(addr) host, _, _ := net.SplitHostPort(addr)
return NewClient(conn, host) return NewClient(conn, host)
} }
@ -316,8 +335,8 @@ var testHookStartTLS func(*tls.Config) // nil, except for tests
// attachments (see the mime/multipart package), or other mail // attachments (see the mime/multipart package), or other mail
// functionality. Higher-level packages exist outside of the standard // functionality. Higher-level packages exist outside of the standard
// library. // library.
// XXX: modified in Oragono to add `requireTLS` and `heloDomain` arguments // XXX: modified in Ergo to add `requireTLS`, `heloDomain`, and `timeout` arguments
func SendMail(addr string, a Auth, heloDomain string, from string, to []string, msg []byte, requireTLS bool) error { func SendMail(addr string, a Auth, heloDomain string, from string, to []string, msg []byte, requireTLS bool, timeout time.Duration) error {
if err := validateLine(from); err != nil { if err := validateLine(from); err != nil {
return err return err
} }
@ -326,7 +345,7 @@ func SendMail(addr string, a Auth, heloDomain string, from string, to []string,
return err return err
} }
} }
c, err := Dial(addr) c, err := Dial(addr, timeout)
if err != nil { if err != nil {
return err return err
} }

View File

@ -386,6 +386,7 @@ accounts:
# password: "hunter2" # password: "hunter2"
blacklist-regexes: blacklist-regexes:
# - ".*@mailinator.com" # - ".*@mailinator.com"
timeout: 60s
# throttle account login attempts (to prevent either password guessing, or DoS # throttle account login attempts (to prevent either password guessing, or DoS
# attacks on the server aimed at forcing repeated expensive bcrypt computations) # attacks on the server aimed at forcing repeated expensive bcrypt computations)