forked from gravitational/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
up.go
223 lines (190 loc) · 5.08 KB
/
up.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
/*
Copyright 2015 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package sshutils
import (
"fmt"
"io"
"github.com/gravitational/trace"
"golang.org/x/crypto/ssh"
)
// NewUpstream returns new upstream connection to the server
func NewUpstream(clt *ssh.Client) (*Upstream, error) {
session, err := clt.NewSession()
if err != nil {
clt.Close()
return nil, trace.Wrap(err)
}
return &Upstream{
addr: clt.Conn.RemoteAddr().String(),
client: clt,
session: session,
}, nil
}
// DialUpstream dials remote server and returns upstream
func DialUpstream(username, addr string, signers []ssh.Signer) (*Upstream, error) {
sshConfig := &ssh.ClientConfig{
User: username,
Auth: []ssh.AuthMethod{ssh.PublicKeys(signers...)},
}
client, err := ssh.Dial("tcp", addr, sshConfig)
if err != nil {
return nil, err
}
session, err := client.NewSession()
if err != nil {
client.Close()
return nil, err
}
return &Upstream{
addr: addr,
client: client,
session: session,
}, nil
}
// Upstream is a wrapper around SSH client connection
// that provides some handy functions to work with interactive shells
// and launching commands
type Upstream struct {
addr string
client *ssh.Client
session *ssh.Session
}
// GetSession returns current active sesson
func (u *Upstream) GetSession() *ssh.Session {
return u.session
}
// Close closes session and client connection
func (u *Upstream) Close() error {
return CloseAll(u.session, u.client)
}
// String returns debug-friendly information about this upstream
func (u *Upstream) String() string {
return fmt.Sprintf("upstream(addr=%v)", u.addr)
}
// Wait waits for the session to complete
func (u *Upstream) Wait() error {
return u.session.Wait()
}
// CommandRW executes a command and returns read writer to communicate
// with the process using it's stdin and stdout
func (u *Upstream) CommandRW(command string) (io.ReadWriter, error) {
stdout, err := u.session.StdoutPipe()
if err != nil {
return nil, trace.Wrap(err, "failed to pipe stdout")
}
stdin, err := u.session.StdinPipe()
if err != nil {
return nil, trace.Wrap(err, "fail to pipe stdin")
}
err = u.session.Start(command)
if err != nil {
return nil, trace.Wrap(err,
fmt.Sprintf("pipe failed to start command '%v'", command))
}
return &combo{r: stdout, w: stdin}, nil
}
// PipeCommand pipes input and output to the read writer, returns
// result code of the command execution
func (u *Upstream) PipeCommand(ch io.ReadWriter, command string) (int, error) {
stderr, err := u.session.StderrPipe()
if err != nil {
return -1, trace.Wrap(err, "fail to pipe stderr")
}
stdout, err := u.session.StdoutPipe()
if err != nil {
return -1, trace.Wrap(err, "fail to pipe stdout")
}
stdin, err := u.session.StdinPipe()
if err != nil {
return -1, trace.Wrap(err, "fail to pipe stdin")
}
closeC := make(chan error, 4)
err = u.session.Start(command)
if err != nil {
return -1, trace.Wrap(err,
fmt.Sprintf("pipe failed to start command '%v'", command))
}
go func() {
_, err := io.Copy(stdin, ch)
closeC <- err
}()
go func() {
_, err := io.Copy(ch, stderr)
closeC <- err
}()
go func() {
_, err := io.Copy(ch, stdout)
closeC <- err
}()
go func() {
closeC <- u.session.Wait()
}()
err = <-closeC
if err != nil {
if err, ok := err.(*ssh.ExitError); ok {
return err.ExitStatus(), nil
}
return -1, trace.Wrap(err,
fmt.Sprintf("failed to collect status of a command '%v'", command))
}
return 0, nil
}
// PipeShell starts interactive shell and pipes stdin, stdout and stderr
// to the given read writer
func (u *Upstream) PipeShell(rw io.ReadWriter, req *PTYReqParams) error {
targetStderr, err := u.session.StderrPipe()
if err != nil {
return trace.Wrap(err, "fail to pipe stderr")
}
targetStdout, err := u.session.StdoutPipe()
if err != nil {
return trace.Wrap(err, "fail to pipe stdout")
}
targetStdin, err := u.session.StdinPipe()
if err != nil {
return trace.Wrap(err, "fail to pipe stdin")
}
closeC := make(chan error, 4)
if err := u.session.Shell(); err != nil {
return trace.Wrap(err, "failed to start shell")
}
if req != nil {
u.session.SendRequest(PTYReq, false, ssh.Marshal(*req))
}
go func() {
_, err := io.Copy(targetStdin, rw)
closeC <- err
}()
go func() {
_, err := io.Copy(rw, targetStderr)
closeC <- err
}()
go func() {
_, err := io.Copy(rw, targetStdout)
closeC <- err
}()
go func() {
closeC <- u.session.Wait()
}()
return <-closeC
}
type combo struct {
r io.Reader
w io.Writer
}
func (c *combo) Read(b []byte) (int, error) {
return c.r.Read(b)
}
func (c *combo) Write(b []byte) (int, error) {
return c.w.Write(b)
}