2020-07-16 13:59:09 +00:00
|
|
|
package main
|
|
|
|
|
2020-09-01 12:32:45 +00:00
|
|
|
import (
|
2020-11-30 09:37:17 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
|
2020-09-01 12:32:45 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2022-03-10 12:55:25 +00:00
|
|
|
"github.com/spf13/cobra"
|
2022-10-07 09:05:35 +00:00
|
|
|
|
2022-12-05 14:39:54 +00:00
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/database"
|
2022-10-07 09:05:35 +00:00
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
2020-09-01 12:32:45 +00:00
|
|
|
)
|
|
|
|
|
2022-03-10 12:55:25 +00:00
|
|
|
func printHelp(cmd *cobra.Command) {
|
2023-11-28 22:51:51 +00:00
|
|
|
if err := cmd.Help(); err != nil {
|
2022-04-13 13:44:23 +00:00
|
|
|
log.Fatalf("unable to print help(): %s", err)
|
2022-03-10 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-19 10:07:35 +00:00
|
|
|
func manageCliDecisionAlerts(ip *string, ipRange *string, scope *string, value *string) error {
|
|
|
|
/*if a range is provided, change the scope*/
|
|
|
|
if *ipRange != "" {
|
|
|
|
_, _, err := net.ParseCIDR(*ipRange)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%s isn't a valid range", *ipRange)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if *ip != "" {
|
|
|
|
ipRepr := net.ParseIP(*ip)
|
|
|
|
if ipRepr == nil {
|
|
|
|
return fmt.Errorf("%s isn't a valid ip", *ip)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//avoid confusion on scope (ip vs Ip and range vs Range)
|
|
|
|
switch strings.ToLower(*scope) {
|
|
|
|
case "ip":
|
|
|
|
*scope = types.Ip
|
|
|
|
case "range":
|
|
|
|
*scope = types.Range
|
|
|
|
case "country":
|
|
|
|
*scope = types.Country
|
|
|
|
case "as":
|
|
|
|
*scope = types.AS
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-05 14:39:54 +00:00
|
|
|
func getDBClient() (*database.Client, error) {
|
|
|
|
if err := csConfig.LoadAPIServer(); err != nil || csConfig.DisableAPI {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ret, err := database.NewClient(csConfig.DbConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|
2023-01-04 15:50:02 +00:00
|
|
|
|
|
|
|
func removeFromSlice(val string, slice []string) []string {
|
|
|
|
var i int
|
|
|
|
var value string
|
|
|
|
|
|
|
|
valueFound := false
|
|
|
|
|
|
|
|
// get the index
|
|
|
|
for i, value = range slice {
|
|
|
|
if value == val {
|
|
|
|
valueFound = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if valueFound {
|
|
|
|
slice[i] = slice[len(slice)-1]
|
|
|
|
slice[len(slice)-1] = ""
|
|
|
|
slice = slice[:len(slice)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
return slice
|
|
|
|
}
|