forked from wireghoul/mimir
/
webserver.go
367 lines (336 loc) · 10.2 KB
/
webserver.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*====================================================================
Copyright 2013 Southfork Security, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
====================================================================*/
package main
import (
"errors"
"fmt"
"net/http"
"os"
"strconv"
"strings"
"io"
"time"
"net"
"./websocket"
)
type WebserverInit struct {
ListenOn string
Root string
UseAuth bool
UseSSL bool
CertFile string
KeyFile string
}
var (
fileHandler http.Handler
flowTracker *FlowList
authCJ AuthCookieJar
checkAuth bool
)
func (wi *WebserverInit) Verify() error {
fi, err := os.Stat(wi.Root)
if err != nil {
return err
}
if !fi.IsDir() {
return errors.New(fmt.Sprintf("%s is not a directory\n", wi.Root))
}
if wi.UseAuth {
//Verify that the root directory has a directory /login in it
loginDir := fmt.Sprintf("%s/login/", wi.Root)
fi, err := os.Stat(loginDir)
if err != nil {
return errors.New(fmt.Sprintf("The root HTTP directory %s does not have a \"login\" directory for authentication", wi.Root))
}
if !fi.IsDir() {
return errors.New(fmt.Sprintf("The HTTP directory %s/login is not a directory", wi.Root))
}
//Verify that the *root*/login directory has login.html in it
loginFile := fmt.Sprintf("%s/login/login.html", wi.Root)
fi, err = os.Stat(loginFile)
if err != nil {
return errors.New(fmt.Sprintf("The login HTTP directory %s does not contain \"login.html\" directory for authentication", wi.Root))
}
if fi.IsDir() {
return errors.New(fmt.Sprintf("The login HTTP file %s/login/login.html is not a file", wi.Root))
}
checkAuth = true
}
//FIXME - enfoce this
//if wi.UseAuth == true && wi.UseSSL == false {
// return errors.New("Requested authentication without SSL. I can not let you do that Dave.")
//}
if wi.UseSSL == true {
if wi.CertFile == "" || wi.KeyFile == "" {
return errors.New("Requested SSL but KeyFile and CertFile were not set")
}
err := verifyFile(wi.CertFile)
if err != nil {
return err
}
err = verifyFile(wi.KeyFile)
if err != nil {
return err
}
} else {
if wi.CertFile != "" || wi.KeyFile != "" {
return errors.New("KeyFile or CertFile specified but UseSSL was not enabled")
}
}
return nil
}
func verifyFile(filepath string) error {
fi, err := os.Stat(filepath)
if err != nil {
return errors.New(fmt.Sprintf("%s does not exist", filepath))
}
if fi.IsDir() {
return errors.New(fmt.Sprintf("%s is not a file", filepath))
}
return nil
}
func StartWebServer(wi WebserverInit, fl *FlowList) error {
err := wi.Verify()
if err != nil {
return err
}
flowTracker = fl
http.HandleFunc("/api/", APIfunc)
http.Handle("/socket", websocket.Handler(FlowWebSocket))
fileHandler = http.FileServer(http.Dir(wi.Root))
http.HandleFunc("/", Filefunc)
if wi.UseAuth {
authCJ.Init(time.Minute*30)
http.HandleFunc("/go_auth/", loginFunc)
}
if wi.UseSSL {
go http.ListenAndServeTLS(wi.ListenOn, wi.CertFile, wi.KeyFile, nil)
} else {
go http.ListenAndServe(wi.ListenOn, nil)
}
return nil
}
func loginFunc(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "Invalid login", http.StatusServiceUnavailable)
return
}
user := r.FormValue("user")
hash := r.FormValue("hash")
addr, _, _ := net.SplitHostPort(r.RemoteAddr)
fmt.Printf("Attempt to login with creds %s %s from %s\n", user, hash, addr)
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
}
func Filefunc(w http.ResponseWriter, r *http.Request) {
if checkAuth && !authCJ.Verify(r) {
if r.RequestURI == "/" || r.RequestURI == "" || strings.HasSuffix("#", r.RequestURI) {
http.Redirect(w, r, "/login/login.html", http.StatusTemporaryRedirect)
return
} else if !strings.HasPrefix(r.URL.Path, "/login/") {
http.Error(w, "Access Denied", http.StatusUnauthorized)
return
}
}
if *verbose || *webDebug {
fmt.Printf("URL REQUEST: %v\n", r.RequestURI)
}
if r.RequestURI == "/" || r.RequestURI == "" {
http.Redirect(w, r, "/app/", http.StatusTemporaryRedirect)
} else {
fileHandler.ServeHTTP(w, r)
}
}
func APIfunc(w http.ResponseWriter, r *http.Request) {
if checkAuth && !authCJ.Verify(r) {
http.Error(w, "Access Denied", http.StatusUnauthorized)
return
}
if *verbose || *webDebug {
fmt.Printf("URL REQUEST: %v\n", r.RequestURI)
}
x := strings.Split(strings.TrimRight(r.RequestURI, "/"), "/")
if len(x) != 3 {
//throw an error, because fuck them
http.NotFoundHandler().ServeHTTP(w, r)
fmt.Printf("Len of args is %v\n", len(x))
return
}
PerformAPIRequest(strings.Split(x[2], "?"), w, r)
}
func PerformAPIRequest(id []string, w http.ResponseWriter, r *http.Request) {
if len(id) <= 0 {
fmt.Fprintf(w, "You hit the API page, congrats. But you need to speak up, and give me some GET parameters.\n")
return
}
switch id[0] {
case "stat":
statAPIRequest(w, r)
case "json":
jsonflowsAPIRequest(w, r)
default:
fmt.Fprintf(w, "You hit the API page, congrats. Unfortunately I don't know what the fuck you want\n")
}
}
func statAPIRequest(w http.ResponseWriter, r *http.Request) {
flowIDs := flowTracker.ActiveFlowIDs()
fmt.Fprintf(w, "Currently tracking %d flows\n", len(flowIDs))
for _, id := range flowIDs {
fi, err := flowTracker.GetFlow(id)
if err == nil {
fmt.Fprintf(w, "%d: %s\n", id, fi.String())
} else {
fmt.Printf("Flow ID %d gave me a bad response\n", id)
}
}
}
func jsonflowsAPIRequest(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
http.NotFoundHandler().ServeHTTP(w, r)
}
flowIDStr := r.FormValue("flowID")
if flowIDStr == "" {
x := jsonflowsSendAll()
fmt.Fprintf(w, "%s", x)
return
}
flowID, err := strconv.ParseUint(flowIDStr, 10, 64)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "Invalid flowID")
return
}
fi, err := flowTracker.GetFlow(flowID)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "Invalid flowID")
return
}
fmt.Fprintf(w, "{%s}", fi.JSON())
}
func jsonflowsSendAll() string {
flowIDs := flowTracker.ActiveFlowIDs()
x := "[ "
isFirst := true
for _, id := range flowIDs {
fi, err := flowTracker.GetFlow(id)
if err == nil {
if isFirst == true {
x += fmt.Sprintf("{%s}", fi.JSON())
isFirst = false
} else {
x += fmt.Sprintf(", {%s}", fi.JSON())
}
} else {
fmt.Fprintf(os.Stderr, "Flow ID %d gave me a bad response\n", id)
}
}
x+= fmt.Sprintf(" ]\n")
return x
}
func webSocketSendError(ws io.Writer, reason, request string) {
fmt.Fprintf(ws, "{ \"error\" : \"%s\", \"requested\" : \"%s\" }\n", reason, request)
}
func sendRegularFlows(ws *websocket.Conn, interval *uint32, die *bool) {
for *die != true {
time.Sleep(time.Duration(*interval) * time.Millisecond)
x := jsonflowsSendAll()
fmt.Fprintf(ws, "{ \"type\" : \"flowUpdate\", \"data\": %s }", x)
}
}
// Echo the data received on the WebSocket.
func FlowWebSocket(ws *websocket.Conn) {
if checkAuth && !authCJ.Verify(ws.Request()) {
ws.Close()
return
}
die := false
interval := uint32(500)
startedRegularFlows := false
cmd, err := getCommand(ws)
for (cmd != "exit" && err == nil) {
if cmd == "START_FLOW_UPDATE" {
if !startedRegularFlows {
go sendRegularFlows(ws, &interval, &die)
startedRegularFlows = true
}
} else if strings.HasPrefix(cmd, "SET_UPDATE_INTERVAL ") {
bits := strings.Split(cmd, " ")
if len(bits) != 2 {
webSocketSendError(ws, "Invalid UPDATE_INTERVAL", strings.Join(bits[1:], " "))
} else {
temp, err := strconv.ParseUint(bits[1], 10, 64)
if err != nil {
webSocketSendError(ws, "Invalid UPDATE_INTERVAL", strings.Join(bits[1:], " "))
} else {
interval = uint32(temp)
}
}
} else if strings.HasPrefix(cmd, "GET_SINGLE_FLOW ") {
bits := strings.Split(cmd, " ")
if len(bits) != 2 {
webSocketSendError(ws, "Invalid flowID", strings.Join(bits[1:], " "))
} else {
flowID, err := strconv.ParseUint(bits[1], 10, 64)
if err != nil {
webSocketSendError(ws, "Invalid flowID", bits[1])
} else {
fi, err := flowTracker.GetFlow(flowID)
if err != nil {
webSocketSendError(ws, "flowID unavailable", bits[1])
} else {
fmt.Fprintf(ws, "{%s}", fi.JSON())
}
}
}
} else if strings.HasPrefix(cmd, "GET_GEO_LOCATION ") {
fmt.Printf("%s\n", cmd)
bits := strings.Split(cmd, " ")
if len(bits) < 3 {
return
}
geoipID := bits[1]
args := bits[2:]
response := "[ "
for i := range(args) {
lat, long, err := geoip.GetLatLong(args[i])
if err != nil {
fmt.Printf("Failed to find %s\n", args[i])
continue
}
if i > 0 {
response += ", "
}
response += fmt.Sprintf("{ \"host\" : \"%s\", \"location\" : { \"lat\" : \"%f\", \"long\" : \"%f\" } }", args[i], lat, long)
}
response += " ]"
fmt.Fprintf(ws, "{ \"type\" : \"geoipResponse\", \"id\": \"%s\", \"data\": %s }", geoipID, response)
} else {
fmt.Fprintf(ws, "{ \"error\" : \"Invalid command\", \"sent\" : \"%s\" }", cmd)
}
cmd, err = getCommand(ws)
}
if err != nil && err.Error() != "EOF" {
fmt.Printf("Got error: %v\n", err)
}
}
func getCommand(ws *websocket.Conn) (string, error) {
var cmd []byte = make([]byte, 128)
n, err := ws.Read(cmd)
if err != nil {
return "", err
} else if n <= 0 {
return "", errors.New("ErrFailRead")
}
return string(cmd[:n]), nil
}