base.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. // GetName 获取插件名称
  111. func (h *HookSender) GetName(ctx *context.Context) string {
  112. dto := framework.HookDTO{
  113. Ctx: ctx,
  114. }
  115. body, _ := json.Marshal(dto)
  116. ret, errL := h.httpc.Post("http://plugin/GetName", "application/json", strings.NewReader(string(body)))
  117. if errL != nil {
  118. log.WithContext(ctx).Errorf("[%s] Error! %v", h.name, errL)
  119. return ""
  120. }
  121. body, _ = io.ReadAll(ret.Body)
  122. return string(body)
  123. }
  124. // SettingsHtml 插件页面
  125. func (h *HookSender) SettingsHtml(ctx *context.Context, url string, requestData string) string {
  126. dto := framework.SettingsHtmlRequest{
  127. Ctx: ctx,
  128. URL: url,
  129. RequestData: requestData,
  130. }
  131. body, _ := json.Marshal(dto)
  132. ret, errL := h.httpc.Post("http://plugin/SettingsHtml", "application/json", strings.NewReader(string(body)))
  133. if errL != nil {
  134. log.WithContext(ctx).Errorf("[%s] Error! %v", h.name, errL)
  135. return ""
  136. }
  137. body, _ = io.ReadAll(ret.Body)
  138. return string(body)
  139. }
  140. func NewHookSender(socketPath string, name string, serverVersion string) *HookSender {
  141. httpc := http.Client{
  142. Timeout: time.Second * 10,
  143. Transport: &http.Transport{
  144. DialContext: func(ctx oContext.Context, network, addr string) (net.Conn, error) {
  145. return net.Dial("unix", socketPath)
  146. },
  147. },
  148. }
  149. return &HookSender{
  150. httpc: httpc,
  151. socket: socketPath,
  152. name: name,
  153. }
  154. }
  155. var processList []*os.Process
  156. // Init 注册hook对象
  157. func Init(serverVersion string) {
  158. HookList = map[string]framework.EmailHook{}
  159. env := os.Environ()
  160. procAttr := &os.ProcAttr{
  161. Env: env,
  162. Files: []*os.File{
  163. os.Stdin,
  164. os.Stdout,
  165. os.Stderr,
  166. },
  167. }
  168. root := "./plugins"
  169. pluginNo := 1
  170. filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
  171. if info != nil && !info.IsDir() && (!strings.Contains(info.Name(), ".") || strings.Contains(info.Name(), ".exe")) {
  172. socketPath := fmt.Sprintf("%s/%d.socket", root, pluginNo)
  173. os.Remove(socketPath)
  174. //socketPath = "/PMail/server/hooks/spam_block/1555.socket" //debug
  175. log.Infof("[%s] Plugin Load", info.Name())
  176. p, err := os.StartProcess(path, []string{
  177. info.Name(),
  178. fmt.Sprintf("%d.socket", pluginNo),
  179. }, procAttr)
  180. if err != nil {
  181. log.Errorf("Plug Load Error! %v", err)
  182. return nil
  183. }
  184. fmt.Printf("[%s] Plugin Start! PID:%d", info.Name(), p.Pid)
  185. processList = append(processList, p)
  186. pluginNo++
  187. go func() {
  188. stat, err := p.Wait()
  189. log.Errorf("[%s] Plugin Stop. Error:%v Stat:%v", info.Name(), err, stat.String())
  190. delete(HookList, info.Name())
  191. os.Remove(socketPath)
  192. }()
  193. loadSucc := false
  194. for i := 0; i < 5; i++ {
  195. time.Sleep(1 * time.Second)
  196. if _, err := os.Stat(socketPath); err == nil {
  197. loadSucc = true
  198. break
  199. }
  200. if i == 4 {
  201. log.Errorf(fmt.Sprintf("[%s] Start Fail!", info.Name()))
  202. }
  203. }
  204. if loadSucc {
  205. hk := NewHookSender(socketPath, info.Name(), serverVersion)
  206. hkName := hk.GetName(&context.Context{})
  207. HookList[hkName] = hk
  208. log.Infof("[%s] Plugin Load Success!", hkName)
  209. }
  210. }
  211. return nil
  212. })
  213. }
  214. func Stop() {
  215. log.Info("Plugin Stop")
  216. for _, process := range processList {
  217. process.Kill()
  218. }
  219. }