-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransaction_headers.go
67 lines (62 loc) · 1.62 KB
/
transaction_headers.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
64
65
66
67
package msmtpd
import (
"crypto/tls"
"fmt"
"net"
"time"
)
/*
* Header manipulation
*/
// AddHeader adds header to the Transaction.Parsed, and to the Transaction.Body, so,
// it should be called before AddReceivedLine, since it adds header to the top
func (t *Transaction) AddHeader(name, value string) {
t.LogDebug("Adding header `%s: %s`", name, value)
line := wrap([]byte(fmt.Sprintf("%s: %s\r\n", name, value)))
t.Body = append(t.Body, line...)
// Move the new newly added header line up front
copy(t.Body[len(line):], t.Body[0:len(t.Body)-len(line)])
copy(t.Body, line)
if t.Parsed != nil {
// add header to parsed body
_, found := t.Parsed.Header[name]
if found {
t.Parsed.Header[name] = append(t.Parsed.Header[name], value)
} else {
t.Parsed.Header[name] = []string{value}
}
}
}
// AddReceivedLine prepends a Received header to the Transaction.Body
func (t *Transaction) AddReceivedLine() {
tlsDetails := ""
if t.TLS != nil {
version := "unknown"
if val, ok := TLSVersions[t.TLS.Version]; ok {
version = val
}
cipher := tls.CipherSuiteName(t.TLS.CipherSuite)
tlsDetails = fmt.Sprintf(
"\r\n\t(version=%s cipher=%s);",
version,
cipher,
)
}
peerIP := ""
if addr, ok := t.Addr.(*net.TCPAddr); ok {
peerIP = addr.IP.String()
}
line := wrap([]byte(fmt.Sprintf(
"Received: from %s ([%s]) by %s with %s;%s\r\n\t%s\r\n",
t.HeloName,
peerIP,
t.ServerName,
t.Protocol,
tlsDetails,
time.Now().Format(timeFormatForHeaders),
)))
t.Body = append(t.Body, line...)
// Move the new Received line up front
copy(t.Body[len(line):], t.Body[0:len(t.Body)-len(line)])
copy(t.Body, line)
}