Compare commits

...

4 commits

Author SHA1 Message Date
marco
82d3ba61e4 comment + drop var declaration + explicit zero return 2024-04-29 15:44:00 +02:00
marco
dbec5b7c9c drop redundant else 2024-04-29 15:41:25 +02:00
marco
64b793c3da if/else -> switch 2024-04-29 15:26:49 +02:00
marco
26faf6fb13 simplify a couple loops 2024-04-29 15:22:38 +02:00
5 changed files with 35 additions and 38 deletions

View file

@ -91,10 +91,7 @@ func truncate(values []string, contextValueLen int) (string, error) {
return "", fmt.Errorf("unable to dump metas: %s", err)
}
ret = string(valueByte)
for {
if len(ret) <= contextValueLen {
break
}
for len(ret) > contextValueLen {
// if there is only 1 value left and that the size is too big, truncate it
if len(values) == 1 {
valueToTruncate := values[0]

View file

@ -911,12 +911,17 @@ func (a *apic) UpdateBlocklists(links *modelscapi.GetDecisionsStreamResponseLink
}
func setAlertScenario(alert *models.Alert, addCounters map[string]map[string]int, deleteCounters map[string]map[string]int) {
if *alert.Source.Scope == types.CAPIOrigin {
switch *alert.Source.Scope {
case types.CAPIOrigin:
*alert.Source.Scope = types.CommunityBlocklistPullSourceScope
alert.Scenario = ptr.Of(fmt.Sprintf("update : +%d/-%d IPs", addCounters[types.CAPIOrigin]["all"], deleteCounters[types.CAPIOrigin]["all"]))
} else if *alert.Source.Scope == types.ListOrigin {
alert.Scenario = ptr.Of(fmt.Sprintf("update : +%d/-%d IPs",
addCounters[types.CAPIOrigin]["all"],
deleteCounters[types.CAPIOrigin]["all"]))
case types.ListOrigin:
*alert.Source.Scope = fmt.Sprintf("%s:%s", types.ListOrigin, *alert.Scenario)
alert.Scenario = ptr.Of(fmt.Sprintf("update : +%d/-%d IPs", addCounters[types.ListOrigin][*alert.Scenario], deleteCounters[types.ListOrigin][*alert.Scenario]))
alert.Scenario = ptr.Of(fmt.Sprintf("update : +%d/-%d IPs",
addCounters[types.ListOrigin][*alert.Scenario],
deleteCounters[types.ListOrigin][*alert.Scenario]))
}
}
@ -988,11 +993,12 @@ func makeAddAndDeleteCounters() (map[string]map[string]int, map[string]map[strin
}
func updateCounterForDecision(counter map[string]map[string]int, origin *string, scenario *string, totalDecisions int) {
if *origin == types.CAPIOrigin {
switch *origin {
case types.CAPIOrigin:
counter[*origin]["all"] += totalDecisions
} else if *origin == types.ListOrigin {
case types.ListOrigin:
counter[*origin][*scenario] += totalDecisions
} else {
default:
log.Warningf("Unknown origin %s", *origin)
}
}

View file

@ -49,20 +49,17 @@ func LastAddress(n *net.IPNet) net.IP {
ip[3]|^n.Mask[3])
}
// GetIpsFromIpRange takes a CIDR range and returns the start and end IP
func GetIpsFromIpRange(host string) (int64, int64, error) {
var ipStart int64
var ipEnd int64
var err error
var parsedRange *net.IPNet
if _, parsedRange, err = net.ParseCIDR(host); err != nil {
return ipStart, ipEnd, fmt.Errorf("'%s' is not a valid CIDR", host)
_, parsedRange, err := net.ParseCIDR(host)
if err != nil {
return 0, 0, fmt.Errorf("'%s' is not a valid CIDR", host)
}
if parsedRange == nil {
return ipStart, ipEnd, fmt.Errorf("unable to parse network : %s", err)
return 0, 0, fmt.Errorf("unable to parse network : %s", err)
}
ipStart = int64(IP2Int(parsedRange.IP))
ipEnd = int64(IP2Int(LastAddress(parsedRange)))
ipStart := int64(IP2Int(parsedRange.IP))
ipEnd := int64(IP2Int(LastAddress(parsedRange)))
return ipStart, ipEnd, nil
}

View file

@ -380,19 +380,17 @@ func LoadBucket(bucketFactory *BucketFactory, tomb *tomb.Tomb) error {
bucketFactory.processors = append(bucketFactory.processors, &BayesianBucket{})
}
if len(bucketFactory.Data) > 0 {
for _, data := range bucketFactory.Data {
if data.DestPath == "" {
bucketFactory.logger.Errorf("no dest_file provided for '%s'", bucketFactory.Name)
continue
}
err = exprhelpers.FileInit(bucketFactory.DataDir, data.DestPath, data.Type)
if err != nil {
bucketFactory.logger.Errorf("unable to init data for file '%s': %s", data.DestPath, err)
}
if data.Type == "regexp" { //cache only makes sense for regexp
exprhelpers.RegexpCacheInit(data.DestPath, *data)
}
for _, data := range bucketFactory.Data {
if data.DestPath == "" {
bucketFactory.logger.Errorf("no dest_file provided for '%s'", bucketFactory.Name)
continue
}
err = exprhelpers.FileInit(bucketFactory.DataDir, data.DestPath, data.Type)
if err != nil {
bucketFactory.logger.Errorf("unable to init data for file '%s': %s", data.DestPath, err)
}
if data.Type == "regexp" { //cache only makes sense for regexp
exprhelpers.RegexpCacheInit(data.DestPath, *data)
}
}

View file

@ -97,13 +97,12 @@ func IP2Ints(pip net.IP) (int, int64, int64, error) {
if pip4 != nil {
ip_nw32 := binary.BigEndian.Uint32(pip4)
return 4, uint2int(uint64(ip_nw32)), uint2int(ip_sfx), nil
} else if pip16 != nil {
}
if pip16 != nil {
ip_nw = binary.BigEndian.Uint64(pip16[0:8])
ip_sfx = binary.BigEndian.Uint64(pip16[8:16])
return 16, uint2int(ip_nw), uint2int(ip_sfx), nil
} else {
return -1, 0, 0, fmt.Errorf("unexpected len %d for %s", len(pip), pip)
}
return -1, 0, 0, fmt.Errorf("unexpected len %d for %s", len(pip), pip)
}