refactor
This commit is contained in:
parent
33ba2d8eea
commit
cbc5261bbc
107
main.go
107
main.go
|
@ -18,9 +18,7 @@ import (
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
var cpuprofile = flag.String("cpuprofile", "", "Destination file for CPU Profile")
|
var cpuprofile = flag.String("cpuprofile", "", "Destination file for CPU Profile")
|
||||||
var image = flag.String("image", "", "Absolute Path to image")
|
var image_path = flag.String("image", "", "Absolute Path to image")
|
||||||
var canvas_xsize = flag.Int("xsize", 800, "Width of the canvas in px")
|
|
||||||
var canvas_ysize = flag.Int("ysize", 600, "Height of the canvas in px")
|
|
||||||
var image_offsetx = flag.Int("xoffset", 0, "Offset of posted image from left border")
|
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")
|
var image_offsety = flag.Int("yoffset", 0, "Offset of posted image from top border")
|
||||||
var connections = flag.Int("connections", 10, "Number of simultaneous connections/threads. Each Thread posts a subimage")
|
var connections = flag.Int("connections", 10, "Number of simultaneous connections/threads. Each Thread posts a subimage")
|
||||||
|
@ -29,8 +27,8 @@ var runtime = flag.String("runtime", "1", "Runtime in Minutes")
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
if *image == "" || *address == "" {
|
if *image_path == "" {
|
||||||
log.Fatal("No image or no server address provided")
|
log.Fatal("No image provided")
|
||||||
}
|
}
|
||||||
|
|
||||||
// check connectivity by opening one test connection
|
// check connectivity by opening one test connection
|
||||||
|
@ -49,10 +47,12 @@ func main() {
|
||||||
pprof.StartCPUProfile(f)
|
pprof.StartCPUProfile(f)
|
||||||
defer pprof.StopCPUProfile()
|
defer pprof.StopCPUProfile()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate and split messages into equal chunks
|
// Generate and split messages into equal chunks
|
||||||
msg := splitmessages(genMessages())
|
commands := genCommands(readImage(*image_path), *image_offsetx, *image_offsety)
|
||||||
for _, message := range msg {
|
commandGroups := chunkCommands(commands, *connections)
|
||||||
go bomb(message)
|
for _, messages := range commandGroups {
|
||||||
|
go bomb(messages)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Terminate after 1 Minute to save resources
|
// Terminate after 1 Minute to save resources
|
||||||
|
@ -63,87 +63,78 @@ func main() {
|
||||||
time.Sleep(time.Minute * timer)
|
time.Sleep(time.Minute * timer)
|
||||||
}
|
}
|
||||||
|
|
||||||
func bomb(messages []byte) {
|
func bomb(messages [][]byte) {
|
||||||
conn, err := net.Dial("tcp", *address)
|
conn, err := net.Dial("tcp", *address)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
||||||
// Start bombardement
|
// Start bombardement
|
||||||
for {
|
for {
|
||||||
_, err := conn.Write(messages)
|
for _, message := range messages {
|
||||||
|
_, err := conn.Write(message)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Creates message based on given image
|
func readImage(path string) (img image.Image) {
|
||||||
func genMessages() (output []byte) {
|
reader, err := os.Open(path)
|
||||||
reader, err := os.Open(*image)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
img, err2 := png.Decode(reader)
|
img, _, err2 := image.Decode(reader)
|
||||||
if err2 != nil {
|
if err2 != nil {
|
||||||
log.Fatal(err2)
|
log.Fatal(err2)
|
||||||
}
|
}
|
||||||
|
|
||||||
for x := img.Bounds().Max.X; x != 0; x-- {
|
return img
|
||||||
for y := img.Bounds().Max.Y; y != 0; y-- {
|
|
||||||
col := img.At(x, y)
|
|
||||||
r, g, b, _ := col.RGBA()
|
|
||||||
|
|
||||||
rStr := strconv.FormatInt(int64(r), 16)
|
|
||||||
if len(rStr) == 1 {
|
|
||||||
rStr = "0" + rStr
|
|
||||||
}
|
}
|
||||||
|
|
||||||
gStr := strconv.FormatInt(int64(g), 16)
|
func intToHex(x uint32) string {
|
||||||
if len(gStr) == 1 {
|
str := strconv.FormatInt(int64(x), 16)
|
||||||
gStr = "0" + gStr
|
if len(str) == 1 {
|
||||||
|
str = "0" + str
|
||||||
|
}
|
||||||
|
return str[0:2]
|
||||||
}
|
}
|
||||||
|
|
||||||
bStr := strconv.FormatInt(int64(b), 16)
|
// Creates message based on given image
|
||||||
if len(bStr) == 1 {
|
func genCommands(img image.Image, offset_x, offset_y int) (commands [][]byte) {
|
||||||
bStr = "0" + bStr
|
max_x := img.Bounds().Max.X
|
||||||
|
max_y := img.Bounds().Max.Y
|
||||||
|
commands = make([][]byte, max_x*max_y)
|
||||||
|
|
||||||
|
for x := 0; x < max_x; x++ {
|
||||||
|
for y := 0; y < max_y; y++ {
|
||||||
|
r, g, b, _ := img.At(x, y).RGBA()
|
||||||
|
colStr := intToHex(r) + intToHex(g) + intToHex(b)
|
||||||
|
cmd := fmt.Sprintf("PX %d %d %s\n", x+offset_x, y+offset_y, colStr)
|
||||||
|
commands[x*max_y+y] = []byte(cmd)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
colStr := rStr[0:2]
|
return commands
|
||||||
colStr += gStr[0:2]
|
|
||||||
colStr += bStr[0:2]
|
|
||||||
|
|
||||||
//Do not draw transparent pixels
|
|
||||||
if colStr == "000000" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
pxStr := fmt.Sprintf("PX %d %d %s\n", x+*image_offsetx, y+*image_offsety, colStr)
|
|
||||||
output = append(output, []byte(pxStr)...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return output
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Splits messages into chunks, splitting on complete commands only
|
// Splits messages into equally sized chunks
|
||||||
func splitmessages(in []byte) [][]byte {
|
func chunkCommands(commands [][]byte, numChunks int) [][][]byte {
|
||||||
index := 0
|
chunks := make([][][]byte, numChunks)
|
||||||
equalsplit := len(in) / *connections
|
|
||||||
output := make([][]byte, *connections)
|
chunkLength := len(commands) / numChunks
|
||||||
for i := 0; i < *connections; i++ {
|
for i := 0; i < numChunks; i++ {
|
||||||
if index+equalsplit > len(in) {
|
cmdOffset := i * chunkLength
|
||||||
output[i] = in[index:]
|
|
||||||
|
if cmdOffset+chunkLength > len(commands) {
|
||||||
|
chunks[i] = commands[cmdOffset:]
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
chunks[i] = commands[cmdOffset : cmdOffset+chunkLength]
|
||||||
tmp := index
|
|
||||||
for in[index+equalsplit] != 80 {
|
|
||||||
index++
|
|
||||||
}
|
}
|
||||||
output[i] = in[tmp : index+equalsplit]
|
return chunks
|
||||||
index += equalsplit
|
|
||||||
}
|
|
||||||
return output
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue