base.go 5.2 KB

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