summaryrefslogtreecommitdiffhomepage
path: root/tsconsensus/monitor.go
blob: b937926a651dd019c09415ae14f6dbb3392e7c54 (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
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
// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause

package tsconsensus

import (
	"context"
	"encoding/json"
	"fmt"
	"io"
	"log"
	"net/http"
	"slices"

	"tailscale.com/ipn"
	"tailscale.com/ipn/ipnstate"
	"tailscale.com/tsnet"
	"tailscale.com/util/dnsname"
)

type status struct {
	Status    *ipnstate.Status
	RaftState string
}

type monitor struct {
	ts  *tsnet.Server
	con *Consensus
	sg  statusGetter
}

func (m *monitor) getStatus(ctx context.Context) (status, error) {
	tStatus, err := m.sg.getStatus(ctx)
	if err != nil {
		return status{}, err
	}
	return status{Status: tStatus, RaftState: m.con.raft.State().String()}, nil
}

func serveMonitor(c *Consensus, ts *tsnet.Server, listenAddr string) (*http.Server, error) {
	ln, err := ts.Listen("tcp", listenAddr)
	if err != nil {
		return nil, err
	}
	m := &monitor{con: c, ts: ts, sg: &tailscaleStatusGetter{
		ts: ts,
	}}
	mux := http.NewServeMux()
	mux.HandleFunc("GET /full", m.handleFullStatus)
	mux.HandleFunc("GET /{$}", m.handleSummaryStatus)
	mux.HandleFunc("GET /netmap", m.handleNetmap)
	mux.HandleFunc("POST /dial", m.handleDial)
	srv := &http.Server{Handler: mux}
	go func() {
		err := srv.Serve(ln)
		log.Printf("MonitorHTTP stopped serving with error: %v", err)
	}()
	return srv, nil
}

func (m *monitor) handleFullStatus(w http.ResponseWriter, r *http.Request) {
	s, err := m.getStatus(r.Context())
	if err != nil {
		log.Printf("monitor: error getStatus: %v", err)
		http.Error(w, "", http.StatusInternalServerError)
		return
	}
	if err := json.NewEncoder(w).Encode(s); err != nil {
		log.Printf("monitor: error encoding full status: %v", err)
		return
	}
}

func (m *monitor) handleSummaryStatus(w http.ResponseWriter, r *http.Request) {
	s, err := m.getStatus(r.Context())
	if err != nil {
		log.Printf("monitor: error getStatus: %v", err)
		http.Error(w, "", http.StatusInternalServerError)
		return
	}
	lines := []string{}
	for _, p := range s.Status.Peer {
		if p.Online {
			name := dnsname.FirstLabel(p.DNSName)
			lines = append(lines, fmt.Sprintf("%s\t\t%d\t%d\t%t", name, p.RxBytes, p.TxBytes, p.Active))
		}
	}
	_, err = w.Write(fmt.Appendf(nil, "RaftState: %s\n", s.RaftState))
	if err != nil {
		log.Printf("monitor: error writing status: %v", err)
		return
	}

	slices.Sort(lines)
	for _, ln := range lines {
		_, err = w.Write(fmt.Appendf(nil, "%s\n", ln))
		if err != nil {
			log.Printf("monitor: error writing status: %v", err)
			return
		}
	}
}

func (m *monitor) handleNetmap(w http.ResponseWriter, r *http.Request) {
	lc, err := m.ts.LocalClient()
	if err != nil {
		log.Printf("monitor: error LocalClient: %v", err)
		http.Error(w, "", http.StatusInternalServerError)
		return
	}
	watcher, err := lc.WatchIPNBus(r.Context(), ipn.NotifyInitialNetMap)
	if err != nil {
		log.Printf("monitor: error WatchIPNBus: %v", err)
		http.Error(w, "", http.StatusInternalServerError)
		return
	}
	defer watcher.Close()

	n, err := watcher.Next()
	if err != nil {
		log.Printf("monitor: error watcher.Next: %v", err)
		http.Error(w, "", http.StatusInternalServerError)
		return
	}
	encoder := json.NewEncoder(w)
	encoder.SetIndent("", "\t")
	if err := encoder.Encode(n); err != nil {
		log.Printf("monitor: error encoding netmap: %v", err)
		return
	}
}

func (m *monitor) handleDial(w http.ResponseWriter, r *http.Request) {
	var dialParams struct {
		Addr string
	}
	defer r.Body.Close()
	bs, err := io.ReadAll(http.MaxBytesReader(w, r.Body, maxBodyBytes))
	if err != nil {
		log.Printf("monitor: error reading body: %v", err)
		http.Error(w, "", http.StatusInternalServerError)
		return
	}
	err = json.Unmarshal(bs, &dialParams)
	if err != nil {
		log.Printf("monitor: error unmarshalling json: %v", err)
		http.Error(w, "", http.StatusBadRequest)
		return
	}
	c, err := m.ts.Dial(r.Context(), "tcp", dialParams.Addr)
	if err != nil {
		log.Printf("monitor: error dialing: %v", err)
		http.Error(w, "", http.StatusInternalServerError)
		return
	}
	c.Close()
	w.Write([]byte("ok\n"))
}