-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
84 lines (70 loc) · 1.89 KB
/
main.go
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Command sloth is an HTTP tarpit.
package main
import (
"flag"
"fmt"
"log"
"math/rand"
"net/http"
"time"
)
// usage displays the program help.
func usage() {
fmt.Printf(
"sloth: A stupid HTTP tarpit.\n\n" +
"usage:\n" +
" sloth -host {host} -port {port} -wait {N}\n" +
"arguments:\n" +
" flag description defaults\n" +
" ---- ----------- --------\n" +
" -host host localhost\n" +
" -port port 8080\n" +
" -wait wait interval (ms) 8000\n",
)
}
// garbage generates a random number of bytes.
func garbage() []byte {
var (
// Send (at most) one megabyte of data per iteration.
num = rand.Intn(1024 * 1024)
buf = make([]byte, num)
)
rand.Seed(time.Now().UnixNano())
rand.Read(buf)
return buf
}
// tarpit does all the work: logging, looping and responding to requests.
func tarpit(t *time.Ticker, w http.ResponseWriter, r *http.Request) {
var buf []byte
log.Printf("%s\t%s\t%s%s", r.RemoteAddr, r.Method, r.Host, r.URL.Path)
w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
w.Header().Set("Connection", "keep-alive")
for {
select {
case <-t.C:
buf = garbage()
buf = append(buf, []byte{'\r', '\n'}...)
w.Write(buf)
w.(http.Flusher).Flush()
}
}
}
func main() {
flag.Usage = usage
var (
host = flag.String("host", "localhost", "host")
port = flag.String("port", "8080", "port")
wait = flag.Uint("wait", 8000, "wait interval")
tick *time.Ticker
)
flag.Parse()
// Setup a timer based on the given interval.
tick = time.NewTicker(time.Duration(*wait) * time.Millisecond)
// Setup the request handler.
http.HandleFunc("/", func(wtr http.ResponseWriter, rdr *http.Request) {
tarpit(tick, wtr, rdr)
})
// Serve!
log.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%s", *host, *port), nil))
}