- Notifications
You must be signed in to change notification settings - Fork927
feat(agent/agentssh): use tcp for X11 forwarding#14560
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
3 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
15 changes: 8 additions & 7 deletionsagent/agentssh/agentssh.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
141 changes: 88 additions & 53 deletionsagent/agentssh/x11.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -7,6 +7,7 @@ import ( | ||
"errors" | ||
"fmt" | ||
"io" | ||
"math" | ||
"net" | ||
"os" | ||
"path/filepath" | ||
@@ -22,102 +23,136 @@ import ( | ||
"cdr.dev/slog" | ||
) | ||
const ( | ||
// X11StartPort is the starting port for X11 forwarding, this is the | ||
// port used for "DISPLAY=localhost:0". | ||
X11StartPort = 6000 | ||
// X11DefaultDisplayOffset is the default offset for X11 forwarding. | ||
X11DefaultDisplayOffset = 10 | ||
) | ||
// x11Callback is called when the client requests X11 forwarding. | ||
func (*Server) x11Callback(_ ssh.Context, _ ssh.X11) bool { | ||
// Always allow. | ||
return true | ||
} | ||
// x11Handler is called when a session has requested X11 forwarding. | ||
// It listens for X11 connections and forwards them to the client. | ||
func (s *Server) x11Handler(ctx ssh.Context, x11 ssh.X11) (displayNumber int, handled bool) { | ||
serverConn, valid := ctx.Value(ssh.ContextKeyConn).(*gossh.ServerConn) | ||
if !valid { | ||
s.logger.Warn(ctx, "failed to get server connection") | ||
return -1, false | ||
} | ||
hostname, err := os.Hostname() | ||
johnstcn marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
if err != nil { | ||
s.logger.Warn(ctx, "failed to get hostname", slog.Error(err)) | ||
s.metrics.x11HandlerErrors.WithLabelValues("hostname").Add(1) | ||
return-1,false | ||
} | ||
ln, display,err:= createX11Listener(ctx, *s.config.X11DisplayOffset) | ||
if err != nil { | ||
s.logger.Warn(ctx, "failed to create X11 listener", slog.Error(err)) | ||
s.metrics.x11HandlerErrors.WithLabelValues("listen").Add(1) | ||
return -1, false | ||
} | ||
s.trackListener(ln, true) | ||
defer func() { | ||
if !handled { | ||
s.trackListener(ln, false) | ||
_ = ln.Close() | ||
} | ||
}() | ||
err = addXauthEntry(ctx, s.fs, hostname, strconv.Itoa(display), x11.AuthProtocol, x11.AuthCookie) | ||
if err != nil { | ||
s.logger.Warn(ctx, "failed to add Xauthority entry", slog.Error(err)) | ||
s.metrics.x11HandlerErrors.WithLabelValues("xauthority").Add(1) | ||
return-1,false | ||
} | ||
go func() { | ||
// Don't leave the listener open after the session is gone. | ||
<-ctx.Done() | ||
_ = ln.Close() | ||
johnstcn marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
}() | ||
go func() { | ||
defer ln.Close() | ||
defer s.trackListener(ln, false) | ||
for { | ||
conn, err :=ln.Accept() | ||
if err != nil { | ||
if errors.Is(err, net.ErrClosed) { | ||
return | ||
} | ||
s.logger.Warn(ctx, "failed to accept X11 connection", slog.Error(err)) | ||
return | ||
} | ||
if x11.SingleConnection { | ||
s.logger.Debug(ctx, "single connection requested, closing X11 listener") | ||
_ = ln.Close() | ||
} | ||
tcpConn, ok := conn.(*net.TCPConn) | ||
if !ok { | ||
s.logger.Warn(ctx, fmt.Sprintf("failed to cast connection to TCPConn. got: %T", conn)) | ||
_ = conn.Close() | ||
continue | ||
} | ||
tcpAddr, ok :=tcpConn.LocalAddr().(*net.TCPAddr) | ||
if !ok { | ||
s.logger.Warn(ctx, fmt.Sprintf("failed to cast local address to TCPAddr. got: %T", tcpConn.LocalAddr())) | ||
_ = conn.Close() | ||
continue | ||
mafredri marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
} | ||
channel, reqs, err := serverConn.OpenChannel("x11", gossh.Marshal(struct { | ||
OriginatorAddress string | ||
OriginatorPort uint32 | ||
}{ | ||
OriginatorAddress:tcpAddr.IP.String(), | ||
OriginatorPort:uint32(tcpAddr.Port), | ||
})) | ||
if err != nil { | ||
s.logger.Warn(ctx, "failed to open X11 channel", slog.Error(err)) | ||
_ = conn.Close() | ||
continue | ||
} | ||
go gossh.DiscardRequests(reqs) | ||
if !s.trackConn(ln, conn, true) { | ||
s.logger.Warn(ctx, "failed to track X11 connection") | ||
_ = conn.Close() | ||
continue | ||
} | ||
go func() { | ||
defer s.trackConn(ln, conn, false) | ||
Bicopy(ctx, conn, channel) | ||
}() | ||
} | ||
}() | ||
return display, true | ||
} | ||
// createX11Listener creates a listener for X11 forwarding, it will use | ||
// the next available port starting from X11StartPort and displayOffset. | ||
func createX11Listener(ctx context.Context, displayOffset int) (ln net.Listener, display int, err error) { | ||
var lc net.ListenConfig | ||
// Look for an open port to listen on. | ||
for port := X11StartPort + displayOffset; port < math.MaxUint16; port++ { | ||
ln, err = lc.Listen(ctx, "tcp", fmt.Sprintf("localhost:%d", port)) | ||
if err == nil { | ||
display = port - X11StartPort | ||
return ln, display, nil | ||
} | ||
} | ||
return nil, -1, xerrors.Errorf("failed to find open port for X11 listener: %w", err) | ||
} | ||
// addXauthEntry adds an Xauthority entry to the Xauthority file. | ||
40 changes: 33 additions & 7 deletionsagent/agentssh/x11_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.