read_content.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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/dto/parsemail"
  11. "pmail/hooks"
  12. "pmail/mysql"
  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. for _, hook := range hooks.HookList {
  24. if hook == nil {
  25. continue
  26. }
  27. async.New(nil).Process(func() {
  28. hook.ReceiveParseBefore(emailData)
  29. })
  30. }
  31. log.Infof("邮件原始内容: %s", emailData)
  32. var dkimStatus, SPFStatus bool
  33. // DKIM校验
  34. dkimStatus = parsemail.Check(bytes.NewReader(emailData))
  35. email := parsemail.NewEmailFromReader(bytes.NewReader(emailData))
  36. if err != nil {
  37. log.Fatalf("邮件内容解析失败! Error : %v \n", err)
  38. }
  39. SPFStatus = spfCheck(s.RemoteAddress.String(), email.Sender, email.Sender.EmailAddress)
  40. var dkimV, spfV int8
  41. if dkimStatus {
  42. dkimV = 1
  43. }
  44. if SPFStatus {
  45. spfV = 1
  46. }
  47. for _, hook := range hooks.HookList {
  48. if hook == nil {
  49. continue
  50. }
  51. async.New(nil).Process(func() {
  52. hook.ReceiveParseAfter(email)
  53. })
  54. }
  55. 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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
  56. _, err = mysql.Instance.Exec(sql,
  57. email.Date,
  58. email.Subject,
  59. json2string(email.ReplyTo),
  60. email.From.Name,
  61. email.From.EmailAddress,
  62. json2string(email.To),
  63. json2string(email.Bcc),
  64. json2string(email.Cc),
  65. email.Text,
  66. email.HTML,
  67. json2string(email.Sender),
  68. json2string(email.Attachments),
  69. spfV,
  70. dkimV,
  71. time.Now())
  72. if err != nil {
  73. log.Println("mysql insert error:", err.Error())
  74. }
  75. return nil
  76. }
  77. func json2string(d any) string {
  78. by, _ := json.Marshal(d)
  79. return string(by)
  80. }
  81. func spfCheck(remoteAddress string, sender *parsemail.User, senderString string) bool {
  82. //spf校验
  83. ipAddress, _ := netip.ParseAddrPort(remoteAddress)
  84. ip := net.ParseIP(ipAddress.Addr().String())
  85. if ip.IsPrivate() {
  86. return true
  87. }
  88. tmp := strings.Split(sender.EmailAddress, "@")
  89. if len(tmp) < 2 {
  90. return false
  91. }
  92. res := spf.CheckHost(ip, tmp[1], senderString, "")
  93. if res == spf.None || res == spf.Pass {
  94. // spf校验通过
  95. return true
  96. }
  97. return false
  98. }