Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Alive Proxy into Options #5903

Merged
merged 2 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 7 additions & 5 deletions internal/runner/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import (
proxyutils "github.com/projectdiscovery/utils/proxy"
)

const (
HTTP_PROXY_ENV = "HTTP_PROXY"
)

// loadProxyServers load list of proxy servers from file or comma separated
func loadProxyServers(options *types.Options) error {
if len(options.Proxy) == 0 {
Expand Down Expand Up @@ -48,15 +52,13 @@ func loadProxyServers(options *types.Options) error {
return errorutil.WrapfWithNil(err, "failed to parse proxy got %v", err)
}
if options.ProxyInternal {
os.Setenv(types.HTTP_PROXY_ENV, proxyURL.String())
os.Setenv(HTTP_PROXY_ENV, proxyURL.String())
}
if proxyURL.Scheme == proxyutils.HTTP || proxyURL.Scheme == proxyutils.HTTPS {
types.ProxyURL = proxyURL.String()
types.ProxySocksURL = ""
gologger.Verbose().Msgf("Using %s as proxy server", proxyURL.String())
options.AliveHttpProxy = proxyURL.String()
} else if proxyURL.Scheme == proxyutils.SOCKS5 {
types.ProxyURL = ""
types.ProxySocksURL = proxyURL.String()
options.AliveSocksProxy = proxyURL.String()
gologger.Verbose().Msgf("Using %s as socket proxy server", proxyURL.String())
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion internal/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func New(options *types.Options) (*Runner, error) {
runner.catalog = disk.NewCatalog(config.DefaultConfig.TemplatesDirectory)

var httpclient *retryablehttp.Client
if options.ProxyInternal && types.ProxyURL != "" || types.ProxySocksURL != "" {
if options.ProxyInternal && options.AliveHttpProxy != "" || options.AliveSocksProxy != "" {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Update field name for consistency

Once the field name in types.go is updated to AliveHTTPProxy, this reference should be updated as well.

-	if options.ProxyInternal && options.AliveHttpProxy != "" || options.AliveSocksProxy != "" {
+	if options.ProxyInternal && options.AliveHTTPProxy != "" || options.AliveSocksProxy != "" {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if options.ProxyInternal && options.AliveHttpProxy != "" || options.AliveSocksProxy != "" {
if options.ProxyInternal && options.AliveHTTPProxy != "" || options.AliveSocksProxy != "" {

var err error
httpclient, err = httpclientpool.Get(options, &httpclientpool.Configuration{})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion lib/sdk_private.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (e *NucleiEngine) init(ctx context.Context) error {
_ = protocolinit.Init(e.opts)
})

if e.opts.ProxyInternal && types.ProxyURL != "" || types.ProxySocksURL != "" {
if e.opts.ProxyInternal && e.opts.AliveHttpProxy != "" || e.opts.AliveSocksProxy != "" {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix the logical operator in proxy condition.

The condition combines multiple checks with incorrect operator precedence which could lead to unintended behavior.

Apply this diff to fix the condition:

-if e.opts.ProxyInternal && e.opts.AliveHttpProxy != "" || e.opts.AliveSocksProxy != "" {
+if e.opts.ProxyInternal && (e.opts.AliveHttpProxy != "" || e.opts.AliveSocksProxy != "") {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if e.opts.ProxyInternal && e.opts.AliveHttpProxy != "" || e.opts.AliveSocksProxy != "" {
if e.opts.ProxyInternal && (e.opts.AliveHttpProxy != "" || e.opts.AliveSocksProxy != "") {

httpclient, err := httpclientpool.Get(e.opts, &httpclientpool.Configuration{})
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions pkg/protocols/common/protocolstate/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ func Init(options *types.Options) error {
},
}
}
if types.ProxySocksURL != "" {
proxyURL, err := url.Parse(types.ProxySocksURL)
if options.AliveSocksProxy != "" {
proxyURL, err := url.Parse(options.AliveSocksProxy)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/protocols/headless/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ func New(options *types.Options) (*Browser, error) {
} else {
chromeLauncher = chromeLauncher.Headless(true)
}
if types.ProxyURL != "" {
chromeLauncher = chromeLauncher.Proxy(types.ProxyURL)
if options.AliveHttpProxy != "" {
chromeLauncher = chromeLauncher.Proxy(options.AliveHttpProxy)
}

for k, v := range options.ParseHeadlessOptionalArguments() {
Expand Down
8 changes: 4 additions & 4 deletions pkg/protocols/headless/engine/http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ func newHttpClient(options *types.Options) (*http.Client, error) {
MaxConnsPerHost: 500,
TLSClientConfig: tlsConfig,
}
if types.ProxyURL != "" {
if proxyURL, err := url.Parse(types.ProxyURL); err == nil {
if options.AliveHttpProxy != "" {
if proxyURL, err := url.Parse(options.AliveHttpProxy); err == nil {
transport.Proxy = http.ProxyURL(proxyURL)
}
} else if types.ProxySocksURL != "" {
socksURL, proxyErr := url.Parse(types.ProxySocksURL)
} else if options.AliveSocksProxy != "" {
socksURL, proxyErr := url.Parse(options.AliveSocksProxy)
if proxyErr != nil {
return nil, err
}
Expand Down
16 changes: 8 additions & 8 deletions pkg/protocols/http/httpclientpool/clientpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,10 @@ func (c *Configuration) HasStandardOptions() bool {
func GetRawHTTP(options *protocols.ExecutorOptions) *rawhttp.Client {
rawHttpClientOnce.Do(func() {
rawHttpOptions := rawhttp.DefaultOptions
if types.ProxyURL != "" {
rawHttpOptions.Proxy = types.ProxyURL
} else if types.ProxySocksURL != "" {
rawHttpOptions.Proxy = types.ProxySocksURL
if options.Options.AliveHttpProxy != "" {
rawHttpOptions.Proxy = options.Options.AliveHttpProxy
} else if options.Options.AliveSocksProxy != "" {
rawHttpOptions.Proxy = options.Options.AliveSocksProxy
} else if protocolstate.Dialer != nil {
rawHttpOptions.FastDialer = protocolstate.Dialer
}
Expand Down Expand Up @@ -278,12 +278,12 @@ func wrappedGet(options *types.Options, configuration *Configuration) (*retryabl
ResponseHeaderTimeout: responseHeaderTimeout,
}

if types.ProxyURL != "" {
if proxyURL, err := url.Parse(types.ProxyURL); err == nil {
if options.AliveHttpProxy != "" {
if proxyURL, err := url.Parse(options.AliveHttpProxy); err == nil {
transport.Proxy = http.ProxyURL(proxyURL)
}
} else if types.ProxySocksURL != "" {
socksURL, proxyErr := url.Parse(types.ProxySocksURL)
} else if options.AliveSocksProxy != "" {
socksURL, proxyErr := url.Parse(options.AliveSocksProxy)
ShubhamRasal marked this conversation as resolved.
Show resolved Hide resolved
if proxyErr != nil {
return nil, proxyErr
}
Expand Down
9 changes: 3 additions & 6 deletions pkg/reporting/trackers/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/projectdiscovery/nuclei/v3/pkg/reporting/exporters/markdown/util"
"github.com/projectdiscovery/nuclei/v3/pkg/reporting/format"
"github.com/projectdiscovery/nuclei/v3/pkg/reporting/trackers/filters"
"github.com/projectdiscovery/nuclei/v3/pkg/types"
"github.com/projectdiscovery/retryablehttp-go"
"golang.org/x/oauth2"
)
Expand Down Expand Up @@ -63,11 +62,9 @@ func New(options *Options) (*Integration, error) {
)
tc := oauth2.NewClient(ctx, ts)

// patch transport to support proxy - only http
// TODO: investigate if it's possible to reuse existing retryablehttp
if types.ProxyURL != "" {
if proxyURL, err := url.Parse(types.ProxyURL); err == nil {
tc.Transport.(*http.Transport).Proxy = http.ProxyURL(proxyURL)
if options.HttpClient != nil && options.HttpClient.HTTPClient != nil {
if tcTransport, ok := tc.Transport.(*http.Transport); ok {
tcTransport.Proxy = options.HttpClient.HTTPClient.Transport.(*http.Transport).Proxy
}
}

Expand Down
9 changes: 4 additions & 5 deletions pkg/reporting/trackers/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,12 @@ func (i *Integration) CreateIssue(event *output.ResultEvent) (*filters.CreateIss
}
if issue.State == "closed" {
reopen := "reopen"
_, resp, err := i.client.Issues.UpdateIssue(i.options.ProjectName, issue.IID, &gitlab.UpdateIssueOptions{
_, _, err := i.client.Issues.UpdateIssue(i.options.ProjectName, issue.IID, &gitlab.UpdateIssueOptions{
StateEvent: &reopen,
})
fmt.Sprintln(resp, err)
}
if err != nil {
return nil, err
if err != nil {
return nil, err
}
}
return &filters.CreateIssueResponse{
IssueID: strconv.FormatInt(int64(issue.ID), 10),
Expand Down
3 changes: 3 additions & 0 deletions pkg/reporting/trackers/linear/linear.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ func New(options *Options) (*Integration, error) {
Key: options.APIKey,
},
}
if options.HttpClient != nil {
httpClient.Transport = options.HttpClient.HTTPClient.Transport
}

integration := &Integration{
url: "https://api.linear.app/graphql",
Expand Down
12 changes: 0 additions & 12 deletions pkg/types/proxy.go

This file was deleted.

4 changes: 4 additions & 0 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ type Options struct {
ListDslSignatures bool
// List of HTTP(s)/SOCKS5 proxy to use (comma separated or file input)
Proxy goflags.StringSlice
// AliveProxy is the alive proxy to use
AliveHttpProxy string
// AliveSocksProxy is the alive socks proxy to use
AliveSocksProxy string
// TemplatesDirectory is the directory to use for storing templates
NewTemplatesDirectory string
// TraceLogFile specifies a file to write with the trace of all requests
Expand Down
Loading