-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.go
130 lines (114 loc) · 3.23 KB
/
routes.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"encoding/json"
"github.com/cockroachdb/errors"
"github.com/gorilla/mux"
"github.com/qup42/loghead/processor"
"github.com/qup42/loghead/ssh"
"github.com/qup42/loghead/types"
"github.com/qup42/loghead/util"
"github.com/rs/zerolog/log"
"io"
"net/http"
"time"
)
type FailableHandler func(http.ResponseWriter, *http.Request) error
func (fn FailableHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if err := fn(w, r); err != nil {
log.Error().Err(err).Str("path", r.RequestURI).Msg("HTTP Request error")
http.Error(w, err.Error(), 500)
}
}
func addSSHRecordingRoutes(
r *mux.Router,
rs *ssh.RecordingService) {
r.Handle("/record", handleSSHRecording(rs))
r.NotFoundHandler = handleNotFound()
}
func addClientLogsRoutes(
r *mux.Router,
c *types.Config,
fwd *processor.ForwardingService,
fl *processor.FileLoggerService,
hi *processor.HostInfoService,
ms *processor.MetricsService) {
r.Handle("/c/{collection:[a-zA-Z0-9-_.]+}/{private_id:[0-9a-f]+}", handleTailnodeLogs(fwd, fl, hi, ms)).Methods(http.MethodPost)
if c.Loghead.Processors.Metrics {
r.Handle("/metrics", handleMetrics(ms))
}
r.NotFoundHandler = handleNotFound()
}
func handleSSHRecording(rec *ssh.RecordingService) http.Handler {
return FailableHandler(func(w http.ResponseWriter, r *http.Request) error {
log.Trace().Msg("Starting SSH Session recording")
rc := http.NewResponseController(w)
// this is a streaming request, disable the read deadline
err := rc.SetReadDeadline(time.Time{})
if err != nil {
return errors.Errorf("setting read deadline: %w", err)
}
err = rec.Record(r.Body)
if err != nil {
return errors.Errorf("recording sesion: %w", err)
}
log.Trace().Msg("SSH Session recording finished")
return nil
})
}
func handleTailnodeLogs(
fwd *processor.ForwardingService,
fl *processor.FileLoggerService,
hi *processor.HostInfoService,
ms *processor.MetricsService) http.Handler {
return FailableHandler(func(w http.ResponseWriter, r *http.Request) error {
vars := mux.Vars(r)
collection := vars["collection"]
private_id := vars["private_id"]
msg, err := io.ReadAll(r.Body)
if err != nil {
return errors.Errorf("reading request body: %w", err)
}
if r.Header.Get("Content-Encoding") == "zstd" {
msg = util.ZstdDecode(msg)
}
if fwd != nil {
err := fwd.Forward(msg)
if err != nil {
log.Error().Err(err).Msg("error forwarding")
}
}
var maps []map[string]interface{}
err = json.Unmarshal(msg, &maps)
if err != nil {
return errors.Errorf("message unmarshal: %w", err)
}
log.Debug().Msgf("Received %d messages for %s/%s", len(maps)+1, collection, private_id)
for _, m := range maps {
msg := processor.LogtailMsg{
Msg: m,
Collection: collection,
PrivateID: private_id,
}
if fl != nil {
fl.Log(msg)
}
if hi != nil {
hi.Process(msg)
}
if ms != nil {
ms.Process(msg)
}
}
w.WriteHeader(http.StatusOK)
return nil
})
}
func handleMetrics(ms *processor.MetricsService) http.Handler {
return ms.PromHandler()
}
func handleNotFound() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Warn().Msgf("Unknown path %s called", r.RequestURI)
w.WriteHeader(http.StatusNotFound)
})
}