forked from charmbracelet/wishlist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_local.go
146 lines (121 loc) · 3.14 KB
/
client_local.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
142
143
144
145
146
package wishlist
import (
"context"
"fmt"
"io"
"os"
"os/signal"
"os/user"
"path/filepath"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/log"
"github.com/muesli/cancelreader"
"golang.org/x/crypto/ssh"
"golang.org/x/term"
)
// NewLocalSSHClient returns a SSH Client for local usage.
func NewLocalSSHClient() SSHClient {
return &localClient{}
}
type localClient struct{}
func (c *localClient) For(e *Endpoint) tea.ExecCommand {
return &localSession{
endpoint: e,
}
}
type localSession struct {
// endpoint we are connecting to
endpoint *Endpoint
stdin io.Reader
stdout, stderr io.Writer
}
func (s *localSession) SetStdin(r io.Reader) {
s.stdin = r
}
func (s *localSession) SetStdout(w io.Writer) {
s.stdout = w
}
func (s *localSession) SetStderr(w io.Writer) {
s.stderr = w
}
func (s *localSession) Run() error {
resetPty(s.stdout)
abort := make(chan os.Signal, 1)
signal.Notify(abort, os.Interrupt)
defer func() {
signal.Stop(abort)
close(abort)
}()
user, err := user.Current()
if err != nil {
return fmt.Errorf("failed to get current username: %w", err)
}
agt, cls, err := getLocalAgent()
if err != nil {
return err
}
defer cls.close()
methods, err := localBestAuthMethod(agt, s.endpoint, os.Stdin, os.Stdout)
if err != nil {
return fmt.Errorf("failed to setup a authentication method: %w", err)
}
conf := &ssh.ClientConfig{
User: FirstNonEmpty(s.endpoint.User, user.Username),
Auth: methods,
HostKeyCallback: hostKeyCallback(s.endpoint, filepath.Join(user.HomeDir, ".ssh/known_hosts")),
Timeout: s.endpoint.Timeout,
}
session, client, cls, err := createSession(conf, s.endpoint, abort, os.Environ()...)
defer cls.close()
if err != nil {
return fmt.Errorf("failed to create session: %w", err)
}
defer closers{func() error {
rc, ok := session.Stdin.(cancelreader.CancelReader)
if ok && !rc.Cancel() {
return fmt.Errorf("could not cancel reader")
}
return nil
}}.close()
session.Stdout = s.stdout
session.Stderr = s.stderr
stdin, err := cancelreader.NewReader(s.stdin)
if err != nil {
return fmt.Errorf("could not create cancel reader")
}
session.Stdin = stdin
if s.endpoint.ForwardAgent {
if err := forwardAgent(agt, session, client); err != nil {
return err
}
}
//nolint:nestif
if s.endpoint.RequestTTY || s.endpoint.RemoteCommand == "" {
fd := int(os.Stdout.Fd())
if !term.IsTerminal(fd) {
return fmt.Errorf("requested a TTY, but current session is not TTY, aborting")
}
log.Info("requesting tty")
restore, err := makeRaw(fd)
if err != nil {
return err
}
defer restore()
w, h, err := term.GetSize(fd)
if err != nil {
return fmt.Errorf("failed to get term size: %w", err)
}
if err := session.RequestPty(os.Getenv("TERM"), h, w, nil); err != nil {
return fmt.Errorf("failed to request a pty: %w", err)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go s.notifyWindowChanges(ctx, session)
} else {
log.Info("did not request a tty")
}
if s.endpoint.RemoteCommand == "" {
return shellAndWait(session)
}
return runAndWait(session, s.endpoint.RemoteCommand)
}