|
| 1 | +// Package main is the rdd-guest agent that runs inside the Lima/WSL2 VM. |
| 2 | +// It listens on a vsock port and forwards connections to the Docker socket, |
| 3 | +// enabling the Windows host to reach /var/run/docker.sock via Hyper-V vsock. |
| 4 | +// |
| 5 | +package main |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "errors" |
| 10 | + "io" |
| 11 | + "log" |
| 12 | + "net" |
| 13 | + "os" |
| 14 | + "os/signal" |
| 15 | + "syscall" |
| 16 | + |
| 17 | + "github.com/mdlayher/vsock" |
| 18 | +) |
| 19 | + |
| 20 | +const ( |
| 21 | + vsockPort = 6660 |
| 22 | + dockerSockPath = "/var/run/docker.sock" |
| 23 | +) |
| 24 | + |
| 25 | +func main() { |
| 26 | + ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) |
| 27 | + defer stop() |
| 28 | + |
| 29 | + l, err := vsock.Listen(vsockPort, nil) |
| 30 | + if err != nil { |
| 31 | + log.Fatalf("vsock listen: %v", err) |
| 32 | + } |
| 33 | + defer l.Close() |
| 34 | + |
| 35 | + log.Printf("rdd-guest: listening on vsock port %d", vsockPort) |
| 36 | + |
| 37 | + go func() { |
| 38 | + <-ctx.Done() |
| 39 | + l.Close() |
| 40 | + }() |
| 41 | + |
| 42 | + for { |
| 43 | + conn, err := l.Accept() |
| 44 | + if err != nil { |
| 45 | + if ctx.Err() != nil { |
| 46 | + return |
| 47 | + } |
| 48 | + log.Printf("rdd-guest: accept: %v", err) |
| 49 | + continue |
| 50 | + } |
| 51 | + go handleConn(conn) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +// TODO: once rancher-desktop-daemon is public, replace the inlined halfCloser, |
| 56 | +// pipe(), and handleConn() here with a direct import of pkg/socketbridge, which |
| 57 | +// contains the implementations (HalfCloser interface, Pipe function). |
| 58 | +// halfCloser is a net.Conn that can independently close the write side. |
| 59 | +type halfCloser interface { |
| 60 | + net.Conn |
| 61 | + CloseWrite() error |
| 62 | +} |
| 63 | + |
| 64 | +// handleConn forwards bytes between the vsock connection and the Docker socket. |
| 65 | +func handleConn(vsockConn net.Conn) { |
| 66 | + defer vsockConn.Close() |
| 67 | + |
| 68 | + dockerConn, err := (&net.Dialer{}).DialContext(context.Background(), "unix", dockerSockPath) |
| 69 | + if err != nil { |
| 70 | + log.Printf("rdd-guest: dial docker: %v", err) |
| 71 | + return |
| 72 | + } |
| 73 | + defer dockerConn.Close() |
| 74 | + |
| 75 | + pipe(vsockConn.(halfCloser), dockerConn.(halfCloser)) |
| 76 | +} |
| 77 | + |
| 78 | +// pipe bidirectionally proxies between a and b until both directions are done. |
| 79 | +func pipe(a, b halfCloser) { |
| 80 | + done := make(chan error, 2) |
| 81 | + |
| 82 | + forward := func(dst, src halfCloser) { |
| 83 | + _, err := io.Copy(dst, src) |
| 84 | + if err != nil && !errors.Is(err, io.EOF) { |
| 85 | + log.Printf("rdd-guest: copy: %v", err) |
| 86 | + } |
| 87 | + _ = dst.CloseWrite() |
| 88 | + done <- err |
| 89 | + } |
| 90 | + |
| 91 | + go forward(a, b) |
| 92 | + go forward(b, a) |
| 93 | + <-done |
| 94 | + <-done |
| 95 | +} |
0 commit comments