46 lines
995 B
Go
46 lines
995 B
Go
package internal
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
"github.com/charmbracelet/ssh"
|
|
"github.com/charmbracelet/wish"
|
|
lm "github.com/charmbracelet/wish/logging"
|
|
)
|
|
|
|
func NewServer(host string, port int) (*ssh.Server, error) {
|
|
srv, err := wish.NewServer(
|
|
wish.WithAddress(fmt.Sprintf("%s:%d", host, port)),
|
|
wish.WithHostKeyPath(".ssh/term_info_ed25519"),
|
|
wish.WithPublicKeyAuth(func(ctx ssh.Context, key ssh.PublicKey) bool {
|
|
return true
|
|
}),
|
|
wish.WithMiddleware(
|
|
appMiddleware,
|
|
lm.Middleware(),
|
|
),
|
|
)
|
|
return srv, err
|
|
}
|
|
|
|
func appMiddleware(sh ssh.Handler) ssh.Handler {
|
|
return func(s ssh.Session) {
|
|
_, _, active := s.Pty()
|
|
if !active {
|
|
wish.Fatalln(s, "no active terminal, skipping")
|
|
return
|
|
}
|
|
|
|
m := initialModel(s)
|
|
p := tea.NewProgram(m, tea.WithInput(s), tea.WithOutput(s), tea.WithAltScreen())
|
|
|
|
if _, err := p.Run(); err != nil {
|
|
log.Println("Error running program:", err)
|
|
}
|
|
|
|
updates.unsubscribe(m.updateChan)
|
|
}
|
|
}
|