refactor: extract NewServer for SSH shutdown control

This commit is contained in:
2026-03-25 17:38:01 +09:00
parent 6e78d8a073
commit 7c9a12cd6e

View File

@@ -14,15 +14,17 @@ import (
"github.com/tolelom/catacombs/ui" "github.com/tolelom/catacombs/ui"
) )
func Start(addr string, lobby *game.Lobby, db *store.DB) error { // NewServer creates the SSH server but does not start it.
// The caller is responsible for calling ListenAndServe() and Shutdown().
func NewServer(addr string, lobby *game.Lobby, db *store.DB) (*ssh.Server, error) {
s, err := wish.NewServer( s, err := wish.NewServer(
wish.WithAddress(addr), wish.WithAddress(addr),
wish.WithHostKeyPath(".ssh/catacombs_host_key"), wish.WithHostKeyPath(".ssh/catacombs_host_key"),
wish.WithPublicKeyAuth(func(_ ssh.Context, _ ssh.PublicKey) bool { wish.WithPublicKeyAuth(func(_ ssh.Context, _ ssh.PublicKey) bool {
return true // accept all keys return true
}), }),
wish.WithPasswordAuth(func(_ ssh.Context, _ string) bool { wish.WithPasswordAuth(func(_ ssh.Context, _ string) bool {
return true // accept any password (game server, not secure shell) return true
}), }),
wish.WithMiddleware( wish.WithMiddleware(
bubbletea.Middleware(func(s ssh.Session) (tea.Model, []tea.ProgramOption) { bubbletea.Middleware(func(s ssh.Session) (tea.Model, []tea.ProgramOption) {
@@ -45,9 +47,17 @@ func Start(addr string, lobby *game.Lobby, db *store.DB) error {
), ),
) )
if err != nil { if err != nil {
return fmt.Errorf("could not create server: %w", err) return nil, fmt.Errorf("could not create server: %w", err)
} }
return s, nil
}
// Start creates and starts the SSH server (blocking).
func Start(addr string, lobby *game.Lobby, db *store.DB) error {
s, err := NewServer(addr, lobby, db)
if err != nil {
return err
}
slog.Info("starting SSH server", "addr", addr) slog.Info("starting SSH server", "addr", addr)
return s.ListenAndServe() return s.ListenAndServe()
} }