controller.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package v1
  2. import (
  3. "context"
  4. "net"
  5. //"github.com/crowdsecurity/crowdsec/pkg/apiserver/controllers"
  6. middlewares "github.com/crowdsecurity/crowdsec/pkg/apiserver/middlewares/v1"
  7. "github.com/crowdsecurity/crowdsec/pkg/csconfig"
  8. "github.com/crowdsecurity/crowdsec/pkg/csplugin"
  9. "github.com/crowdsecurity/crowdsec/pkg/csprofiles"
  10. "github.com/crowdsecurity/crowdsec/pkg/database"
  11. "github.com/crowdsecurity/crowdsec/pkg/models"
  12. "github.com/pkg/errors"
  13. )
  14. type Controller struct {
  15. Ectx context.Context
  16. DBClient *database.Client
  17. APIKeyHeader string
  18. Middlewares *middlewares.Middlewares
  19. Profiles []*csprofiles.Runtime
  20. CAPIChan chan []*models.Alert
  21. PluginChannel chan csplugin.ProfileAlert
  22. ConsoleConfig csconfig.ConsoleConfig
  23. TrustedIPs []net.IPNet
  24. }
  25. type ControllerV1Config struct {
  26. DbClient *database.Client
  27. Ctx context.Context
  28. ProfilesCfg []*csconfig.ProfileCfg
  29. CapiChan chan []*models.Alert
  30. PluginChannel chan csplugin.ProfileAlert
  31. ConsoleConfig csconfig.ConsoleConfig
  32. TrustedIPs []net.IPNet
  33. }
  34. func New(cfg *ControllerV1Config) (*Controller, error) {
  35. var err error
  36. profiles, err := csprofiles.NewProfile(cfg.ProfilesCfg)
  37. if err != nil {
  38. return &Controller{}, errors.Wrapf(err, "failed to compile profiles")
  39. }
  40. v1 := &Controller{
  41. Ectx: cfg.Ctx,
  42. DBClient: cfg.DbClient,
  43. APIKeyHeader: middlewares.APIKeyHeader,
  44. Profiles: profiles,
  45. CAPIChan: cfg.CapiChan,
  46. PluginChannel: cfg.PluginChannel,
  47. ConsoleConfig: cfg.ConsoleConfig,
  48. TrustedIPs: cfg.TrustedIPs,
  49. }
  50. v1.Middlewares, err = middlewares.NewMiddlewares(cfg.DbClient)
  51. if err != nil {
  52. return v1, err
  53. }
  54. return v1, nil
  55. }