nuclei icon indicating copy to clipboard operation
nuclei copied to clipboard

globals => shared struct

Open Mzack9999 opened this issue 1 year ago • 1 comments
trafficstars

Proposed changes

Closes #5239

Checklist

  • [ ] Pull request is created against the dev branch
  • [ ] All checks passed (lint, unit/integration/regression tests etc.) with my changes
  • [ ] I have added tests that prove my fix is effective or that my feature works
  • [ ] I have added necessary documentation (if appropriate)

Mzack9999 avatar Jun 19 '24 09:06 Mzack9999

sharing some thoughts about removing global dialer from javascript protocols

i think the challenging part would be to remove global dialer references from javascript modules at js/libs from my research on this it seems there are two ways

  • fork goja and add context support to .Run and expose it as first argument of any/all functions/methods ( ex: currently in goja, when first argument of function is set to runtime instance (https://github.com/projectdiscovery/nuclei/blob/9a441c23cce354c5575bafa4811422c9ae86dfbc/pkg/js/libs/bytes/buffer.go#L29-L40) , the current runtime used in exposed/shared here. in similar way we can add support for context.Context, this will be helpful to pass dialer instance with context.Value as well as context support in javascript protocol ( which is not supported yet, we have global runtime timeout but that's not the same thing i guess)

  • another way i can think is similar to ^ but instead of fork and updating core, we can use code generation while generating bindings ( https://github.com/projectdiscovery/nuclei/blob/dev/pkg/js/generated/go/libbytes/bytes.go)

imo i think more scalable + easier approach would be 1) because it will improve other components as well ( ex: flow , pre-condition etc )

cc: @Mzack9999 , @Ice3man543

tarunKoyalwar avatar Jun 24 '24 16:06 tarunKoyalwar

Closing as outdated

Mzack9999 avatar May 01 '25 21:05 Mzack9999

[!CAUTION]

Review failed

The pull request is closed.

Walkthrough

This change removes the use of global singleton client pools and dialers across the codebase, replacing them with per-instance "Dialers" objects that are created and passed through execution options. All protocol modules (HTTP, DNS, network, RDAP, etc.) now obtain their clients from these Dialers instances rather than global state. Client pooling logic is removed, and clients are instantiated as needed. Test utilities and test cases are updated to handle the new dialer initialization and error handling. Panic recovery is removed from several locations, and debug option handling is consolidated. The codebase is refactored to support multiple independent nuclei engine instances without global resource conflicts.

Changes

Files/Groups Change Summary
pkg/protocols/protocols.go, lib/sdk.go, lib/sdk_private.go, cmd/integration-test/library.go, internal/runner/runner.go, internal/runner/options.go, lib/multi.go, pkg/tmplexec/flow/flow_executor_test.go, pkg/tmplexec/multiproto/multi_test.go, pkg/testutils/testutils.go, pkg/tmplexec/flow/flow_executor.go Introduce per-instance Dialers struct, update ExecutorOptions, refactor initialization, remove panic recovery, update test setup.
pkg/protocols/http/httpclientpool/clientpool.go, pkg/protocols/dns/dnsclientpool/clientpool.go, pkg/protocols/network/networkclientpool/clientpool.go, pkg/protocols/whois/rdapclientpool/clientpool.go Remove global client pools, delete pooling logic, always create fresh clients, delete related types and functions.
pkg/protocols/common/protocolinit/init.go, pkg/protocols/common/protocolstate/state.go Remove singleton initialization, refactor dialer creation and assignment, separate dialer logic.
pkg/protocols/http/http.go, pkg/protocols/http/request.go, pkg/protocols/http/build_request_test.go, pkg/protocols/http/http_test.go, pkg/protocols/http/operators_test.go, pkg/protocols/http/request_test.go Refactor to use dialers from ExecutorOptions, update client acquisition logic, update tests for new error handling.
pkg/protocols/dns/dns.go, pkg/protocols/dns/request_test.go, pkg/protocols/dns/operators_test.go, pkg/protocols/dns/dns_test.go Use dialers for DNS client, remove global pool usage, update tests for error handling.
pkg/protocols/network/network.go, pkg/protocols/network/request.go, pkg/protocols/network/network_test.go, pkg/protocols/network/operators_test.go Use per-instance dialers, remove global pool usage, update tests for error handling.
pkg/protocols/ssl/ssl.go, pkg/protocols/ssl/ssl_test.go Use dialers for SSL requests, update tests for error handling.
pkg/protocols/websocket/websocket.go Use dialers for WebSocket requests, remove pool usage.
pkg/protocols/whois/whois.go Use dialers for RDAP client, remove pool usage.
pkg/protocols/common/automaticscan/automaticscan.go, pkg/protocols/common/dsl/dsl.go Refactor client acquisition to use dialers, adjust retry and resolver logic.
pkg/js/compiler/compiler.go, pkg/js/compiler/pool.go, pkg/protocols/headless/engine/page_actions.go, pkg/tmplexec/flow/flow_executor.go, pkg/testutils/fuzzplayground/server.go Remove panic recovery logic and middleware.
pkg/reporting/exporters/es/elasticsearch.go, pkg/reporting/exporters/splunk/splunkhec.go Require explicit HTTP client in options, remove internal client creation.
pkg/protocols/offlinehttp/find_test.go, pkg/protocols/offlinehttp/operators_test.go, pkg/templates/cluster_test.go, pkg/protocols/file/find_test.go, pkg/protocols/file/operators_test.go, pkg/protocols/file/request_test.go Update tests to use dialers and handle errors from mock executer options.
pkg/types/types.go Add HasDebug() method to consolidate debug flag checks.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Engine
    participant Dialers
    participant ProtocolModule

    User->>Engine: Initialize NucleiEngine(options)
    Engine->>Dialers: NewDialers(options)
    Dialers-->>Engine: Dialers instance
    Engine->>ProtocolModule: ExecutorOptions{Dialers: dialers}
    ProtocolModule->>Dialers: Obtain client (HTTP/DNS/Network/etc.)
    ProtocolModule-->>Engine: Use protocol client for execution

Assessment against linked issues

Objective Addressed Explanation
Remove global singletons for protocol clients and dialers (#5239)
Allow multiple nuclei instances with separate configs, no resource collision (#5239)
Refactor all protocol modules to use per-instance dialers, not global state (#5239)
Remove all client pooling and singleton logic from protocol client pools (#5239)

Possibly related PRs

  • projectdiscovery/nuclei#5903: Also refactors dialer and proxy integration, replacing global proxy variables with per-instance dialers and updating runner and executor option initialization, showing strong code-level overlap.

Poem

In the warren of code, where the dialers once slept,
Singleton bunnies have quietly crept.
Now each rabbit may burrow, with configs unique—
No more global carrots, no more hide-and-seek!
With fresh new dialers, the protocols hop,
Each engine instance can freely flip-flop.
Hooray for the patch—now the garden is neat,
And every bunny's connection is sweet! 🐇✨


📜 Recent review details

Configuration used: CodeRabbit UI Review profile: CHILL Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between cc03b7591a70822dd1aa7265f875c63ed3795af2 and 6aaabb4544bd35d4399ec98c63ff8284cb39ccb7.

📒 Files selected for processing (52)
  • cmd/integration-test/library.go (2 hunks)
  • internal/runner/options.go (1 hunks)
  • internal/runner/runner.go (3 hunks)
  • lib/multi.go (1 hunks)
  • lib/sdk.go (2 hunks)
  • lib/sdk_private.go (2 hunks)
  • pkg/js/compiler/compiler.go (0 hunks)
  • pkg/js/compiler/pool.go (0 hunks)
  • pkg/operators/common/dsl/dsl.go (1 hunks)
  • pkg/protocols/code/code_test.go (1 hunks)
  • pkg/protocols/common/automaticscan/automaticscan.go (1 hunks)
  • pkg/protocols/common/protocolinit/init.go (0 hunks)
  • pkg/protocols/common/protocolstate/state.go (7 hunks)
  • pkg/protocols/dns/dns.go (1 hunks)
  • pkg/protocols/dns/dns_test.go (3 hunks)
  • pkg/protocols/dns/dnsclientpool/clientpool.go (2 hunks)
  • pkg/protocols/dns/operators_test.go (5 hunks)
  • pkg/protocols/dns/request_test.go (2 hunks)
  • pkg/protocols/file/find_test.go (1 hunks)
  • pkg/protocols/file/operators_test.go (5 hunks)
  • pkg/protocols/file/request_test.go (2 hunks)
  • pkg/protocols/headless/engine/page_actions.go (0 hunks)
  • pkg/protocols/http/build_request_test.go (5 hunks)
  • pkg/protocols/http/http.go (3 hunks)
  • pkg/protocols/http/http_test.go (1 hunks)
  • pkg/protocols/http/httpclientpool/clientpool.go (1 hunks)
  • pkg/protocols/http/operators_test.go (4 hunks)
  • pkg/protocols/http/request.go (3 hunks)
  • pkg/protocols/http/request_test.go (4 hunks)
  • pkg/protocols/network/network.go (1 hunks)
  • pkg/protocols/network/network_test.go (1 hunks)
  • pkg/protocols/network/networkclientpool/clientpool.go (0 hunks)
  • pkg/protocols/network/operators_test.go (4 hunks)
  • pkg/protocols/network/request.go (1 hunks)
  • pkg/protocols/network/request_test.go (1 hunks)
  • pkg/protocols/offlinehttp/find_test.go (1 hunks)
  • pkg/protocols/offlinehttp/operators_test.go (5 hunks)
  • pkg/protocols/protocols.go (3 hunks)
  • pkg/protocols/ssl/ssl.go (2 hunks)
  • pkg/protocols/ssl/ssl_test.go (1 hunks)
  • pkg/protocols/websocket/websocket.go (1 hunks)
  • pkg/protocols/whois/rdapclientpool/clientpool.go (0 hunks)
  • pkg/protocols/whois/whois.go (1 hunks)
  • pkg/reporting/exporters/es/elasticsearch.go (3 hunks)
  • pkg/reporting/exporters/splunk/splunkhec.go (2 hunks)
  • pkg/templates/cluster_test.go (1 hunks)
  • pkg/testutils/fuzzplayground/server.go (0 hunks)
  • pkg/testutils/testutils.go (2 hunks)
  • pkg/tmplexec/flow/flow_executor.go (0 hunks)
  • pkg/tmplexec/flow/flow_executor_test.go (2 hunks)
  • pkg/tmplexec/multiproto/multi_test.go (3 hunks)
  • pkg/types/types.go (1 hunks)
✨ Finishing Touches
  • [ ] 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

coderabbitai[bot] avatar May 01 '25 21:05 coderabbitai[bot]