-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
386 lines (318 loc) · 9.17 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package main
import (
"bytes"
"crypto/tls"
"crypto/x509"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/emersion/go-sasl"
"github.com/emersion/go-smtp"
"golang.org/x/exp/maps"
)
var TERMINATED string
type Credential struct {
Password string `json:"password"`
AllowedDomains []string `json:"allowedDomains"`
}
type ServerConfig struct {
Host string `json:"host" default:"localhost"`
Listen int `json:"listen" default:"2525"`
StartTLS bool `json:"startTls" default:"false"`
SMTPS bool `json:"smtps" default:"false"`
ListenSMTPS int `json:"listenSmtps" default:"4650"`
TLSCert string `json:"tlsCert" default:""`
TLSKey string `json:"tlsKey" default:""`
AllowInsecure bool `json:"allowInsecure" default:"true"`
ReadTimeout time.Duration `json:"readTimeout" default:"10"`
WriteTimeout time.Duration `json:"writeTimeout" default:"10"`
MaxRecipients int `json:"maxRecipients" default:"50"`
MaxMessageSizeMb int `json:"maxMessageSizeMb" default:"30"`
}
type RemoteConfig struct {
Host string `json:"host" default:"localhost"`
Port int `json:"port" default:"2525"`
StartTls bool `json:"startTls" default:"false"`
AuthPlain bool `json:"authPlain" default:"false"`
AuthLogin bool `json:"authLogin" default:"false"`
Username string `json:"username" default:""`
Password string `json:"password" default:""`
TlsSkipVerify bool `json:"tlsSkipVerify" default:"false"`
}
type Config struct {
Server ServerConfig `json:"server"`
Remote RemoteConfig `json:"remote"`
Credentials map[string]Credential `json:"credentials"`
}
// The Backend implements SMTP server methods.
type RelayBackend struct{}
type Mail struct {
Credential Credential
From string
To []string
Data []byte
}
type Remote struct {
Config RemoteConfig
}
var remote Remote
var config *Config
func (bkd *RelayBackend) NewSession(_ *smtp.Conn) (smtp.Session, error) {
log.Println("Session started")
return &Session{Anonymous: true}, nil
}
type Session struct {
Anonymous bool
RelayMessage Mail
}
func (s *Session) SendMail() error {
c, err := smtp.Dial(remote.Config.Host + ":" + fmt.Sprint(remote.Config.Port))
if err != nil {
return err
}
defer c.Close()
reader := bytes.NewReader(s.RelayMessage.Data)
if remote.Config.StartTls {
var tlsc *tls.Config
if remote.Config.TlsSkipVerify {
tlsc = &tls.Config{
InsecureSkipVerify: true,
}
}
if err := c.StartTLS(tlsc); err != nil {
return err
}
}
if remote.Config.AuthPlain {
auth := sasl.NewPlainClient("", remote.Config.Username, remote.Config.Password)
if err := c.Auth(auth); err != nil {
return err
}
} else if remote.Config.AuthLogin {
auth := sasl.NewLoginClient(remote.Config.Username, remote.Config.Password)
if err := c.Auth(auth); err != nil {
return err
}
}
log.Println("Mail from", s.RelayMessage.From, "to", s.RelayMessage.To)
err = c.SendMail(s.RelayMessage.From, s.RelayMessage.To, reader)
s.RelayMessage = Mail{}
if err != nil {
log.Println("Sending email failed: ", err.Error())
return err
}
log.Println("Sent successfully")
return nil
}
func (s *Session) AuthPlain(username, password string) error {
fmt.Println(config.Credentials)
val, ok := config.Credentials[username]
if ok && val.Password == password {
log.Println(username, "authenticated successfully")
s.Anonymous = false
s.RelayMessage.Credential = val
return nil
}
log.Println("User", username, "authenticated failed")
return errors.New("invalid username or password")
}
func sliceContains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
func (s *Session) Mail(from string, opts *smtp.MailOptions) error {
if s.Anonymous {
return errors.New("anonymous users are not allowed to send mail")
}
// log.Println("sending mail from:", from)
if len(s.RelayMessage.Credential.AllowedDomains) > 0 {
splt := strings.Split(from, "@")
if !sliceContains(s.RelayMessage.Credential.AllowedDomains, splt[1]) {
allowstr := strings.Join(s.RelayMessage.Credential.AllowedDomains, ", ")
return fmt.Errorf("invalid sender domain: %s not allowed (%s)", splt[1], allowstr)
}
}
s.RelayMessage.From = from
return nil
}
func (s *Session) Rcpt(to string) error {
s.RelayMessage.To = append(s.RelayMessage.To, to)
return nil
}
func (s *Session) Data(r io.Reader) error {
if b, err := io.ReadAll(r); err != nil {
return err
} else {
s.RelayMessage.Data = b
}
err := s.SendMail()
if err != nil {
return errors.New("sending email failed: " + err.Error())
}
return nil
}
func (s *Session) Reset() {
// log.Println("resetting message, preparing for next")
s.RelayMessage = Mail{}
}
func (s *Session) Logout() error {
// log.Println("session ended, closing")
s = &Session{}
return nil
}
func Listen(s *smtp.Server) {
log.Println("Starting server at", s.Addr)
if err := s.ListenAndServe(); err != nil {
log.Fatal(err)
panic(err)
}
}
func ListemSmtps(tlss *smtp.Server) {
log.Println("Starting TLS server at ", tlss.Addr)
if err := tlss.ListenAndServeTLS(); err != nil {
log.Fatal(err)
panic(err)
}
}
func ListenHealthcheck(cfg *Config) {
updateToken := os.Getenv("X_UPDATE_TOKEN")
http.ListenAndServe(":8080", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "accounts.json" && r.Method != "POST" {
w.WriteHeader(200)
w.Write([]byte("OK"))
return
}
if updateToken == "" {
w.WriteHeader(500)
w.Write([]byte("X_UPDATE_TOKEN not set"))
return
}
headerToken := r.Header.Get("X-Update-Token")
if headerToken == "" || headerToken != updateToken {
w.WriteHeader(403)
w.Write([]byte("Invalid token"))
return
}
var updatedAccounts map[string]Credential
err := json.NewDecoder(r.Body).Decode(&updatedAccounts)
if err != nil {
fmt.Println("Failed updating accounts")
fmt.Println(err.Error())
w.WriteHeader(500)
w.Write([]byte("Failed updating accounts"))
return
}
cfg.Credentials = updatedAccounts
fmt.Println("Updated accounts: ", maps.Keys(cfg.Credentials))
})) // nolint:errcheck
}
func main() {
TERMINATED = ""
// Create config
config = &Config{}
credentials := &map[string]Credential{}
// Read config.json into temp string
configFile, err := os.ReadFile("config.json")
if err != nil {
log.Fatal(err)
TERMINATED = err.Error()
}
credentialFile, err := os.ReadFile("credentials.json")
if err != nil {
log.Fatal(err)
TERMINATED = err.Error()
}
// Unmarshal config
json.Unmarshal(configFile, &config)
json.Unmarshal(credentialFile, &credentials)
config.Credentials = *credentials
be := &RelayBackend{}
remote = Remote{
Config: config.Remote,
}
smtps := smtp.NewServer(be)
var smtpss *smtp.Server
var tlsc *tls.Config
if config.Server.SMTPS || config.Server.StartTLS {
log.Println("StartTLS/TLS enabled, checking certificates...")
tlsCert, err := tls.LoadX509KeyPair(
config.Server.TLSCert,
config.Server.TLSKey,
)
if err != nil {
log.Fatal(err)
}
certInfo, err := x509.ParseCertificate(tlsCert.Certificate[0])
if err != nil {
log.Fatal(err)
}
log.Println("Certificate hostnames: ", certInfo.DNSNames)
tlsc = &tls.Config{
ServerName: config.Server.Host,
Certificates: []tls.Certificate{
tlsCert,
},
NameToCertificate: map[string]*tls.Certificate{
config.Server.Host: &tlsCert,
},
}
}
smtps.Addr = ":" + fmt.Sprint(config.Server.Listen)
smtps.Domain = config.Server.Host
smtps.ReadTimeout = config.Server.ReadTimeout * time.Second
smtps.WriteTimeout = config.Server.WriteTimeout * time.Second
smtps.MaxMessageBytes = config.Server.MaxMessageSizeMb * 1024 * 1024
smtps.MaxRecipients = config.Server.MaxRecipients
smtps.AllowInsecureAuth = config.Server.AllowInsecure
smtps.AuthDisabled = false
smtps.TLSConfig = tlsc
smtps.EnableAuth(sasl.Login,
func(conn *smtp.Conn) sasl.Server {
return sasl.NewLoginServer(func(username, password string) error {
sess := conn.Session()
if sess == nil {
panic("No session when AUTH is called")
}
return sess.AuthPlain(username, password)
})
},
)
if config.Server.SMTPS {
smtpss = smtp.NewServer(be)
smtpss.Addr = ":" + fmt.Sprint(config.Server.ListenSMTPS)
smtpss.Domain = config.Server.Host
smtpss.ReadTimeout = config.Server.ReadTimeout * time.Second
smtpss.WriteTimeout = config.Server.WriteTimeout * time.Second
smtpss.MaxMessageBytes = config.Server.MaxMessageSizeMb * 1024 * 1024
smtpss.MaxRecipients = config.Server.MaxRecipients
smtpss.AllowInsecureAuth = config.Server.AllowInsecure
smtpss.AuthDisabled = false
smtpss.TLSConfig = tlsc
smtpss.EnableAuth(sasl.Login,
func(conn *smtp.Conn) sasl.Server {
return sasl.NewLoginServer(func(username, password string) error {
sess := conn.Session()
if sess == nil {
panic("No session when AUTH is called")
}
return sess.AuthPlain(username, password)
})
},
)
}
if config.Server.SMTPS {
go ListemSmtps(smtpss)
}
go Listen(smtps)
ListenHealthcheck(config)
}