mirror of
https://github.com/db9979/sshtermy.git
synced 2026-12-06 20:54:09 +01:00
- F5: zweispaltiger Datei-Browser (lokal | Server) über dieselbe SSH-Verbindung, Dateien und Ordner hoch- und herunterladen mit Fortschritt, Abbruch, Überschreiben-Rückfrage, Ordner anlegen, löschen - Runbooks: Ziel samt ausgeführten Befehlen speichern (Strg+S), im Ziel-Modus und in der Palette wieder starten; der Agent bekommt die Befehle als Vorlage, bestätigt wird weiterhin jeder Schritt - devsshd mit sftp-Subsystem, fakellm mit /v1/models - README, Hilfe und Screenshots Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018i4GUgYsYk78PSL5QZPeim
219 lines
4.4 KiB
Go
219 lines
4.4 KiB
Go
package ui
|
|
|
|
import (
|
|
"context"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
tea "charm.land/bubbletea/v2"
|
|
|
|
"github.com/db9979/sshtermy/internal/agent"
|
|
"github.com/db9979/sshtermy/internal/domain"
|
|
"github.com/db9979/sshtermy/internal/sshx"
|
|
"github.com/db9979/sshtermy/internal/termemu"
|
|
)
|
|
|
|
type sessState int
|
|
|
|
const (
|
|
stConnecting sessState = iota
|
|
stConnected
|
|
stClosed
|
|
)
|
|
|
|
type panelKind int
|
|
|
|
const (
|
|
panelNone panelKind = iota
|
|
panelGoal
|
|
panelAsk
|
|
panelTunnels
|
|
)
|
|
|
|
// session ist eine offene SSH-Sitzung (ein Tab).
|
|
type session struct {
|
|
id int
|
|
conn domain.Connection
|
|
client *sshx.Client
|
|
shell *sshx.Shell
|
|
term *termemu.Term
|
|
tunnels *sshx.TunnelManager
|
|
state sessState
|
|
err string
|
|
started time.Time
|
|
|
|
pending atomic.Bool // Ausgabe-Nachricht unterwegs (Koaleszieren)
|
|
lastOutput time.Time
|
|
|
|
// Ghost-Text
|
|
ghost, ghostFor string
|
|
ghostSeq int
|
|
ghostCancel context.CancelFunc
|
|
|
|
// eigene Befehle und Folgebefehle
|
|
history []string
|
|
chips []agent.Suggestion
|
|
chipSeq int
|
|
chipArmed int // Index eines roten Chips nach dem ersten Druck, sonst -1
|
|
lastFollow time.Time
|
|
followDue time.Time
|
|
|
|
panel panelKind
|
|
panelFocus bool
|
|
goal goalState
|
|
ask askState
|
|
tunSel int
|
|
files filesState
|
|
}
|
|
|
|
// writer für den PTY-Strom: füttert den Emulator und meldet der Oberfläche,
|
|
// dass neu gezeichnet werden soll (höchstens eine Nachricht gleichzeitig).
|
|
type outWriter struct {
|
|
s *session
|
|
ev chan<- tea.Msg
|
|
}
|
|
|
|
type outputMsg struct{ sid int }
|
|
|
|
func (w outWriter) Write(p []byte) (int, error) {
|
|
n, err := w.s.term.Write(p)
|
|
if w.s.pending.CompareAndSwap(false, true) {
|
|
select {
|
|
case w.ev <- outputMsg{w.s.id}:
|
|
default:
|
|
w.s.pending.Store(false)
|
|
}
|
|
}
|
|
return n, err
|
|
}
|
|
|
|
type connectedMsg struct {
|
|
sid int
|
|
client *sshx.Client
|
|
shell *sshx.Shell
|
|
}
|
|
type connectErrMsg struct {
|
|
sid int
|
|
err error
|
|
}
|
|
type shellClosedMsg struct {
|
|
sid int
|
|
err error
|
|
}
|
|
|
|
// hostKeyAskMsg / secretAskMsg: der Verbindungsaufbau wartet auf die Antwort.
|
|
type hostKeyAskMsg struct {
|
|
host, fp, typ string
|
|
reply chan bool
|
|
}
|
|
type secretAskMsg struct {
|
|
label string
|
|
reply chan string
|
|
}
|
|
|
|
func (a *App) connectCmd(s *session) tea.Cmd {
|
|
ev := a.events
|
|
hk := a.hostKeys
|
|
st := a.st
|
|
cols, rows := a.termSize(s)
|
|
return func() tea.Msg {
|
|
opts := sshx.Options{
|
|
HostKeys: hk,
|
|
Resolve: st.Connection,
|
|
Prompt: func(host, fp, typ string) bool {
|
|
r := make(chan bool, 1)
|
|
ev <- hostKeyAskMsg{host, fp, typ, r}
|
|
return <-r
|
|
},
|
|
AskSecret: func(label string) (string, error) {
|
|
r := make(chan string, 1)
|
|
ev <- secretAskMsg{label, r}
|
|
v := <-r
|
|
if v == "\x00cancel" {
|
|
return "", context.Canceled
|
|
}
|
|
return v, nil
|
|
},
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
|
|
defer cancel()
|
|
c, err := sshx.Dial(ctx, s.conn, opts)
|
|
if err != nil {
|
|
return connectErrMsg{s.id, err}
|
|
}
|
|
s.term = termemu.New(cols, rows, shellWriter{s})
|
|
sh, err := c.OpenShell(cols, rows, outWriter{s, ev})
|
|
if err != nil {
|
|
c.Close()
|
|
return connectErrMsg{s.id, err}
|
|
}
|
|
go func() { err := <-sh.Done; ev <- shellClosedMsg{s.id, err} }()
|
|
return connectedMsg{s.id, c, sh}
|
|
}
|
|
}
|
|
|
|
// shellWriter: vom Emulator kodierte Tasten → SSH (Shell kann kurz nil sein).
|
|
type shellWriter struct{ s *session }
|
|
|
|
func (w shellWriter) Write(p []byte) (int, error) {
|
|
for i := 0; i < 100 && w.s.shell == nil; i++ {
|
|
time.Sleep(20 * time.Millisecond)
|
|
}
|
|
if w.s.shell == nil {
|
|
return len(p), nil
|
|
}
|
|
return w.s.shell.Write(p)
|
|
}
|
|
|
|
func (s *session) close() {
|
|
if s.ghostCancel != nil {
|
|
s.ghostCancel()
|
|
}
|
|
s.goal.cancel()
|
|
s.ask.cancel()
|
|
if s.tunnels != nil {
|
|
s.tunnels.StopAll()
|
|
}
|
|
if s.files.xfer != nil {
|
|
s.files.xfer.cancel()
|
|
}
|
|
if s.files.remote != nil {
|
|
s.files.remote.Close()
|
|
}
|
|
if s.shell != nil {
|
|
s.shell.Close()
|
|
}
|
|
if s.client != nil {
|
|
s.client.Close()
|
|
}
|
|
if s.term != nil {
|
|
s.term.Close()
|
|
}
|
|
s.state = stClosed
|
|
}
|
|
|
|
// context fürs Sprachmodell.
|
|
func (a *App) agentContext(s *session, withScreen bool) agent.Context {
|
|
cfg := a.st.LLM()
|
|
c := agent.Context{Host: s.conn.Alias, Commands: lastN(s.history, 10)}
|
|
if s.client != nil {
|
|
c.OS = s.client.Remote.String()
|
|
c.Shell = s.client.Remote.Shell
|
|
}
|
|
if withScreen && cfg.SendScreen && s.term != nil {
|
|
n := cfg.ContextLines
|
|
if n <= 0 {
|
|
n = 80
|
|
}
|
|
c.Screen = s.term.PlainLines(n)
|
|
}
|
|
return c
|
|
}
|
|
|
|
func lastN(x []string, n int) []string {
|
|
if len(x) > n {
|
|
return x[len(x)-n:]
|
|
}
|
|
return x
|
|
}
|