Repository navigation
Expand file tree
/
Copy pathssh.go
More file actions
314 lines (258 loc) · 7.34 KB
/
Copy pathssh.go
File metadata and controls
314 lines (258 loc) · 7.34 KB
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package main
import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"encoding/base64"
"errors"
"fmt"
"io"
"net"
"os"
"strings"
"sync"
"time"
"golang.org/x/crypto/ssh"
"code.cloudfoundry.org/cli/cf/models"
"code.cloudfoundry.org/cli/cf/ssh/options"
)
const (
md5FingerprintLength = 47 // inclusive of space between bytes
hexSha1FingerprintLength = 59 // inclusive of space between bytes
base64Sha256FingerprintLength = 43
)
//go:generate counterfeiter . SecureDialer
type SecureDialer interface {
Dial(network, address string, config *ssh.ClientConfig) (*SecureClient, error)
}
//go:generate counterfeiter . ListenerFactory
type ListenerFactory interface {
Listen(network, address string) (net.Listener, error)
}
func NewSecureShell(
secureDialer SecureDialer,
listenerFactory ListenerFactory,
keepAliveInterval time.Duration,
app models.Application,
sshEndpointFingerprint string,
sshEndpoint string,
token string,
) *SecureShell {
return &SecureShell{
secureDialer: secureDialer,
listenerFactory: listenerFactory,
keepAliveInterval: keepAliveInterval,
app: app,
sshEndpointFingerprint: sshEndpointFingerprint,
sshEndpoint: sshEndpoint,
token: token,
localListeners: []net.Listener{},
}
}
type SecureShell struct {
secureDialer SecureDialer
listenerFactory ListenerFactory
keepAliveInterval time.Duration
app models.Application
sshEndpointFingerprint string
sshEndpoint string
token string
secureClient *SecureClient
opts *options.SSHOptions
localListeners []net.Listener
}
func (c *SecureShell) Connect(opts *options.SSHOptions) error {
err := c.validateTarget(opts)
if err != nil {
return err
}
clientConfig := &ssh.ClientConfig{
User: fmt.Sprintf("cf:%s/%d", c.app.GUID, opts.Index),
Auth: []ssh.AuthMethod{
ssh.Password(c.token),
},
HostKeyCallback: fingerprintCallback(opts, c.sshEndpointFingerprint),
}
secureClient, err := c.secureDialer.Dial("tcp", c.sshEndpoint, clientConfig)
if err != nil {
return err
}
c.secureClient = secureClient
c.opts = opts
return nil
}
func (c *SecureShell) Close() error {
for _, listener := range c.localListeners {
_ = listener.Close()
}
return c.secureClient.Close()
}
func (c *SecureShell) LocalPortForward() error {
for _, forwardSpec := range c.opts.ForwardSpecs {
listener, err := c.listenerFactory.Listen("tcp", forwardSpec.ListenAddress)
if err != nil {
return err
}
c.localListeners = append(c.localListeners, listener)
go c.localForwardAcceptLoop(listener, forwardSpec.ConnectAddress)
}
return nil
}
func (c *SecureShell) localForwardAcceptLoop(listener net.Listener, addr string) {
defer listener.Close()
for {
conn, err := listener.Accept()
if err != nil {
if netErr, ok := err.(net.Error); ok && netErr.Temporary() {
time.Sleep(100 * time.Millisecond)
continue
}
return
}
go c.handleForwardConnection(conn, addr)
}
}
func (c *SecureShell) handleForwardConnection(conn net.Conn, targetAddr string) {
defer conn.Close()
target, err := c.secureClient.Dial("tcp", targetAddr)
if err != nil {
fmt.Printf("connect to %s failed: %s\n", targetAddr, err.Error())
return
}
defer target.Close()
wg := &sync.WaitGroup{}
wg.Add(2)
go copyAndClose(wg, conn, target)
go copyAndClose(wg, target, conn)
wg.Wait()
}
func copyAndClose(wg *sync.WaitGroup, dest io.WriteCloser, src io.Reader) {
_, _ = io.Copy(dest, src)
_ = dest.Close()
if wg != nil {
wg.Done()
}
}
func copyAndDone(wg *sync.WaitGroup, dest io.Writer, src io.Reader) {
_, _ = io.Copy(dest, src)
wg.Done()
}
func (c *SecureShell) Wait() error {
keepaliveStopCh := make(chan struct{})
defer close(keepaliveStopCh)
go keepalive(c.secureClient.Conn(), time.NewTicker(c.keepAliveInterval), keepaliveStopCh)
return c.secureClient.Wait()
}
func (c *SecureShell) validateTarget(opts *options.SSHOptions) error {
if strings.ToUpper(c.app.State) != "STARTED" {
return fmt.Errorf("Application %q is not in the STARTED state", opts.AppName)
}
if !c.app.Diego {
return fmt.Errorf("Application %q is not running on Diego", opts.AppName)
}
return nil
}
func md5Fingerprint(key ssh.PublicKey) string {
sum := md5.Sum(key.Marshal())
return strings.Replace(fmt.Sprintf("% x", sum), " ", ":", -1)
}
func hexSha1Fingerprint(key ssh.PublicKey) string {
sum := sha1.Sum(key.Marshal())
return strings.Replace(fmt.Sprintf("% x", sum), " ", ":", -1)
}
func base64Sha256Fingerprint(key ssh.PublicKey) string {
sum := sha256.Sum256(key.Marshal())
return base64.RawStdEncoding.EncodeToString(sum[:])
}
type hostKeyCallback func(hostname string, remote net.Addr, key ssh.PublicKey) error
func fingerprintCallback(opts *options.SSHOptions, expectedFingerprint string) hostKeyCallback {
if opts.SkipHostValidation {
return nil
}
return func(hostname string, remote net.Addr, key ssh.PublicKey) error {
var fingerprint string
switch len(expectedFingerprint) {
case base64Sha256FingerprintLength:
fingerprint = base64Sha256Fingerprint(key)
case hexSha1FingerprintLength:
fingerprint = hexSha1Fingerprint(key)
case md5FingerprintLength:
fingerprint = md5Fingerprint(key)
case 0:
fingerprint = md5Fingerprint(key)
return fmt.Errorf("Unable to verify identity of host.\n\nThe fingerprint of the received key was %q.", fingerprint)
default:
return errors.New("Unsupported host key fingerprint format")
}
if fingerprint != expectedFingerprint {
return fmt.Errorf("Host key verification failed.\n\nThe fingerprint of the received key was %q.", fingerprint)
}
return nil
}
}
func (c *SecureShell) shouldAllocateTerminal(opts *options.SSHOptions, stdinIsTerminal bool) bool {
switch opts.TerminalRequest {
case options.RequestTTYForce:
return true
case options.RequestTTYNo:
return false
case options.RequestTTYYes:
return stdinIsTerminal
case options.RequestTTYAuto:
return len(opts.Command) == 0 && stdinIsTerminal
default:
return false
}
}
func keepalive(conn ssh.Conn, ticker *time.Ticker, stopCh chan struct{}) {
for {
select {
case <-ticker.C:
_, _, _ = conn.SendRequest("keepalive@cloudfoundry.org", true, nil)
case <-stopCh:
ticker.Stop()
return
}
}
}
func (c *SecureShell) terminalType() string {
term := os.Getenv("TERM")
if term == "" {
term = "xterm"
}
return term
}
type secureDialer struct{}
func (d *secureDialer) Dial(network string, address string, config *ssh.ClientConfig) (*SecureClient, error) {
client, err := ssh.Dial(network, address, config)
if err != nil {
return nil, err
}
return &SecureClient{client: client}, nil
}
func DefaultSecureDialer() SecureDialer {
return &secureDialer{}
}
type SecureClient struct{ client *ssh.Client }
func (sc *SecureClient) Close() error {
return sc.client.Close()
}
func (sc *SecureClient) Conn() ssh.Conn {
return sc.client.Conn
}
func (sc *SecureClient) Wait() error {
return sc.client.Wait()
}
func (sc *SecureClient) Dial(n, addr string) (net.Conn, error) {
return sc.client.Dial(n, addr)
}
func (sc *SecureClient) NewSession() (*ssh.Session, error) {
return sc.client.NewSession()
}
type listenerFactory struct{}
func (lf *listenerFactory) Listen(network, address string) (net.Listener, error) {
return net.Listen(network, address)
}
func DefaultListenerFactory() ListenerFactory {
return &listenerFactory{}
}