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

v0.4.0 #121

Merged
merged 14 commits into from Nov 22, 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
3 changes: 1 addition & 2 deletions .github/workflows/build-test.yml
@@ -1,7 +1,6 @@
name: 🔨 Build Test

on:
push:
pull_request:
workflow_dispatch:

Expand All @@ -15,7 +14,7 @@ jobs:

- uses: actions/setup-go@v3
with:
go-version: 1.17
go-version: 1.18

- name: Test
run: go test ./...
Expand Down
4 changes: 1 addition & 3 deletions .github/workflows/codeql-analysis.yml
@@ -1,10 +1,8 @@
name: 🚨 CodeQL Analysis

on:
workflow_dispatch:
pull_request:
branches:
- dev
workflow_dispatch:

jobs:
analyze:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/dockerhub-push.yml
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Get Github tag
id: meta
run: |
echo "::set-output name=tag::$(curl --silent "https://api.github.com/repos/projectdiscovery/chaos-client/releases/latest" | jq -r .tag_name)"
curl --silent "https://api.github.com/repos/projectdiscovery/chaos-client/releases/latest" | jq -r .tag_name | xargs -I {} echo TAG={} >> $GITHUB_OUTPUT

- name: Set up QEMU
uses: docker/setup-qemu-action@v2
Expand All @@ -37,4 +37,4 @@ jobs:
context: .
platforms: linux/amd64,linux/arm64,linux/arm
push: true
tags: projectdiscovery/chaos-client:latest,projectdiscovery/chaos-client:${{ steps.meta.outputs.tag }}
tags: projectdiscovery/chaos-client:latest,projectdiscovery/chaos-client:${{ steps.meta.outputs.TAG }}
11 changes: 7 additions & 4 deletions .github/workflows/lint-test.yml
@@ -1,6 +1,6 @@
name: 🙏🏻 Lint Test

on:
push:
pull_request:
workflow_dispatch:

Expand All @@ -11,10 +11,13 @@ jobs:
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.18
- name: Run golangci-lint
uses: golangci/golangci-lint-action@v3.2.0
uses: golangci/golangci-lint-action@v3.3.1
with:
version: latest
args: --timeout 5m
working-directory: .
working-directory: .
7 changes: 5 additions & 2 deletions .github/workflows/release-binary.yml
Expand Up @@ -2,6 +2,8 @@ name: 🎉 Release Binary

on:
create:
tags:
- v*
workflow_dispatch:

jobs:
Expand All @@ -21,5 +23,6 @@ jobs:
args: "release --rm-dist"
version: latest
workdir: .
env:
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
env:
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
SLACK_WEBHOOK: "${{ secrets.RELEASE_SLACK_WEBHOOK }}"
7 changes: 7 additions & 0 deletions .goreleaser.yml
Expand Up @@ -33,3 +33,10 @@ archives:

checksum:
algorithm: sha256

announce:
slack:
enabled: true
channel: '#release'
username: GoReleaser
message_template: '{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .ReleaseURL }}'
2 changes: 1 addition & 1 deletion Dockerfile
@@ -1,4 +1,4 @@
FROM golang:1.19.0-alpine as build-env
FROM golang:1.19.3-alpine as build-env
RUN go install -v github.com/projectdiscovery/chaos-client/cmd/chaos@latest

FROM alpine:latest
Expand Down
9 changes: 5 additions & 4 deletions go.mod
@@ -1,19 +1,20 @@
module github.com/projectdiscovery/chaos-client

go 1.17
go 1.18

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=
4 changes: 2 additions & 2 deletions internal/runner/banner.go
Expand Up @@ -7,11 +7,11 @@ const banner = `
_____/ /_ ____ _____ _____
/ ___/ __ \/ __ / __ \/ ___/
/ /__/ / / / /_/ / /_/ (__ )
\___/_/ /_/\__,_/\____/____/ v0.3.0
\___/_/ /_/\__,_/\____/____/ v0.4.0
`

// Version is the current version of chaos
const Version = `0.3.0`
const Version = `0.4.0`

// showBanner is used to show the banner to the user
func showBanner() {
Expand Down
50 changes: 35 additions & 15 deletions pkg/chaos/chaos.go
Expand Up @@ -2,23 +2,47 @@ package chaos

import (
"bufio"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"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 @@ -43,15 +67,14 @@ 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.")
}

if resp.StatusCode != http.StatusOK {
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "could not read response.")
}
Expand Down Expand Up @@ -93,16 +116,15 @@ 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
}

if resp.StatusCode != http.StatusOK {
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
results <- &Result{Error: errors.Wrap(err, "could not read response.")}
return
Expand Down Expand Up @@ -186,16 +208,15 @@ 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
}

if resp.StatusCode != http.StatusOK {
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
results <- &BBQResult{Error: errors.Wrap(err, "could not read response.")}
return
Expand Down Expand Up @@ -238,22 +259,21 @@ 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.")
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "could not read response.")
}
return nil, InvalidStatusCodeError{StatusCode: resp.StatusCode, Message: body}
}
_, _ = io.Copy(ioutil.Discard, resp.Body)
_, _ = io.Copy(io.Discard, resp.Body)
return &PutSubdomainsResponse{}, nil
}

Expand Down