/
circumambient.go
135 lines (110 loc) · 2.76 KB
/
circumambient.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/garyburd/redigo/redis"
"log"
"net/http"
"net/http/httputil"
"net/url"
"os"
"strings"
"time"
)
type Json map[string]interface{}
var (
in = flag.String("in", "", "")
out = flag.String("out", "", "")
console = flag.Bool("console", false, "")
redisPort = flag.String("redis", ":6379", "")
redisChannel = flag.String("channel", "requests", "")
help = flag.Bool("help", false, "")
)
type Sender interface {
Send(payload []byte)
}
type consoleSender struct {}
func NewConsoleSender() Sender {
return &consoleSender{}
}
func (r *consoleSender) Send(payload []byte) {
log.Println(string(payload))
}
type redisSender struct {
pool *redis.Pool
channel string
}
func NewRedisSender(port, channel string) Sender {
return &redisSender{
redis.NewPool(func() (redis.Conn, error) {
return redis.Dial("tcp", port)
}, 3),
channel,
}
}
func (r *redisSender) Send(payload []byte) {
c := r.pool.Get()
defer c.Close()
err := c.Send("PUBLISH", r.channel, string(payload))
if err != nil {
log.Println(err)
}
c.Flush()
}
func main() {
flag.Parse()
if *help {
fmt.Println(
"Usage: circumambient [options]\n",
"\n",
" Circumambient provides a simple proxy that publishes details of\n",
" the request to redis.\n",
"\n",
" --in <host:port> # What to route through the proxy\n",
" --out <host:port> # Where to send proxied traffic\n",
" --redis <:port> # Port of running redis server (default. :6379)\n",
" --channel <name> # Name of channel to publish to (default: requests)\n",
)
os.Exit(0)
}
if *out == "" || *in == "" {
fmt.Println("Usage: circumambient --in <host:port> --out <host:port>")
os.Exit(2)
}
remote, err := url.Parse("http://" + *out)
if err != nil {
log.Fatal(err)
}
sender := NewConsoleSender()
if !*console {
sender = NewRedisSender(*redisPort, *redisChannel)
}
proxy := httputil.NewSingleHostReverseProxy(remote)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
log.Println(r.Method + " " + r.URL.String())
proxy.ServeHTTP(w, r)
publish(sender, start, time.Since(start), r)
})
log.Println("started proxy on " + *in + " to " + *out + "...")
log.Fatal(http.ListenAndServe(*in, nil))
}
func publish(sender Sender, start time.Time, duration time.Duration, r *http.Request) {
headers := Json{}
for k, v := range r.Header {
headers[k] = strings.Join(v, ", ")
}
payload, _ := json.Marshal(Json{
"method": r.Method,
"url": Json{
"path": r.URL.Path,
"query": r.URL.RawQuery,
"fragment": r.URL.Fragment,
},
"headers": headers,
"timestamp": start.UnixNano(),
"duration": duration.Nanoseconds(),
})
sender.Send(payload)
}