utils.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package main
  2. import (
  3. "fmt"
  4. "net"
  5. "strings"
  6. log "github.com/sirupsen/logrus"
  7. "github.com/spf13/cobra"
  8. "github.com/crowdsecurity/crowdsec/pkg/database"
  9. "github.com/crowdsecurity/crowdsec/pkg/types"
  10. )
  11. func printHelp(cmd *cobra.Command) {
  12. if err := cmd.Help(); err != nil {
  13. log.Fatalf("unable to print help(): %s", err)
  14. }
  15. }
  16. func manageCliDecisionAlerts(ip *string, ipRange *string, scope *string, value *string) error {
  17. /*if a range is provided, change the scope*/
  18. if *ipRange != "" {
  19. _, _, err := net.ParseCIDR(*ipRange)
  20. if err != nil {
  21. return fmt.Errorf("%s isn't a valid range", *ipRange)
  22. }
  23. }
  24. if *ip != "" {
  25. ipRepr := net.ParseIP(*ip)
  26. if ipRepr == nil {
  27. return fmt.Errorf("%s isn't a valid ip", *ip)
  28. }
  29. }
  30. //avoid confusion on scope (ip vs Ip and range vs Range)
  31. switch strings.ToLower(*scope) {
  32. case "ip":
  33. *scope = types.Ip
  34. case "range":
  35. *scope = types.Range
  36. case "country":
  37. *scope = types.Country
  38. case "as":
  39. *scope = types.AS
  40. }
  41. return nil
  42. }
  43. func getDBClient() (*database.Client, error) {
  44. if err := csConfig.LoadAPIServer(); err != nil || csConfig.DisableAPI {
  45. return nil, err
  46. }
  47. ret, err := database.NewClient(csConfig.DbConfig)
  48. if err != nil {
  49. return nil, err
  50. }
  51. return ret, nil
  52. }
  53. func removeFromSlice(val string, slice []string) []string {
  54. var i int
  55. var value string
  56. valueFound := false
  57. // get the index
  58. for i, value = range slice {
  59. if value == val {
  60. valueFound = true
  61. break
  62. }
  63. }
  64. if valueFound {
  65. slice[i] = slice[len(slice)-1]
  66. slice[len(slice)-1] = ""
  67. slice = slice[:len(slice)-1]
  68. }
  69. return slice
  70. }