Skip to content

Commit

Permalink
add more test and checking URL
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin8105 committed Sep 20, 2017
1 parent 46af639 commit b9ac6db
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 8 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,23 @@
[![GoDoc](https://godoc.org/github.com/Konstantin8105/DDoS?status.svg)](https://godoc.org/github.com/Konstantin8105/DDoS)

DDoS attack. Creating infinite http GET requests.
If you need more information, then please see [wiki](https://en.wikipedia.org/wiki/Denial-of-service_attack#Defense_techniques).

**Library created just for education task.**

Minimal example of using:

```golang
func main() {
workers := 100
d, err := ddos.New("http://127.0.0.1:80", workers)
if err != nil {
panic(err)
}
d.Run()
time.Sleep(time.Second)
d.Stop()
fmt.Println("DDoS attack server: http://127.0.0.1:80")
// Output: DDoS attack server: http://127.0.0.1:80
}
```
18 changes: 12 additions & 6 deletions ddos.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package ddos

import (
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"runtime"
"sync/atomic"
)
Expand All @@ -19,17 +21,21 @@ type DDoS struct {
amountRequests int64
}

// NewDDoS - initialization of new DDoS attack
func NewDDoS(url string, workers int) *DDoS {
s := make(chan bool)
// New - initialization of new DDoS attack
func New(URL string, workers int) (*DDoS, error) {
if workers < 1 {
workers = 1
return nil, fmt.Errorf("Amount of workers cannot be less 1")
}
u, err := url.Parse(URL)
if err != nil || len(u.Host) == 0 {
return nil, fmt.Errorf("Undefined host or error = %v", err)
}
s := make(chan bool)
return &DDoS{
url: url,
url: URL,
stop: &s,
amountWorkers: workers,
}
}, nil
}

// Run - run DDoS attack
Expand Down
37 changes: 35 additions & 2 deletions ddos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import (
)

func TestNewDDoS(t *testing.T) {
d := ddos.NewDDoS("127.0.0.1", 0)
d, err := ddos.New("http://127.0.0.1", 1)
if err != nil {
t.Error("Cannot create a new ddos structure. Error = ", err)
}
if d == nil {
t.Error("Cannot create a new ddos structure")
}
Expand All @@ -26,7 +29,10 @@ func TestDDoS(t *testing.T) {
createServer(port, t)

url := "http://127.0.0.1:" + strconv.Itoa(port)
d := ddos.NewDDoS(url, 100)
d, err := ddos.New(url, 100)
if err != nil {
t.Error("Cannot create a new ddos structure")
}
d.Run()
time.Sleep(time.Second)
d.Stop()
Expand All @@ -47,3 +53,30 @@ func createServer(port int, t *testing.T) {
}
}()
}

func TestWorkers(t *testing.T) {
_, err := ddos.New("127.0.0.1", 0)
if err == nil {
t.Error("Cannot create a new ddos structure")
}
}

func TestUrl(t *testing.T) {
_, err := ddos.New("some_strange_host", 1)
if err == nil {
t.Error("Cannot create a new ddos structure")
}
}

func ExampleNew() {
workers := 100
d, err := ddos.New("http://127.0.0.1:80", workers)
if err != nil {
panic(err)
}
d.Run()
time.Sleep(time.Second)
d.Stop()
fmt.Println("DDoS attack server: http://127.0.0.1:80")
// Output: DDoS attack server: http://127.0.0.1:80
}

0 comments on commit b9ac6db

Please sign in to comment.