| 1 | |
| 2 | |
| 3 | |
| 4 | package client |
| 5 | |
| 6 | import ( |
| 7 | "context" |
| 8 | "math" |
| 9 | "net" |
| 10 | "time" |
| 11 | |
| 12 | "google.golang.org/grpc" |
| 13 | "google.golang.org/grpc/resolver" |
| 14 | "google.golang.org/protobuf/types/known/emptypb" |
| 15 | "google.golang.org/protobuf/types/known/timestamppb" |
| 16 | |
| 17 | "github.com/lima-vm/lima/v2/pkg/guestagent/api" |
| 18 | ) |
| 19 | |
| 20 | type GuestAgentClient struct { |
| 21 | cli api.GuestServiceClient |
| 22 | conn *grpc.ClientConn |
| 23 | } |
| 24 | |
| 25 | func NewGuestAgentClient(dialFn func(ctx context.Context) (net.Conn, error)) (*GuestAgentClient, error) { |
| 26 | opts := []grpc.DialOption{ |
| 27 | grpc.WithTransportCredentials(NewCredentials()), |
| 28 | grpc.WithInitialWindowSize(512 << 20), |
| 29 | grpc.WithInitialConnWindowSize(512 << 20), |
| 30 | grpc.WithReadBufferSize(8 << 20), |
| 31 | grpc.WithWriteBufferSize(8 << 20), |
| 32 | grpc.WithDefaultCallOptions( |
| 33 | grpc.MaxCallRecvMsgSize(math.MaxInt32), |
| 34 | grpc.MaxCallSendMsgSize(math.MaxInt32), |
| 35 | ), |
| 36 | grpc.WithContextDialer(func(ctx context.Context, _ string) (net.Conn, error) { |
| 37 | return dialFn(ctx) |
| 38 | }), |
| 39 | } |
| 40 | |
| 41 | resolver.SetDefaultScheme("passthrough") |
| 42 | clientConn, err := grpc.NewClient("", opts...) |
| 43 | if err != nil { |
| 44 | return nil, err |
| 45 | } |
| 46 | client := api.NewGuestServiceClient(clientConn) |
| 47 | return &GuestAgentClient{ |
| 48 | cli: client, |
| 49 | conn: clientConn, |
| 50 | }, nil |
| 51 | } |
| 52 | |
| 53 | |
| 54 | |
| 55 | func (c *GuestAgentClient) Close() error { |
| 56 | if c == nil || c.conn == nil { |
| 57 | return nil |
| 58 | } |
| 59 | return c.conn.Close() |
| 60 | } |
| 61 | |
| 62 | func (c *GuestAgentClient) Info(ctx context.Context) (*api.Info, error) { |
| 63 | return c.cli.GetInfo(ctx, &emptypb.Empty{}) |
| 64 | } |
| 65 | |
| 66 | func (c *GuestAgentClient) Events(ctx context.Context, eventCb func(response *api.Event) error) error { |
| 67 | events, err := c.cli.GetEvents(ctx, &emptypb.Empty{}) |
| 68 | if err != nil { |
| 69 | return err |
| 70 | } |
| 71 | |
| 72 | for { |
| 73 | recv, err := events.Recv() |
| 74 | if err != nil { |
| 75 | return err |
| 76 | } |
| 77 | if err := eventCb(recv); err != nil { |
| 78 | return err |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | func (c *GuestAgentClient) Inotify(ctx context.Context) (api.GuestService_PostInotifyClient, error) { |
| 84 | inotify, err := c.cli.PostInotify(ctx) |
| 85 | if err != nil { |
| 86 | return nil, err |
| 87 | } |
| 88 | return inotify, nil |
| 89 | } |
| 90 | |
| 91 | func (c *GuestAgentClient) Tunnel(ctx context.Context) (api.GuestService_TunnelClient, error) { |
| 92 | stream, err := c.cli.Tunnel(ctx) |
| 93 | if err != nil { |
| 94 | return nil, err |
| 95 | } |
| 96 | return stream, nil |
| 97 | } |
| 98 | |
| 99 | func (c *GuestAgentClient) SyncTime(ctx context.Context, hostTime time.Time) (*api.TimeSyncResponse, error) { |
| 100 | req := &api.TimeSyncRequest{ |
| 101 | HostTime: timestamppb.New(hostTime), |
| 102 | } |
| 103 | return c.cli.SyncTime(ctx, req) |
| 104 | } |