2020-02-05 19:26:43 +01:00
|
|
|
package pixelflut
|
|
|
|
|
|
|
|
import (
|
2020-02-14 16:37:19 +01:00
|
|
|
"fmt"
|
2022-01-02 11:14:11 +01:00
|
|
|
"image"
|
2020-12-29 21:08:31 +01:00
|
|
|
"math/rand"
|
2020-02-06 01:01:05 +01:00
|
|
|
"net"
|
2020-02-13 22:26:50 +01:00
|
|
|
"sync"
|
2020-02-14 16:37:19 +01:00
|
|
|
"time"
|
2020-02-05 19:26:43 +01:00
|
|
|
)
|
|
|
|
|
2020-12-31 17:17:24 +01:00
|
|
|
const (
|
|
|
|
timeoutMin = 100 * time.Millisecond
|
|
|
|
timeoutMax = 10 * time.Second
|
|
|
|
)
|
|
|
|
|
2022-01-02 11:14:11 +01:00
|
|
|
type Offsetter interface {
|
|
|
|
Next() (x, y int)
|
|
|
|
SetMaximumOffset(max image.Point)
|
|
|
|
}
|
|
|
|
|
|
|
|
type RandOffsetter struct {
|
|
|
|
image.Point
|
|
|
|
Mask *image.NRGBA
|
|
|
|
Random bool
|
|
|
|
Max image.Point
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r RandOffsetter) String() string {
|
|
|
|
mask := "nil"
|
|
|
|
if r.Mask != nil {
|
|
|
|
mask = r.Mask.Bounds().Size().String()
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("[%v + random %v, mask %v]", r.Point, r.Random, mask)
|
|
|
|
}
|
|
|
|
|
|
|
|
// override image.Point interface
|
|
|
|
func (r RandOffsetter) Add(p image.Point) RandOffsetter {
|
|
|
|
r.Point = r.Point.Add(p)
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RandOffsetter) SetMaximumOffset(max image.Point) {
|
|
|
|
if r.Mask != nil {
|
|
|
|
mask := r.Mask.Bounds().Canon().Max.Sub(r.Point)
|
|
|
|
r.Max.X = clamp(mask.X, 1, max.X)
|
|
|
|
r.Max.Y = clamp(mask.Y, 1, max.Y)
|
|
|
|
} else {
|
|
|
|
r.Max.X = clamp(max.X, 1, max.X)
|
|
|
|
r.Max.Y = clamp(max.Y, 1, max.Y)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func clamp(val, min, max int) int {
|
|
|
|
if val < min {
|
|
|
|
return min
|
|
|
|
}
|
|
|
|
if val > max {
|
|
|
|
return max
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r RandOffsetter) Next() (x, y int) {
|
|
|
|
if r.Random {
|
|
|
|
for i := 0; i < 1000; i++ {
|
|
|
|
x := rand.Intn(r.Max.X)
|
|
|
|
y := rand.Intn(r.Max.Y)
|
|
|
|
if r.Mask != nil {
|
|
|
|
if _, _, _, a := r.Mask.At(x, y).RGBA(); a != 0 {
|
|
|
|
return r.X + x, r.Y + y
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return r.X + x, r.Y + y
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return r.X, r.Y
|
|
|
|
}
|
|
|
|
|
2020-02-14 16:37:19 +01:00
|
|
|
// Performance contains pixelflut metrics
|
|
|
|
type Performance struct {
|
|
|
|
Enabled bool
|
|
|
|
Conns int
|
|
|
|
BytesPerSec int
|
|
|
|
BytesTotal int
|
|
|
|
|
|
|
|
connsReporter chan int
|
|
|
|
bytesReporter chan int
|
|
|
|
bytes int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p Performance) String() string {
|
|
|
|
return fmt.Sprintf("%v conns\t%v\t%v/s",
|
2020-12-31 17:17:24 +01:00
|
|
|
p.Conns, fmtBytes(p.BytesTotal), fmtBit(p.BytesPerSec))
|
2020-02-14 16:37:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// https://yourbasic.org/golang/byte-count.go
|
|
|
|
func fmtBytes(b int) string {
|
|
|
|
const unit = 1024
|
|
|
|
if b < unit {
|
|
|
|
return fmt.Sprintf("%d B", b)
|
|
|
|
}
|
|
|
|
div, exp := int64(unit), 0
|
|
|
|
for n := b / unit; n >= unit; n /= unit {
|
|
|
|
div *= unit
|
|
|
|
exp++
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%.1f %ciB",
|
|
|
|
float64(b)/float64(div), "KMGTPE"[exp])
|
|
|
|
}
|
|
|
|
|
2020-12-31 17:17:24 +01:00
|
|
|
func fmtBit(b int) string {
|
|
|
|
const unit = 1000
|
|
|
|
b *= 8
|
|
|
|
if b < unit {
|
|
|
|
return fmt.Sprintf("%d b", b)
|
|
|
|
}
|
|
|
|
div, exp := int64(unit), 0
|
|
|
|
for n := b / unit; n >= unit; n /= unit {
|
|
|
|
div *= unit
|
|
|
|
exp++
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%.1f %cb",
|
|
|
|
float64(b)/float64(div), "kMGTPE"[exp])
|
|
|
|
}
|
|
|
|
|
2020-02-14 16:37:19 +01:00
|
|
|
// PerformanceReporter provides pixelflut performance metrics, when Enabled is true.
|
|
|
|
// @speed: Note that enabling costs ~9% bomb performance under high throughput.
|
|
|
|
var PerformanceReporter = initPerfReporter()
|
|
|
|
|
|
|
|
// should be called only once
|
|
|
|
func initPerfReporter() *Performance {
|
|
|
|
r := new(Performance)
|
|
|
|
r.bytesReporter = make(chan int, 512)
|
|
|
|
r.connsReporter = make(chan int, 512)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case b := <-r.bytesReporter:
|
|
|
|
r.bytes += b
|
|
|
|
r.BytesTotal += b
|
|
|
|
case c := <-r.connsReporter:
|
|
|
|
r.Conns += c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
r.BytesPerSec = r.bytes
|
|
|
|
r.bytes = 0
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
2020-02-06 01:01:05 +01:00
|
|
|
|
2021-01-02 15:51:13 +01:00
|
|
|
// bombAddress opens a TCP connection to `address`, and writes `message` repeatedly, until `stop` is closed.
|
|
|
|
// It retries with exponential backoff on network errors.
|
2022-01-02 11:14:11 +01:00
|
|
|
func bombAddress(message []byte, address string, offsetter Offsetter, stop chan bool, wg *sync.WaitGroup) {
|
2020-12-29 21:08:31 +01:00
|
|
|
wg.Add(1)
|
|
|
|
defer wg.Done()
|
2020-12-31 10:23:13 +01:00
|
|
|
|
2020-12-31 17:17:24 +01:00
|
|
|
timeout := timeoutMin
|
2020-12-31 10:23:13 +01:00
|
|
|
|
|
|
|
for {
|
|
|
|
conn, err := net.Dial("tcp", address)
|
|
|
|
if err != nil {
|
|
|
|
// this was a network error, retry!
|
|
|
|
fmt.Printf("[net] error: %s. retrying in %s\n", err, timeout)
|
|
|
|
time.Sleep(timeout)
|
|
|
|
timeout *= 2
|
|
|
|
if timeout > timeoutMax {
|
|
|
|
timeout = timeoutMax
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("[net] bombing %s with new connection\n", address)
|
|
|
|
|
2022-01-02 11:14:11 +01:00
|
|
|
err = bombConn(message, offsetter, conn, stop)
|
2020-12-31 10:23:13 +01:00
|
|
|
conn.Close()
|
|
|
|
timeout = timeoutMin
|
|
|
|
if err == nil {
|
|
|
|
break // we're supposed to exit
|
|
|
|
}
|
2021-01-01 04:04:11 +01:00
|
|
|
fmt.Printf("[net] error: %s\n", err)
|
2020-02-05 19:26:43 +01:00
|
|
|
}
|
2020-02-06 01:01:05 +01:00
|
|
|
}
|
|
|
|
|
2021-01-02 15:51:13 +01:00
|
|
|
// bombConn writes the given message to the given connection in a tight loop, until `stop` is closed.
|
|
|
|
// Does no transformation on the given message, so make sure packet splitting / nagle works.
|
2022-01-02 11:14:11 +01:00
|
|
|
func bombConn(message []byte, offsetter Offsetter, conn net.Conn, stop chan bool) error {
|
2020-02-14 16:37:19 +01:00
|
|
|
PerformanceReporter.connsReporter <- 1
|
|
|
|
defer func() { PerformanceReporter.connsReporter <- -1 }()
|
|
|
|
|
2020-12-29 21:08:31 +01:00
|
|
|
var msg = make([]byte, len(message)+16) // leave some space for offset cmd
|
|
|
|
msg = message
|
2022-01-02 11:14:11 +01:00
|
|
|
// randOffset := maxOffsetX > 0 && maxOffsetY > 0
|
2020-12-29 21:08:31 +01:00
|
|
|
|
2020-02-05 19:26:43 +01:00
|
|
|
for {
|
2020-02-13 22:26:50 +01:00
|
|
|
select {
|
|
|
|
case <-stop:
|
2020-12-31 10:23:13 +01:00
|
|
|
return nil
|
2020-02-13 22:26:50 +01:00
|
|
|
default:
|
2022-01-02 11:14:11 +01:00
|
|
|
// if randOffset {
|
|
|
|
msg = append(
|
|
|
|
OffsetCmd(offsetter.Next()),
|
|
|
|
message...,
|
|
|
|
)
|
|
|
|
// }
|
2020-12-29 21:08:31 +01:00
|
|
|
b, err := conn.Write(msg)
|
2020-02-13 22:26:50 +01:00
|
|
|
if err != nil {
|
2020-12-31 10:23:13 +01:00
|
|
|
return err
|
2020-02-13 22:26:50 +01:00
|
|
|
}
|
2020-02-14 16:37:19 +01:00
|
|
|
if PerformanceReporter.Enabled {
|
|
|
|
PerformanceReporter.bytesReporter <- b
|
|
|
|
}
|
2020-02-05 19:26:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|