hook.go 1003 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. mainlog().Infof("disconnect in dashboard, nclients: %d", store.nClients)
  46. store.lock.Lock()
  47. store.nClients--
  48. store.lock.Unlock()
  49. }
  50. return nil
  51. }