repos / pico

pico services - prose.sh, pastes.sh, imgs.sh, feeds.sh, pgs.sh
git clone https://github.com/picosh/pico.git

pico / wish
Eric Bower · 08 Apr 24

logger.go

 1package wish
 2
 3import (
 4	"log/slog"
 5	"time"
 6
 7	"github.com/charmbracelet/ssh"
 8	"github.com/charmbracelet/wish"
 9)
10
11func LogMiddleware(logger *slog.Logger) wish.Middleware {
12	return func(sh ssh.Handler) ssh.Handler {
13		return func(s ssh.Session) {
14			ct := time.Now()
15			pty, _, ok := s.Pty()
16
17			logger.Info(
18				"connect",
19				"user", s.User(),
20				"ip", s.RemoteAddr().String(),
21				"pty", ok,
22				"term", pty.Term,
23				"windowWidth", pty.Window.Width,
24				"windowHeight", pty.Window.Height,
25			)
26
27			sh(s)
28
29			logger.Info(
30				"disconnect",
31				"ip", s.RemoteAddr().String(),
32				"duration", time.Since(ct),
33			)
34		}
35	}
36}