- Notifications
You must be signed in to change notification settings - Fork1k
chore: refactor TestServer_X11 to use inproc networking#18564
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
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
10 changes: 10 additions & 0 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
49 changes: 34 additions & 15 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 |
---|---|---|
@@ -37,12 +37,30 @@ const ( | ||
X11MaxPort = X11StartPort + X11MaxDisplays | ||
) | ||
// X11Network abstracts the creation of network listeners for X11 forwarding. | ||
// It is intended mainly for testing; production code uses the default | ||
// implementation backed by the operating system networking stack. | ||
type X11Network interface { | ||
Listen(network, address string) (net.Listener, error) | ||
spikecurtis marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
} | ||
// osNet is the default X11Network implementation that uses the standard | ||
// library network stack. | ||
type osNet struct{} | ||
func (osNet) Listen(network, address string) (net.Listener, error) { | ||
return net.Listen(network, address) | ||
} | ||
type x11Forwarder struct { | ||
logger slog.Logger | ||
x11HandlerErrors *prometheus.CounterVec | ||
fs afero.Fs | ||
displayOffset int | ||
// network creates X11 listener sockets. Defaults to osNet{}. | ||
network X11Network | ||
mu sync.Mutex | ||
sessions map[*x11Session]struct{} | ||
connections map[net.Conn]struct{} | ||
@@ -147,26 +165,27 @@ func (x *x11Forwarder) listenForConnections( | ||
x.closeAndRemoveSession(session) | ||
} | ||
var originAddr string | ||
var originPort uint32 | ||
if tcpConn, ok := conn.(*net.TCPConn); ok { | ||
if tcpAddr, ok := tcpConn.LocalAddr().(*net.TCPAddr); ok { | ||
originAddr = tcpAddr.IP.String() | ||
// #nosec G115 - Safe conversion as TCP port numbers are within uint32 range (0-65535) | ||
originPort = uint32(tcpAddr.Port) | ||
} | ||
} | ||
// Fallback values for in-memory or non-TCP connections. | ||
if originAddr == "" { | ||
originAddr = "127.0.0.1" | ||
} | ||
channel, reqs, err := serverConn.OpenChannel("x11", gossh.Marshal(struct { | ||
OriginatorAddress string | ||
OriginatorPort uint32 | ||
}{ | ||
OriginatorAddress: originAddr, | ||
OriginatorPort: originPort, | ||
})) | ||
if err != nil { | ||
x.logger.Warn(ctx, "failed to open X11 channel", slog.Error(err)) | ||
@@ -287,13 +306,13 @@ func (x *x11Forwarder) evictLeastRecentlyUsedSession() { | ||
// createX11Listener creates a listener for X11 forwarding, it will use | ||
// the next available port starting from X11StartPort and displayOffset. | ||
func (x *x11Forwarder) createX11Listener(ctx context.Context) (ln net.Listener, display int, err error) { | ||
// Look for an open port to listen on. | ||
for port := X11StartPort + x.displayOffset; port <= X11MaxPort; port++ { | ||
if ctx.Err() != nil { | ||
return nil, -1, ctx.Err() | ||
} | ||
ln, err = x.network.Listen("tcp", fmt.Sprintf("localhost:%d", port)) | ||
if err == nil { | ||
display = port - X11StartPort | ||
return ln, display, nil | ||
32 changes: 19 additions & 13 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.