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 Ratelimit using X-Ratelimit-Limit header #118

Merged
merged 2 commits into from Nov 21, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions go.mod
Expand Up @@ -6,14 +6,15 @@ require (
github.com/json-iterator/go v1.1.12
github.com/pkg/errors v0.9.1
github.com/projectdiscovery/gologger v1.1.4
github.com/projectdiscovery/httputil v0.0.0-20210906072657-f3a099cb20bc
github.com/projectdiscovery/retryablehttp-go v1.0.2
)

require (
github.com/logrusorgru/aurora v2.0.3+incompatible // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
golang.org/x/net v0.0.0-20210521195947-fe42d452be8f // indirect
golang.org/x/text v0.3.6 // indirect
github.com/projectdiscovery/ratelimit v0.0.1
github.com/projectdiscovery/utils v0.0.3
golang.org/x/net v0.1.0 // indirect
golang.org/x/text v0.4.0 // indirect
)
21 changes: 14 additions & 7 deletions go.sum
Expand Up @@ -25,25 +25,32 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/projectdiscovery/gologger v1.1.4 h1:qWxGUq7ukHWT849uGPkagPKF3yBPYAsTtMKunQ8O2VI=
github.com/projectdiscovery/gologger v1.1.4/go.mod h1:Bhb6Bdx2PV1nMaFLoXNBmHIU85iROS9y1tBuv7T5pMY=
github.com/projectdiscovery/httputil v0.0.0-20210906072657-f3a099cb20bc h1:C0L6pUvVI+sPJSBaPQJEG/HjPtg8Mgs2vEpsdrl064A=
github.com/projectdiscovery/httputil v0.0.0-20210906072657-f3a099cb20bc/go.mod h1:BueJPSPWAX11IFS6bdAqTkekiIz5Fgco5LVc1kqO9L4=
github.com/projectdiscovery/ratelimit v0.0.1 h1:GnCfbKmkLdDLXT3QS4KS0zCsuDGkoRQE0YDbTqzQmS8=
github.com/projectdiscovery/ratelimit v0.0.1/go.mod h1:zenrIElIcKg0Y9h7pMfTlw5vaI/kCl8uxXm+PfgbBSw=
github.com/projectdiscovery/retryablehttp-go v1.0.2 h1:LV1/KAQU+yeWhNVlvveaYFsjBYRwXlNEq0PvrezMV0U=
github.com/projectdiscovery/retryablehttp-go v1.0.2/go.mod h1:dx//aY9V247qHdsRf0vdWHTBZuBQ2vm6Dq5dagxrDYI=
github.com/projectdiscovery/utils v0.0.3 h1:pAjZTGYpnATRc6uaNACdiHv4joZ0Ml7Wpu0dudpcGfM=
github.com/projectdiscovery/utils v0.0.3/go.mod h1:ne3eSlZlUKuhjHr8FfsfGcGteCzxcbJvFBx4VDBCxK0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/net v0.0.0-20210521195947-fe42d452be8f h1:Si4U+UcgJzya9kpiEUJKQvjr512OLli+gL4poHrz93U=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
golang.org/x/net v0.0.0-20210521195947-fe42d452be8f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.1.0 h1:hZ/3BUoy5aId7sCpA/Tc5lt8DkFgdVS2onTpJsZ/fl0=
golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
39 changes: 30 additions & 9 deletions pkg/chaos/chaos.go
Expand Up @@ -2,22 +2,47 @@ package chaos

import (
"bufio"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strconv"
"strings"
"time"

jsoniter "github.com/json-iterator/go"
"github.com/pkg/errors"
pdhttputil "github.com/projectdiscovery/httputil"
"github.com/projectdiscovery/ratelimit"
"github.com/projectdiscovery/retryablehttp-go"
pdhttputil "github.com/projectdiscovery/utils/http"
)

// Client is a client for making requests to chaos API
type Client struct {
apiKey string
httpClient *retryablehttp.Client
ratelimit *ratelimit.Limiter
}

// do adds apiKey and implements rate limit
func (c *Client) do(request *retryablehttp.Request) (*http.Response, error) {
request.Header.Set("Authorization", c.apiKey)
if c.ratelimit != nil {
c.ratelimit.Take()
}
resp, err := c.httpClient.Do(request)
if err != nil {
if c.ratelimit == nil {
rl := resp.Header.Get("X-Ratelimit-Limit")
rlMax, er := strconv.Atoi(rl)
if er == nil {
// if er then ratelimit header is not present. Hence no rate limit
c.ratelimit = ratelimit.New(context.Background(), int64(rlMax), time.Minute)
}
}
}
return resp, err
}

// New creates a new client for chaos API communication
Expand All @@ -42,9 +67,8 @@ func (c *Client) GetStatistics(req *GetStatisticsRequest) (*GetStatisticsRespons
if err != nil {
return nil, errors.Wrap(err, "could not create request.")
}
request.Header.Set("Authorization", c.apiKey)

resp, err := c.httpClient.Do(request)
resp, err := c.do(request)
if err != nil {
return nil, errors.Wrap(err, "could not make request.")
}
Expand Down Expand Up @@ -92,9 +116,8 @@ func (c *Client) GetSubdomains(req *SubdomainsRequest) chan *Result {
results <- &Result{Error: errors.Wrap(err, "could not create request.")}
return
}
request.Header.Set("Authorization", c.apiKey)

resp, err := c.httpClient.Do(request)
resp, err := c.do(request)
if err != nil {
results <- &Result{Error: errors.Wrap(err, "could not make request.")}
return
Expand Down Expand Up @@ -185,9 +208,8 @@ func (c *Client) GetBBQSubdomains(req *SubdomainsRequest) chan *BBQResult {
results <- &BBQResult{Error: errors.Wrap(err, "could not create request.")}
return
}
request.Header.Set("Authorization", c.apiKey)

resp, err := c.httpClient.Do(request)
resp, err := c.do(request)
if err != nil {
results <- &BBQResult{Error: errors.Wrap(err, "could not make request.")}
return
Expand Down Expand Up @@ -237,9 +259,8 @@ func (c *Client) PutSubdomains(req *PutSubdomainsRequest) (*PutSubdomainsRespons
if err != nil {
return nil, errors.Wrap(err, "could not create request.")
}
request.Header.Set("Authorization", c.apiKey)

resp, err := c.httpClient.Do(request)
resp, err := c.do(request)
if err != nil {
return nil, errors.Wrap(err, "could not make request.")
}
Expand Down