| 1 | |
| 2 | |
| 3 | |
| 4 | package portfwd |
| 5 | |
| 6 | import ( |
| 7 | "context" |
| 8 | "errors" |
| 9 | "fmt" |
| 10 | "net" |
| 11 | "sync/atomic" |
| 12 | "time" |
| 13 | |
| 14 | "github.com/containers/gvisor-tap-vsock/pkg/services/forwarder" |
| 15 | "github.com/inetaf/tcpproxy" |
| 16 | "github.com/sirupsen/logrus" |
| 17 | |
| 18 | "github.com/lima-vm/lima/v2/pkg/guestagent/api" |
| 19 | guestagentclient "github.com/lima-vm/lima/v2/pkg/guestagent/api/client" |
| 20 | ) |
| 21 | |
| 22 | func HandleTCPConnection(_ context.Context, dialContext func(ctx context.Context, network string, addr string) (net.Conn, error), conn net.Conn, guestAddr string) { |
| 23 | proxy := tcpproxy.DialProxy{Addr: guestAddr, DialContext: dialContext} |
| 24 | proxy.HandleConn(conn) |
| 25 | logrus.Debugf("tcp proxy for guestAddr: %s closed", guestAddr) |
| 26 | } |
| 27 | |
| 28 | func HandleUDPConnection(ctx context.Context, dialContext func(ctx context.Context, network string, addr string) (net.Conn, error), conn net.PacketConn, guestAddr string) { |
| 29 | proxy, err := forwarder.NewUDPProxy(conn, func() (net.Conn, error) { |
| 30 | return dialContext(ctx, "udp", guestAddr) |
| 31 | }) |
| 32 | if err != nil { |
| 33 | logrus.WithError(err).Error("error in udp tunnel proxy") |
| 34 | return |
| 35 | } |
| 36 | |
| 37 | defer func() { |
| 38 | err := proxy.Close() |
| 39 | if err != nil { |
| 40 | logrus.WithError(err).Error("error in closing udp tunnel proxy") |
| 41 | } |
| 42 | }() |
| 43 | proxy.Run() |
| 44 | logrus.Debugf("udp proxy for guestAddr: %s closed", guestAddr) |
| 45 | } |
| 46 | |
| 47 | func DialContextToGRPCTunnel(client *guestagentclient.GuestAgentClient) func(ctx context.Context, network, addr string) (net.Conn, error) { |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | var connectionCounter atomic.Uint32 |
| 53 | return func(_ context.Context, network, addr string) (net.Conn, error) { |
| 54 | |
| 55 | |
| 56 | streamCtx, cancel := context.WithCancel(context.Background()) |
| 57 | stream, err := client.Tunnel(streamCtx) |
| 58 | if err != nil { |
| 59 | cancel() |
| 60 | return nil, fmt.Errorf("could not open tunnel for addr: %s error:%w", addr, err) |
| 61 | } |
| 62 | |
| 63 | id := fmt.Sprintf("%s-%s-%d", network, addr, connectionCounter.Add(1)) |
| 64 | if err := stream.Send(&api.TunnelMessage{Id: id, Protocol: network, GuestAddr: addr}); err != nil { |
| 65 | cancel() |
| 66 | return nil, fmt.Errorf("could not start tunnel for id: %s addr: %s error:%w", id, addr, err) |
| 67 | } |
| 68 | rw := &GrpcClientRW{stream: stream, cancel: cancel, id: id, addr: addr, protocol: network} |
| 69 | return rw, nil |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | type GrpcClientRW struct { |
| 74 | id string |
| 75 | addr string |
| 76 | |
| 77 | protocol string |
| 78 | stream api.GuestService_TunnelClient |
| 79 | cancel context.CancelFunc |
| 80 | |
| 81 | |
| 82 | |
| 83 | rxBuf []byte |
| 84 | } |
| 85 | |
| 86 | var _ net.Conn = (*GrpcClientRW)(nil) |
| 87 | |
| 88 | func (g *GrpcClientRW) Write(p []byte) (n int, err error) { |
| 89 | err = g.stream.Send(&api.TunnelMessage{ |
| 90 | Id: g.id, |
| 91 | GuestAddr: g.addr, |
| 92 | Data: p, |
| 93 | Protocol: g.protocol, |
| 94 | }) |
| 95 | if err != nil { |
| 96 | return 0, err |
| 97 | } |
| 98 | return len(p), nil |
| 99 | } |
| 100 | |
| 101 | func (g *GrpcClientRW) Read(p []byte) (n int, err error) { |
| 102 | if len(g.rxBuf) == 0 { |
| 103 | in, err := g.stream.Recv() |
| 104 | if err != nil { |
| 105 | return 0, err |
| 106 | } |
| 107 | g.rxBuf = in.Data |
| 108 | } |
| 109 | n = copy(p, g.rxBuf) |
| 110 | g.rxBuf = g.rxBuf[n:] |
| 111 | return n, nil |
| 112 | } |
| 113 | |
| 114 | func (g *GrpcClientRW) Close() error { |
| 115 | logrus.Debugf("closing GrpcClientRW for id: %s", g.id) |
| 116 | err := g.stream.CloseSend() |
| 117 | g.cancel() |
| 118 | return err |
| 119 | } |
| 120 | |
| 121 | func (g *GrpcClientRW) CloseRead() error { |
| 122 | logrus.Debugf("closing read GrpcClientRW for id: %s", g.id) |
| 123 | g.cancel() |
| 124 | return nil |
| 125 | } |
| 126 | |
| 127 | func (g *GrpcClientRW) CloseWrite() error { |
| 128 | logrus.Debugf("closing write GrpcClientRW for id: %s", g.id) |
| 129 | err := g.stream.CloseSend() |
| 130 | if errors.Is(err, context.Canceled) { |
| 131 | return nil |
| 132 | } |
| 133 | return err |
| 134 | } |
| 135 | |
| 136 | func (g *GrpcClientRW) LocalAddr() net.Addr { |
| 137 | return &net.UnixAddr{Name: "grpc", Net: "unixpacket"} |
| 138 | } |
| 139 | |
| 140 | func (g *GrpcClientRW) RemoteAddr() net.Addr { |
| 141 | return &net.UnixAddr{Name: "grpc", Net: "unixpacket"} |
| 142 | } |
| 143 | |
| 144 | func (g *GrpcClientRW) SetDeadline(_ time.Time) error { |
| 145 | return nil |
| 146 | } |
| 147 | |
| 148 | func (g *GrpcClientRW) SetReadDeadline(_ time.Time) error { |
| 149 | return nil |
| 150 | } |
| 151 | |
| 152 | func (g *GrpcClientRW) SetWriteDeadline(_ time.Time) error { |
| 153 | return nil |
| 154 | } |