From 34976029d39ef2e7d35cdecc789767c4f053df9c Mon Sep 17 00:00:00 2001 From: Mzack9999 Date: Sat, 24 Dec 2022 14:52:14 +0100 Subject: [PATCH] removing most go routine leaks (#3073) Co-authored-by: sandeep <8293321+ehsandeep@users.noreply.github.com> --- v2/cmd/integration-test/code.go | 4 +++- v2/cmd/nuclei/main.go | 1 + v2/go.mod | 2 +- v2/go.sum | 4 ++-- v2/internal/runner/runner.go | 3 +++ v2/pkg/protocols/common/interactsh/interactsh.go | 12 ++++++++++++ v2/pkg/protocols/common/uncover/uncover.go | 3 +++ 7 files changed, 25 insertions(+), 4 deletions(-) diff --git a/v2/cmd/integration-test/code.go b/v2/cmd/integration-test/code.go index 73414d21..8b8fe5b3 100644 --- a/v2/cmd/integration-test/code.go +++ b/v2/cmd/integration-test/code.go @@ -93,13 +93,15 @@ func executeNucleiAsCode(templatePath, templateURL string) ([]string, error) { home, _ := os.UserHomeDir() catalog := disk.NewCatalog(path.Join(home, "nuclei-templates")) + ratelimiter := ratelimit.New(context.Background(), 150, time.Second) + defer ratelimiter.Stop() executerOpts := protocols.ExecuterOptions{ Output: outputWriter, Options: defaultOpts, Progress: mockProgress, Catalog: catalog, IssuesClient: reportingClient, - RateLimiter: ratelimit.New(context.Background(), 150, time.Second), + RateLimiter: ratelimiter, Interactsh: interactClient, HostErrorsCache: cache, Colorizer: aurora.NewAurora(true), diff --git a/v2/cmd/nuclei/main.go b/v2/cmd/nuclei/main.go index 04f84e78..98f4e9d1 100644 --- a/v2/cmd/nuclei/main.go +++ b/v2/cmd/nuclei/main.go @@ -82,6 +82,7 @@ func main() { // Setup graceful exits resumeFileName := types.DefaultResumeFilePath() c := make(chan os.Signal, 1) + defer close(c) signal.Notify(c, os.Interrupt) go func() { for range c { diff --git a/v2/go.mod b/v2/go.mod index 94e12723..234e4859 100644 --- a/v2/go.mod +++ b/v2/go.mod @@ -74,7 +74,7 @@ require ( github.com/projectdiscovery/fasttemplate v0.0.2 github.com/projectdiscovery/goflags v0.1.6 github.com/projectdiscovery/nvd v1.0.9 - github.com/projectdiscovery/ratelimit v0.0.3 + github.com/projectdiscovery/ratelimit v0.0.4-0.20221222024635-17151503fe59 github.com/projectdiscovery/rdap v0.9.1-0.20221108103045-9865884d1917 github.com/projectdiscovery/sarif v0.0.1 github.com/projectdiscovery/tlsx v1.0.0 diff --git a/v2/go.sum b/v2/go.sum index 39726757..5ca19e6f 100644 --- a/v2/go.sum +++ b/v2/go.sum @@ -629,8 +629,8 @@ github.com/projectdiscovery/networkpolicy v0.0.3 h1:OZFPkMVY6SJxc1ncuRXB2VlT6xlz github.com/projectdiscovery/networkpolicy v0.0.3/go.mod h1:DIXwKs3sQyfCoWHKRLQiRrEorSQW4Zrh4ftu7oDVK6w= github.com/projectdiscovery/nvd v1.0.9 h1:2DdMm7lu3GnCQsyYDEQiQ/LRYDmpEm654kvGQS6jzjE= github.com/projectdiscovery/nvd v1.0.9/go.mod h1:nGHAo7o6G4V4kscZlm488qKp/ZrZYiBoKqAQrn3X4Og= -github.com/projectdiscovery/ratelimit v0.0.3 h1:6c8QKL3ivOdjXqbP+UA2jsTNHcH0OMHk+4AMkanOkUo= -github.com/projectdiscovery/ratelimit v0.0.3/go.mod h1:WBz8N1P+CyxnfUoGfVCqah4NZ2SreSX7v9dY8wIlK70= +github.com/projectdiscovery/ratelimit v0.0.4-0.20221222024635-17151503fe59 h1:XHi//FUJTo+RRKfLT9Mj6oqkQq9E65uNMhL2RVuG5zA= +github.com/projectdiscovery/ratelimit v0.0.4-0.20221222024635-17151503fe59/go.mod h1:WBz8N1P+CyxnfUoGfVCqah4NZ2SreSX7v9dY8wIlK70= github.com/projectdiscovery/rawhttp v0.1.4 h1:zdOqU1DUY2dGoyCzBqzHnHVwPyv32j+Hke8KfLRUouI= github.com/projectdiscovery/rawhttp v0.1.4/go.mod h1:mhSXo96awUUr20VdReDYUKxldsvR5841FRgiaoaxDCY= github.com/projectdiscovery/rdap v0.9.1-0.20221108103045-9865884d1917 h1:m03X4gBVSorSzvmm0bFa7gDV4QNSOWPL/fgZ4kTXBxk= diff --git a/v2/internal/runner/runner.go b/v2/internal/runner/runner.go index 3f140883..8ce6e2c4 100644 --- a/v2/internal/runner/runner.go +++ b/v2/internal/runner/runner.go @@ -332,6 +332,9 @@ func (r *Runner) Close() { if r.pprofServer != nil { _ = r.pprofServer.Shutdown(context.Background()) } + if r.ratelimiter != nil { + r.ratelimiter.Stop() + } } // RunEnumeration sets up the input layer for giving input nuclei. diff --git a/v2/pkg/protocols/common/interactsh/interactsh.go b/v2/pkg/protocols/common/interactsh/interactsh.go index 9e9ea638..40c212b8 100644 --- a/v2/pkg/protocols/common/interactsh/interactsh.go +++ b/v2/pkg/protocols/common/interactsh/interactsh.go @@ -261,6 +261,18 @@ func (c *Client) Close() bool { c.interactsh.StopPolling() c.interactsh.Close() } + + closeCache := func(cc *ccache.Cache) { + if cc != nil { + cc.Clear() + cc.Stop() + } + } + closeCache(c.requests) + closeCache(c.interactions) + closeCache(c.matchedTemplates) + closeCache(c.interactshURLs) + return c.matched } diff --git a/v2/pkg/protocols/common/uncover/uncover.go b/v2/pkg/protocols/common/uncover/uncover.go index b4a60049..b8fd2cf9 100644 --- a/v2/pkg/protocols/common/uncover/uncover.go +++ b/v2/pkg/protocols/common/uncover/uncover.go @@ -107,6 +107,9 @@ func getTargets(uncoverOptions *ucRunner.Options, field string) (chan string, er } else { rateLimiter = ratelimit.NewUnlimited(context.Background()) } + if rateLimiter != nil { + defer rateLimiter.Stop() + } var agents []uncover.Agent // declare clients for _, engine := range uncoverOptions.Engine {