- Notifications
You must be signed in to change notification settings - Fork926
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
base:spike/testutil-in-proc-net
Are you sure you want to change the base?
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -7,6 +7,7 @@ import ( | ||
"errors" | ||
"fmt" | ||
"io" | ||
"math" | ||
"net" | ||
"os" | ||
"path/filepath" | ||
@@ -37,12 +38,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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Suggestion: It'd be nice to keep this as a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I decided against this because I wanted to reuse | ||
} | ||
// 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{} | ||
@@ -145,26 +164,35 @@ func (x *x11Forwarder) listenForConnections(ctx context.Context, session *x11Ses | ||
x.cleanSession(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" | ||
} | ||
if originPort == 0 { | ||
p := X11StartPort + session.display | ||
if p > math.MaxUint32 { | ||
panic("overflow") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I don't think we should panic here as that risks killing the whole agent in the unlikely event that we don't have a | ||
} | ||
// #nosec G115 - Safe conversion as port number is within uint32 range | ||
originPort = uint32(p) | ||
} | ||
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)) | ||
@@ -281,13 +309,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 | ||
Uh oh!
There was an error while loading.Please reload this page.