-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp2log.go
61 lines (55 loc) · 1.34 KB
/
http2log.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
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"os"
)
var (
path string
key string
file string
addr string
port string
)
func initFlag() {
flag.StringVar(&path, "u", "/http2log", "URL path of log interface;")
flag.StringVar(&key, "k", os.Getenv("KEY"), "HTTP header 'X-API-KEY' content;")
flag.StringVar(&file, "f", "/var/log/http2log.log", "Path to log file;")
flag.StringVar(&addr, "a", "127.0.0.1", "Bind address;")
flag.StringVar(&port, "p", "8283", "Bind port;")
flag.Parse()
}
func main() {
initFlag()
http.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
auth := r.Header.Get("X-API-KEY")
if auth != key {
w.WriteHeader(http.StatusForbidden)
fmt.Fprintf(w, http.StatusText(http.StatusForbidden)+".\n")
log.Printf("Authentication failed, rejected.")
return
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
}
f, err := os.OpenFile(file, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
panic(err)
}
b := string(body) + "\n"
defer func() {
_ = f.Close()
}()
if _, err = f.WriteString(b); err != nil {
panic(err)
}
fmt.Fprintf(w, "Succeed.\n")
log.Printf("Authentication succeeded, written.")
})
log.Fatal(http.ListenAndServe(net.JoinHostPort(addr, port), nil))
}