mirror of
https://github.com/henrygd/beszel.git
synced 2025-12-17 18:56:17 +01:00
36 lines
849 B
Go
36 lines
849 B
Go
package agent
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log/slog"
|
|
"os"
|
|
|
|
sshServer "github.com/gliderlabs/ssh"
|
|
)
|
|
|
|
func (a *Agent) startServer() {
|
|
sshServer.Handle(a.handleSession)
|
|
|
|
slog.Info("Starting SSH server", "address", a.addr)
|
|
if err := sshServer.ListenAndServe(a.addr, nil, sshServer.NoPty(),
|
|
sshServer.PublicKeyAuth(func(ctx sshServer.Context, key sshServer.PublicKey) bool {
|
|
allowed, _, _, _, _ := sshServer.ParseAuthorizedKey(a.pubKey)
|
|
return sshServer.KeysEqual(key, allowed)
|
|
}),
|
|
); err != nil {
|
|
slog.Error("Error starting SSH server", "err", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func (a *Agent) handleSession(s sshServer.Session) {
|
|
stats := a.gatherStats()
|
|
slog.Debug("Sending stats", "data", stats)
|
|
if err := json.NewEncoder(s).Encode(stats); err != nil {
|
|
slog.Error("Error encoding stats", "err", err)
|
|
s.Exit(1)
|
|
return
|
|
}
|
|
s.Exit(0)
|
|
}
|