Initial upload

This commit is contained in:
speckij 2019-01-06 22:00:43 +01:00
parent 8e54536faa
commit 0181a83a4c
No known key found for this signature in database
GPG Key ID: E3C4FBF52A5E0ABC
3 changed files with 312 additions and 0 deletions

73
.gitignore vendored Normal file
View File

@ -0,0 +1,73 @@
# Created by .ignore support plugin (hsz.mobi)
### Go template
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
# Test binary, build with `go test -c`
*.test
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
*.prof
### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/dictionaries
.idea/**/shelf
# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml
# Gradle
.idea/**/gradle.xml
.idea/**/libraries
# CMake
cmake-build-debug/
cmake-build-release/
# Mongo Explorer plugin
.idea/**/mongoSettings.xml
# File-based project format
*.iws
# IntelliJ
out/
# mpeltonen/sbt-idea plugin
.idea_modules/
# JIRA plugin
atlassian-ide-plugin.xml
# Cursive Clojure plugin
.idea/replstate.xml
# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
# Editor-based Rest Client
.idea/httpRequests
### Custom
# Additional Dev Files
/tcpDialer.go

141
main.go Normal file
View File

@ -0,0 +1,141 @@
package main
import (
"flag"
"net"
"runtime/pprof"
"time"
)
import "fmt"
import "image/png"
import "log"
import "os"
import "strconv"
import _ "net/http/pprof"
var err error
var cpuprofile = flag.String("cpuprofile", "", "Destination file for CPU Profile")
var image = 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_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 address = flag.String("host", "127.0.0.1:1337", "Server address")
var runtime = flag.String("runtime", "1", "Runtime in Minutes")
func main() {
flag.Parse()
if *image == "" || *address == "" {
log.Fatal("No image or no server address provided")
}
// Start cpu profiling if wanted
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
// Generate and split messages into equal chunks
msg := splitmessages(genMessages())
for _, message := range msg {
go bomb(message)
}
// Terminate after 1 Minute to save resources
timer, err := time.ParseDuration(*runtime + "m")
if err != nil {
log.Fatal("Invalid runtime specified: " + err.Error())
}
time.Sleep(time.Minute * timer)
}
func bomb(messages []byte) {
conn, err := net.Dial("tcp", *address)
if err != nil {
log.Print("error establishing tcp connection: " + err.Error())
}
//TODO: Actually close the connection and not just terminate main thread
defer conn.Close()
// Start bombardement
for {
_, err := conn.Write(messages)
if err != nil {
log.Println(err.Error())
}
}
}
// Creates message based on given image
func genMessages() (output []byte) {
reader, err := os.Open(*image)
if err != nil {
log.Fatal(err)
}
img, err2 := png.Decode(reader)
if err2 != nil {
log.Fatal(err2)
}
for x := img.Bounds().Max.X; x != 0; x-- {
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)
if len(gStr) == 1 {
gStr = "0" + gStr
}
bStr := strconv.FormatInt(int64(b), 16)
if len(bStr) == 1 {
bStr = "0" + bStr
}
colStr := rStr[0:2]
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
func splitmessages(in []byte) [][]byte {
index := 0
equalsplit := len(in) / *connections
output := make([][]byte, *connections)
for i := 0; i < *connections; i++ {
if index+equalsplit > len(in) {
output[i] = in[index:]
break
}
tmp := index
for in[index+equalsplit] != 80 {
index++
}
output[i] = in[tmp : index+equalsplit]
index += equalsplit
}
return output
}

98
writer.go Normal file
View File

@ -0,0 +1,98 @@
// Copyright 2009 The Go Authors. All rights reserved.
// Modified 2018 by Jan Speckamp. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// This implements buffered I/O. It wraps an io.Reader or io.Writer
// object, creating another object (Reader or Writer) that also implements
// the interface but provides buffering and some help for textual I/O.
package main
import (
"io"
)
const (
defaultBufSize = 4096
)
// Writer implements buffering for an io.Writer object.
// If an error occurs writing to a Writer, no more data will be
// accepted and all subsequent writes, and Flush, will return the error.
// After all data has been written, the client should call the
// Flush method to guarantee all data has been forwarded to
// the underlying io.Writer.
type Writer struct {
err error
buf []byte
n int
wr io.Writer
}
// NewWriterSize returns a new Writer whose buffer has at least the specified
// size. If the argument io.Writer is already a Writer with large enough
// size, it returns the underlying Writer.
func NewWriterSize(w io.Writer, size int) *Writer {
// Is it already a Writer?
b, ok := w.(*Writer)
if ok && len(b.buf) >= size {
return b
}
if size <= 0 {
size = defaultBufSize
}
return &Writer{
buf: make([]byte, size),
wr: w,
}
}
// Flush writes any buffered data to the underlying io.Writer.
func (b *Writer) Flush() error {
if b.err != nil {
return b.err
}
if b.n == 0 {
return nil
}
n, err := b.wr.Write(b.buf[0:b.n])
if n < b.n && err == nil {
err = io.ErrShortWrite
}
if err != nil {
if n > 0 && n < b.n {
copy(b.buf[0:b.n-n], b.buf[n:b.n])
}
b.n -= n
b.err = err
return err
}
return nil
}
// Write writes the contents of p into the buffer.
// It returns the number of bytes written.
// If nn < len(p), it also returns an error explaining
// why the write is short.
func (b *Writer) Write(p []byte) (nn int, err error) {
for len(p) > (len(b.buf)-b.n) && b.err == nil {
var n int
if b.n == 0 {
// Large write, empty buffer.
// Write directly from p to avoid copy.
n, b.err = b.wr.Write(p)
} else {
n = copy(b.buf[b.n:], p)
b.n += n
}
nn += n
p = p[n:]
}
if b.err != nil {
return nn, b.err
}
n := copy(b.buf[b.n:], p)
b.n += n
nn += n
return nn, nil
}