跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

client.go

104 行 · 2.6 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package client
5
6import (
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
20type GuestAgentClient struct {
21 cli api.GuestServiceClient
22 conn *grpc.ClientConn
23}
24
25func 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// Close releases the underlying gRPC connection. It is safe to call on a nil
54// receiver or a client whose connection has already been closed.
55func (c *GuestAgentClient) Close() error {
56 if c == nil || c.conn == nil {
57 return nil
58 }
59 return c.conn.Close()
60}
61
62func (c *GuestAgentClient) Info(ctx context.Context) (*api.Info, error) {
63 return c.cli.GetInfo(ctx, &emptypb.Empty{})
64}
65
66func (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
83func (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
91func (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
99func (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}