-
Notifications
You must be signed in to change notification settings - Fork 2
/
httpstream.go
201 lines (162 loc) · 4.29 KB
/
httpstream.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"net/http"
"sync"
"github.com/golang/glog"
"github.com/google/gopacket"
"github.com/google/gopacket/tcpassembly"
"github.com/google/gopacket/tcpassembly/tcpreader"
"github.com/jda/nanofi/inform"
)
// SafeUniqueList is safe to use concurrently.
type SafeUniqueList struct {
v []string
mux sync.RWMutex
}
// Keys returns list of keys
func (sk *SafeUniqueList) Keys() []string {
sk.mux.RLock()
defer sk.mux.RUnlock()
return sk.v
}
// AddKey adds a unique key to sk
func (sk *SafeUniqueList) AddKey(key string) {
sk.mux.Lock()
defer sk.mux.Unlock()
for _, k := range sk.v {
if key == k { // bail if key already exists
return
}
}
sk.v = append(sk.v, key)
}
// Exists returns true if key exists in list
func (sk *SafeUniqueList) Exists(key string) bool {
sk.mux.RLock()
defer sk.mux.RUnlock()
for _, k := range sk.v {
if key == k {
return true
}
}
return false
}
var sk SafeUniqueList
// Build a simple HTTP request parser using tcpassembly.StreamFactory and tcpassembly.Stream interfaces
// httpStreamFactory implements tcpassembly.StreamFactory
type httpStreamFactory struct {
wg *sync.WaitGroup
}
// httpStream will handle the actual decoding of http requests.
type httpStream struct {
net, transport gopacket.Flow
r tcpreader.ReaderStream
}
func (h *httpStreamFactory) New(net, transport gopacket.Flow) tcpassembly.Stream {
hstream := &httpStream{
net: net,
transport: transport,
r: tcpreader.NewReaderStream(),
}
go hstream.run(h.wg) // Important... we must guarantee that data from the reader stream is read.
// ReaderStream implements tcpassembly.Stream, so we can return a pointer to it.
return &hstream.r
}
func (h *httpStream) run(wg *sync.WaitGroup) {
buf := bufio.NewReader(&h.r)
src := h.net.Src().String()
dest := h.net.Dst().String()
for {
hint, err := buf.Peek(4)
if err == io.EOF {
break
} else if err != nil {
glog.Fatalf("could not read packet: %s", err)
}
if bytes.Equal(hint, []byte("POST")) {
req, err := http.ReadRequest(buf)
if err != nil {
glog.Warningf("%s->%s: could not read request: %s", src, dest, err)
continue
}
decodeRequest(req, src, dest)
req.Body.Close()
continue
}
if bytes.Equal(hint, []byte("HTTP")) {
res, err := http.ReadResponse(buf, nil)
if err != nil {
glog.Warningf("%s->%s: could not read response: %s", src, dest, err)
continue
}
decodeResponse(res, src, dest)
res.Body.Close()
continue
}
buf.Discard(4) // hint failed, so skip forward
}
}
func decodeResponse(r *http.Response, src string, dest string) {
if r.StatusCode == http.StatusNotFound {
//glog.Infof("device %s not known to %s (or latter is not a controller)\n", dest, src)
return
}
if r.StatusCode == http.StatusContinue {
// whatever
return
}
ctype := r.Header.Get("Content-type")
if r.StatusCode == http.StatusOK && ctype == inform.InformContentType {
handleInform(r.Body, src, dest)
return
}
glog.Warningf("unhandled code %d or content-type %s from %s\n", r.StatusCode, ctype, dest)
}
func decodeRequest(r *http.Request, src string, dest string) {
if r.Method != http.MethodPost {
return
}
if ctype := r.Header.Get("Content-type"); ctype != inform.InformContentType {
glog.Infof("%s->%s: unexpected content-type: %s", src, dest, ctype)
return
}
handleInform(r.Body, src, dest)
}
func handleInform(body io.ReadCloser, src string, dest string) {
imsg, err := inform.DecodeHeader(body)
if err != nil {
glog.Warningf("%s: could not parse inform header: %s", src, err)
return
}
if showHeader {
fmt.Printf("%s->%s: %+v\n", src, dest, imsg)
}
payload, err := tryDecodePayload(imsg, body)
if err != nil {
glog.Warningf("%s->%s: could not decrypt inform payload: %s", src, dest, err)
return
}
if showMsg {
fmt.Printf("%s\n", payload)
}
extractInfo(payload, dest) // dest because keys come in responses
}
func tryDecodePayload(imsg inform.Header, eb io.ReadCloser) (clearBody []byte, err error) {
ct, _ := io.ReadAll(eb)
payload, err := imsg.DecodePayload(bytes.NewReader(ct), "")
if err == nil {
return payload, nil
}
keys := sk.Keys()
for _, k := range keys {
payload, err := imsg.DecodePayload(bytes.NewReader(ct), k)
if err == nil {
return payload, nil
}
}
return nil, err
}