Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions lib/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package nuclei
import (
"context"
"errors"
"os"
"time"

"github.com/projectdiscovery/goflags"
Expand Down Expand Up @@ -559,3 +560,21 @@ func WithOptions(opts *pkgtypes.Options) NucleiSDKOptions {
return nil
}
}

// WithTemporaryDirectory allows setting a parent directory for SDK-managed temporary files.
// A temporary directory will be created inside the provided directory and cleaned up on engine close.
// If not set, a temporary directory will be automatically created in the system temp location.
// The parent directory will be created if it doesn't exist.
func WithTemporaryDirectory(parentDir string) NucleiSDKOptions {
return func(e *NucleiEngine) error {
if err := os.MkdirAll(parentDir, 0755); err != nil {
return err
}
tmpDir, err := os.MkdirTemp(parentDir, "nuclei-tmp-*")
if err != nil {
return err
}
e.tmpDir = tmpDir
return nil
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Comment thread
AuditeMarlow marked this conversation as resolved.
7 changes: 7 additions & 0 deletions lib/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"bytes"
"context"
"io"
"os"
"sync"

"github.com/projectdiscovery/gologger"
Expand Down Expand Up @@ -92,6 +93,9 @@ type NucleiEngine struct {

// Logger instance for the engine
Logger *gologger.Logger

// Temporary directory for SDK-managed template files
tmpDir string
}

// LoadAllTemplates loads all nuclei template based on given options
Expand Down Expand Up @@ -231,6 +235,9 @@ func (e *NucleiEngine) closeInternal() {
if e.httpxClient != nil {
_ = e.httpxClient.Close()
}
if e.tmpDir != "" {
_ = os.RemoveAll(e.tmpDir)
}
}

// Close all resources used by nuclei engine
Expand Down
36 changes: 23 additions & 13 deletions lib/sdk_private.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package nuclei
import (
"context"
"fmt"
"os"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -170,20 +171,29 @@ func (e *NucleiEngine) init(ctx context.Context) error {
e.catalog = disk.NewCatalog(config.DefaultConfig.TemplatesDirectory)
}

if e.tmpDir == "" {
tmpDir, err := os.MkdirTemp("", "nuclei-tmp-*")
if err != nil {
return err
}
e.tmpDir = tmpDir
}
Comment thread
AuditeMarlow marked this conversation as resolved.

e.executerOpts = &protocols.ExecutorOptions{
Output: e.customWriter,
Options: e.opts,
Progress: e.customProgress,
Catalog: e.catalog,
IssuesClient: e.rc,
RateLimiter: e.rateLimiter,
Interactsh: e.interactshClient,
Colorizer: aurora.NewAurora(true),
ResumeCfg: types.NewResumeCfg(),
Browser: e.browserInstance,
Parser: e.parser,
InputHelper: input.NewHelper(),
Logger: e.opts.Logger,
Output: e.customWriter,
Options: e.opts,
Progress: e.customProgress,
Catalog: e.catalog,
IssuesClient: e.rc,
RateLimiter: e.rateLimiter,
Interactsh: e.interactshClient,
Colorizer: aurora.NewAurora(true),
ResumeCfg: types.NewResumeCfg(),
Browser: e.browserInstance,
Parser: e.parser,
InputHelper: input.NewHelper(),
TemporaryDirectory: e.tmpDir,
Logger: e.opts.Logger,
}
if e.opts.ShouldUseHostError() && e.hostErrCache != nil {
e.executerOpts.HostErrorsCache = e.hostErrCache
Expand Down
Loading