about summary refs log tree commit diff
path: root/stdin-service/main.go
blob: df0c5035bac39010b7e45d35ad7cc0e8d5050b66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main

import (
    "flag"
    "io/ioutil"
    "io"
    "log"
    "os"
    "net"
    "fmt"
)

var (
    inputFile = flag.String("file", "default.txt", "input file")
    host = flag.String("h", "localhost", "the host to pee against")
    port = flag.Int("p", 1337, "the port to use")
    protocol = flag.String("protocol", "tcp", "the protocol to use")
    stdin = flag.Bool("stdin", false, "read from stdin")
)

func main() {
    flag.Parse()

    var dat []byte

    if !*stdin {
        var ReadFileErr error
        dat, ReadFileErr = ioutil.ReadFile(*inputFile)
        if ReadFileErr != nil {
            log.Printf("Error reading file: %v", ReadFileErr)
            os.Exit(1)
        }
    } else {
        dat = make([]byte, 1000)
        _, err := io.ReadFull(os.Stdin, dat)
        if err != nil {
            log.Printf("Error reading from stdin: %v", err)
        }
    }

   conn, _ := net.Dial(*protocol, fmt.Sprintf("%s:%d", *host, *port))

   writtenBytes, err := conn.Write(dat)
   if err != nil {
           log.Printf("Error accepting: %v", err)
           os.Exit(1)
   }
   fmt.Printf("Written %d bytes", writtenBytes)
}