testing fastdialer native

This commit is contained in:
Mzack9999
2024-06-11 12:21:43 +02:00
parent fd2311d034
commit e13c7cc967
4 changed files with 32 additions and 31 deletions

View File

@@ -1,6 +1,7 @@
package hosterrorscache
import (
"log"
"net"
"net/url"
"regexp"
@@ -8,7 +9,7 @@ import (
"sync"
"sync/atomic"
"github.com/bluele/gcache"
"github.com/Mzack9999/gcache"
"github.com/projectdiscovery/gologger"
"github.com/projectdiscovery/nuclei/v3/pkg/catalog/config"
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/contextargs"
@@ -37,7 +38,7 @@ var (
type Cache struct {
MaxHostError int
verbose bool
failedTargets gcache.Cache
failedTargets gcache.Cache[string, *cacheItem]
TrackError []string
}
@@ -52,7 +53,7 @@ const DefaultMaxHostsCount = 10000
// New returns a new host max errors cache
func New(maxHostError, maxHostsCount int, trackError []string) *Cache {
gc := gcache.New(maxHostsCount).
gc := gcache.New[string, *cacheItem](maxHostsCount).
ARC().
Build()
return &Cache{failedTargets: gc, MaxHostError: maxHostError, TrackError: trackError}
@@ -67,11 +68,7 @@ func (c *Cache) SetVerbose(verbose bool) {
func (c *Cache) Close() {
if config.DefaultConfig.IsDebugArgEnabled(config.DebugArgHostErrorStats) {
items := c.failedTargets.GetALL(false)
for k, v := range items {
val, ok := v.(*cacheItem)
if !ok {
continue
}
for k, val := range items {
gologger.Info().Label("MaxHostErrorStats").Msgf("Host: %s, Errors: %d", k, val.errors.Load())
}
}
@@ -115,16 +112,15 @@ func (c *Cache) Check(ctx *contextargs.Context) bool {
if err != nil {
return false
}
existingCacheItemValue := existingCacheItem.(*cacheItem)
if existingCacheItemValue.isPermanentErr {
if existingCacheItem.isPermanentErr {
// skipping permanent errors is expected so verbose instead of info
gologger.Verbose().Msgf("Skipped %s from target list as found unresponsive permanently: %s", finalValue, existingCacheItemValue.cause)
gologger.Verbose().Msgf("Skipped %s from target list as found unresponsive permanently: %s", finalValue, existingCacheItem.cause)
return true
}
if existingCacheItemValue.errors.Load() >= int32(c.MaxHostError) {
existingCacheItemValue.Do(func() {
gologger.Info().Msgf("Skipped %s from target list as found unresponsive %d times", finalValue, existingCacheItemValue.errors.Load())
if existingCacheItem.errors.Load() >= int32(c.MaxHostError) {
existingCacheItem.Do(func() {
gologger.Info().Msgf("Skipped %s from target list as found unresponsive %d times", finalValue, existingCacheItem.errors.Load())
})
return true
}
@@ -133,6 +129,7 @@ func (c *Cache) Check(ctx *contextargs.Context) bool {
// MarkFailed marks a host as failed previously
func (c *Cache) MarkFailed(ctx *contextargs.Context, err error) {
log.Printf("%#+v", err)
if !c.checkError(err) {
return
}
@@ -151,9 +148,8 @@ func (c *Cache) MarkFailed(ctx *contextargs.Context, err error) {
_ = c.failedTargets.Set(finalValue, newItem)
return
}
existingCacheItemValue := existingCacheItem.(*cacheItem)
existingCacheItemValue.errors.Add(1)
_ = c.failedTargets.Set(finalValue, existingCacheItemValue)
existingCacheItem.errors.Add(1)
_ = c.failedTargets.Set(finalValue, existingCacheItem)
}
// GetKeyFromContext returns the key for the cache from the context
@@ -186,10 +182,11 @@ func (c *Cache) checkError(err error) bool {
return false
}
// todo: fixed in fastdialer - ref: https://github.com/projectdiscovery/fastdialer/pull/301
// we do not consider timeouts as temporary
if strings.Contains(errkit.FromError(err).Cause().Error(), "i/o timeout") {
return true
}
// if strings.Contains(errkit.FromError(err).Cause().Error(), "i/o timeout") {
// return true
// }
kind := errkit.GetErrorKind(err, nucleierr.ErrTemplateLogic)
switch kind {

View File

@@ -91,9 +91,7 @@ func TestCacheMarkFailed(t *testing.T) {
require.Nil(t, err)
require.NotNil(t, failedTarget)
value, ok := failedTarget.(*cacheItem)
require.True(t, ok)
require.EqualValues(t, test.expected, value.errors.Load())
require.EqualValues(t, test.expected, failedTarget.errors.Load())
}
}
@@ -138,9 +136,7 @@ func TestCacheMarkFailedConcurrent(t *testing.T) {
require.Nil(t, err)
require.NotNil(t, failedTarget)
value, ok := failedTarget.(*cacheItem)
require.True(t, ok)
require.EqualValues(t, test.expected, value.errors.Load())
require.EqualValues(t, test.expected, failedTarget.errors.Load())
}
}