framework.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package framework
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. log "github.com/sirupsen/logrus"
  7. "io"
  8. "net"
  9. "net/http"
  10. "os"
  11. "path/filepath"
  12. "pmail/dto/parsemail"
  13. "pmail/utils/context"
  14. "time"
  15. )
  16. type EmailHook interface {
  17. // SendBefore 邮件发送前的数据
  18. SendBefore(ctx *context.Context, email *parsemail.Email)
  19. // SendAfter 邮件发送后的数据,err是每个收信服务器的错误信息
  20. SendAfter(ctx *context.Context, email *parsemail.Email, err map[string]error)
  21. // ReceiveParseBefore 接收到邮件,解析之前的原始数据
  22. ReceiveParseBefore(ctx *context.Context, email *[]byte)
  23. // ReceiveParseAfter 接收到邮件,解析之后的结构化数据
  24. ReceiveParseAfter(ctx *context.Context, email *parsemail.Email)
  25. }
  26. // HookDTO PMail 主程序和插件通信的结构体
  27. type HookDTO struct {
  28. ServerVersion string // 服务端程序版本
  29. Ctx *context.Context // 上下文
  30. Email *parsemail.Email // 邮件内容
  31. EmailByte *[]byte // 未解析前的文件内容
  32. ErrMap map[string]error // 错误信息
  33. }
  34. type Plugin struct {
  35. name string
  36. hook EmailHook
  37. }
  38. func CreatePlugin(name string, hook EmailHook) *Plugin {
  39. return &Plugin{
  40. name: name,
  41. hook: hook,
  42. }
  43. }
  44. type logFormatter struct {
  45. }
  46. // Format 定义日志输出格式
  47. func (l *logFormatter) Format(entry *log.Entry) ([]byte, error) {
  48. b := bytes.Buffer{}
  49. b.WriteString(fmt.Sprintf("[%s]", entry.Level.String()))
  50. b.WriteString(fmt.Sprintf("[%s]", entry.Time.Format("2006-01-02 15:04:05")))
  51. if entry.Context != nil {
  52. ctx := entry.Context.(*context.Context)
  53. if ctx != nil {
  54. b.WriteString(fmt.Sprintf("[%s]", ctx.GetValue(context.LogID)))
  55. }
  56. }
  57. b.WriteString(fmt.Sprintf("[%s:%d]", entry.Caller.File, entry.Caller.Line))
  58. b.WriteString(entry.Message)
  59. b.WriteString("\n")
  60. return b.Bytes(), nil
  61. }
  62. func (p *Plugin) Run() {
  63. // 设置日志格式为json格式
  64. log.SetFormatter(&logFormatter{})
  65. log.SetReportCaller(true)
  66. // 设置将日志输出到标准输出(默认的输出为stderr,标准错误)
  67. // 日志消息输出可以是任意的io.writer类型
  68. log.SetOutput(os.Stdout)
  69. if len(os.Args) < 2 {
  70. panic("Command Params Error!")
  71. }
  72. mux := http.NewServeMux()
  73. mux.HandleFunc("/SendBefore", func(writer http.ResponseWriter, request *http.Request) {
  74. log.Debugf("[%s] SendBefore Start", p.name)
  75. var hookDTO HookDTO
  76. body, _ := io.ReadAll(request.Body)
  77. err := json.Unmarshal(body, &hookDTO)
  78. if err != nil {
  79. log.Errorf("params error %+v", err)
  80. return
  81. }
  82. p.hook.SendBefore(hookDTO.Ctx, hookDTO.Email)
  83. body, _ = json.Marshal(hookDTO)
  84. writer.Write(body)
  85. log.Debugf("[%s] SendBefore End", p.name)
  86. })
  87. mux.HandleFunc("/SendAfter", func(writer http.ResponseWriter, request *http.Request) {
  88. log.Debugf("[%s] SendAfter Start", p.name)
  89. var hookDTO HookDTO
  90. body, _ := io.ReadAll(request.Body)
  91. err := json.Unmarshal(body, &hookDTO)
  92. if err != nil {
  93. log.Errorf("params error %+v", err)
  94. return
  95. }
  96. p.hook.SendAfter(hookDTO.Ctx, hookDTO.Email, hookDTO.ErrMap)
  97. body, _ = json.Marshal(hookDTO)
  98. writer.Write(body)
  99. log.Debugf("[%s] SendAfter End", p.name)
  100. })
  101. mux.HandleFunc("/ReceiveParseBefore", func(writer http.ResponseWriter, request *http.Request) {
  102. log.Debugf("[%s] ReceiveParseBefore Start", p.name)
  103. var hookDTO HookDTO
  104. body, _ := io.ReadAll(request.Body)
  105. err := json.Unmarshal(body, &hookDTO)
  106. if err != nil {
  107. log.Errorf("params error %+v", err)
  108. return
  109. }
  110. p.hook.ReceiveParseBefore(hookDTO.Ctx, hookDTO.EmailByte)
  111. body, _ = json.Marshal(hookDTO)
  112. writer.Write(body)
  113. log.Debugf("[%s] ReceiveParseBefore End", p.name)
  114. })
  115. mux.HandleFunc("/ReceiveParseAfter", func(writer http.ResponseWriter, request *http.Request) {
  116. log.Debugf("[%s] ReceiveParseAfter Start", p.name)
  117. var hookDTO HookDTO
  118. body, _ := io.ReadAll(request.Body)
  119. err := json.Unmarshal(body, &hookDTO)
  120. if err != nil {
  121. log.Errorf("params error %+v", err)
  122. return
  123. }
  124. p.hook.ReceiveParseAfter(hookDTO.Ctx, hookDTO.Email)
  125. body, _ = json.Marshal(hookDTO)
  126. writer.Write(body)
  127. log.Debugf("[%s] ReceiveParseAfter End", p.name)
  128. })
  129. server := http.Server{
  130. ReadTimeout: 5 * time.Second,
  131. WriteTimeout: 5 * time.Second,
  132. Handler: mux,
  133. }
  134. unixListener, err := net.Listen("unix", getExePath()+"/"+os.Args[1])
  135. if err != nil {
  136. panic(err)
  137. }
  138. err = server.Serve(unixListener)
  139. if err != nil {
  140. panic(err)
  141. }
  142. }
  143. func getExePath() string {
  144. ex, err := os.Executable()
  145. if err != nil {
  146. panic(err)
  147. }
  148. exePath := filepath.Dir(ex)
  149. return exePath
  150. }