/
drain.go
90 lines (73 loc) · 1.79 KB
/
drain.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
package main
import (
"fmt"
"io/ioutil"
"os"
"encoding/json"
"strings"
"net/http"
"regexp"
"github.com/garyburd/redigo/redis"
)
func DrainHandler(redis redis.Conn, w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
bodyInBytes, _ := ioutil.ReadAll(r.Body)
body := string(bodyInBytes)
id, secret := ParsePath(r.URL.Path)
DrainStore(redis, id, secret, body)
fmt.Fprintf(w, "")
}
func DrainStore(r redis.Conn, id string, secret string, body string) bool {
// STABLE AS FUCK
if (!strings.Contains(body, "host heroku router - at=info method=")) {
return false
}
key := fmt.Sprintf("auth-%s", id)
existing, _ := redis.String(r.Do("GET", key))
if (existing != secret) {
return false
}
request := make(map[string]string)
request["app_id"] = id
matchVerb, _ := regexp.Compile(`method=(\w+)`)
request["verb"] = matchVerb.FindStringSubmatch(body)[1]
matchPath, _ := regexp.Compile(`path=([^ ]+)`)
request["path"] = matchPath.FindStringSubmatch(body)[1]
matchStatus, _ := regexp.Compile(`status=(\d+)`)
request["status"] = matchStatus.FindStringSubmatch(body)[1]
raw, err := json.Marshal(request)
if err != nil {
panic(err)
}
r.Do("RPUSH", "requests", raw)
return true
}
func ParsePath(path string) (string, string) {
parts := strings.Split(path, "/")
if (len(parts) != 4) {
return "", ""
}
return parts[2], parts[3]
}
func InitRedis() redis.Conn {
redis, err := redis.Dial("tcp", ":6379")
if err != nil {
panic(err)
}
return redis
}
func main() {
redis := InitRedis()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
DrainHandler(redis, w, r)
})
port := "8000"
if os.Getenv("PORT") != "" {
port = os.Getenv("PORT")
}
fmt.Println("listening on " + port)
err := http.ListenAndServe(":" + port, nil)
if err != nil {
panic(err)
}
}