read_content.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package smtp_server
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "github.com/mileusna/spf"
  6. log "github.com/sirupsen/logrus"
  7. "io"
  8. "net"
  9. "net/netip"
  10. "pmail/db"
  11. "pmail/dto/parsemail"
  12. "pmail/hooks"
  13. "pmail/utils/async"
  14. "strings"
  15. "time"
  16. )
  17. func (s *Session) Data(r io.Reader) error {
  18. emailData, err := io.ReadAll(r)
  19. if err != nil {
  20. log.Error("邮件内容无法读取", err)
  21. return err
  22. }
  23. as1 := async.New(nil)
  24. for _, hook := range hooks.HookList {
  25. if hook == nil {
  26. continue
  27. }
  28. as1.WaitProcess(func(hk any) {
  29. hk.(hooks.EmailHook).ReceiveParseBefore(emailData)
  30. }, hook)
  31. }
  32. as1.Wait()
  33. log.Infof("邮件原始内容: %s", emailData)
  34. var dkimStatus, SPFStatus bool
  35. // DKIM校验
  36. dkimStatus = parsemail.Check(bytes.NewReader(emailData))
  37. email := parsemail.NewEmailFromReader(bytes.NewReader(emailData))
  38. if err != nil {
  39. log.Fatalf("邮件内容解析失败! Error : %v \n", err)
  40. }
  41. SPFStatus = spfCheck(s.RemoteAddress.String(), email.Sender, email.Sender.EmailAddress)
  42. var dkimV, spfV int8
  43. if dkimStatus {
  44. dkimV = 1
  45. }
  46. if SPFStatus {
  47. spfV = 1
  48. }
  49. as2 := async.New(nil)
  50. for _, hook := range hooks.HookList {
  51. if hook == nil {
  52. continue
  53. }
  54. as2.WaitProcess(func(hk any) {
  55. hk.(hooks.EmailHook).ReceiveParseAfter(email)
  56. }, hook)
  57. }
  58. as2.Wait()
  59. sql := "INSERT INTO email (send_date, subject, reply_to, from_name, from_address, `to`, bcc, cc, text, html, sender, attachments,spf_check, dkim_check, create_time) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
  60. _, err = db.Instance.Exec(sql,
  61. email.Date,
  62. email.Subject,
  63. json2string(email.ReplyTo),
  64. email.From.Name,
  65. email.From.EmailAddress,
  66. json2string(email.To),
  67. json2string(email.Bcc),
  68. json2string(email.Cc),
  69. email.Text,
  70. email.HTML,
  71. json2string(email.Sender),
  72. json2string(email.Attachments),
  73. spfV,
  74. dkimV,
  75. time.Now())
  76. if err != nil {
  77. log.Println("mysql insert error:", err.Error())
  78. }
  79. return nil
  80. }
  81. func json2string(d any) string {
  82. by, _ := json.Marshal(d)
  83. return string(by)
  84. }
  85. func spfCheck(remoteAddress string, sender *parsemail.User, senderString string) bool {
  86. //spf校验
  87. ipAddress, _ := netip.ParseAddrPort(remoteAddress)
  88. ip := net.ParseIP(ipAddress.Addr().String())
  89. if ip.IsPrivate() {
  90. return true
  91. }
  92. tmp := strings.Split(sender.EmailAddress, "@")
  93. if len(tmp) < 2 {
  94. return false
  95. }
  96. res := spf.CheckHost(ip, tmp[1], senderString, "")
  97. if res == spf.None || res == spf.Pass {
  98. // spf校验通过
  99. return true
  100. }
  101. return false
  102. }