-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
286 lines (256 loc) · 7.82 KB
/
main.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package main
import (
"bytes"
"context"
"elasticsearch-gmail-go/mail_utils"
"encoding/json"
"fmt"
"github.com/elastic/go-elasticsearch/v8"
"github.com/elastic/go-elasticsearch/v8/esutil"
"github.com/emersion/go-mbox"
"github.com/gookit/config/v2"
"github.com/gookit/config/v2/toml"
"github.com/gookit/config/v2/yaml"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"io"
"net/mail"
"os"
"strings"
"sync"
"time"
)
// Config Keys
var BatchSize = "batch"
var ThreadSize = "threads"
var MboxPath = "mbox"
var processed = 0
var Index = "index"
var Init = "init"
var Shards = "shards"
// Defaults
var IndexName = "email"
var NumOfShards = 2
type IndexRequest struct {
Settings struct {
NumberOfShards int `json:"number_of_shards"`
NumberOfReplicas int `json:"number_of_replicas"`
} `json:"settings"`
Mappings struct {
Email struct {
Source struct {
Enabled interface{} `json:"enabled"`
} `json:"_source"`
Properties struct {
From struct {
Type string `json:"type"`
Index string `json:"index"`
} `json:"from"`
ReturnPath struct {
Type string `json:"type"`
Index string `json:"index"`
} `json:"return-path"`
DeliveredTo struct {
Type string `json:"type"`
Index string `json:"index"`
} `json:"delivered-to"`
MessageId struct {
Type string `json:"type"`
Index string `json:"index"`
} `json:"message-id"`
To struct {
Type string `json:"type"`
Index string `json:"index"`
} `json:"to"`
DateTs struct {
Type string `json:"type"`
} `json:"date_ts"`
} `json:"properties"`
} `json:"email"`
} `json:"mappings"`
Refresh interface{} `json:"refresh"`
}
type Email struct {
From string `json:"from"`
ReturnPath string `json:"return-path"`
DeliveredTo string `json:"delivered-to"`
MessageId string `json:"message-id"`
To string `json:"to"`
Domain string `json:"domain"`
SecondLevelDomain string `json:"second-level-domain"`
DateTs time.Time `json:"date_ts"`
}
func DeleteIndex(es *elasticsearch.Client) {
log.Info().Msgf("Deleting index %s...", IndexName)
response, err := es.Indices.Delete([]string{IndexName}, es.Indices.Delete.WithIgnoreUnavailable(true))
if err != nil {
log.Err(err).Msgf("Can not delete index %s", IndexName)
} else {
log.Trace().Msg(response.String())
}
log.Info().Msg("... done")
}
func CreateIndex(es *elasticsearch.Client) {
x := fmt.Sprintf(`{
"settings": {
"number_of_shards": %d,
"number_of_replicas": 0
},
"mappings": {
"email": {
"_source": {"enabled": True},
"properties": {
"from": {"type": "string", "index": "not_analyzed"},
"return-path": {"type": "string", "index": "not_analyzed"},
"delivered-to": {"type": "string", "index": "not_analyzed"},
"message-id": {"type": "string", "index": "not_analyzed"},
"to": {"type": "string", "index": "not_analyzed"},
"domain": {"type": "string", "index": "not_analyzed"},
"second-level-domain": {"type": "string", "index": "not_analyzed"},
"date_ts": {"type": "date"},
},
}
},
"refresh": True
}`, config.Int(Shards, 2))
reader := strings.NewReader(x)
create, err := es.Indices.Create(IndexName, es.Indices.Create.WithBody(reader))
if err != nil {
log.Panic().Err(err).Msg("Could not create index")
}
log.Trace().Msgf(create.String())
}
func readMbox(reader io.Reader, ch chan<- []*mail.Message, wg *sync.WaitGroup) {
defer wg.Done()
mr := mbox.NewReader(reader)
batchSize := config.Int(BatchSize)
var done = false
for !done {
var messages = make([]*mail.Message, 0, batchSize)
for idx := 0; idx < config.Int(BatchSize, 10); idx++ {
messageReader, err := mr.NextMessage()
if err != nil {
if err == io.EOF {
log.Info().Msg("Reached end of mbox file")
done = true
break
}
log.Err(err).Msgf("Could not retrieve next message (%d) from mbox", processed+idx)
}
message, err := mail.ReadMessage(messageReader)
if err != nil {
log.Err(err).Msgf("Can not read message")
}
messages = append(messages, message)
}
if len(messages) > 0 {
log.Trace().Msgf("Sending [%d]mail.Message(%p) group to channel", len(messages), &messages)
ch <- messages
processed += len(messages)
}
}
log.Debug().Msgf("Processed %d from %s", processed, "file")
}
func bulkIndex(es *elasticsearch.Client, ch <-chan []*mail.Message, wg *sync.WaitGroup) {
defer wg.Done()
indexer, _ := esutil.NewBulkIndexer(esutil.BulkIndexerConfig{Client: es})
for messages := range ch {
log.Trace().Msgf("Received %d message(s) from channel", len(messages))
for _, message := range messages {
messageId := message.Header.Get("message-id")
fromString := message.Header.Get("from")
from, err := mail_utils.GetEmailAddress(fromString)
if err != nil {
log.Err(err).Str("from", fromString).Msg("Could not get email address")
break
}
domainParts, err := mail_utils.ParseDomains(from.Domain)
if err != nil {
log.Err(err).Str("domain", from.Domain).Msgf("Could not parse domains")
break
}
date, err := message.Header.Date()
if err != nil {
log.Err(err).Str("date", message.Header.Get("date")).Msg("Gould not parse date")
break
}
byts, err := json.Marshal(Email{
From: strings.Join([]string{from.Username, from.Domain}, "@"),
ReturnPath: message.Header.Get("return-path"),
DeliveredTo: message.Header.Get("delivered-to"),
MessageId: messageId,
To: message.Header.Get("to"),
Domain: from.Domain,
SecondLevelDomain: strings.Join((*domainParts)[len(*domainParts)-2:len(*domainParts)], "."),
DateTs: date,
})
if err != nil {
log.Err(err).Msg("Could not marshal json")
}
err = indexer.Add(
context.Background(),
esutil.BulkIndexerItem{
Action: "index",
Index: IndexName,
Body: bytes.NewReader(byts),
})
if err != nil {
log.Err(err).Str("message-id", messageId).Msg("")
}
}
}
log.Debug().Interface("Stats", indexer.Stats()).Msgf("")
indexer.Close(context.Background())
}
func main() {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
zerolog.SetGlobalLevel(zerolog.DebugLevel)
// Config processing
log.Debug().Msg("Parsing environment")
config.WithOptions(config.ParseEnv)
// load flag info
log.Debug().Msgf("Loading flags")
keys := []string{"config", Init + ":bool", MboxPath, ThreadSize, BatchSize}
err := config.LoadFlags(keys)
if err != nil {
log.Panic().Err(err).Msg("Error loading flags")
}
filePath := config.String("config")
if filePath == "" {
filePath = "config.yaml"
}
config.AddDriver(yaml.Driver)
config.AddDriver(toml.Driver)
err = config.LoadFiles(filePath)
if err != nil {
log.Err(err).Msg("Error loading files")
}
IndexName = config.String(Index, IndexName)
NumOfShards = config.Int(Shards, NumOfShards)
// Set up ES
es, _ := elasticsearch.NewDefaultClient()
// Init index
if config.Bool(Init, false) {
DeleteIndex(es)
}
CreateIndex(es)
// Parallel writer setup
var wg sync.WaitGroup
messageChan := make(chan []*mail.Message)
for idx := 0; idx < config.Int(ThreadSize, 1); idx++ {
wg.Add(1)
go bulkIndex(es, messageChan, &wg)
}
// Reader setup
reader, err := os.Open(config.String(MboxPath))
if err != nil {
log.Panic().Err(err).Msgf("Error opening file '%s'", config.String(MboxPath))
}
wg.Add(1)
readMbox(reader, messageChan, &wg)
log.Info().Msg("Closing message channel")
close(messageChan)
log.Info().Msg("Waiting for goroutines to finish")
wg.Wait()
log.Info().Msgf("Processed %d messages, exiting...", processed)
}