adds -track-error option to add custom errors to max-host-error watchlist (#3399)

* Allow user to specify for "context deadline exceeded" errors to count toward the max host error count

* Convert flag to a string slice `--track-error`

* Minimize diff

* Add documentation for `-track-error`

* adds unit test & minor improvements

* update flag description

---------

Co-authored-by: Austin Traver <austin_traver@intuit.com>
Co-authored-by: Tarun Koyalwar <tarun@projectdiscovery.io>
This commit is contained in:
Austin Traver
2023-03-14 01:29:42 -07:00
committed by GitHub
parent bcb1a8811d
commit 0d90a555f6
11 changed files with 43 additions and 9 deletions

View File

@@ -30,6 +30,7 @@ type Cache struct {
MaxHostError int
verbose bool
failedTargets gcache.Cache
TrackError []string
}
type cacheItem struct {
@@ -40,11 +41,11 @@ type cacheItem struct {
const DefaultMaxHostsCount = 10000
// New returns a new host max errors cache
func New(maxHostError, maxHostsCount int) *Cache {
func New(maxHostError, maxHostsCount int, trackError []string) *Cache {
gc := gcache.New(maxHostsCount).
ARC().
Build()
return &Cache{failedTargets: gc, MaxHostError: maxHostError}
return &Cache{failedTargets: gc, MaxHostError: maxHostError, TrackError: trackError}
}
// SetVerbose sets the cache to log at verbose level
@@ -128,6 +129,14 @@ var reCheckError = regexp.MustCompile(`(no address found for host|Client\.Timeou
// checkError checks if an error represents a type that should be
// added to the host skipping table.
func (c *Cache) checkError(err error) bool {
if err == nil {
return false
}
errString := err.Error()
for _, msg := range c.TrackError {
if strings.Contains(errString, msg) {
return true
}
}
return reCheckError.MatchString(errString)
}