-
Notifications
You must be signed in to change notification settings - Fork 13
/
http.go
135 lines (117 loc) · 3.83 KB
/
http.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// ClueGetter - Does things with mail
//
// Copyright 2016 Dolf Schimmel, Freeaqingme.
//
// This Source Code Form is subject to the terms of the two-clause BSD license.
// For its contents, please refer to the LICENSE file.
//
package elasticsearch
import (
"encoding/json"
"net/http"
"strconv"
"cluegetter/address"
"cluegetter/core"
)
func (m *module) HttpHandlers() map[string]core.HttpCallback {
return map[string]core.HttpCallback{
"/es/message/searchEmail/": func(w http.ResponseWriter, r *http.Request) {
m.httpHandlerMessageSearchEmail(w, r)
},
}
}
func (m *module) httpHandlerMessageSearchEmail(w http.ResponseWriter, r *http.Request) {
address := address.FromAddressOrDomain(r.URL.Path[len("/es/message/searchEmail/"):])
instances, err := core.HttpParseFilterInstance(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
sessions, err := m.getSessionsByAddress(instances, address)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
messages := make([]*core.Message, 0)
for _, sess := range sessions {
for _, msg := range sess.Messages {
messages = append(messages, msg)
}
}
// Ideally we'd just pass the messages slice to the view, but to be able
// to put this live more quickly we'll just use the old (legacy) interface
// for now. Also keep in mind API BC.
viewData := struct {
*core.HttpViewData
Messages []*core.HttpMessage
}{
HttpViewData: core.HttpGetViewData(),
Messages: httpHydrateLegacyViewObject(messages),
}
core.HttpRenderOutput(w, r, "messageSearchEmail.html", viewData, viewData.Messages)
}
func httpHydrateLegacyViewObject(messages []*core.Message) []*core.HttpMessage {
out := make([]*core.HttpMessage, 0)
for _, msg := range messages {
sess := msg.Session()
recipients := make([]*core.HttpMessageRecipient, 0)
for _, rcpt := range msg.Rcpt {
recipients = append(recipients, &core.HttpMessageRecipient{
Local: rcpt.Local(),
Domain: rcpt.Local(),
Email: rcpt.String(),
})
}
headers := make([]*core.HttpMessageHeader, 0)
for _, hdr := range msg.Headers {
headers = append(headers, &core.HttpMessageHeader{
Name: hdr.Key,
Body: hdr.Value,
})
}
checkResults := make([]*core.HttpMessageCheckResult, 0)
for _, res := range msg.CheckResults {
determinantStr, _ := json.Marshal(res.Determinants)
checkResults = append(checkResults, &core.HttpMessageCheckResult{
Module: res.Module,
Verdict: core.Proto_Message_Verdict_name[int32(res.SuggestedAction)],
Score: res.Score,
WeightedScore: res.WeightedScore,
Duration: res.Duration.Seconds(),
Determinants: string(determinantStr),
})
}
out = append(out, &core.HttpMessage{
Recipients: recipients,
Headers: headers,
CheckResults: checkResults,
Ip: sess.Ip,
ReverseDns: sess.ReverseDns,
Helo: sess.Helo,
SaslUsername: sess.SaslUsername,
SaslMethod: sess.SaslMethod,
CertIssuer: sess.CertIssuer,
CertSubject: sess.CertSubject,
CipherBits: strconv.Itoa(int(sess.CipherBits)),
Cipher: sess.Cipher,
TlsVersion: sess.TlsVersion,
MtaHostname: sess.MtaHostName,
MtaDaemonName: sess.MtaDaemonName,
Id: msg.QueueId,
SessionId: string(sess.Id()), // TODO Encode?
Date: &msg.Date,
BodySize: uint32(msg.BodySize),
// BodyHash: msg.BodyHash,
Sender: msg.From.String(),
RcptCount: len(msg.Rcpt),
Verdict: core.Proto_Message_Verdict_name[int32(msg.Verdict)],
VerdictMsg: msg.VerdictMsg,
RejectScore: msg.RejectScore,
RejectScoreThreshold: msg.RejectScoreThreshold,
TempfailScore: msg.TempfailScore,
TempfailScoreThreshold: msg.TempfailScoreThreshold,
ScoreCombined: (msg.RejectScore + msg.TempfailScore),
})
}
return out
}