-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsshapp.go
141 lines (123 loc) · 2.89 KB
/
sshapp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"fmt"
"log"
"os"
"strings"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/ssh"
"github.com/charmbracelet/wish"
"github.com/charmbracelet/wish/activeterm"
"github.com/charmbracelet/wish/logging"
"github.com/creack/pty"
"github.com/muesli/termenv"
"github.com/shafreeck/cortana"
"github.com/shafreeck/guru/tui"
)
type guruSSHServer struct {
s *ssh.Server
cmd *cortana.Cortana
auth string
address string
}
func newGuruSSHServer(address, auth string) *guruSSHServer {
return &guruSSHServer{cmd: cortana.New(), address: address, auth: auth}
}
func (g *guruSSHServer) serve() error {
tui.SSHAPPMode = true
var opts []ssh.Option
opts = append(opts, wish.WithAddress(g.address))
if g.auth != "" {
opts = append(opts, wish.WithPasswordAuth(func(ctx ssh.Context, password string) bool {
// no auth
if g.auth == "" {
return true
}
return password == g.auth
}))
}
opts = append(opts, wish.WithMiddleware(activeterm.Middleware(),
func(h ssh.Handler) ssh.Handler {
return g.handle
}, logging.Middleware()))
s, err := wish.NewServer(opts...)
if err != nil {
log.Fatal(err)
}
g.s = s
return s.ListenAndServe()
}
type sshOutput struct {
ssh.Session
tty *os.File
}
func (s *sshOutput) Write(p []byte) (int, error) {
return s.Session.Write(p)
}
func (s *sshOutput) Read(p []byte) (int, error) {
return s.Session.Read(p)
}
func (s *sshOutput) Close() error {
return s.Session.Close()
}
func (s *sshOutput) Name() string {
return s.tty.Name()
}
func (s *sshOutput) Fd() uintptr {
return s.tty.Fd()
}
type sshEnviron struct {
environ []string
}
func (s *sshEnviron) Getenv(key string) string {
for _, v := range s.environ {
if strings.HasPrefix(v, key+"=") {
return v[len(key)+1:]
}
}
return ""
}
func (s *sshEnviron) Environ() []string {
return s.environ
}
func outputFromSession(s ssh.Session) *termenv.Output {
sshPty, _, _ := s.Pty()
_, tty, err := pty.Open()
if err != nil {
panic(err)
}
o := &sshOutput{
Session: s,
tty: tty,
}
environ := s.Environ()
environ = append(environ, fmt.Sprintf("TERM=%s", sshPty.Term))
e := &sshEnviron{
environ: environ,
}
return termenv.NewOutput(o, termenv.WithUnsafe(), termenv.WithEnvironment(e))
}
func (g *guruSSHServer) handle(sess ssh.Session) {
out := outputFromSession(sess)
tui.Stdin = sess
tui.Stdout = out
tui.Stderr = out
builtins.Use(cortana.WithStdout(sess))
builtins.Use(cortana.WithStderr(sess))
cortana.Use(cortana.WithStdout(sess))
cortana.Use(cortana.WithStderr(sess))
lipgloss.SetColorProfile(out.ColorProfile())
args := sess.Command()
if len(args) == 0 {
args = append(args, "chat")
}
// filter the serve self
if args[0] == "serve" {
fmt.Sprintln(sess, "serve command is not supported in the sshapp mode")
}
builtins.AddCommand(":exit", func() string {
sess.Close()
return ""
}, "exit the session")
cortana.Launch(args...)
}