aboutsummaryrefslogtreecommitdiff
path: root/pkg/nspkt/monitor.go
blob: cc17e7f34aa5fcfcc8aadfcfda6e8fd524f9dfee (plain)
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
package nspkt

import (
	_ "embed"
	"encoding/hex"
	"encoding/json"
	"io"
	"net/http"
	"strconv"
)

//go:embed monitor.html
var monitorHTML []byte

// DebugMonitorHandler returns a HTTP handler which serves a webpage to monitor
// sent and received connectionless packets in real-time.
func DebugMonitorHandler(l *Listener) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Cache-Control", "private, no-cache, no-store")
		w.Header().Set("Expires", "0")
		w.Header().Set("Pragma", "no-cache")

		if r.URL.RawQuery != "sse" {
			w.Header().Set("Content-Type", "text/html; charset=utf-8")
			w.Header().Set("Content-Length", strconv.Itoa(len(monitorHTML)))
			w.WriteHeader(http.StatusOK)
			w.Write(monitorHTML)
			return
		}

		f, ok := w.(http.Flusher)
		if !ok {
			http.Error(w, "cannot stream events", http.StatusInternalServerError)
			return
		}

		c := make(chan MonitorPacket, 16)
		go l.Monitor(r.Context(), c)

		w.Header().Set("Content-Type", "text/event-stream")
		w.Header().Set("Connection", "keep-alive")
		w.WriteHeader(http.StatusOK)

		io.WriteString(w, "event: init\ndata: ")
		if addr := l.LocalAddr(); addr != nil {
			io.WriteString(w, addr.String())
		}
		io.WriteString(w, "\n\n")
		f.Flush()

		e := json.NewEncoder(w)
		for p := range c {
			io.WriteString(w, "event: packet\ndata: ")
			e.Encode(map[string]any{
				"in":     p.In,
				"remote": p.Remote.String(),
				"desc":   p.Desc,
				"data":   hex.Dump(p.Data),
			})
			io.WriteString(w, "\n")
			f.Flush()
		}
	})
}