This repository has been archived by the owner on Oct 2, 2022. It is now read-only.
generated from ContainerSSH/library-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sshConnectionHandler.go
89 lines (78 loc) · 2.31 KB
/
sshConnectionHandler.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
package sshproxy
import (
"context"
"errors"
"sync"
"github.com/containerssh/log"
"golang.org/x/crypto/ssh"
"github.com/containerssh/sshserver"
)
type sshConnectionHandler struct {
networkHandler *networkConnectionHandler
sshConn ssh.Conn
newChannels <-chan ssh.NewChannel
requests <-chan *ssh.Request
cli *ssh.Client
logger log.Logger
}
func (s *sshConnectionHandler) OnUnsupportedGlobalRequest(_ uint64, _ string, _ []byte) {
}
func (s *sshConnectionHandler) OnUnsupportedChannel(_ uint64, _ string, _ []byte) {
}
func (s *sshConnectionHandler) OnSessionChannel(
_ uint64,
extraData []byte,
session sshserver.SessionChannel,
) (channel sshserver.SessionChannelHandler, failureReason sshserver.ChannelRejection) {
s.networkHandler.lock.Lock()
if s.networkHandler.done {
failureReason = sshserver.NewChannelRejection(
ssh.ConnectionFailed,
EShuttingDown,
"Cannot open session.",
"Rejected new session because connection is closing.",
)
s.networkHandler.lock.Unlock()
return
}
s.networkHandler.wg.Add(1)
s.networkHandler.lock.Unlock()
s.logger.Debug(log.NewMessage(MSession, "Opening new session on SSH backend..."))
backingChannel, requests, err := s.cli.OpenChannel("session", extraData)
if err != nil {
realErr := &ssh.OpenChannelError{}
if errors.As(err, &realErr) {
failureReason = sshserver.NewChannelRejection(
realErr.Reason,
EBackendSessionFailed,
realErr.Message,
"Backend rejected channel with message: %s",
realErr.Message,
)
} else {
failureReason = sshserver.NewChannelRejection(
ssh.ConnectionFailed,
EBackendSessionFailed,
"Cannot open session.",
"Backend rejected channel with message: %s",
err.Error(),
)
}
s.logger.Debug(failureReason)
return nil, failureReason
}
sshChannelHandlerInstance := &sshChannelHandler{
ssh: s,
lock: &sync.Mutex{},
backingChannel: backingChannel,
requests: requests,
session: session,
logger: s.logger,
done: make(chan struct{}),
}
go sshChannelHandlerInstance.handleBackendClientRequests(requests, session)
s.logger.Debug(log.NewMessage(MSessionOpen, "Session open on SSH backend..."))
return sshChannelHandlerInstance, nil
}
func (s *sshConnectionHandler) OnShutdown(_ context.Context) {
}