-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
101 lines (85 loc) · 1.79 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"bytes"
"encoding/hex"
"flag"
"html/template"
"io"
"io/ioutil"
"log"
"math/rand"
"net/http"
"strings"
"time"
)
var (
templatePath = flag.String("template", "./index.html", "path to template file")
addr = flag.String("addr", "127.0.0.1:5000", "listen address")
factsPath = flag.String("facts", "./facts.txt", "path to facts file")
facts [][]byte
tmpl *template.Template
)
func readFacts() ([][]byte, error) {
facts, err := ioutil.ReadFile(*factsPath)
if err != nil {
return nil, err
}
lines := bytes.Split(facts, []byte("\n"))
for i, line := range lines {
lines[i] = []byte(hex.Dump(line))
}
return lines, nil
}
func main() {
flag.Parse()
var err error
facts, err = readFacts()
if err != nil {
log.Fatal(err)
}
tmpl, err = tmpl.ParseFiles(*templatePath)
if err != nil {
log.Fatal(err)
}
rand.Seed(time.Now().Unix())
http.HandleFunc("/", routeRequest)
log.Println("starting shark-hexdump")
err = http.ListenAndServe(*addr, nil)
if err != nil {
log.Fatal(err)
}
}
func routeRequest(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/":
serveFact(w, r)
case "/upload":
dumpBody(w, r)
default:
w.WriteHeader(404)
io.WriteString(w, "not found")
}
}
func serveFact(w http.ResponseWriter, r *http.Request) {
fact := facts[rand.Intn(len(facts))]
if strings.HasPrefix(r.UserAgent(), "curl") {
w.Write(fact)
} else {
err := tmpl.Execute(w, string(fact))
if err != nil {
w.WriteHeader(500)
io.WriteString(w, "internal server error")
log.Println(err)
}
}
}
func dumpBody(w http.ResponseWriter, r *http.Request) {
dumper := hex.Dumper(w)
defer dumper.Close()
_, err := io.Copy(dumper, r.Body)
if err != nil {
w.WriteHeader(500)
io.WriteString(w, "internal server error")
log.Println(err)
}
}