-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
81 lines (73 loc) · 2.06 KB
/
server.go
File metadata and controls
81 lines (73 loc) · 2.06 KB
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
package main
import (
"encoding/base64"
"encoding/json"
"flag"
"log"
"net/http"
"strings"
)
// Only used as the json response for the API
type mkResp struct {
Input string
}
func (h *chainHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
tokens, err := h.getMutatedInput()
// Print tokens in "human readable" form
log.Println(tokens)
// Converts the markov generated text to base64 then to json
j, err := json.Marshal(mkResp{base64.StdEncoding.EncodeToString([]byte(strings.Join(tokens, "")))})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(j)
case "POST":
// handle "Live Updates"
var j mkResp
err := json.NewDecoder(r.Body).Decode(&j)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
data, err := base64.StdEncoding.DecodeString(j.Input)
err = h.writeNewEntry(data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
}
func startServers(listenPort string, fuzzingCorpi []string) {
for _, corpi := range fuzzingCorpi {
var url string
if strings.HasPrefix(corpi, "./") {
// fix awkward url when using ./directory
url = corpi[2:]
} else {
// Use corpi name as default path
url = corpi
}
// Use tokenizeBySpaces by default
// Other tokenizers are in tokenizer.go
createEndpoint(corpi, url, tokenizeBySpaces{}, nonMutator{}, sortMutator{})
}
log.Println("Listening on :" + listenPort)
log.Fatal(http.ListenAndServe(":"+listenPort, nil))
}
// Creates a markov chain and a coresponding API endpoink
func createEndpoint(corpi, url string, t Tokenizer, pre Premutator, post Postmutator) {
h := createChain(corpi, t, pre, post)
http.Handle("/"+url, h)
log.Println("Serving ", corpi, "on /"+url)
}
func main() {
listenPort := flag.String("port", "8080", "Port to serve on")
flag.Parse()
// Take each trailing input as an input corpus
fuzzingCorpi := flag.Args()
startServers(*listenPort, fuzzingCorpi)
}