-
Notifications
You must be signed in to change notification settings - Fork 3
/
http-logger.go
116 lines (100 loc) · 2.77 KB
/
http-logger.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
package main
import (
"encoding/json"
"flag"
"fmt"
"html/template"
"log"
"net/http"
"net/url"
"path"
"strings"
"time"
)
var Version string = "0.1.0"
type context map[string]interface{}
type request struct {
Timestamp string `json:"ts"`
ClientIP string `json:"clientip"`
Method string `json:"method"`
Host string `json:"host"`
URL string `json:"url"`
Headers http.Header `json:"headers"`
FormValues url.Values `json:"formvalues"`
TLS bool `json:"tls"`
}
func logJSON(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
now := time.Now()
addr := r.RemoteAddr
lastColon := strings.LastIndex(addr, ":")
ip := addr[0:lastColon]
r.ParseForm()
req := &request{
Timestamp: now.String(),
ClientIP: ip,
Method: r.Method,
Host: r.Host,
URL: r.URL.String(),
Headers: r.Header,
FormValues: r.PostForm,
TLS: r.TLS != nil,
}
reqJSON, err := json.Marshal(req)
if err != nil {
log.Print(err)
} else {
log.Println(string(reqJSON))
}
handler.ServeHTTP(w, r)
})
}
type blockedHandler struct {
templates *template.Template
}
func newBlockedHandler(templatePath string) *blockedHandler {
indexTemplate := path.Join(templatePath, "index.html")
templates := template.Must(template.ParseFiles(indexTemplate))
return &blockedHandler{
templates: templates,
}
}
func (bh *blockedHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
ctx := &context{
"Host": req.Host,
}
err := bh.templates.ExecuteTemplate(w, "index.html", ctx)
if err != nil {
e := fmt.Sprintf("Error rendering template: %s", err.Error())
http.Error(w, e, http.StatusInternalServerError)
return
}
}
func main() {
addr := flag.String("addr", ":8080", "Address to bind to")
templatePath := flag.String("template-path", "./", "Path to templates")
logfile := flag.String("log", "/dev/stdout", "Path to log file")
sslAddr := flag.String("ssl-addr", ":8443", "Address to bind to")
certFile := flag.String("certfile", "cert.pem", "TLS certificate file")
keyFile := flag.String("keyfile", "key.pem", "TLS key file")
version := flag.Bool("version", false, "print the version number and then exit")
flag.Parse()
if *version {
fmt.Println(Version)
return
}
log.SetOutput(NewReOpeningWriter(*logfile))
log.SetFlags(0)
http.Handle("/", newBlockedHandler(*templatePath))
service := logJSON(http.DefaultServeMux)
log.Printf("Listening for HTTPS on %s\n", *sslAddr)
go func() {
if err := http.ListenAndServeTLS(*sslAddr, *certFile, *keyFile, service); err != nil {
log.Print(err)
}
}()
log.Printf("Listening for HTTP on %s\n", *addr)
if err := http.ListenAndServe(*addr, service); err != nil {
log.Fatal(err)
}
}