- commit
- bdec0fc
- parent
- 5f405b9
- author
- Antonio Mika
- date
- 2025-04-10 15:25:06 -0400 EDT
Fix prometheus labels and utf8
1 files changed,
+17,
-7
+17,
-7
1@@ -5,6 +5,7 @@ import (
2 "crypto/ed25519"
3 "crypto/rand"
4 "crypto/subtle"
5+ "encoding/base64"
6 "encoding/pem"
7 "errors"
8 "fmt"
9@@ -17,6 +18,7 @@ import (
10 "strings"
11 "sync"
12 "time"
13+ "unicode/utf8"
14
15 "github.com/antoniomika/syncmap"
16 "github.com/prometheus/client_golang/prometheus"
17@@ -485,6 +487,8 @@ func NewSSHServer(ctx context.Context, logger *slog.Logger, config *SSHServerCon
18 return
19 }
20
21+ command := "shell"
22+
23 if len(req.Payload) > 0 {
24 var payload = struct{ Value string }{}
25 err := ssh.Unmarshal(req.Payload, &payload)
26@@ -494,17 +498,23 @@ func NewSSHServer(ctx context.Context, logger *slog.Logger, config *SSHServerCon
27 return
28 }
29
30- if sc.SSHServer.Config.PromListenAddr != "" {
31- sc.SSHServer.SessionsCreated.WithLabelValues(payload.Value).Inc()
32- defer func() {
33- sc.SSHServer.SessionsFinished.WithLabelValues(payload.Value).Inc()
34- sc.SSHServer.SessionsDuration.WithLabelValues(payload.Value).Add(time.Since(sc.Start).Seconds())
35- }()
36- }
37+ command = payload.Value
38
39 sesh.SetValue("command", strings.Fields(payload.Value))
40 }
41
42+ if !utf8.ValidString(command) {
43+ command = base64.StdEncoding.EncodeToString([]byte(command))
44+ }
45+
46+ if sc.SSHServer.Config.PromListenAddr != "" {
47+ sc.SSHServer.SessionsCreated.WithLabelValues(command).Inc()
48+ defer func() {
49+ sc.SSHServer.SessionsFinished.WithLabelValues(command).Inc()
50+ sc.SSHServer.SessionsDuration.WithLabelValues(command).Add(time.Since(sc.Start).Seconds())
51+ }()
52+ }
53+
54 h := func(*SSHServerConnSession) error { return nil }
55 for _, m := range sc.SSHServer.Config.Middleware {
56 h = m(h)