跳到主要内容

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

浏览文件

0004-cage-proxy-egress.patch

153 行 · 5.5 KB历史下载
1Subject: [PATCH] Route cage TCP and DNS through host-owned transports
2
3Optional transport hooks retain the upstream default. DNS exchange failures
4return SERVFAIL without invoking the host resolver. Cage supplies a proxy-only
5TCP dialer and DoH exchange outside the guest.
6
7---
8diff --git a/pkg/services/dns/dns.go b/pkg/services/dns/dns.go
9index 968331a..3ee284a 100644
10--- a/pkg/services/dns/dns.go
11+++ b/pkg/services/dns/dns.go
12@@ -24,6 +24,7 @@ type upstreamResolver interface {
13 }
14
15 type dnsHandler struct {
16+ exchange func(context.Context, *dns.Msg) (*dns.Msg, error)
17 zones []types.Zone
18 zonesLock sync.RWMutex
19 upstream upstreamResolver
20@@ -36,7 +37,16 @@ func (h *dnsHandler) handle(w dns.ResponseWriter, r *dns.Msg, responseMessageSiz
21 m := new(dns.Msg)
22 m.SetReply(r)
23 m.RecursionAvailable = true
24- h.addAnswers(m)
25+ if h.exchange != nil {
26+ answer, err := h.exchange(context.Background(), r)
27+ if err != nil {
28+ m.SetRcode(r, dns.RcodeServerFailure)
29+ } else {
30+ m = answer
31+ }
32+ } else {
33+ h.addAnswers(m)
34+ }
35 edns0 := r.IsEdns0()
36 if edns0 != nil {
37 responseMessageSize = int(edns0.UDPSize())
38@@ -332,3 +342,8 @@ func (s *Server) addZone(req types.Zone) {
39 // No existing zone for req.Name, add new one
40 s.handler.zones = append(s.handler.zones, req)
41 }
42+
43+// SetExchange replaces all upstream DNS resolution; errors never fall back.
44+func (s *Server) SetExchange(exchange func(context.Context, *dns.Msg) (*dns.Msg, error)) {
45+ s.handler.exchange = exchange
46+}
47diff --git a/pkg/services/forwarder/tcp.go b/pkg/services/forwarder/tcp.go
48index f6b78ba..b1d2219 100644
49--- a/pkg/services/forwarder/tcp.go
50+++ b/pkg/services/forwarder/tcp.go
51@@ -19,7 +19,7 @@ import (
52
53 const linkLocalSubnet = "169.254.0.0/16"
54
55-func TCP(s *stack.Stack, nat map[tcpip.Address]tcpip.Address, natLock *sync.Mutex, ec2MetadataAccess bool, outbound types.Outbound) *tcp.Forwarder {
56+func TCP(s *stack.Stack, nat map[tcpip.Address]tcpip.Address, natLock *sync.Mutex, ec2MetadataAccess bool, outbound types.Outbound, dialers ...types.DialContext) *tcp.Forwarder {
57 return tcp.NewForwarder(s, 0, 10, func(r *tcp.ForwarderRequest) {
58 localAddress := r.ID().LocalAddress
59
60@@ -37,7 +37,11 @@ func TCP(s *stack.Stack, nat map[tcpip.Address]tcpip.Address, natLock *sync.Mute
61 r.Complete(true)
62 return
63 }
64- outbound, err := net.Dial("tcp", net.JoinHostPort(localAddress.String(), fmt.Sprint(r.ID().LocalPort)))
65+ dial := (&net.Dialer{}).DialContext
66+ if len(dialers) > 0 && dialers[0] != nil {
67+ dial = dialers[0]
68+ }
69+ outbound, err := dial(context.Background(), "tcp", net.JoinHostPort(localAddress.String(), fmt.Sprint(r.ID().LocalPort)))
70 if err != nil {
71 log.Tracef("net.Dial() = %v", err)
72 r.Complete(true)
73diff --git a/pkg/types/configuration.go b/pkg/types/configuration.go
74index c5a2902..755a866 100644
75--- a/pkg/types/configuration.go
76+++ b/pkg/types/configuration.go
77@@ -1,6 +1,8 @@
78 package types
79
80 import (
81+ "context"
82+ "github.com/miekg/dns"
83 "net"
84 "net/netip"
85 "regexp"
86@@ -15,7 +17,11 @@ func (o Outbound) Allows(network string, dst netip.AddrPort) bool {
87 return o == nil || o(network, dst)
88 }
89
90+type DialContext func(context.Context, string, string) (net.Conn, error)
91+
92 type Configuration struct {
93+ DialContext DialContext `yaml:"-"`
94+ DNSExchange func(context.Context, *dns.Msg) (*dns.Msg, error) `yaml:"-"`
95 // Print packets on stderr
96 Debug bool `yaml:"debug,omitempty"`
97
98diff --git a/pkg/virtualnetwork/services.go b/pkg/virtualnetwork/services.go
99index 48f9e76..887bdab 100644
100--- a/pkg/virtualnetwork/services.go
101+++ b/pkg/virtualnetwork/services.go
102@@ -24,7 +24,7 @@ func addServices(configuration *types.Configuration, s *stack.Stack, ipPool *tap
103 var natLock sync.Mutex
104 translation := parseNATTable(configuration)
105
106- tcpForwarder := forwarder.TCP(s, translation, &natLock, configuration.Ec2MetadataAccess, configuration.Outbound)
107+ tcpForwarder := forwarder.TCP(s, translation, &natLock, configuration.Ec2MetadataAccess, configuration.Outbound, configuration.DialContext)
108 s.SetTransportProtocolHandler(tcp.ProtocolNumber, tcpForwarder.HandlePacket)
109 udpForwarder := forwarder.UDP(s, translation, &natLock, configuration.Ec2MetadataAccess, configuration.Outbound)
110 s.SetTransportProtocolHandler(udp.ProtocolNumber, udpForwarder.HandlePacket)
111@@ -83,6 +83,7 @@ func dnsServer(configuration *types.Configuration, s *stack.Stack) (http.Handler
112 }
113
114 server.SetPolicy(configuration.DNSNames, configuration.DNSAnswered)
115+ server.SetExchange(configuration.DNSExchange)
116
117 go func() {
118 if err := server.Serve(); err != nil {
119diff --git a/pkg/services/dns/cage_test.go b/pkg/services/dns/cage_test.go
120new file mode 100644
121index 0000000..21ffa81
122--- /dev/null
123+++ b/pkg/services/dns/cage_test.go
124@@ -0,0 +1,29 @@
125+// SPDX-License-Identifier: Apache-2.0
126+package dns
127+
128+import (
129+ "context"
130+ "errors"
131+ "testing"
132+
133+ "github.com/miekg/dns"
134+)
135+
136+type cageWriter struct {
137+ dns.ResponseWriter
138+ msg *dns.Msg
139+}
140+
141+func (w *cageWriter) WriteMsg(m *dns.Msg) error { w.msg = m; return nil }
142+
143+func TestCageDNSFailureDoesNotUseHostResolver(t *testing.T) {
144+ q := new(dns.Msg)
145+ q.SetQuestion("example.com.", dns.TypeA)
146+ w := &cageWriter{}
147+ // A nil upstream would panic if the ordinary resolver path were used.
148+ h := &dnsHandler{exchange: func(context.Context, *dns.Msg) (*dns.Msg, error) { return nil, errors.New("proxy down") }}
149+ h.handle(w, q, dns.MaxMsgSize)
150+ if w.msg == nil || w.msg.Rcode != dns.RcodeServerFailure {
151+ t.Fatal("expected fail-closed SERVFAIL")
152+ }
153+}