base.go 5.2 KB

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