-
Notifications
You must be signed in to change notification settings - Fork 1
/
smtp.go
63 lines (59 loc) · 1.24 KB
/
smtp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package certexpire
import (
"net"
"os"
"strings"
"time"
)
func smtpHasStartTLS(s []string) bool {
for _, l := range s {
if strings.Index(l, "STARTTLS") >= 0 {
return true
}
}
return false
}
func smtpprelude(conn net.Conn) error {
r := NewLineReader(conn)
c, _, err := r.ReadNumericContinuous()
if c != "220" {
return ErrProtocol
}
hostname, err := os.Hostname()
if err != nil {
hostname = "local"
}
_, err = conn.Write([]byte("EHLO " + hostname + "\r\n"))
if err != nil {
return err
}
c, m, err := r.ReadNumericContinuous()
if c != "250" {
return ErrProtocol
}
if !smtpHasStartTLS(m) {
return ErrProtocol
}
_, err = conn.Write([]byte("STARTTLS\r\n"))
if err != nil {
return err
}
c, _, err = r.ReadNumericContinuous()
if c != "220" {
return ErrProtocol
}
return nil
}
// GetCertSMTP returns the expiration date of an SMTP STARTTLS cert.
func GetCertSMTP(servername, port string, timeout time.Duration, proxy *Proxy) (*CertValues, error) {
conn, err := TCPDailer(servername+":"+port, proxy, timeout)
if err != nil {
return nil, err
}
conn.SetDeadline(deadline(timeout))
defer conn.Close()
if err := smtpprelude(conn); err != nil {
return nil, err
}
return GetCertificate(conn, servername, timeout)
}