2019-01-06 22:00:43 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
2019-01-11 17:29:00 +01:00
|
|
|
"fmt"
|
|
|
|
"image"
|
2020-02-04 13:40:30 +01:00
|
|
|
"image/color"
|
2019-01-11 17:29:00 +01:00
|
|
|
_ "image/gif"
|
|
|
|
_ "image/jpeg"
|
|
|
|
_ "image/png"
|
|
|
|
"log"
|
2019-01-11 18:27:29 +01:00
|
|
|
"math/rand"
|
2019-01-06 22:00:43 +01:00
|
|
|
"net"
|
2019-01-11 17:29:00 +01:00
|
|
|
"os"
|
2019-01-06 22:00:43 +01:00
|
|
|
"runtime/pprof"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var err error
|
|
|
|
var cpuprofile = flag.String("cpuprofile", "", "Destination file for CPU Profile")
|
2019-01-11 17:31:14 +01:00
|
|
|
var image_path = flag.String("image", "", "Absolute Path to image")
|
2019-01-06 22:00:43 +01:00
|
|
|
var image_offsetx = flag.Int("xoffset", 0, "Offset of posted image from left border")
|
|
|
|
var image_offsety = flag.Int("yoffset", 0, "Offset of posted image from top border")
|
2019-01-11 19:37:41 +01:00
|
|
|
var connections = flag.Int("connections", 4, "Number of simultaneous connections. Each connection posts a subimage")
|
2019-01-06 22:00:43 +01:00
|
|
|
var address = flag.String("host", "127.0.0.1:1337", "Server address")
|
2020-02-04 14:59:15 +01:00
|
|
|
var runtime = flag.String("runtime", "60s", "exit after timeout")
|
2019-01-11 18:27:29 +01:00
|
|
|
var shuffle = flag.Bool("shuffle", false, "pixel send ordering")
|
2019-01-06 22:00:43 +01:00
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
2019-01-11 17:31:14 +01:00
|
|
|
if *image_path == "" {
|
|
|
|
log.Fatal("No image provided")
|
2019-01-06 22:00:43 +01:00
|
|
|
}
|
|
|
|
|
2019-01-11 17:22:51 +01:00
|
|
|
// check connectivity by opening one test connection
|
|
|
|
conn, err := net.Dial("tcp", *address)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
conn.Close()
|
|
|
|
|
2019-01-06 22:00:43 +01:00
|
|
|
// Start cpu profiling if wanted
|
|
|
|
if *cpuprofile != "" {
|
|
|
|
f, err := os.Create(*cpuprofile)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-02-04 14:59:15 +01:00
|
|
|
defer f.Close()
|
2019-01-06 22:00:43 +01:00
|
|
|
pprof.StartCPUProfile(f)
|
|
|
|
defer pprof.StopCPUProfile()
|
|
|
|
}
|
2019-01-11 17:31:14 +01:00
|
|
|
|
2019-01-06 22:00:43 +01:00
|
|
|
// Generate and split messages into equal chunks
|
2019-01-11 17:31:14 +01:00
|
|
|
commands := genCommands(readImage(*image_path), *image_offsetx, *image_offsety)
|
2019-01-11 18:27:29 +01:00
|
|
|
if *shuffle {
|
|
|
|
shuffleCommands(commands)
|
|
|
|
}
|
|
|
|
|
2019-01-11 17:31:14 +01:00
|
|
|
commandGroups := chunkCommands(commands, *connections)
|
|
|
|
for _, messages := range commandGroups {
|
|
|
|
go bomb(messages)
|
2019-01-06 22:00:43 +01:00
|
|
|
}
|
|
|
|
|
2020-02-04 14:59:15 +01:00
|
|
|
// Terminate after timeout to save resources
|
|
|
|
timer, err := time.ParseDuration(*runtime)
|
2019-01-06 22:00:43 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Invalid runtime specified: " + err.Error())
|
|
|
|
}
|
2020-02-04 14:59:15 +01:00
|
|
|
time.Sleep(timer)
|
2019-01-06 22:00:43 +01:00
|
|
|
}
|
|
|
|
|
2019-01-11 19:37:41 +01:00
|
|
|
func bomb(messages []byte) {
|
2019-01-06 22:00:43 +01:00
|
|
|
conn, err := net.Dial("tcp", *address)
|
|
|
|
if err != nil {
|
2019-01-11 17:22:51 +01:00
|
|
|
log.Fatal(err)
|
2019-01-06 22:00:43 +01:00
|
|
|
}
|
2019-01-11 17:31:14 +01:00
|
|
|
|
2019-01-06 22:00:43 +01:00
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
// Start bombardement
|
|
|
|
for {
|
2019-01-11 19:37:41 +01:00
|
|
|
_, err := conn.Write(messages)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
2019-01-06 22:00:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-11 17:31:14 +01:00
|
|
|
func readImage(path string) (img image.Image) {
|
|
|
|
reader, err := os.Open(path)
|
2019-01-06 22:00:43 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-01-11 17:31:14 +01:00
|
|
|
img, _, err2 := image.Decode(reader)
|
2019-01-06 22:00:43 +01:00
|
|
|
if err2 != nil {
|
|
|
|
log.Fatal(err2)
|
|
|
|
}
|
|
|
|
|
2019-01-11 17:31:14 +01:00
|
|
|
return img
|
|
|
|
}
|
2019-01-06 22:00:43 +01:00
|
|
|
|
2019-01-11 17:31:14 +01:00
|
|
|
// Creates message based on given image
|
|
|
|
func genCommands(img image.Image, offset_x, offset_y int) (commands [][]byte) {
|
|
|
|
max_x := img.Bounds().Max.X
|
|
|
|
max_y := img.Bounds().Max.Y
|
|
|
|
commands = make([][]byte, max_x*max_y)
|
2020-02-04 13:40:30 +01:00
|
|
|
numCmds := 0
|
2019-01-11 17:31:14 +01:00
|
|
|
|
|
|
|
for x := 0; x < max_x; x++ {
|
|
|
|
for y := 0; y < max_y; y++ {
|
2020-02-04 13:40:30 +01:00
|
|
|
// ensure we're working with RGBA colors (non-alpha-pre-multiplied)
|
2020-02-04 13:41:59 +01:00
|
|
|
c := color.NRGBAModel.Convert(img.At(x, y)).(color.NRGBA)
|
2020-02-04 13:40:30 +01:00
|
|
|
|
2020-02-04 13:41:59 +01:00
|
|
|
// ignore transparent pixels
|
2020-02-04 14:59:15 +01:00
|
|
|
if c.A == 0 {
|
|
|
|
continue
|
2020-02-04 13:40:30 +01:00
|
|
|
}
|
2020-02-04 14:59:15 +01:00
|
|
|
cmd := fmt.Sprintf("PX %d %d %.2x%.2x%.2x\n",
|
|
|
|
x+offset_x, y+offset_y, c.R, c.G, c.B)
|
|
|
|
commands[numCmds] = []byte(cmd)
|
|
|
|
numCmds += 1
|
2019-01-06 22:00:43 +01:00
|
|
|
}
|
|
|
|
}
|
2019-01-11 17:31:14 +01:00
|
|
|
|
2020-02-04 13:40:30 +01:00
|
|
|
return commands[:numCmds]
|
2019-01-06 22:00:43 +01:00
|
|
|
}
|
|
|
|
|
2019-01-11 17:31:14 +01:00
|
|
|
// Splits messages into equally sized chunks
|
2019-01-11 19:37:41 +01:00
|
|
|
func chunkCommands(commands [][]byte, numChunks int) [][]byte {
|
|
|
|
chunks := make([][]byte, numChunks)
|
2019-01-06 22:00:43 +01:00
|
|
|
|
2019-01-11 17:31:14 +01:00
|
|
|
chunkLength := len(commands) / numChunks
|
|
|
|
for i := 0; i < numChunks; i++ {
|
|
|
|
cmdOffset := i * chunkLength
|
2019-01-11 19:37:41 +01:00
|
|
|
for j := 0; j < chunkLength; j++ {
|
|
|
|
chunks[i] = append(chunks[i], commands[cmdOffset+j]...)
|
2019-01-06 22:00:43 +01:00
|
|
|
}
|
|
|
|
}
|
2019-01-11 17:31:14 +01:00
|
|
|
return chunks
|
2019-01-06 22:00:43 +01:00
|
|
|
}
|
2019-01-11 18:27:29 +01:00
|
|
|
|
|
|
|
func shuffleCommands(slice [][]byte) {
|
|
|
|
for i := range slice {
|
|
|
|
j := rand.Intn(i + 1)
|
|
|
|
slice[i], slice[j] = slice[j], slice[i]
|
|
|
|
}
|
|
|
|
}
|