Skip to content
Open
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
11 changes: 11 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module github.com/CarterTsai/http_attack_mq

go 1.20

require (
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/urfave/cli/v2 v2.25.0 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
)
26 changes: 26 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7 h1:lDH9UUVJtmYCjyT0CI4q8xvlXPxeZ0gYCVvWbmPlp88=
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7/go.mod h1:HzydrMdWErDVzsI23lYNej1Htcns9BCg93Dk0bBINWk=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
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/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
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/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/urfave/cli v1.22.12 h1:igJgVw1JdKH+trcLWLeLwZjU9fEfPesQ+9/e4MQ44S8=
github.com/urfave/cli v1.22.12/go.mod h1:sSBEIC79qR6OvcmsD4U3KABeOTxDqQtdDnaFuUN30b8=
github.com/urfave/cli/v2 v2.25.0 h1:ykdZKuQey2zq0yin/l7JOm9Mh+pg72ngYMeB0ABn6q8=
github.com/urfave/cli/v2 v2.25.0/go.mod h1:GHupkWPMM0M/sj1a2b4wUrWBPzazNrIjouW6fmdJLxc=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
4 changes: 2 additions & 2 deletions http_attack.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"strings"
"time"

"http_attack_mq/lib"
"github.com/CarterTsai/http_attack_mq/lib"

"github.com/op/go-logging"
"gopkg.in/urfave/cli.v2"
"github.com/urfave/cli/v2"
)

// VERSION the tool version
Expand Down
Binary file added http_attack_mq
Binary file not shown.
100 changes: 46 additions & 54 deletions lib/attack.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,32 @@ type Attack struct {
func (a *Attack) readBody(resp *http.Response) {
if a.Debug {
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
log.Infof("%s", body)
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Errorf("Error reading response body: %s", err)
return
}
log.Infof("Response body: %s", body)
}
}

func (a *Attack) do(req *http.Request, ii int) {
client := &http.Client{}
var startTime = time.Now()
resp, err := client.Do(req)
var endTime = time.Now()

if err != nil {
log.Errorf("Error sending request: %s", err)
return
}
defer resp.Body.Close()

if resp.StatusCode >= 400 {
log.Errorf("HTTP %s %s [%d] respTime %s", req.Method, req.URL.String(), resp.StatusCode, endTime.Sub(startTime))
} else {
log.Infof("HTTP %s %s [%d] respTime %s", req.Method, req.URL.String(), resp.StatusCode, endTime.Sub(startTime))
a.readBody(resp)
}
}

Expand All @@ -42,24 +66,16 @@ func (a *Attack) Get(uri string, attackNum int) {
for i := 0; i < attackNum; i++ {
wg.Add(1)
go func(ii int) {
var startTime = time.Now()
resp, errp := http.Get(uri)
var endTime = time.Now()

if errp != nil {
log.Error(errp)
} else {
log.Infof("Get [%d] => %s %s respTime %s", ii, resp.Status, startTime.Format("2006-01-02T15:04:05.999999-07:00"), endTime.Sub(startTime))
defer wg.Done()
req, err := http.NewRequest("GET", uri, nil)
if err != nil {
log.Errorf("Error creating GET request: %s", err)
return
}

wg.Done()
a.do(req, ii)
}(i)
}

if err := recover(); err != nil {
log.Error(err)
}

wg.Wait()
}

Expand All @@ -74,25 +90,17 @@ func (a *Attack) Post(uri string, attackNum int, params url.Values) {
for i := 0; i < attackNum; i++ {
wg.Add(1)
go func(ii int) {
var startTime = time.Now()
resp, errp := http.PostForm(uri, params)
var endTime = time.Now()

if errp != nil {
log.Error(errp)
} else {
log.Infof("Post [%d] => %s %s respTime %s", ii, resp.Status, startTime.Format("2006-01-02T15:04:05.999999-07:00"), endTime.Sub(startTime))
a.readBody(resp)
defer wg.Done()
req, err := http.NewRequest("POST", uri, bytes.NewBufferString(params.Encode()))
if err != nil {
log.Errorf("Error creating POST request: %s", err)
return
}

wg.Done()
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
a.do(req, ii)
}(i)
}

if err := recover(); err != nil {
log.Error(err)
}

wg.Wait()
}

Expand All @@ -107,33 +115,17 @@ func (a *Attack) PostJSON(uri string, attackNum int, params string) {
for i := 0; i < attackNum; i++ {
wg.Add(1)
go func(ii int) {
var startTime = time.Now()
var jsonStr = []byte(params)
log.Info(jsonStr)
req, err := http.NewRequest("POST", uri, bytes.NewBuffer(jsonStr))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
defer wg.Done()
req, err := http.NewRequest("POST", uri, bytes.NewBufferString(params))
if err != nil {
panic(err)
log.Errorf("Error creating POST request: %s", err)
return
}
defer resp.Body.Close()
var endTime = time.Now()

if err != nil {
log.Error(err)
} else {
log.Infof("Post [%d] => %s %s respTime %s", ii, resp.Status, startTime.Format("2006-01-02T15:04:05.999999-07:00"), endTime.Sub(startTime))
a.readBody(resp)
}

wg.Done()
req.Header.Set("Content-Type", "application/json")
req.Header.Set("User-Agent", "http-attack")
a.do(req, ii)
}(i)
}

if err := recover(); err != nil {
log.Error(err)
}

wg.Wait()
}