init
This commit is contained in:
13 files changed
+1700
No files matched your search
@@ -0,0 +1,137 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
tea "charm.land/bubbletea/v2"
|
||||
"charm.land/ssh"
|
||||
"charm.land/wish/v2"
|
||||
wishtea "charm.land/wish/v2/bubbletea"
|
||||
gossh "golang.org/x/crypto/ssh"
|
||||
|
||||
"thermal-snake/internal/arcade"
|
||||
"thermal-snake/internal/scores"
|
||||
)
|
||||
|
||||
func main() {
|
||||
addr := flag.String("addr", "127.0.0.1:2222", "SSH listen address")
|
||||
data := flag.String("data", ".thermal-snake", "directory for the score database and SSH host key")
|
||||
flag.Parse()
|
||||
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer stop()
|
||||
if err := serve(ctx, *addr, *data); err != nil {
|
||||
slog.Error("thermal snake", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func serve(ctx context.Context, addr, data string) error {
|
||||
if err := os.MkdirAll(data, 0700); err != nil {
|
||||
return fmt.Errorf("open data directory: %w", err)
|
||||
}
|
||||
store, err := scores.Open(filepath.Join(data, "scores.db"))
|
||||
if err != nil {
|
||||
return fmt.Errorf("open score database: %w", err)
|
||||
}
|
||||
defer store.Close()
|
||||
|
||||
sessions := make(chan struct{}, 32)
|
||||
server, err := wish.NewServer(
|
||||
wish.WithAddress(addr),
|
||||
wish.WithHostKeyPath(filepath.Join(data, "host_ed25519")),
|
||||
wish.WithPublicKeyAuth(func(_ ssh.Context, key ssh.PublicKey) bool {
|
||||
_, certificate := key.(*gossh.Certificate)
|
||||
return key != nil && !certificate
|
||||
}),
|
||||
wish.WithIdleTimeout(30*time.Minute),
|
||||
wish.WithMiddleware(wishtea.MiddlewareWithProgramHandler(func(session ssh.Session) *tea.Program {
|
||||
pty, _, ok := session.Pty()
|
||||
if !ok {
|
||||
wish.Fatalln(session, "Thermal Snake needs a terminal. Reconnect with ssh -t.")
|
||||
return nil
|
||||
}
|
||||
key := session.PublicKey()
|
||||
if key == nil {
|
||||
wish.Fatalln(session, "Thermal Snake needs an SSH public key.")
|
||||
return nil
|
||||
}
|
||||
fingerprint := gossh.FingerprintSHA256(key)
|
||||
pilot, err := store.Register(fingerprint)
|
||||
if err != nil {
|
||||
slog.Error("open pilot profile", "error", err)
|
||||
wish.Fatalln(session, "The score database is unavailable. Try reconnecting.")
|
||||
return nil
|
||||
}
|
||||
size := limitTerminalSize(tea.WindowSizeMsg{Width: pty.Window.Width, Height: pty.Window.Height})
|
||||
model := arcade.New(store, fingerprint, pilot, size.Width, size.Height)
|
||||
options := append(wishtea.MakeOptions(session),
|
||||
tea.WithWindowSize(size.Width, size.Height),
|
||||
tea.WithContext(session.Context()),
|
||||
tea.WithFilter(func(_ tea.Model, msg tea.Msg) tea.Msg {
|
||||
switch msg := msg.(type) {
|
||||
case tea.WindowSizeMsg:
|
||||
return limitTerminalSize(msg)
|
||||
case tea.SuspendMsg:
|
||||
return tea.ResumeMsg{}
|
||||
default:
|
||||
return msg
|
||||
}
|
||||
}),
|
||||
)
|
||||
return tea.NewProgram(model, options...)
|
||||
}), func(next ssh.Handler) ssh.Handler {
|
||||
return func(session ssh.Session) {
|
||||
select {
|
||||
case sessions <- struct{}{}:
|
||||
defer func() { <-sessions }()
|
||||
next(session)
|
||||
default:
|
||||
wish.Fatalln(session, "The arcade is full. Try again in a moment.")
|
||||
}
|
||||
}
|
||||
}),
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("configure SSH: %w", err)
|
||||
}
|
||||
server.HandshakeTimeout = 10 * time.Second
|
||||
server.SessionRequestCallback = func(_ ssh.Session, request string) bool { return request == "shell" }
|
||||
|
||||
listener, err := net.Listen("tcp", server.Addr)
|
||||
if err != nil {
|
||||
return fmt.Errorf("listen for SSH: %w", err)
|
||||
}
|
||||
slog.Info("Thermal Snake listening", "address", listener.Addr(), "host_key", gossh.FingerprintSHA256(server.HostSigners[0].PublicKey()))
|
||||
stopClosing := context.AfterFunc(ctx, func() { _ = listener.Close() })
|
||||
defer stopClosing()
|
||||
err = server.Serve(listener)
|
||||
if ctx.Err() != nil {
|
||||
shutdown, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
if err := server.Shutdown(shutdown); err != nil {
|
||||
_ = server.Close()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
_ = server.Close()
|
||||
if !errors.Is(err, ssh.ErrServerClosed) {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Bound rendering and terminal buffers to a size larger than a typical terminal.
|
||||
func limitTerminalSize(size tea.WindowSizeMsg) tea.WindowSizeMsg {
|
||||
return tea.WindowSizeMsg{Width: min(size.Width, 300), Height: min(size.Height, 150)}
|
||||
}
|
||||
Reference in new issue
Block a user