utils.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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/types"
  9. )
  10. func printHelp(cmd *cobra.Command) {
  11. if err := cmd.Help(); err != nil {
  12. log.Fatalf("unable to print help(): %s", err)
  13. }
  14. }
  15. func manageCliDecisionAlerts(ip *string, ipRange *string, scope *string, value *string) error {
  16. /*if a range is provided, change the scope*/
  17. if *ipRange != "" {
  18. _, _, err := net.ParseCIDR(*ipRange)
  19. if err != nil {
  20. return fmt.Errorf("%s isn't a valid range", *ipRange)
  21. }
  22. }
  23. if *ip != "" {
  24. ipRepr := net.ParseIP(*ip)
  25. if ipRepr == nil {
  26. return fmt.Errorf("%s isn't a valid ip", *ip)
  27. }
  28. }
  29. //avoid confusion on scope (ip vs Ip and range vs Range)
  30. switch strings.ToLower(*scope) {
  31. case "ip":
  32. *scope = types.Ip
  33. case "range":
  34. *scope = types.Range
  35. case "country":
  36. *scope = types.Country
  37. case "as":
  38. *scope = types.AS
  39. }
  40. return nil
  41. }
  42. func removeFromSlice(val string, slice []string) []string {
  43. var i int
  44. var value string
  45. valueFound := false
  46. // get the index
  47. for i, value = range slice {
  48. if value == val {
  49. valueFound = true
  50. break
  51. }
  52. }
  53. if valueFound {
  54. slice[i] = slice[len(slice)-1]
  55. slice[len(slice)-1] = ""
  56. slice = slice[:len(slice)-1]
  57. }
  58. return slice
  59. }