base.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package hooks
  2. import (
  3. oContext "context"
  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/hooks/framework"
  14. "pmail/utils/context"
  15. "strings"
  16. "time"
  17. )
  18. // HookList
  19. var HookList []framework.EmailHook
  20. type HookSender struct {
  21. httpc http.Client
  22. name string
  23. socket string
  24. }
  25. func (h *HookSender) SendBefore(ctx *context.Context, email *parsemail.Email) {
  26. log.WithContext(ctx).Debugf("[%s]Plugin SendBefore Start", h.name)
  27. dto := framework.HookDTO{
  28. Ctx: ctx,
  29. Email: email,
  30. }
  31. body, _ := json.Marshal(dto)
  32. ret, err := h.httpc.Post("http://plugin/SendBefore", "application/json", strings.NewReader(string(body)))
  33. if err != nil {
  34. log.WithContext(ctx).Errorf("[%s] Error! %v", h.name, err)
  35. return
  36. }
  37. body, _ = io.ReadAll(ret.Body)
  38. json.Unmarshal(body, &dto)
  39. ctx = dto.Ctx
  40. email = dto.Email
  41. log.WithContext(ctx).Debugf("[%s]Plugin SendBefore End", h.name)
  42. }
  43. func (h *HookSender) SendAfter(ctx *context.Context, email *parsemail.Email, err map[string]error) {
  44. log.WithContext(ctx).Debugf("[%s]Plugin SendAfter Start", h.name)
  45. dto := framework.HookDTO{
  46. Ctx: ctx,
  47. Email: email,
  48. ErrMap: err,
  49. }
  50. body, _ := json.Marshal(dto)
  51. ret, errL := h.httpc.Post("http://plugin/SendAfter", "application/json", strings.NewReader(string(body)))
  52. if errL != nil {
  53. log.WithContext(ctx).Errorf("[%s] Error! %v", h.name, errL)
  54. return
  55. }
  56. body, _ = io.ReadAll(ret.Body)
  57. json.Unmarshal(body, &dto)
  58. ctx = dto.Ctx
  59. email = dto.Email
  60. err = dto.ErrMap
  61. log.WithContext(ctx).Debugf("[%s]Plugin SendAfter End", h.name)
  62. }
  63. func (h *HookSender) ReceiveParseBefore(ctx *context.Context, email *[]byte) {
  64. log.WithContext(ctx).Debugf("[%s]Plugin ReceiveParseBefore Start", h.name)
  65. dto := framework.HookDTO{
  66. Ctx: ctx,
  67. EmailByte: email,
  68. }
  69. body, _ := json.Marshal(dto)
  70. ret, errL := h.httpc.Post("http://plugin/ReceiveParseBefore", "application/json", strings.NewReader(string(body)))
  71. if errL != nil {
  72. log.WithContext(ctx).Errorf("[%s] Error! %v", h.name, errL)
  73. return
  74. }
  75. body, _ = io.ReadAll(ret.Body)
  76. json.Unmarshal(body, &dto)
  77. ctx = dto.Ctx
  78. email = dto.EmailByte
  79. log.WithContext(ctx).Debugf("[%s]Plugin ReceiveParseBefore End", h.name)
  80. }
  81. func (h *HookSender) ReceiveParseAfter(ctx *context.Context, email *parsemail.Email) {
  82. log.WithContext(ctx).Debugf("[%s]Plugin ReceiveParseAfter Start", h.name)
  83. dto := framework.HookDTO{
  84. Ctx: ctx,
  85. Email: email,
  86. }
  87. body, _ := json.Marshal(dto)
  88. ret, errL := h.httpc.Post("http://plugin/ReceiveParseAfter", "application/json", strings.NewReader(string(body)))
  89. if errL != nil {
  90. log.WithContext(ctx).Errorf("[%s] Error! %v", h.name, errL)
  91. return
  92. }
  93. body, _ = io.ReadAll(ret.Body)
  94. json.Unmarshal(body, &dto)
  95. ctx = dto.Ctx
  96. email = dto.Email
  97. log.WithContext(ctx).Debugf("[%s]Plugin ReceiveParseAfter End", h.name)
  98. }
  99. func NewHookSender(socketPath string, name string, serverVersion string) *HookSender {
  100. httpc := http.Client{
  101. Timeout: time.Second * 10,
  102. Transport: &http.Transport{
  103. DialContext: func(ctx oContext.Context, network, addr string) (net.Conn, error) {
  104. return net.Dial("unix", socketPath)
  105. },
  106. },
  107. }
  108. return &HookSender{
  109. httpc: httpc,
  110. socket: socketPath,
  111. name: name,
  112. }
  113. }
  114. // Init 注册hook对象
  115. func Init(serverVersion string) {
  116. env := os.Environ()
  117. procAttr := &os.ProcAttr{
  118. Env: env,
  119. Files: []*os.File{
  120. os.Stdin,
  121. os.Stdout,
  122. os.Stderr,
  123. },
  124. }
  125. root := "./plugins"
  126. pluginNo := 1
  127. filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
  128. if info != nil && !info.IsDir() && (!strings.Contains(info.Name(), ".") || strings.Contains(info.Name(), ".exe")) {
  129. socketPath := fmt.Sprintf("%s/%d.socket", root, pluginNo)
  130. os.Remove(socketPath)
  131. log.Infof("[%s] Plugin Load", info.Name())
  132. p, err := os.StartProcess(path, []string{
  133. info.Name(),
  134. fmt.Sprintf("%d.socket", pluginNo),
  135. }, procAttr)
  136. if err != nil {
  137. log.Errorf("Plug Load Error! %v", err)
  138. return nil
  139. }
  140. fmt.Printf("[%s] Plugin Start! PID:%d", info.Name(), p.Pid)
  141. pluginNo++
  142. go func() {
  143. stat, err := p.Wait()
  144. log.Errorf("[%s] Plugin Stop. Error:%v Stat:%v", info.Name(), err, stat.String())
  145. }()
  146. loadSucc := false
  147. for i := 0; i < 5; i++ {
  148. time.Sleep(1 * time.Second)
  149. if _, err := os.Stat(socketPath); err == nil {
  150. loadSucc = true
  151. break
  152. }
  153. if i == 4 {
  154. log.Errorf(fmt.Sprintf("[%s] Start Fail!", info.Name()))
  155. }
  156. }
  157. if loadSucc {
  158. HookList = append(HookList, NewHookSender(socketPath, info.Name(), serverVersion))
  159. log.Infof("[%s] Plugin Load Success!", info.Name())
  160. }
  161. }
  162. return nil
  163. })
  164. }