跳到主要内容

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

浏览文件

network.go

161 行 · 5.1 KB历史下载
1// Package containerenv runs the Linux Cage environment without a nested VM.
2// The host owns the network stack and its policy; a container only exchanges
3// Ethernet frames over a process pipe and receives no host network interface.
4package containerenv
5
6import (
7 "context"
8 "errors"
9 "io"
10 "net"
11 "os/exec"
12 "sync"
13 "time"
14
15 "github.com/containers/gvisor-tap-vsock/pkg/types"
16 "github.com/containers/gvisor-tap-vsock/pkg/virtualnetwork"
17 "github.com/sylvinus/agent-vm/internal/cage"
18)
19
20const (
21 GuestIP = "192.168.64.2"
22 GatewayIP = "192.168.64.1"
23 GuestMAC = "5a:94:ef:e4:0c:02"
24 GatewayMAC = "5a:94:ef:e4:0c:01"
25 TapName = "cagetap0"
26 NetworkMTU = 1500
27)
28
29// Network owns the only path between the container and the outside network.
30// The caller must close the process endpoint when Run returns. A gate starts
31// closed; connecting SSH does not authorize any guest outbound traffic.
32type Network struct {
33 Gate *cage.EgressGate
34 stack *virtualnetwork.VirtualNetwork
35}
36
37func networkConfiguration(gate *cage.EgressGate) types.Configuration {
38 return types.Configuration{
39 MTU: NetworkMTU,
40 Subnet: "192.168.64.0/24", GatewayIP: GatewayIP,
41 GatewayMacAddress: GatewayMAC,
42 DHCPStaticLeases: map[string]string{GuestIP: GuestMAC},
43 // No NAT-to-host entries and no automatically published ports.
44 NAT: map[string]string{}, Forwards: map[string]string{},
45 Ec2MetadataAccess: false,
46 Outbound: gate.Allows, DialContext: gate.DialContext,
47 DNSExchange: gate.ExchangeDNS,
48 }
49}
50
51func NewNetwork(cfg cage.Config) (*Network, error) {
52 gate, err := cage.NewEgressGate(cfg)
53 if err != nil {
54 return nil, err
55 }
56 configuration := networkConfiguration(gate)
57 stack, err := virtualnetwork.New(&configuration)
58 if err != nil {
59 gate.Close("容器网络初始化失败")
60 return nil, err
61 }
62 return &Network{Gate: gate, stack: stack}, nil
63}
64
65// Run consumes the documented four-byte big-endian frame length followed by
66// an Ethernet frame. AcceptQemu names the wire format, not a VM dependency.
67func (n *Network) Run(ctx context.Context, endpoint net.Conn) error {
68 stop := context.AfterFunc(ctx, func() { _ = endpoint.Close() })
69 defer stop()
70 defer n.Gate.Close("容器网络连接已断开")
71 defer endpoint.Close()
72 return n.stack.AcceptQemu(ctx, &boundedFrameConnection{Conn: endpoint})
73}
74
75// ServeSSH only accepts the listener supplied by the host. It never listens
76// inside the virtual gateway, so a guest cannot use this as a reverse proxy.
77func (n *Network) ServeSSH(ctx context.Context, listener net.Listener) error {
78 address, ok := listener.Addr().(*net.TCPAddr)
79 if !ok || !address.IP.IsLoopback() {
80 return errors.New("容器 SSH 入口必须监听本机回环地址")
81 }
82 ctx, cancel := context.WithCancel(ctx)
83 stop := context.AfterFunc(ctx, func() { _ = listener.Close() })
84 defer stop()
85 var active sync.WaitGroup
86 defer func() { cancel(); _ = listener.Close(); active.Wait() }()
87 for {
88 local, err := listener.Accept()
89 if err != nil {
90 if ctx.Err() != nil {
91 return ctx.Err()
92 }
93 return err
94 }
95 active.Add(1)
96 go func() {
97 defer active.Done()
98 defer local.Close()
99 cancelLocal := context.AfterFunc(ctx, func() { _ = local.Close() })
100 defer cancelLocal()
101 dialCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
102 remote, err := n.stack.DialContextTCP(dialCtx, GuestIP+":22")
103 cancel()
104 if err != nil {
105 return
106 }
107 defer remote.Close()
108 cancelRemote := context.AfterFunc(ctx, func() { _ = remote.Close() })
109 defer cancelRemote()
110 done := make(chan struct{})
111 go func() { _, _ = io.Copy(remote, local); _ = remote.Close(); _ = local.Close(); close(done) }()
112 _, _ = io.Copy(local, remote)
113 _ = local.Close()
114 _ = remote.Close()
115 <-done
116 }()
117 }
118}
119
120// ProcessEndpoint connects an exec -i process to the frame protocol. The
121// process must emit only frames on stdout; diagnostics belong to stderr.
122// Closing the endpoint terminates this process and releases both pipes.
123func ProcessEndpoint(command *exec.Cmd) (net.Conn, func() error, error) {
124 input, err := command.StdinPipe()
125 if err != nil {
126 return nil, nil, err
127 }
128 output, err := command.StdoutPipe()
129 if err != nil {
130 input.Close()
131 return nil, nil, err
132 }
133 if err = command.Start(); err != nil {
134 input.Close()
135 output.Close()
136 return nil, nil, err
137 }
138 endpoint, bridge := net.Pipe()
139 var closeOnce sync.Once
140 closeProcess := func() {
141 closeOnce.Do(func() { _ = input.Close(); _ = output.Close(); _ = bridge.Close(); _ = command.Process.Kill() })
142 }
143 var copies sync.WaitGroup
144 copies.Add(2)
145 go func() { defer copies.Done(); defer closeProcess(); _, _ = io.Copy(input, bridge) }()
146 go func() { defer copies.Done(); defer closeProcess(); _, _ = io.Copy(bridge, output) }()
147 var waitOnce sync.Once
148 var waitErr error
149 wait := func() error {
150 waitOnce.Do(func() { _ = endpoint.Close(); closeProcess(); copies.Wait(); waitErr = command.Wait() })
151 return waitErr
152 }
153 return &processConnection{Conn: endpoint, closeProcess: closeProcess}, wait, nil
154}
155
156type processConnection struct {
157 net.Conn
158 closeProcess func()
159}
160
161func (c *processConnection) Close() error { err := c.Conn.Close(); c.closeProcess(); return err }