(session ssh.Session)
| 409 | } |
| 410 | |
| 411 | func (s *Server) sessionHandler(session ssh.Session) { |
| 412 | ctx := session.Context() |
| 413 | id := uuid.New() |
| 414 | logger := s.logger.With( |
| 415 | slog.F("remote_addr", session.RemoteAddr()), |
| 416 | slog.F("local_addr", session.LocalAddr()), |
| 417 | // Assigning a random uuid for each session is useful for tracking |
| 418 | // logs for the same ssh session. |
| 419 | slog.F("id", id.String()), |
| 420 | ) |
| 421 | logger.Info(ctx, "handling ssh session") |
| 422 | |
| 423 | env := session.Environ() |
| 424 | magicType, magicTypeRaw, env := extractMagicSessionType(env) |
| 425 | |
| 426 | // It's not safe to assume RemoteAddr() returns a non-nil value. slog.F usage is fine because it correctly |
| 427 | // handles nil. |
| 428 | // c.f. https://github.com/coder/internal/issues/1143 |
| 429 | remoteAddr := session.RemoteAddr() |
| 430 | remoteAddrString := "" |
| 431 | if remoteAddr != nil { |
| 432 | remoteAddrString = remoteAddr.String() |
| 433 | } |
| 434 | |
| 435 | if !s.trackSession(session, true) { |
| 436 | reason := "unable to accept new session, server is closing" |
| 437 | // Report connection attempt even if we couldn't accept it. |
| 438 | disconnected := s.config.ReportConnection(id, magicType, remoteAddrString) |
| 439 | defer disconnected(1, reason) |
| 440 | |
| 441 | logger.Info(ctx, reason) |
| 442 | // See (*Server).Close() for why we call Close instead of Exit. |
| 443 | _ = session.Close() |
| 444 | return |
| 445 | } |
| 446 | defer s.trackSession(session, false) |
| 447 | |
| 448 | reportSession := true |
| 449 | |
| 450 | switch magicType { |
| 451 | case MagicSessionTypeVSCode: |
| 452 | s.connCountVSCode.Add(1) |
| 453 | defer s.connCountVSCode.Add(-1) |
| 454 | case MagicSessionTypeJetBrains: |
| 455 | // Do nothing here because JetBrains launches hundreds of ssh sessions. |
| 456 | // We instead track JetBrains in the single persistent tcp forwarding channel. |
| 457 | reportSession = false |
| 458 | case MagicSessionTypeSSH: |
| 459 | s.connCountSSHSession.Add(1) |
| 460 | defer s.connCountSSHSession.Add(-1) |
| 461 | case MagicSessionTypeUnknown: |
| 462 | logger.Warn(ctx, "invalid magic ssh session type specified", slog.F("raw_type", magicTypeRaw)) |
| 463 | } |
| 464 | |
| 465 | closeCause := func(_ string) {} |
| 466 | if reportSession { |
| 467 | var reason codersdk.DisconnectReason |
| 468 | closeCause = func(r string) { reason = codersdk.DisconnectReason(r) } |
nothing calls this directly
no test coverage detected