framework.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package framework
  2. import (
  3. "encoding/json"
  4. log "github.com/sirupsen/logrus"
  5. "io"
  6. "net"
  7. "net/http"
  8. "os"
  9. "path/filepath"
  10. "pmail/dto/parsemail"
  11. "pmail/utils/context"
  12. )
  13. type EmailHook interface {
  14. // SendBefore 邮件发送前的数据
  15. SendBefore(ctx *context.Context, email *parsemail.Email)
  16. // SendAfter 邮件发送后的数据,err是每个收信服务器的错误信息
  17. SendAfter(ctx *context.Context, email *parsemail.Email, err map[string]error)
  18. // ReceiveParseBefore 接收到邮件,解析之前的原始数据
  19. ReceiveParseBefore(ctx *context.Context, email *[]byte)
  20. // ReceiveParseAfter 接收到邮件,解析之后的结构化数据
  21. ReceiveParseAfter(ctx *context.Context, email *parsemail.Email)
  22. }
  23. type HookDTO struct {
  24. Ctx *context.Context
  25. Email *parsemail.Email
  26. EmailByte *[]byte
  27. ErrMap map[string]error
  28. }
  29. type Plugin struct {
  30. hook EmailHook
  31. }
  32. func CreatePlugin(hook EmailHook) *Plugin {
  33. return &Plugin{
  34. hook: hook,
  35. }
  36. }
  37. func (p *Plugin) Run() {
  38. if len(os.Args) < 2 {
  39. panic("Command Params Error!")
  40. }
  41. mux := http.NewServeMux()
  42. mux.HandleFunc("/SendBefore", func(writer http.ResponseWriter, request *http.Request) {
  43. var hookDTO HookDTO
  44. body, _ := io.ReadAll(request.Body)
  45. err := json.Unmarshal(body, &hookDTO)
  46. if err != nil {
  47. log.Errorf("params error %+v", err)
  48. return
  49. }
  50. p.hook.SendBefore(hookDTO.Ctx, hookDTO.Email)
  51. body, _ = json.Marshal(hookDTO)
  52. writer.Write(body)
  53. })
  54. mux.HandleFunc("/SendAfter", func(writer http.ResponseWriter, request *http.Request) {
  55. var hookDTO HookDTO
  56. body, _ := io.ReadAll(request.Body)
  57. err := json.Unmarshal(body, &hookDTO)
  58. if err != nil {
  59. log.Errorf("params error %+v", err)
  60. return
  61. }
  62. p.hook.SendAfter(hookDTO.Ctx, hookDTO.Email, hookDTO.ErrMap)
  63. body, _ = json.Marshal(hookDTO)
  64. writer.Write(body)
  65. })
  66. mux.HandleFunc("/ReceiveParseBefore", func(writer http.ResponseWriter, request *http.Request) {
  67. var hookDTO HookDTO
  68. body, _ := io.ReadAll(request.Body)
  69. err := json.Unmarshal(body, &hookDTO)
  70. if err != nil {
  71. log.Errorf("params error %+v", err)
  72. return
  73. }
  74. p.hook.ReceiveParseBefore(hookDTO.Ctx, hookDTO.EmailByte)
  75. body, _ = json.Marshal(hookDTO)
  76. writer.Write(body)
  77. })
  78. mux.HandleFunc("/ReceiveParseAfter", func(writer http.ResponseWriter, request *http.Request) {
  79. var hookDTO HookDTO
  80. body, _ := io.ReadAll(request.Body)
  81. err := json.Unmarshal(body, &hookDTO)
  82. if err != nil {
  83. log.Errorf("params error %+v", err)
  84. return
  85. }
  86. p.hook.ReceiveParseAfter(hookDTO.Ctx, hookDTO.Email)
  87. body, _ = json.Marshal(hookDTO)
  88. writer.Write(body)
  89. })
  90. server := http.Server{
  91. Handler: mux,
  92. }
  93. unixListener, err := net.Listen("unix", getExePath()+"/"+os.Args[1])
  94. if err != nil {
  95. panic(err)
  96. }
  97. err = server.Serve(unixListener)
  98. if err != nil {
  99. panic(err)
  100. }
  101. }
  102. func getExePath() string {
  103. ex, err := os.Executable()
  104. if err != nil {
  105. panic(err)
  106. }
  107. exePath := filepath.Dir(ex)
  108. return exePath
  109. }