This repository was archived by the owner on Oct 2, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathnetworkConnectionHandler.go
More file actions
228 lines (208 loc) · 6.19 KB
/
Copy pathnetworkConnectionHandler.go
File metadata and controls
228 lines (208 loc) · 6.19 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
package sshproxy
import (
"context"
"fmt"
"net"
"sync"
"time"
"github.com/containerssh/log"
"github.com/containerssh/metrics"
"golang.org/x/crypto/ssh"
"github.com/containerssh/sshserver"
)
type networkConnectionHandler struct {
lock *sync.Mutex
wg *sync.WaitGroup
client net.TCPAddr
connectionID string
config Config
logger log.Logger
backendRequestsMetric metrics.SimpleCounter
backendFailuresMetric metrics.SimpleCounter
tcpConn net.Conn
disconnected bool
privateKey ssh.Signer
done bool
}
func (s *networkConnectionHandler) OnAuthPassword(_ string, _ []byte) (
_ sshserver.AuthResponse,
_ error,
) {
return sshserver.AuthResponseUnavailable, fmt.Errorf(
"ssh proxy does not support authentication",
)
}
func (s *networkConnectionHandler) OnAuthPubKey(_ string, _ string) (
response sshserver.AuthResponse,
reason error,
) {
return sshserver.AuthResponseUnavailable, fmt.Errorf(
"ssh proxy does not support authentication",
)
}
func (s *networkConnectionHandler) OnAuthKeyboardInteractive(
_ string,
_ func(
_ string,
_ sshserver.KeyboardInteractiveQuestions,
) (answers sshserver.KeyboardInteractiveAnswers, err error),
) (response sshserver.AuthResponse, reason error) {
return sshserver.AuthResponseUnavailable, fmt.Errorf(
"ssh proxy does not support authentication",
)
}
func (s *networkConnectionHandler) OnHandshakeFailed(_ error) {}
func (s *networkConnectionHandler) OnHandshakeSuccess(username string) (
connection sshserver.SSHConnectionHandler,
failureReason error,
) {
s.lock.Lock()
defer s.lock.Unlock()
if s.disconnected {
return nil, log.NewMessage(
EDisconnected,
"could not connect to backend because the user already disconnected",
)
}
sshConn, newChannels, requests, cli, err := s.createBackendSSHConnection(username)
if err != nil {
return nil, err
}
return &sshConnectionHandler{
networkHandler: s,
cli: cli,
sshConn: sshConn,
newChannels: newChannels,
requests: requests,
logger: s.logger,
}, nil
}
func (s *networkConnectionHandler) createBackendSSHConnection(username string) (
ssh.Conn,
<-chan ssh.NewChannel,
<-chan *ssh.Request,
*ssh.Client,
error,
) {
s.backendRequestsMetric.Increment()
target := fmt.Sprintf("%s:%d", s.config.Server, s.config.Port)
tcpConn, err := s.createBackendTCPConnection(username, target)
if err != nil {
return nil, nil, nil, nil, err
}
s.tcpConn = tcpConn
sshClientConfig := s.createClientConfig(username)
sshConn, newChannels, requests, err := ssh.NewClientConn(s.tcpConn, target, sshClientConfig)
if err != nil {
s.backendFailuresMetric.Increment(metrics.Label("failure", "handshake"))
return nil, nil, nil, nil, log.WrapUser(
err,
EBackendHandshakeFailed,
"SSH service is currently unavailable.",
"Failed to authenticate with the backend.",
).Label("backend", target)
}
cli := ssh.NewClient(sshConn, newChannels, requests)
return sshConn, newChannels, requests, cli, nil
}
func (s *networkConnectionHandler) createClientConfig(username string) *ssh.ClientConfig {
if !s.config.UsernamePassThrough {
username = s.config.Username
}
authMethods := []ssh.AuthMethod{
ssh.Password(s.config.Password),
}
if s.privateKey != nil {
authMethods = append(
authMethods, ssh.PublicKeys(
s.privateKey,
),
)
}
sshClientConfig := &ssh.ClientConfig{
Config: ssh.Config{
KeyExchanges: s.config.KexAlgorithms.StringList(),
Ciphers: s.config.Ciphers.StringList(),
MACs: s.config.MACs.StringList(),
},
User: username,
Auth: authMethods,
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
fingerprint := ssh.FingerprintSHA256(key)
for _, fp := range s.config.AllowedHostKeyFingerprints {
if fingerprint == fp {
return nil
}
}
err := log.UserMessage(
EInvalidFingerprint,
"SSH service currently unavailable",
"invalid host key fingerprint: %s",
fingerprint,
).Label("fingerprint", fingerprint)
s.logger.Error(err)
return err
},
ClientVersion: s.config.ClientVersion.String(),
HostKeyAlgorithms: s.config.HostKeyAlgorithms.StringList(),
Timeout: s.config.Timeout,
}
return sshClientConfig
}
func (s *networkConnectionHandler) createBackendTCPConnection(
_ string,
target string,
) (net.Conn, error) {
s.logger.Debug(log.NewMessage(MConnecting, "Connecting to backend server %s", target))
ctx, cancelFunc := context.WithTimeout(context.Background(), s.config.Timeout)
defer cancelFunc()
var networkConnection net.Conn
var lastError error
loop:
for {
networkConnection, lastError = net.Dial("tcp", target)
if lastError == nil {
return networkConnection, nil
}
s.backendFailuresMetric.Increment(metrics.Label("failure", "tcp"))
s.logger.Debug(log.WrapUser(
lastError,
EBackendConnectionFailed,
"service currently unavailable",
"connection to SSH backend failed, retrying in 10 seconds",
))
select {
case <-ctx.Done():
break loop
case <-time.After(10 * time.Second):
}
}
err := log.WrapUser(
lastError,
EBackendConnectionFailed,
"service currently unavailable",
"connection to SSH backend failed, giving up",
)
s.logger.Error(err)
return nil, err
}
func (s *networkConnectionHandler) OnDisconnect() {
s.logger.Debug(log.NewMessage(MDisconnected, "Client disconnected, waiting for network connection lock..."))
s.lock.Lock()
defer s.lock.Unlock()
s.logger.Debug(log.NewMessage(MDisconnected, "Client disconnected, waiting for all sessions to terminate..."))
s.wg.Wait()
s.done = true
s.disconnected = true
if s.tcpConn != nil {
s.logger.Debug(log.NewMessage(MBackendDisconnecting, "Disconnecting backend connection..."))
if err := s.tcpConn.Close(); err != nil {
s.logger.Debug(log.Wrap(err, MBackendDisconnectFailed, "Failed to disconnect backend connection."))
} else {
s.logger.Debug(log.NewMessage(MBackendDisconnected, "Backend connection disconnected."))
}
} else {
s.logger.Debug(log.NewMessage(MBackendDisconnected, "Backend connection already disconnected."))
}
}
func (s *networkConnectionHandler) OnShutdown(_ context.Context) {}