base.go 5.5 KB

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