Преглед на файлове

add support for email timeouts

tags/v2.8.0-rc1
Shivaram Lingamneni преди 2 години
родител
ревизия
032ca175e4
променени са 4 файла, в които са добавени 29 реда и са изтрити 6 реда
  1. 1
    0
      default.yaml
  2. 3
    1
      irc/email/email.go
  3. 24
    5
      irc/smtp/smtp.go
  4. 1
    0
      traditional.yaml

+ 1
- 0
default.yaml Целия файл

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

+ 3
- 1
irc/email/email.go Целия файл

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

+ 24
- 5
irc/smtp/smtp.go Целия файл

@@ -24,6 +24,11 @@ import (
24 24
 	"net"
25 25
 	"net/textproto"
26 26
 	"strings"
27
+	"time"
28
+)
29
+
30
+var (
31
+	ErrTimedOut = errors.New("Timed out")
27 32
 )
28 33
 
29 34
 // A Client represents a client connection to an SMTP server.
@@ -48,11 +53,25 @@ type Client struct {
48 53
 
49 54
 // Dial returns a new Client connected to an SMTP server at addr.
50 55
 // The addr must include a port, as in "mail.example.com:smtp".
51
-func Dial(addr string) (*Client, error) {
52
-	conn, err := net.Dial("tcp", addr)
56
+func Dial(addr string, timeout time.Duration) (*Client, error) {
57
+	var conn net.Conn
58
+	var err error
59
+	start := time.Now()
60
+	if timeout == 0 {
61
+		conn, err = net.Dial("tcp", addr)
62
+	} else {
63
+		conn, err = net.DialTimeout("tcp", addr, timeout)
64
+	}
53 65
 	if err != nil {
54 66
 		return nil, err
55 67
 	}
68
+	if timeout != 0 {
69
+		remaining := timeout - time.Since(start)
70
+		if remaining <= 0 {
71
+			return nil, ErrTimedOut
72
+		}
73
+		conn.SetDeadline(time.Now().Add(remaining))
74
+	}
56 75
 	host, _, _ := net.SplitHostPort(addr)
57 76
 	return NewClient(conn, host)
58 77
 }
@@ -316,8 +335,8 @@ var testHookStartTLS func(*tls.Config) // nil, except for tests
316 335
 // attachments (see the mime/multipart package), or other mail
317 336
 // functionality. Higher-level packages exist outside of the standard
318 337
 // library.
319
-// XXX: modified in Oragono to add `requireTLS` and `heloDomain` arguments
320
-func SendMail(addr string, a Auth, heloDomain string, from string, to []string, msg []byte, requireTLS bool) error {
338
+// XXX: modified in Ergo to add `requireTLS`, `heloDomain`, and `timeout` arguments
339
+func SendMail(addr string, a Auth, heloDomain string, from string, to []string, msg []byte, requireTLS bool, timeout time.Duration) error {
321 340
 	if err := validateLine(from); err != nil {
322 341
 		return err
323 342
 	}
@@ -326,7 +345,7 @@ func SendMail(addr string, a Auth, heloDomain string, from string, to []string,
326 345
 			return err
327 346
 		}
328 347
 	}
329
-	c, err := Dial(addr)
348
+	c, err := Dial(addr, timeout)
330 349
 	if err != nil {
331 350
 		return err
332 351
 	}

+ 1
- 0
traditional.yaml Целия файл

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

Loading…
Отказ
Запис