-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsmtp.go
683 lines (661 loc) · 21.7 KB
/
smtp.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
package main
import (
"crypto/sha256"
"crypto/tls"
"encoding/base64"
"errors"
"io"
"log"
"net"
"os"
"regexp"
"sort"
"strconv"
"strings"
"time"
)
func stmpSendHandshake(targetAddress []string, targetDomain string, internalAddress string) (*connStruct, error) { //连接邮件服务器
mxRecords, err := net.LookupMX(targetDomain) //查询mx记录
if err != nil {
return nil, errors.New("cannot lookup MX records")
}
sort.Slice(mxRecords, func(i, j int) bool { return mxRecords[i].Pref < mxRecords[j].Pref }) //按照pref从小到大排序
conn := new(connStruct)
var supportStartTls bool = false
dialAddr, _ := net.ResolveTCPAddr("tcp", config.Smtp.Inbound.PlainListenAddress)
dialer := net.Dialer{LocalAddr: dialAddr, Timeout: time.Millisecond*time.Duration(config.Smtp.Outbound.RemoteConnectTimeoutMs)}
for _, mxRecord := range mxRecords { //尝试连接25端口
for i := 0; i < config.Smtp.Outbound.RemoteConnectRetryTimes; i++ {
plainConn, err := dialer.Dial("tcp", mxRecord.Host+":25")
if err == nil {
conn.plainConn = plainConn
conn.connType = 0x00
goto connected
}
}
}
return nil, errors.New("cannot connected to remote smtp server")
connected: //下面是握手流程
ret, err := ConnReadLine(conn)
if err != nil {
conn.Close()
return nil, errors.New("network error")
}
if string(ret[:3]) != "220" {
conn.Close()
return nil, errors.New("connect failed: " + string(ret[:len(ret)-2]))
}
conn.Write([]byte("EHLO " + config.General.ServerAddress + "\r\n"))
for {
ret, err = ConnReadLine(conn)
if err != nil {
conn.Close()
return nil, errors.New("network error")
}
if string(ret[:3]) != "250" {
conn.Close()
return nil, errors.New("EHLO failed: " + string(ret[:len(ret)-2]))
}
if len(ret) == 14 && string(ret)[4:12] == "STARTTLS" {
supportStartTls = true
}
if ret[3] == ' ' {
break
}
}
if conn.connType == 0x00 && supportStartTls {
for i := 0; i < 5; i++ {
conn.Write([]byte("STARTTLS\r\n"))
ret, err = ConnReadLine(conn)
if err != nil {
conn.Close()
return nil, errors.New("network error")
}
if string(ret[:3]) == "454" {
time.Sleep(time.Millisecond * 10)
continue
}
conn.tlsConn = tls.Client(conn.plainConn, &tls.Config{InsecureSkipVerify: true})
conn.connType = 0x01
conn.Write([]byte("EHLO " + config.General.ServerAddress + "\r\n"))
for {
ret, err = ConnReadLine(conn)
if err != nil {
conn.Close()
return nil, errors.New("network error")
}
if string(ret[:3]) != "250" {
conn.Close()
return nil, errors.New("EHLO failed: " + string(ret[:len(ret)-2]))
}
if ret[3] == ' ' {
break
}
}
break
}
}
conn.Write([]byte("MAIL FROM:<" + internalAddress + ">\r\n"))
ret, err = ConnReadLine(conn)
if err != nil {
conn.Close()
return nil, errors.New("network error")
}
if string(ret[:3]) != "250" {
conn.Close()
return nil, errors.New("MAIL FROM failed: " + string(ret[:len(ret)-2]))
}
for _, addr := range targetAddress {
conn.Write([]byte("RCPT TO:<" + addr + ">\r\n"))
ret, err = ConnReadLine(conn)
if err != nil {
conn.Close()
return nil, errors.New("network error")
}
if string(ret[:3]) != "250" {
conn.Close()
return nil, errors.New("RCPT TO failed: " + string(ret[:len(ret)-2]))
}
}
conn.Write([]byte("DATA\r\n"))
ret, err = ConnReadLine(conn)
if err != nil {
conn.Close()
return nil, errors.New("network error")
}
if string(ret[:3]) != "354" {
conn.Close()
return nil, errors.New("DATA failed: " + string(ret[:len(ret)-2]))
}
return conn, nil
}
func stmpEndBody(conn *connStruct) error { //结束邮件发送
conn.Write([]byte(".\r\n"))
ret, err := ConnReadLine(conn)
if err != nil {
return errors.New("network error")
}
if string(ret[:3]) != "250" {
return errors.New("DATA failed: " + string(ret[:len(ret)-2]))
}
conn.Write([]byte("QUIT\r\n"))
ret, err = ConnReadLine(conn)
if err != nil {
return errors.New("network error")
}
if string(ret[:3]) != "221" {
return errors.New("QUIT failed: " + string(ret[:len(ret)-2]))
}
conn.Close()
return nil
}
func smtpHandleSendFalure(fromMail string, toMail []string, failureDomains map[string]error) { //退信通知
cacheFilePath := generateCacheFilePath()
f, err := os.OpenFile(cacheFilePath, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return
}
f.Write([]byte("Subject: Mail can't be delivered\r\nFrom: simpmailserv\r\nTo: " + fromMail + "\r\n\r\n"))
for k, v := range failureDomains {
f.Write([]byte(k + ": " + v.Error() + "\r\n"))
}
f.Close()
os.Rename(cacheFilePath, getMailStoragePath(fromMail))
}
func smtpMailSendHandler(fromMail string, toMail []string, cacheFilePath string, dkimHeader string) { //发送被缓存的邮件
cacheFile, _ := os.Open(cacheFilePath)
domainAddressMap := make(map[string][]string)
connMap := make(map[string]*connStruct)
failureDomains := make(map[string]error)
var err error
var readData []byte
var toInternalCachePathList []string
var toInternalStoragePathList []string
for _, targetAddress := range toMail { //获取每个邮箱地址对应的服务器地址(同时对回到本机的邮件做特判处理)
targetDomain := strings.Split(targetAddress, "@")[1]
if targetDomain == config.General.MailDomain {
if failureDomains[targetDomain] != nil {
continue
}
toInternalCachePathList = append(toInternalCachePathList, generateCacheFilePath())
_, err = copyFile(cacheFilePath, toInternalCachePathList[len(toInternalCachePathList)-1])
if err != nil {
failureDomains[targetDomain] = errors.New("DATA failed: 431 The Recipient's Mail Server Is Experiencing a Disk Full Condition")
for _, cacheFilePath := range toInternalCachePathList {
os.Remove(cacheFilePath)
}
toInternalStoragePathList = []string{}
}
toInternalStoragePathList = append(toInternalStoragePathList, getMailStoragePath(targetAddress))
} else {
domainAddressMap[targetDomain] = append(domainAddressMap[targetDomain], targetAddress)
}
}
for i := 0; i < len(toInternalStoragePathList); i++ { //照样是回到本机的特判
os.Rename(toInternalCachePathList[i], toInternalStoragePathList[i])
}
for targetDomain, targetAddress := range domainAddressMap { //连接每个邮件服务器并握手(获取conn)
targetConn, err := stmpSendHandshake(targetAddress, targetDomain, fromMail)
if err != nil {
failureDomains[targetDomain] = err
delete(connMap, targetDomain)
continue
}
connMap[targetDomain] = targetConn
}
if config.Smtp.Outbound.EnableDkim { //启用DKIM的话就先向每个服务器发送DKIM的头
for targetDomain, targetConn := range connMap {
_, err = targetConn.Write([]byte(dkimHeader))
if err != nil {
failureDomains[targetDomain] = err
targetConn.Close()
delete(connMap, targetDomain)
}
}
}
for { //读一行发一行
readData, err = FileReadLine(cacheFile)
if err != nil {
if err == io.EOF { //读到没
break
}
cacheFile.Close()
os.Remove(cacheFilePath)
}
for targetDomain, targetConn := range connMap {
_, err = targetConn.Write(readData)
if err != nil {
failureDomains[targetDomain] = err
targetConn.Close()
delete(connMap, targetDomain)
}
}
}
cacheFile.Close()
os.Remove(cacheFilePath) //删除cache文件
for targetDomain, targetConn := range connMap { //逐个服务器关闭连接
err = stmpEndBody(targetConn)
if err != nil {
failureDomains[targetDomain] = err
targetConn.Close()
delete(connMap, targetDomain)
}
}
if len(failureDomains) != 0 { //如果有发送失败的就进入发送失败处理流程
smtpHandleSendFalure(fromMail, toMail, failureDomains)
}
}
func smtpClientHandler(plainConn net.Conn, enableStartTls bool, startTlsConfig *tls.Config) { //处理客户端连接
conn := &connStruct{tlsConn: nil, plainConn: plainConn, connType: 0x00}
conn.Write([]byte("220 " + config.General.ServerAddress + " simpmailserv\r\n"))
var hostName string
var authenticatedUsername string
var fromMail string
var toMail []string
var isSend = false //默认接收模式
for {
data, err := ConnReadLine(conn)
if err != nil {
conn.Close()
return
}
data = data[:len(data)-2]
dataSplit := strings.Split(string(data), " ")
command := strings.ToLower(dataSplit[0])
switch command {
case "helo": //获取对端客户端主机名
if len(dataSplit) == 1 {
conn.Write([]byte("500 Error: bad syntax\r\n"))
continue
}
hostName = dataSplit[1]
conn.Write([]byte("250 OK\r\n"))
case "ehlo": //获取对端客户端主机名/返回功能列表
if len(dataSplit) == 1 {
conn.Write([]byte("500 Error: bad syntax\r\n"))
continue
}
hostName = dataSplit[1]
if enableStartTls {
conn.Write([]byte("250-mail\r\n250-AUTH LOGIN\r\n250-AUTH=LOGIN\r\n250-ID\r\n250-STARTTLS\r\n250 8BITMIME\r\n"))
} else {
conn.Write([]byte("250-mail\r\n250-AUTH LOGIN\r\n250-AUTH=LOGIN\r\n250-ID\r\n250 8BITMIME\r\n"))
}
case "noop": //emmm就是啥也不干
conn.Write([]byte("250 OK\r\n"))
case "rset": //重置发件邮箱和收件邮箱
conn.Write([]byte("250 OK\r\n"))
fromMail = ""
toMail = []string{}
case "starttls": //升级到TLS
if !enableStartTls {
conn.Write([]byte("502 Error: command not implemented\r\n"))
continue
}
conn.Write([]byte("220 Go ahead\r\n"))
tlsConn := tls.Server(conn.plainConn, startTlsConfig)
conn.tlsConn = tlsConn
conn.connType = 0x01
case "quit": //断开连接
conn.Write([]byte("221 Bye"))
conn.Close()
return
case "auth": //鉴权
if hostName == "" {
conn.Write([]byte("503 Error: send HELO/EHLO first\r\n"))
continue
}
if len(dataSplit) == 1 {
conn.Write([]byte("500 Error: bad syntax\r\n"))
continue
}
switch strings.ToLower(dataSplit[1]) {
case "login": //login方式鉴权
conn.Write([]byte("334 VXNlcm5hbWU6\r\n"))
usernameBase64, err := ConnReadLine(conn)
if err != nil {
conn.Close()
return
}
usernameBase64 = usernameBase64[:len(usernameBase64)-2]
conn.Write([]byte("334 UGFzc3dvcmQ6\r\n"))
passwordBase64, err := ConnReadLine(conn)
if err != nil {
conn.Close()
return
}
passwordBase64 = passwordBase64[:len(passwordBase64)-2]
usernameBytes, err := base64.StdEncoding.DecodeString(string(usernameBase64))
if err != nil {
conn.Write([]byte("535 Error: authentication failed\r\n"))
continue
}
username := string(usernameBytes)
passwordBytes, err := base64.StdEncoding.DecodeString(string(passwordBase64))
if err != nil {
conn.Write([]byte("535 Error: authentication failed\r\n"))
continue
}
password := string(passwordBytes)
if clientAuth(username, password) {
conn.Write([]byte("235 Authentication successful\r\n"))
} else {
conn.Write([]byte("535 Error: authentication failed\r\n"))
}
authenticatedUsername = username
default:
conn.Write([]byte("504 Unrecognized authentication type\r\n"))
}
case "mail": //来件地址
if hostName == "" {
conn.Write([]byte("503 Error: send HELO/EHLO first\r\n"))
continue
}
if len(dataSplit) < 2 {
conn.Write([]byte("503 bad sequence of commands\r\n"))
continue
}
from := strings.Join(dataSplit[1:], "")
fromSplit := strings.Split(from, ":")
if len(fromSplit) < 2 {
conn.Write([]byte("503 bad sequence of commands\r\n"))
continue
}
if strings.ToLower(fromSplit[0]) != "from" {
conn.Write([]byte("503 bad sequence of commands\r\n"))
continue
}
mailSplitLeft := strings.Split(fromSplit[1], "<")
if len(mailSplitLeft) < 2 {
conn.Write([]byte("503 bad sequence of commands\r\n"))
continue
}
mailSplitRight := strings.Split(mailSplitLeft[1], ">")
mailSplit := strings.Split(mailSplitRight[0], "@")
if len(mailSplit) == 1 {
conn.Write([]byte("550 Invalid User\r\n"))
continue
}
if mailSplit[1] != config.General.MailDomain { //如果不是本机邮箱域名就设置为接收模式
isSend = false
} else { //如果是本机邮箱域名就设置为发送模式
if authenticatedUsername != "" { //没有鉴权过就拒绝
if !smtpAddressClientAuth(authenticatedUsername, mailSplitRight[0]) {
conn.Write([]byte("553 Mail from must equal authorized user\r\n"))
continue
}
} else {
conn.Write([]byte("553 authentication is required\r\n"))
continue
}
isSend = true
}
fromMail = mailSplitRight[0]
conn.Write([]byte("250 Mail OK\r\n"))
case "rcpt": //接收地址
if hostName == "" {
conn.Write([]byte("503 Error: send HELO/EHLO first\r\n"))
continue
}
if len(dataSplit) < 2 {
conn.Write([]byte("503 bad sequence of commands\r\n"))
continue
}
to := strings.Join(dataSplit[1:], "")
toSplit := strings.Split(to, ":")
if len(toSplit) < 2 {
conn.Write([]byte("503 bad sequence of commands\r\n"))
continue
}
if strings.ToLower(toSplit[0]) != "to" {
conn.Write([]byte("503 bad sequence of commands\r\n"))
continue
}
mailSplitLeft := strings.Split(toSplit[1], "<")
if len(mailSplitLeft) < 2 {
conn.Write([]byte("503 bad sequence of commands\r\n"))
continue
}
mailSplitRight := strings.Split(mailSplitLeft[1], ">")
mailSplit := strings.Split(mailSplitRight[0], "@")
if len(mailSplit) == 1 {
conn.Write([]byte("550 Invalid User\r\n"))
continue
}
if mailSplit[1] == config.General.MailDomain { //如果是本机的域名的话就查找本地是否存在这个地址
if !smtpCheckAddressExists(mailSplitRight[0]) {
conn.Write([]byte("550 User not found: " + mailSplitRight[0] + "\r\n"))
continue
}
} else { //不是本机域名且是不发送模式的话拒绝
if !isSend {
conn.Write([]byte("550 Invalid User\r\n"))
continue
}
}
toMail = append(toMail, mailSplitRight[0])
conn.Write([]byte("250 Mail OK\r\n"))
case "data": //开始处理邮件
if hostName == "" {
conn.Write([]byte("503 Error: send HELO/EHLO first\r\n"))
continue
}
if fromMail == "" || len(toMail) == 0 {
conn.Write([]byte("503 bad sequence of commands\r\n"))
continue
}
conn.Write([]byte("354 End data with <CR><LF>.<CR><LF>\r\n"))
dkimBodyHash := sha256.New()
var headerList []string
var keepedHeaderList []string
var toKeepHeaders []string
if !isSend { //接收模式就写到对应的文件中就行
var recvData []byte
var storagePathList []string
var tempRecvPathList []string
var tempRecvFileList []*os.File
var err error
var endHead bool = false
var writeError bool = false
for i := 0; i < len(toMail); i++ {
storagePathList = append(storagePathList, getMailStoragePath(toMail[i]))
}
for i := 0; i < len(toMail); i++ {
tempRecvPathList = append(tempRecvPathList, generateCacheFilePath())
}
for i := 0; i < len(tempRecvPathList); i++ {
f, err := os.OpenFile(tempRecvPathList[i], os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
conn.Write([]byte("431 The Recipient's Mail Server Is Experiencing a Disk Full Condition\r\n"))
writeError = true
goto endInternalSave
}
tempRecvFileList = append(tempRecvFileList, f)
}
for {
recvData, err = ConnReadLine(conn)
if err != nil {
conn.Close()
return
}
if string(recvData) == ".\r\n" {
break
}
if string(recvData) == "\r\n" && !endHead {
endHead = true
t := time.Now().UTC()
for i := 0; i < len(tempRecvFileList); i++ {
_, err = tempRecvFileList[i].Write([]byte("Date: " + t.Weekday().String()[:3] + ", " + strconv.Itoa(t.Day()) + " " + t.Month().String()[:3] + " " + strconv.Itoa(t.Year()) + " " + strconv.Itoa(t.Hour()) + ":" + strconv.Itoa(t.Minute()) + ":" + strconv.Itoa(t.Second()) + " +0000 (CST)\r\nSender: " + fromMail + "\r\n\r\n"))
if err != nil {
conn.Write([]byte("431 The Recipient's Mail Server Is Experiencing a Disk Full Condition\r\n"))
writeError = true
goto endInternalSave
}
}
continue
}
for i := 0; i < len(tempRecvFileList); i++ {
_, err := tempRecvFileList[i].Write(recvData)
if err != nil {
conn.Write([]byte("431 The Recipient's Mail Server Is Experiencing a Disk Full Condition\r\n"))
writeError = true
goto endInternalSave
}
}
}
endInternalSave:
for i := 0; i < len(tempRecvFileList); i++ {
tempRecvFileList[i].Close()
}
if writeError {
for i := 0; i < len(tempRecvFileList); i++ {
os.Remove(tempRecvPathList[i])
}
continue
}
for i := 0; i < len(tempRecvFileList); i++ {
os.Rename(tempRecvPathList[i], storagePathList[i])
}
conn.Write([]byte("250 Mail OK\r\n"))
} else { //发送模式先把邮件存到一个临时文件中(如果启用了DKIM就同时计算hash)然后转交给发送程序处理
var recvData []byte
var hashBuffer string
var err error
var endHead bool = false
var writeError bool = false
var startBody bool = false
rxReduceWS := regexp.MustCompile(`[ \t]+`)
tempRecvPath := generateCacheFilePath()
tempRecvFile, err := os.OpenFile(tempRecvPath, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
conn.Write([]byte("431 The Recipient's Mail Server Is Experiencing a Disk Full Condition\r\n"))
writeError = true
goto endSendSave
}
for {
recvData, err = ConnReadLine(conn)
if err != nil {
conn.Close()
return
}
if string(recvData) == ".\r\n" {
if config.Smtp.Outbound.EnableDkim {
if !startBody {
dkimBodyHash.Write([]byte("\r\n"))
}
}
break
}
if string(recvData) == "\r\n" && !endHead { //结束头部就把头部列表给规范化了
endHead = true
_, err := tempRecvFile.Write(recvData)
if err != nil {
conn.Write([]byte("431 The Recipient's Mail Server Is Experiencing a Disk Full Condition\r\n"))
writeError = true
goto endSendSave
}
if config.Smtp.Outbound.EnableDkim {
headerList[len(headerList)-1] = strings.TrimRight(headerList[len(headerList)-1], "\r\n")
for _, header := range headerList {
headerSplit := strings.Split(header, ":")
if len(headerSplit) < 2 {
continue
}
toKeepHeaders = append(toKeepHeaders, strings.ToLower(headerSplit[0]))
keepedHeaderList = append(keepedHeaderList, header)
}
keepedHeaderList = canonicalizeHeaderList(keepedHeaderList)
}
continue
}
if config.Smtp.Outbound.EnableDkim { //结束了头部就规范化然后加入hash, 没结束就加入头部列表
if !endHead {
if recvData[0] == 32 || recvData[0] == 9 {
headerList[len(headerList)-1] += string(recvData)
} else {
if len(headerList) >= 1 {
headerList[len(headerList)-1] = strings.TrimRight(headerList[len(headerList)-1], "\r\n")
}
headerList = append(headerList, string(recvData))
}
} else {
line := strings.TrimRight(string(recvData), "\r\n")
line += "\r\n"
line = rxReduceWS.ReplaceAllString(line, " ")
if line == "\r\n" {
hashBuffer += "\r\n"
} else {
dkimBodyHash.Write([]byte(line + hashBuffer))
hashBuffer = ""
}
}
}
if endHead && !startBody {
startBody = true
}
_, err := tempRecvFile.Write(recvData)
if err != nil {
conn.Write([]byte("431 The Recipient's Mail Server Is Experiencing a Disk Full Condition\r\n"))
writeError = true
goto endSendSave
}
}
endSendSave:
tempRecvFile.Close()
if writeError {
os.Remove(tempRecvPath)
continue
}
var dkimHeader string
if config.Smtp.Outbound.EnableDkim { //计算DKIM头部
dkimBaseHeader := generateDkimBaseHeader(base64.StdEncoding.EncodeToString(dkimBodyHash.Sum(nil)), config.Smtp.Outbound.DkimDomain, config.Smtp.Outbound.DkimSelector, toKeepHeaders, smtpDkimPrivateKey)
dkimHeader = generateDkimFullHeaderWithSign(keepedHeaderList, dkimBaseHeader, smtpDkimPrivateKey)
}
conn.Write([]byte("250 Mail OK\r\n"))
go smtpMailSendHandler(fromMail, toMail, tempRecvPath, dkimHeader) //发送~
}
fromMail = ""
toMail = []string{}
default:
conn.Write([]byte("502 Error: command not implemented\r\n"))
}
}
}
func smtpClientListenHandler(listener net.Listener, enableStartTls bool, startTlsConfig *tls.Config) { //监听
for !serverStop {
conn, err := listener.Accept()
if err != nil {
log.Println("Error: smtp listen error: " + err.Error())
}
go smtpClientHandler(conn, enableStartTls, startTlsConfig)
}
}
func smtpServer() { //启用smtp服务
if config.Smtp.Inbound.EnablePlain {
listener, err := net.Listen("tcp", config.Smtp.Inbound.PlainListenAddress+":"+strconv.Itoa(config.Smtp.Inbound.PlainListenPort))
if err != nil {
log.Println("Error: start smtp server error: " + err.Error())
goto next
}
log.Println("Info: start smtp server at: " + listener.Addr().String())
if config.Smtp.Inbound.PlainEnableStartTls {
log.Println("Info: smtp STARTTLS enabled")
go smtpClientListenHandler(listener, true, &tls.Config{Certificates: []tls.Certificate{smtpStartTlsCert}})
} else {
go smtpClientListenHandler(listener, false, nil)
}
}
next:
if config.Smtp.Inbound.EnableTls {
listener, err := tls.Listen("tcp", config.Smtp.Inbound.TlsListenAddress+":"+strconv.Itoa(config.Smtp.Inbound.TlsListenPort), &tls.Config{Certificates: []tls.Certificate{smtpTlsCert}})
if err != nil {
log.Println("Error: start smtp tls server error: " + err.Error())
return
}
log.Println("Info: start smtp tls server at: " + listener.Addr().String())
go smtpClientListenHandler(listener, false, nil)
}
if (config.Smtp.Inbound.EnablePlain || config.Smtp.Inbound.EnableTls) && config.Smtp.Outbound.EnableDkim {
log.Println("Info: smtp DKIM enabled")
}
}