forked from kung-foo/freki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_http.go
65 lines (54 loc) · 1.13 KB
/
log_http.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
package freki
import (
"encoding/hex"
"fmt"
"io/ioutil"
"net"
"net/http"
)
type HTTPLogger struct {
port uint
//maxReadSize uint
processor *Processor
}
func NewHTTPLogger(port uint) *HTTPLogger {
return &HTTPLogger{
port: port,
}
}
func (h *HTTPLogger) Port() uint {
return h.port
}
func (h *HTTPLogger) Type() string {
return "log.http"
}
func (h *HTTPLogger) Start(p *Processor) error {
h.processor = p
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
host, port, _ := net.SplitHostPort(r.RemoteAddr)
ck := NewConnKeyByString(host, port)
md := h.processor.Connections.GetByFlow(ck)
logger.Infof("[log.http] %s -> %s\n%s %s\n%v",
host,
md.TargetPort.String(),
r.Method, r.URL,
r.Header)
if r.Body != nil {
defer r.Body.Close()
body, _ := ioutil.ReadAll(r.Body)
if len(body) > 0 {
logger.Infof("[log.http] %s -> %s\n%s",
host,
md.TargetPort.String(),
hex.Dump(body),
)
}
}
fmt.Fprintf(w, "OK\n")
})
return http.ListenAndServe(fmt.Sprintf(":%d", h.port), nil)
}
func (h *HTTPLogger) Shutdown() error {
// TODO: go1.8 add server shutdown
return nil
}