hook.go 928 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package dashboard
  2. import (
  3. log "github.com/sirupsen/logrus"
  4. )
  5. type logHook int
  6. func (h logHook) Levels() []log.Level {
  7. return log.AllLevels
  8. }
  9. // Checks fired logs for information that is relevant to the dashboard
  10. func (h logHook) Fire(e *log.Entry) error {
  11. event, ok := e.Data["event"].(string)
  12. if !ok {
  13. return nil
  14. }
  15. var helo, ip, domain string
  16. if event == "mailfrom" {
  17. helo, ok = e.Data["helo"].(string)
  18. if !ok {
  19. return nil
  20. }
  21. if len(helo) > 16 {
  22. helo = helo[:16]
  23. }
  24. ip, ok = e.Data["address"].(string)
  25. if !ok {
  26. return nil
  27. }
  28. domain, ok = e.Data["domain"].(string)
  29. if !ok {
  30. return nil
  31. }
  32. }
  33. switch event {
  34. case "connect":
  35. store.lock.Lock()
  36. store.nClients++
  37. store.lock.Unlock()
  38. case "mailfrom":
  39. store.newConns <- conn{
  40. domain: domain,
  41. helo: helo,
  42. ip: ip,
  43. }
  44. case "disconnect":
  45. store.lock.Lock()
  46. store.nClients--
  47. store.lock.Unlock()
  48. }
  49. return nil
  50. }