-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.go
216 lines (176 loc) · 3.69 KB
/
server.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
package main
import (
"crypto/tls"
"errors"
"flag"
"log"
"net"
"net/smtp"
"strings"
"time"
"github.com/oliverjanik/scalemail/daemon"
"github.com/oliverjanik/scalemail/emailq"
)
var (
q *emailq.EmailQ
localname string
signal chan struct{}
)
func main() {
flag.StringVar(&localname, "localname", "localhost", "What server sends out as helo greeting")
flag.Parse()
log.Println("Localname:", localname)
// open up persistent queue
var err error
q, err = emailq.New("emails.db")
if err != nil {
log.Panic(err)
}
defer q.Close()
// signals new message just arrived
signal = make(chan struct{}, 1)
// wakes up sending goroutine every minute to check queue and run scheduled messages
t := time.NewTicker(time.Duration(1) * time.Minute)
go sendLoop(t.C)
daemon.HandleFunc(handle)
log.Println("Listening on localhost:587")
daemon.ListenAndServe("localhost:587")
t.Stop()
}
func handle(msg *daemon.Msg) {
for _, m := range group(msg) {
err := q.Push(m)
if err != nil {
log.Print(err)
continue
}
log.Println("Pushing incoming email. Queue length", q.Length())
}
// wake up sender
select {
case signal <- struct{}{}:
default:
}
}
// groups messages by host for easier delivery
func group(msg *daemon.Msg) (messages []*emailq.Msg) {
hostMap := make(map[string][]string)
for _, to := range msg.To {
host := strings.Split(to, "@")[1]
hostMap[host] = append(hostMap[host], to)
}
for k, v := range hostMap {
messages = append(messages, &emailq.Msg{
From: msg.From,
Host: k,
To: v,
Data: msg.Data,
})
}
return messages
}
func sendLoop(tick <-chan time.Time) {
err := q.Recover()
if err != nil {
log.Println("Error recovering:", err)
}
for {
key, msg, err := q.Pop()
if err != nil {
log.Print(err)
}
if key != nil {
go sendMsg(key, msg)
}
// wait for signal or tick
select {
case <-tick:
case <-signal:
}
}
}
func sendMsg(key []byte, msg *emailq.Msg) {
if msg.Retry == 0 {
log.Println("Sending email out to", msg.To)
} else {
log.Printf("Retrying (%v) email out to %v\n", msg.Retry, msg.To)
}
err := send(msg)
if err == nil {
err = q.RemoveDelivered(key)
if err != nil {
log.Println("Error removing delivered:", err)
}
return
}
log.Println("Sending failed, message scheduled for retry:", err)
if msg.Retry == 6 {
log.Println("Maximum retries reached:", msg.To)
err = q.Kill(key)
if err != nil {
log.Println("Error killing msg:", err)
}
return
}
// schedule for retry
err = q.Retry(key)
if err != nil {
log.Println("Error retrying:", err)
}
}
func send(msg *emailq.Msg) error {
mda, err := findMDA(msg.Host)
if err != nil {
return err
}
host := mda[:len(mda)-1]
c, err := smtp.Dial(host + ":25") // remove dot and add port
if err != nil {
return err
}
defer c.Close()
if err = c.Hello(localname); err != nil {
return err
}
// attempt TLS
if ok, _ := c.Extension("STARTTLS"); ok {
config := &tls.Config{
ServerName: host,
InsecureSkipVerify: true,
}
if err = c.StartTLS(config); err != nil {
return err
}
}
if err = c.Mail(msg.From); err != nil {
return err
}
for _, addr := range msg.To {
if err = c.Rcpt(addr); err != nil {
return err
}
}
w, err := c.Data()
if err != nil {
return err
}
if _, err = w.Write(msg.Data); err != nil {
return err
}
if err = w.Close(); err != nil {
return err
}
return c.Quit()
}
// Find Mail Delivery Agent based on DNS MX record
func findMDA(host string) (string, error) {
results, err := net.LookupMX(host)
if err != nil {
return "", err
}
if len(results) == 0 {
return "", errors.New("No MX records found")
}
// todo: support for multiple MX records
return results[0].Host, nil
}