write.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. package database
  2. import (
  3. "fmt"
  4. "sync/atomic"
  5. "github.com/crowdsecurity/crowdsec/pkg/types"
  6. log "github.com/sirupsen/logrus"
  7. )
  8. //we simply append the event to the transaction
  9. func (c *Context) WriteBanApplication(ban types.BanApplication) error {
  10. atomic.AddInt32(&c.count, 1)
  11. c.lock.Lock()
  12. defer c.lock.Unlock()
  13. log.Debugf("Ban application being called : %s %s", ban.Scenario, ban.IpText)
  14. ret := c.tx.Where(types.BanApplication{IpText: ban.IpText}).Assign(types.BanApplication{Until: ban.Until}).Assign(types.BanApplication{Reason: ban.Reason}).Assign(types.BanApplication{MeasureType: ban.MeasureType}).FirstOrCreate(&ban)
  15. if ret.Error != nil {
  16. return fmt.Errorf("failed to write ban record : %v", ret.Error)
  17. }
  18. return nil
  19. }
  20. func (c *Context) WriteSignal(sig types.SignalOccurence) error {
  21. atomic.AddInt32(&c.count, 1)
  22. c.lock.Lock()
  23. defer c.lock.Unlock()
  24. //log.Debugf("Ban signal being called : %s %s", sig.Scenario, sig.Source.Ip.String())
  25. ret := c.tx.Create(&sig)
  26. //sig.Scenario = sig.Scenario
  27. if ret.Error != nil {
  28. log.Errorf("FAILED : %+v \n", ret.Error)
  29. return fmt.Errorf("failed to write signal occurrence : %v", ret.Error)
  30. }
  31. return nil
  32. }