-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
540 lines (436 loc) · 13.4 KB
/
server.go
File metadata and controls
540 lines (436 loc) · 13.4 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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
package sshproxy
import (
"context"
"errors"
"fmt"
"io"
"net"
"slices"
"strconv"
"strings"
"sync"
"sync/atomic"
"syscall"
"time"
"golang.org/x/crypto/ssh"
"github.com/dstackai/sshproxy/internal/log"
"github.com/dstackai/sshproxy/internal/ttlcache"
)
var serverVersion = "SSH-2.0-dstack_sshproxy_" + Version
const (
upstreamCacheTTL = time.Second * 10
upstreamCacheCleanupInterval = time.Minute * 5
upstreamExtraDataKey = "upstream"
upstreamDialTimeout = time.Second * 10
)
var blacklistedGlobalRequests = []string{
// Host key update mechanism for SSH: https://www.ietf.org/archive/id/draft-miller-sshm-hostkey-update-02.html
// Reasons to blacklist:
// 1. Signature check always fail as the signed data contains session identifier, which is not the same on client
// and upstream side, since they don't talk directly but through sshproxy (there are two SSH transport sessions
// with their own unique identifiers).
// 2. Even if it worked somehow, we don't want to inflate user's known_hosts file with garbage records,
// since container host keys are ephemeral -- they are generated on dstack-runner startup (= unique for each job).
"hostkeys",
"hostkeys-00@openssh.com",
"hostkeys-prove",
"hostkeys-prove-00@openssh.com",
}
type direction string
var (
clientToUpstream direction = direction("C-U")
upstreamToClient direction = direction("U-C")
)
func (d direction) reverse() direction {
if d == clientToUpstream {
return upstreamToClient
}
return clientToUpstream
}
var ErrUpstreamNotFound = errors.New("upstream not found")
var (
errServerClosed = errors.New("server closed")
errUnknownPublicKey = errors.New("unknown public key")
)
type Server struct {
address string
getUpstream GetUpstreamCallback
upstreamCache *ttlcache.Cache[string, Upstream]
config *ssh.ServerConfig
listener net.Listener
serveCtx context.Context
inShutdown atomic.Bool
mu sync.Mutex
conns map[net.Conn]struct{}
connsWg sync.WaitGroup
}
func NewServer(
ctx context.Context, address string, port int,
hostKeys []HostKey, getUpstream GetUpstreamCallback,
) *Server {
logger := log.GetLogger(ctx)
config := &ssh.ServerConfig{
ServerVersion: serverVersion,
}
for _, key := range hostKeys {
logger.WithField("type", key.PublicKey().Type()).Debug("host key added")
config.AddHostKey(key)
}
server := Server{
address: net.JoinHostPort(address, strconv.Itoa(port)),
getUpstream: getUpstream,
upstreamCache: ttlcache.NewCache[string, Upstream](upstreamCacheTTL),
config: config,
conns: make(map[net.Conn]struct{}),
}
server.config.PublicKeyCallback = server.publicKeyCallback
return &server
}
func (s *Server) ListenAndServe(ctx context.Context) error {
if s.inShutdown.Load() {
return errServerClosed
}
var lc net.ListenConfig
listener, err := lc.Listen(ctx, "tcp", s.address)
if err != nil {
return fmt.Errorf("listen on %s: %w", s.address, err)
}
s.mu.Lock()
s.listener = listener
s.serveCtx = ctx
s.mu.Unlock()
logger := log.GetLogger(ctx)
logger.WithField("address", s.address).Info("listening for client connections")
_ = s.upstreamCache.StartCleanup(upstreamCacheCleanupInterval)
for {
conn, err := listener.Accept()
if err != nil {
if s.inShutdown.Load() {
return nil
}
logger.WithError(err).Error("failed to accept incoming connection")
continue
}
logger := logger.WithField("client", conn.RemoteAddr().String())
s.addConnection(conn)
s.connsWg.Go(func() {
handleConnection(log.WithLogger(ctx, logger), conn, s.config)
s.removeConnection(conn)
})
}
}
func (s *Server) Close(ctx context.Context) error {
s.inShutdown.Store(true)
s.mu.Lock()
defer s.mu.Unlock()
if s.listener == nil {
return errServerClosed
}
logger := log.GetLogger(ctx)
logger.Info("closing listener and connections")
err := s.listener.Close()
for conn := range s.conns {
_ = conn.Close()
delete(s.conns, conn)
}
s.mu.Unlock()
s.connsWg.Wait()
s.mu.Lock()
_ = s.upstreamCache.StopCleanup()
return err
}
func (s *Server) publicKeyCallback(conn ssh.ConnMetadata, publicKey ssh.PublicKey) (*ssh.Permissions, error) {
upstreamID := conn.User()
logger := log.GetLogger(s.serveCtx).WithField("id", upstreamID)
upstream, found := s.upstreamCache.Get(upstreamID)
if !found {
var err error
upstream, err = s.getUpstream(s.serveCtx, upstreamID)
if err != nil {
if errors.Is(err, ErrUpstreamNotFound) {
logger.Debug("upstream not found")
} else {
logger.WithError(err).Error("failed to get upstream")
}
return nil, fmt.Errorf("get upstream: %w", err)
}
s.upstreamCache.Set(upstreamID, upstream)
logger.Trace("got upstream")
} else {
logger.Trace("using cached upstream")
}
if upstream.IsAuthorized(publicKey) {
return &ssh.Permissions{
ExtraData: map[any]any{
upstreamExtraDataKey: upstream,
},
}, nil
}
return nil, errUnknownPublicKey
}
func (s *Server) addConnection(conn net.Conn) {
s.mu.Lock()
defer s.mu.Unlock()
s.conns[conn] = struct{}{}
}
func (s *Server) removeConnection(conn net.Conn) {
s.mu.Lock()
defer s.mu.Unlock()
delete(s.conns, conn)
}
func handleConnection(ctx context.Context, conn net.Conn, config *ssh.ServerConfig) {
logger := log.GetLogger(ctx)
defer func() {
err := conn.Close()
if err != nil && !isClosedError(err) {
logger.WithError(err).Error("failed to close connection")
}
}()
clientConn, clientNewChans, clientReqs, err := ssh.NewServerConn(conn, config)
if err != nil {
handleConnectionError(ctx, err)
return
}
logger.Debug("client logged in")
upstream := clientConn.Permissions.ExtraData[upstreamExtraDataKey].(Upstream)
upstreamConn, upstreamNewChans, upstreamReqs, err := connectToUpstream(ctx, upstream)
if err != nil {
logger.WithError(err).Error("failed to connect to upstream")
return
}
var wg sync.WaitGroup
wg.Go(func() {
bridgeGlobalRequests(ctx, clientToUpstream, clientReqs, upstreamConn)
// <-chan *Request (and <-chan NewChannel) is closed when an error is encountered,
// including closed connection, see x/crypto/ssh/mux.go, mux.loop()
// We close the upstream connection here to interrupt goroutines
// spawned by bridgeNewChannels -> handleChannel that io.Copy() stdout/stderr,
// otherwise they may stuck trying to read from a Channel, as Channel.Read()
// doesn't fail after sending Channel.Close()
err := upstreamConn.Close()
if err != nil && !isClosedError(err) {
logger.WithError(err).Error("failed to close upstream connection")
} else {
logger.Debug("upstream connection closed")
}
})
wg.Go(func() {
bridgeNewChannels(ctx, clientToUpstream, clientNewChans, upstreamConn)
})
wg.Go(func() {
bridgeGlobalRequests(ctx, upstreamToClient, upstreamReqs, clientConn)
err := clientConn.Close()
if err != nil && !isClosedError(err) {
logger.WithError(err).Error("failed to close client connection")
} else {
logger.Debug("client connection closed")
}
})
wg.Go(func() {
bridgeNewChannels(ctx, upstreamToClient, upstreamNewChans, clientConn)
})
wg.Wait()
}
func handleConnectionError(ctx context.Context, err error) {
logger := log.GetLogger(ctx)
if isClosedError(err) {
return
}
if errors.Is(err, syscall.ECONNRESET) {
// For example, OpenSSH client may send RST during key exchange if the host keys have changed
logger.WithError(err).Debug("connection reset by client")
return
}
if errors.Is(err, syscall.ETIMEDOUT) {
logger.WithError(err).Debug("client connection timed out")
return
}
if authErr, ok := errors.AsType[*ssh.ServerAuthError](err); ok {
for _, err := range authErr.Errors {
if errors.Is(err, ErrUpstreamNotFound) {
logger.Debug("client requested unknown upstream")
return
}
}
logger.WithError(err).Debug("client auth failed")
return
}
if algoErr, ok := errors.AsType[*ssh.AlgorithmNegotiationError](err); ok {
logger.WithField("offered", algoErr.RequestedAlgorithms).Debugf("no common algorithm for %s", algoErr.What)
return
}
if sshErr := getSSHError(err); sshErr != nil {
errMsg := sshErr.Error()
for _, msg := range [...]string{
// https://github.com/golang/crypto/blob/982eaa62dfb7273603b97fc1835561450096f3bd/ssh/transport.go#L369
"overflow reading version string",
// https://github.com/golang/crypto/blob/982eaa62dfb7273603b97fc1835561450096f3bd/ssh/messages.go#L385
// e.g., "unmarshal error for field Language of type disconnectMsg"
"unmarshal error",
// https://github.com/golang/crypto/blob/982eaa62dfb7273603b97fc1835561450096f3bd/ssh/common.go#L382
"unexpected message type",
} {
if strings.Contains(errMsg, msg) {
logger.WithError(err).Debug("suspicious client")
return
}
}
// https://github.com/golang/crypto/blob/982eaa62dfb7273603b97fc1835561450096f3bd/ssh/messages.go#L47
// e.g., "ssh: disconnect, reason 11: disconnected by user"
// Most probably this is also a suspicious client, but may be a legitimate use of SSH_MSG_DISCONNECT
if strings.Contains(errMsg, "disconnect, reason") {
logger.WithError(err).Debug("client disconnected")
return
}
}
logger.WithError(err).Error("failed to handshake client")
}
func connectToUpstream(
ctx context.Context,
upstream Upstream,
) (ssh.Conn, <-chan ssh.NewChannel, <-chan *ssh.Request, error) {
var conn ssh.Conn
var chans <-chan ssh.NewChannel
var reqs <-chan *ssh.Request
for i, host := range upstream.hosts {
config := &ssh.ClientConfig{
User: host.user,
Auth: []ssh.AuthMethod{
ssh.PublicKeys(host.privateKey),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
var netConn net.Conn
var err error
if i == 0 {
d := net.Dialer{
Timeout: upstreamDialTimeout,
}
netConn, err = d.DialContext(ctx, "tcp", host.address)
} else {
client := ssh.NewClient(conn, chans, reqs)
// TODO: Is it possible to specify timeout?
netConn, err = client.Dial("tcp", host.address)
}
if err != nil {
return nil, nil, nil, fmt.Errorf("dial upstream %d %s: %w", i, host.address, err)
}
conn, chans, reqs, err = ssh.NewClientConn(netConn, host.address, config)
if err != nil {
return nil, nil, nil, fmt.Errorf("create SSH connection %d %s: %w", i, host.address, err)
}
}
return conn, chans, reqs, nil
}
func bridgeGlobalRequests(ctx context.Context, dir direction, inReqs <-chan *ssh.Request, outConn ssh.Conn) {
logger := log.GetLogger(ctx).WithField("dir", dir)
for req := range inReqs {
logger := logger.WithField("type", req.Type)
if slices.Contains(blacklistedGlobalRequests, req.Type) {
logger.Trace("blacklisted global request, ignoring")
if req.WantReply {
_ = req.Reply(false, nil)
}
} else {
logger.Trace("global request")
ok, payload, err := outConn.SendRequest(req.Type, req.WantReply, req.Payload)
if req.WantReply {
_ = req.Reply(ok, payload)
}
if err != nil && !isClosedError(err) {
logger.WithError(err).Error("failed to forward global request")
}
}
}
}
func bridgeNewChannels(ctx context.Context, dir direction, inNewChans <-chan ssh.NewChannel, outConn ssh.Conn) {
logger := log.GetLogger(ctx)
var wg sync.WaitGroup
for inNewChan := range inNewChans {
logger := logger.WithField("chan", inNewChan.ChannelType())
logger.WithField("dir", dir).Trace("new channel requested")
wg.Go(func() {
handleChannel(log.WithLogger(ctx, logger), dir, inNewChan, outConn)
})
}
wg.Wait()
logger.WithField("dir", dir).Trace("channels done")
}
func handleChannel(ctx context.Context, dir direction, inNewChan ssh.NewChannel, outConn ssh.Conn) {
logger := log.GetLogger(ctx)
outChan, outReqs, err := outConn.OpenChannel(inNewChan.ChannelType(), inNewChan.ExtraData())
if err != nil {
// Trace level to avoid spamming in case of rejected port forwarding
logger.WithError(err).Trace("new channel rejected by the other side")
_ = inNewChan.Reject(ssh.ConnectionFailed, err.Error())
return
}
inChan, inReqs, err := inNewChan.Accept()
if err != nil {
if !isClosedError(err) {
logger.WithError(err).Error("failed to accept new channel")
}
_ = outChan.Close()
return
}
logger.Trace("new channel accepted")
var outWg sync.WaitGroup
outWg.Go(func() {
_, _ = io.Copy(inChan, outChan)
_ = inChan.CloseWrite()
})
outWg.Go(func() {
_, _ = io.Copy(inChan.Stderr(), outChan.Stderr())
})
outWg.Go(func() {
bridgeChannelRequests(ctx, dir.reverse(), outReqs, inChan)
})
var inWg sync.WaitGroup
inWg.Go(func() {
_, _ = io.Copy(outChan, inChan)
_ = outChan.CloseWrite()
})
inWg.Go(func() {
bridgeChannelRequests(ctx, dir, inReqs, outChan)
})
var wg sync.WaitGroup
wg.Go(func() {
outWg.Wait()
_ = inChan.Close()
})
wg.Go(func() {
inWg.Wait()
_ = outChan.Close()
})
wg.Wait()
logger.Trace("channel done")
}
func bridgeChannelRequests(ctx context.Context, dir direction, inReqs <-chan *ssh.Request, outConn ssh.Channel) {
logger := log.GetLogger(ctx).WithField("dir", dir)
for req := range inReqs {
logger := logger.WithField("type", req.Type)
logger.Trace("request")
ok, err := outConn.SendRequest(req.Type, req.WantReply, req.Payload)
if req.WantReply {
_ = req.Reply(ok, nil)
}
if err != nil && !isClosedError(err) {
logger.WithError(err).Error("failed to forward channel request")
}
}
}
func isClosedError(err error) bool {
return errors.Is(err, net.ErrClosed) || errors.Is(err, io.EOF)
}
func getSSHError(err error) error {
for {
if strings.HasPrefix(err.Error(), "ssh: ") {
return err
}
err = errors.Unwrap(err)
if err == nil {
break
}
}
return nil
}