x11Handler is called when a session has requested X11 forwarding. It listens for X11 connections and forwards them to the client.
(sshCtx ssh.Context, sshSession ssh.Session)
| 85 | // x11Handler is called when a session has requested X11 forwarding. |
| 86 | // It listens for X11 connections and forwards them to the client. |
| 87 | func (x *x11Forwarder) x11Handler(sshCtx ssh.Context, sshSession ssh.Session) (displayNumber int, handled bool) { |
| 88 | x11, hasX11 := sshSession.X11() |
| 89 | if !hasX11 { |
| 90 | return -1, false |
| 91 | } |
| 92 | serverConn, valid := sshCtx.Value(ssh.ContextKeyConn).(*gossh.ServerConn) |
| 93 | if !valid { |
| 94 | x.logger.Warn(sshCtx, "failed to get server connection") |
| 95 | return -1, false |
| 96 | } |
| 97 | ctx := slog.With(sshCtx, slog.F("session_id", fmt.Sprintf("%x", serverConn.SessionID()))) |
| 98 | |
| 99 | hostname, err := os.Hostname() |
| 100 | if err != nil { |
| 101 | x.logger.Warn(ctx, "failed to get hostname", slog.Error(err)) |
| 102 | x.x11HandlerErrors.WithLabelValues("hostname").Add(1) |
| 103 | return -1, false |
| 104 | } |
| 105 | |
| 106 | x11session, err := x.createX11Session(ctx, sshSession) |
| 107 | if err != nil { |
| 108 | x.logger.Warn(ctx, "failed to create X11 listener", slog.Error(err)) |
| 109 | x.x11HandlerErrors.WithLabelValues("listen").Add(1) |
| 110 | return -1, false |
| 111 | } |
| 112 | defer func() { |
| 113 | if !handled { |
| 114 | x.closeAndRemoveSession(x11session) |
| 115 | } |
| 116 | }() |
| 117 | |
| 118 | err = addXauthEntry(ctx, x.fs, hostname, strconv.Itoa(x11session.display), x11.AuthProtocol, x11.AuthCookie) |
| 119 | if err != nil { |
| 120 | x.logger.Warn(ctx, "failed to add Xauthority entry", slog.Error(err)) |
| 121 | x.x11HandlerErrors.WithLabelValues("xauthority").Add(1) |
| 122 | return -1, false |
| 123 | } |
| 124 | |
| 125 | // clean up the X11 session if the SSH session completes. |
| 126 | go func() { |
| 127 | <-ctx.Done() |
| 128 | x.closeAndRemoveSession(x11session) |
| 129 | }() |
| 130 | |
| 131 | go x.listenForConnections(ctx, x11session, serverConn, x11) |
| 132 | x.logger.Debug(ctx, "X11 forwarding started", slog.F("display", x11session.display)) |
| 133 | |
| 134 | return x11session.display, true |
| 135 | } |
| 136 | |
| 137 | func (x *x11Forwarder) trackGoroutine() (closing bool, done func()) { |
| 138 | x.mu.Lock() |
no test coverage detected