diff --git a/README.md b/README.md index 36d5801..190cd97 100644 --- a/README.md +++ b/README.md @@ -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 +} +``` diff --git a/ddos.go b/ddos.go index db97a28..c696320 100644 --- a/ddos.go +++ b/ddos.go @@ -1,9 +1,11 @@ package ddos import ( + "fmt" "io" "io/ioutil" "net/http" + "net/url" "runtime" "sync/atomic" ) @@ -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 diff --git a/ddos_test.go b/ddos_test.go index ec04d14..b61ec25 100644 --- a/ddos_test.go +++ b/ddos_test.go @@ -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") } @@ -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() @@ -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 +}