/
ssh_terminal.go
250 lines (205 loc) · 5.82 KB
/
ssh_terminal.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package cmd
import (
"bytes"
"context"
"fmt"
"io"
"os"
"time"
"github.com/AlecAivazis/survey/v2"
"github.com/briandowns/spinner"
"github.com/docker/docker/pkg/ioutils"
"github.com/pkg/errors"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/cmdctx"
"github.com/superfly/flyctl/helpers"
"github.com/superfly/flyctl/internal/sentry"
"github.com/superfly/flyctl/pkg/agent"
"github.com/superfly/flyctl/pkg/ip"
"github.com/superfly/flyctl/pkg/ssh"
"github.com/superfly/flyctl/terminal"
)
func runSSHCommand(cmdCtx *cmdctx.CmdContext, app *api.App, dialer agent.Dialer, cmd string) ([]byte, error) {
var inBuf bytes.Buffer
var errBuf bytes.Buffer
var outBuf bytes.Buffer
stdoutWriter := ioutils.NewWriteCloserWrapper(&outBuf, func() error { return nil })
stderrWriter := ioutils.NewWriteCloserWrapper(&errBuf, func() error { return nil })
inReader := ioutils.NewReadCloserWrapper(&inBuf, func() error { return nil })
addr := fmt.Sprintf("%s.internal", app.Name)
err := sshConnect(&SSHParams{
Ctx: cmdCtx,
Org: &app.Organization,
Dialer: dialer,
App: app.Name,
Cmd: cmd,
Stdin: inReader,
Stdout: stdoutWriter,
Stderr: stderrWriter,
DisableSpinner: true,
}, addr)
if err != nil {
return nil, err
}
if len(errBuf.Bytes()) > 0 {
return nil, fmt.Errorf(errBuf.String())
}
return outBuf.Bytes(), nil
}
func runSSHConsole(cc *cmdctx.CmdContext) error {
client := cc.Client.API()
ctx := cc.Command.Context()
terminal.Debugf("Retrieving app info for %s\n", cc.AppName)
app, err := client.GetAppBasic(ctx, cc.AppName)
if err != nil {
return fmt.Errorf("get app: %w", err)
}
captureError := func(err error) {
// ignore cancelled errors
if errors.Is(err, context.Canceled) {
return
}
sentry.CaptureException(err,
sentry.WithTag("feature", "ssh-console"),
sentry.WithContexts(map[string]interface{}{
"app": map[string]interface{}{
"name": app.Name,
},
"organization": map[string]interface{}{
"name": app.Organization.Slug,
},
}),
)
}
agentclient, err := agent.Establish(ctx, client)
if err != nil {
captureError(err)
return errors.Wrap(err, "can't establish agent")
}
dialer, err := agentclient.Dialer(ctx, app.Organization.Slug)
if err != nil {
captureError(err)
return fmt.Errorf("ssh: can't build tunnel for %s: %s\n", app.Organization.Slug, err)
}
cc.IO.StartProgressIndicatorMsg("Connecting to tunnel")
if err := agentclient.WaitForTunnel(ctx, app.Organization.Slug); err != nil {
captureError(err)
return errors.Wrapf(err, "tunnel unavailable")
}
cc.IO.StopProgressIndicator()
var addr string
if cc.Config.GetBool("select") {
instances, err := agentclient.Instances(ctx, app.Organization.Slug, cc.AppName)
if err != nil {
return fmt.Errorf("look up %s: %w", cc.AppName, err)
}
selected := 0
prompt := &survey.Select{
Message: "Select instance:",
Options: instances.Labels,
PageSize: 15,
}
if err := survey.AskOne(prompt, &selected); err != nil {
return fmt.Errorf("selecting instance: %w", err)
}
addr = fmt.Sprintf("[%s]", instances.Addresses[selected])
} else if len(cc.Args) != 0 {
addr = cc.Args[0]
} else {
addr = fmt.Sprintf("top1.nearest.of.%s.internal", cc.AppName)
}
// wait for the addr to be resolved in dns unless it's an ip address
if !ip.IsV6(addr) {
if err := agentclient.WaitForDNS(ctx, dialer, app.Organization.Slug, addr); err != nil {
captureError(err)
return errors.Wrapf(err, "host unavailable")
}
}
err = sshConnect(&SSHParams{
Ctx: cc,
Org: app.Organization,
Dialer: dialer,
App: cc.AppName,
Cmd: cc.Config.GetString("command"),
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
}, addr)
if err != nil {
captureError(err)
}
return err
}
func spin(in, out string) context.CancelFunc {
ctx, cancel := context.WithCancel(context.Background())
if !helpers.IsTerminal() {
fmt.Fprintln(os.Stderr, in)
return cancel
}
go func() {
s := spinner.New(spinner.CharSets[11], 100*time.Millisecond)
s.Writer = os.Stderr
s.Prefix = in
s.FinalMSG = out
s.Start()
defer s.Stop()
<-ctx.Done()
}()
return cancel
}
type SSHParams struct {
Ctx *cmdctx.CmdContext
Org api.OrganizationImpl
App string
Dialer agent.Dialer
Cmd string
Stdin io.Reader
Stdout io.WriteCloser
Stderr io.WriteCloser
DisableSpinner bool
}
func sshConnect(p *SSHParams, addr string) error {
terminal.Debugf("Fetching certificate for %s\n", addr)
cert, err := singleUseSSHCertificate(p.Ctx, p.Org)
if err != nil {
return fmt.Errorf("create ssh certificate: %w (if you haven't created a key for your org yet, try `flyctl ssh establish`)", err)
}
pk, err := parsePrivateKey(cert.Key)
if err != nil {
return errors.Wrap(err, "parse ssh certificate")
}
pemkey := MarshalED25519PrivateKey(pk, "single-use certificate")
terminal.Debugf("Keys for %s configured; connecting...\n", addr)
sshClient := &ssh.Client{
Addr: addr + ":22",
User: "root",
Dial: p.Dialer.DialContext,
Certificate: cert.Certificate,
PrivateKey: string(pemkey),
}
var endSpin context.CancelFunc
if !p.DisableSpinner {
endSpin = spin(fmt.Sprintf("Connecting to %s...", addr),
fmt.Sprintf("Connecting to %s... complete\n", addr))
defer endSpin()
}
ctx := p.Ctx.Command.Context()
if err := sshClient.Connect(ctx); err != nil {
return errors.Wrap(err, "error connecting to SSH server")
}
defer sshClient.Close()
terminal.Debugf("Connection completed.\n", addr)
if !p.DisableSpinner {
endSpin()
}
term := &ssh.Terminal{
Stdin: p.Stdin,
Stdout: p.Stdout,
Stderr: p.Stderr,
Mode: "xterm",
}
if err := sshClient.Shell(ctx, term, p.Cmd); err != nil {
return errors.Wrap(err, "ssh shell")
}
return nil
}