@@ -56,24 +56,24 @@ type agent struct {
56
56
sshServer * ssh.Server
57
57
}
58
58
59
- func (s * agent ) run (ctx context.Context ) {
59
+ func (a * agent ) run (ctx context.Context ) {
60
60
var peerListener * peerbroker.Listener
61
61
var err error
62
62
// An exponential back-off occurs when the connection is failing to dial.
63
63
// This is to prevent server spam in case of a coderd outage.
64
64
for retrier := retry .New (50 * time .Millisecond , 10 * time .Second ); retrier .Wait (ctx ); {
65
- peerListener , err = s .clientDialer (ctx , s .options )
65
+ peerListener , err = a .clientDialer (ctx , a .options )
66
66
if err != nil {
67
67
if errors .Is (err , context .Canceled ) {
68
68
return
69
69
}
70
- if s .isClosed () {
70
+ if a .isClosed () {
71
71
return
72
72
}
73
- s .options .Logger .Warn (context .Background (), "failed to dial" , slog .Error (err ))
73
+ a .options .Logger .Warn (context .Background (), "failed to dial" , slog .Error (err ))
74
74
continue
75
75
}
76
- s .options .Logger .Info (context .Background (), "connected" )
76
+ a .options .Logger .Info (context .Background (), "connected" )
77
77
break
78
78
}
79
79
select {
@@ -85,48 +85,48 @@ func (s *agent) run(ctx context.Context) {
85
85
for {
86
86
conn , err := peerListener .Accept ()
87
87
if err != nil {
88
- if s .isClosed () {
88
+ if a .isClosed () {
89
89
return
90
90
}
91
- s .options .Logger .Debug (ctx , "peer listener accept exited; restarting connection" , slog .Error (err ))
92
- s .run (ctx )
91
+ a .options .Logger .Debug (ctx , "peer listener accept exited; restarting connection" , slog .Error (err ))
92
+ a .run (ctx )
93
93
return
94
94
}
95
- s .closeMutex .Lock ()
96
- s .connCloseWait .Add (1 )
97
- s .closeMutex .Unlock ()
98
- go s .handlePeerConn (ctx , conn )
95
+ a .closeMutex .Lock ()
96
+ a .connCloseWait .Add (1 )
97
+ a .closeMutex .Unlock ()
98
+ go a .handlePeerConn (ctx , conn )
99
99
}
100
100
}
101
101
102
- func (s * agent ) handlePeerConn (ctx context.Context , conn * peer.Conn ) {
102
+ func (a * agent ) handlePeerConn (ctx context.Context , conn * peer.Conn ) {
103
103
go func () {
104
104
<- conn .Closed ()
105
- s .connCloseWait .Done ()
105
+ a .connCloseWait .Done ()
106
106
}()
107
107
for {
108
108
channel , err := conn .Accept (ctx )
109
109
if err != nil {
110
- if errors .Is (err , peer .ErrClosed ) || s .isClosed () {
110
+ if errors .Is (err , peer .ErrClosed ) || a .isClosed () {
111
111
return
112
112
}
113
- s .options .Logger .Debug (ctx , "accept channel from peer connection" , slog .Error (err ))
113
+ a .options .Logger .Debug (ctx , "accept channel from peer connection" , slog .Error (err ))
114
114
return
115
115
}
116
116
117
117
switch channel .Protocol () {
118
118
case "ssh" :
119
- s .sshServer .HandleConn (channel .NetConn ())
119
+ a .sshServer .HandleConn (channel .NetConn ())
120
120
default :
121
- s .options .Logger .Warn (ctx , "unhandled protocol from channel" ,
121
+ a .options .Logger .Warn (ctx , "unhandled protocol from channel" ,
122
122
slog .F ("protocol" , channel .Protocol ()),
123
123
slog .F ("label" , channel .Label ()),
124
124
)
125
125
}
126
126
}
127
127
}
128
128
129
- func (s * agent ) init (ctx context.Context ) {
129
+ func (a * agent ) init (ctx context.Context ) {
130
130
// Clients' should ignore the host key when connecting.
131
131
// The agent needs to authenticate with coderd to SSH,
132
132
// so SSH authentication doesn't improve security.
@@ -138,17 +138,17 @@ func (s *agent) init(ctx context.Context) {
138
138
if err != nil {
139
139
panic (err )
140
140
}
141
- sshLogger := s .options .Logger .Named ("ssh-server" )
141
+ sshLogger := a .options .Logger .Named ("ssh-server" )
142
142
forwardHandler := & ssh.ForwardedTCPHandler {}
143
- s .sshServer = & ssh.Server {
143
+ a .sshServer = & ssh.Server {
144
144
ChannelHandlers : ssh .DefaultChannelHandlers ,
145
145
ConnectionFailedCallback : func (conn net.Conn , err error ) {
146
146
sshLogger .Info (ctx , "ssh connection ended" , slog .Error (err ))
147
147
},
148
148
Handler : func (session ssh.Session ) {
149
- err := s .handleSSHSession (session )
149
+ err := a .handleSSHSession (session )
150
150
if err != nil {
151
- s .options .Logger .Debug (ctx , "ssh session failed" , slog .Error (err ))
151
+ a .options .Logger .Warn (ctx , "ssh session failed" , slog .Error (err ))
152
152
_ = session .Exit (1 )
153
153
return
154
154
}
@@ -177,35 +177,26 @@ func (s *agent) init(ctx context.Context) {
177
177
},
178
178
ServerConfigCallback : func (ctx ssh.Context ) * gossh.ServerConfig {
179
179
return & gossh.ServerConfig {
180
- Config : gossh.Config {
181
- // "arcfour" is the fastest SSH cipher. We prioritize throughput
182
- // over encryption here, because the WebRTC connection is already
183
- // encrypted. If possible, we'd disable encryption entirely here.
184
- Ciphers : []string {"arcfour" },
185
- },
186
180
NoClientAuth : true ,
187
181
}
188
182
},
189
183
}
190
184
191
- go s .run (ctx )
185
+ go a .run (ctx )
192
186
}
193
187
194
- func (* agent ) handleSSHSession (session ssh.Session ) error {
188
+ func (a * agent ) handleSSHSession (session ssh.Session ) error {
195
189
var (
196
190
command string
197
191
args = []string {}
198
192
err error
199
193
)
200
194
201
- username := session .User ()
202
- if username == "" {
203
- currentUser , err := user .Current ()
204
- if err != nil {
205
- return xerrors .Errorf ("get current user: %w" , err )
206
- }
207
- username = currentUser .Username
195
+ currentUser , err := user .Current ()
196
+ if err != nil {
197
+ return xerrors .Errorf ("get current user: %w" , err )
208
198
}
199
+ username := currentUser .Username
209
200
210
201
// gliderlabs/ssh returns a command slice of zero
211
202
// when a shell is requested.
@@ -249,9 +240,9 @@ func (*agent) handleSSHSession(session ssh.Session) error {
249
240
}
250
241
go func () {
251
242
for win := range windowSize {
252
- err : = ptty .Resize (uint16 (win .Width ), uint16 (win .Height ))
243
+ err = ptty .Resize (uint16 (win .Width ), uint16 (win .Height ))
253
244
if err != nil {
254
- panic ( err )
245
+ a . options . Logger . Warn ( context . Background (), "failed to resize tty" , slog . Error ( err ) )
255
246
}
256
247
}
257
248
}()
@@ -286,24 +277,24 @@ func (*agent) handleSSHSession(session ssh.Session) error {
286
277
}
287
278
288
279
// isClosed returns whether the API is closed or not.
289
- func (s * agent ) isClosed () bool {
280
+ func (a * agent ) isClosed () bool {
290
281
select {
291
- case <- s .closed :
282
+ case <- a .closed :
292
283
return true
293
284
default :
294
285
return false
295
286
}
296
287
}
297
288
298
- func (s * agent ) Close () error {
299
- s .closeMutex .Lock ()
300
- defer s .closeMutex .Unlock ()
301
- if s .isClosed () {
289
+ func (a * agent ) Close () error {
290
+ a .closeMutex .Lock ()
291
+ defer a .closeMutex .Unlock ()
292
+ if a .isClosed () {
302
293
return nil
303
294
}
304
- close (s .closed )
305
- s .closeCancel ()
306
- _ = s .sshServer .Close ()
307
- s .connCloseWait .Wait ()
295
+ close (a .closed )
296
+ a .closeCancel ()
297
+ _ = a .sshServer .Close ()
298
+ a .connCloseWait .Wait ()
308
299
return nil
309
300
}
0 commit comments