Skip to content

Instantly share code, notes, and snippets.

@fritz0705
Last active October 17, 2015 12:39
Show Gist options
  • Save fritz0705/11230420 to your computer and use it in GitHub Desktop.
Save fritz0705/11230420 to your computer and use it in GitHub Desktop.
Evil Pixelflut Client
package main
import (
"flag"
"fmt"
"image"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"io"
"log"
"net"
"os"
"strings"
"time"
)
func sender(c chan string, src net.IP, dst *net.TCPAddr) {
var (
conn *net.TCPConn
err error
)
for {
conn, err = net.DialTCP("tcp", &net.TCPAddr{src, 0, ""}, dst)
if err != nil {
log.Printf("Error while connecting from %s: %s\n", src, err)
time.Sleep(5 * time.Second)
continue
}
defer conn.Close()
conn.CloseRead()
log.Printf("Connected from %s\n", src)
for msg := range c {
if msg == "" {
return
}
_, err := io.WriteString(conn, msg)
if err != nil {
log.Printf("Disconnected at %s: %s\n", src, err)
// Requeue message when possible
select {
case c <- msg:
default:
log.Printf("Dropped message %q\n", msg)
}
break
}
}
time.Sleep(5 * time.Second)
}
}
func getSize(dst *net.TCPAddr) (x int, y int, err error) {
return 800, 576, nil
}
func parseIPList(list string) (res []net.IP, err error) {
for _, part := range strings.Split(list, ",") {
ip := net.ParseIP(part)
if ip == nil {
err = fmt.Errorf("Invalid IP address: %s", ip)
return
}
res = append(res, ip)
}
return
}
func writeImage(c chan string, img image.Image, xBase int, yBase int) {
bounds := img.Bounds()
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ {
r, g, b, a := img.At(x, y).RGBA()
if uint8(a) == uint8(0) {
continue
}
msg := fmt.Sprintf("PX %d %d %02X%02X%02X%02X\n", x+xBase, y+yBase, uint8(r/256), uint8(g/256), uint8(b/256), uint8(a/256))
c <- msg
}
}
}
func main() {
var (
flAddress = flag.String("address", "31.172.102.140:1234", "remote address")
flSources = flag.String("sources", "", "source networks separated by comma")
flX = flag.Int("x", 0, "x offset")
flY = flag.Int("y", 0, "y offset")
flLoop = flag.Bool("loop", false, "loop forever")
flConns = flag.Int("conns", 1, "number of connections per address")
)
flag.Parse()
var (
sources []net.IP
destination *net.TCPAddr
err error
imagePath = flag.Arg(0)
imageReader io.Reader
img image.Image
c = make(chan string)
)
sources, err = parseIPList(*flSources)
if err != nil {
log.Fatalf("Error while parsing IP list: %s", *flSources)
}
destination, err = net.ResolveTCPAddr("tcp", *flAddress)
if err != nil {
log.Fatalf("Invalid destination TCP address: %s", *flAddress)
}
if imagePath == "" {
log.Fatalf("Missing image argument")
}
imageReader, err = os.Open(imagePath)
if err != nil {
log.Fatalf("Error while opening %s: %v", imagePath, err)
}
img, _, err = image.Decode(imageReader)
if err != nil {
log.Fatalf("Error while decoding image: %v", err)
}
jobs := 0
for _, source := range sources {
for i := 0; i < *flConns; i++ {
go sender(c, source, destination)
jobs++
}
}
if *flLoop {
for {
writeImage(c, img, *flX, *flY)
}
} else {
writeImage(c, img, *flX, *flY)
}
log.Print("Exit...")
for jobs != 0 {
c <- ""
jobs--
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment