跳到主要内容

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

浏览文件

0010-cage-proxy-egress.patch

109 行 · 3.7 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/hostagent/dns/dns.go b/pkg/hostagent/dns/dns.go
9index 7d1e75d..fd676b8 100644
10--- a/pkg/hostagent/dns/dns.go
11+++ b/pkg/hostagent/dns/dns.go
12@@ -150,7 +150,20 @@ func NewHandler(opts HandlerOptions) (dns.Handler, error) {
13 return h, nil
14 }
15
16+// Exchange overrides upstream resolution; a failed exchange returns SERVFAIL.
17+var Exchange func(context.Context, *dns.Msg) (*dns.Msg, error)
18+
19 func (h *Handler) handleQuery(ctx context.Context, w dns.ResponseWriter, req *dns.Msg) {
20+ if Exchange != nil {
21+ defer w.Close()
22+ answer, err := Exchange(ctx, req)
23+ if err != nil {
24+ answer = new(dns.Msg)
25+ answer.SetRcode(req, dns.RcodeServerFailure)
26+ }
27+ _ = w.WriteMsg(answer)
28+ return
29+ }
30 var (
31 reply dns.Msg
32 handled bool
33diff --git a/pkg/networks/usernet/gvproxy.go b/pkg/networks/usernet/gvproxy.go
34index ff4260c..90254db 100644
35--- a/pkg/networks/usernet/gvproxy.go
36+++ b/pkg/networks/usernet/gvproxy.go
37@@ -22,6 +22,7 @@ import (
38 "github.com/containers/gvisor-tap-vsock/pkg/transport"
39 "github.com/containers/gvisor-tap-vsock/pkg/types"
40 "github.com/containers/gvisor-tap-vsock/pkg/virtualnetwork"
41+ "github.com/miekg/dns"
42 "github.com/sirupsen/logrus"
43 "golang.org/x/sync/errgroup"
44 )
45@@ -54,6 +55,23 @@ const gatewayMacAddr = "5a:94:ef:e4:0c:dd"
46 // The host services the guest needs (AllowHostService) are reached past it.
47 var Outbound types.Outbound
48
49+// GuestDial and DNSExchange are host-owned transport hooks.
50+var GuestDial types.DialContext
51+var DNSExchange func(context.Context, *dns.Msg) (*dns.Msg, error)
52+
53+func dialGuest(ctx context.Context, network, addr string) (net.Conn, error) {
54+ if GuestDial == nil {
55+ return (&net.Dialer{}).DialContext(ctx, network, addr)
56+ }
57+ dst, err := netip.ParseAddrPort(addr)
58+ if err == nil && dst.Addr().Unmap() == netip.MustParseAddr("127.0.0.1") {
59+ if _, ok := hostServices.Load(hostService{network, dst.Port()}); ok {
60+ return (&net.Dialer{}).DialContext(ctx, network, addr)
61+ }
62+ }
63+ return GuestDial(ctx, network, addr)
64+}
65+
66 // Names and Answered, when set by the program running the hostagent, are
67 // applied by the gateway's DNS server of the per-instance netstacks to every
68 // query it gets (types.Configuration.DNSNames and DNSAnswered).
69@@ -125,6 +143,8 @@ func StartGVisorNetstack(ctx context.Context, gVisorOpts *GVisorNetstackOpts) er
70 },
71 GatewayVirtualIPs: []string{gatewayIP},
72 Outbound: outbound,
73+ DialContext: dialGuest,
74+ DNSExchange: DNSExchange,
75 DNSNames: Names,
76 DNSAnswered: Answered,
77 }
78diff --git a/pkg/hostagent/dns/cage_test.go b/pkg/hostagent/dns/cage_test.go
79new file mode 100644
80index 0000000..5e16ce3
81--- /dev/null
82+++ b/pkg/hostagent/dns/cage_test.go
83@@ -0,0 +1,26 @@
84+// SPDX-License-Identifier: Apache-2.0
85+package dns
86+
87+import (
88+ "context"
89+ "errors"
90+ "testing"
91+
92+ "github.com/miekg/dns"
93+)
94+
95+func TestCageDNSFailureDoesNotUseStaticOrHostResolver(t *testing.T) {
96+ previous := Exchange
97+ t.Cleanup(func() { Exchange = previous })
98+ Exchange = func(context.Context, *dns.Msg) (*dns.Msg, error) { return nil, errors.New("proxy down") }
99+ h, err := NewHandler(HandlerOptions{StaticHosts: map[string]string{"example.com": "1.1.1.1"}})
100+ if err != nil {
101+ t.Fatal(err)
102+ }
103+ q := new(dns.Msg)
104+ q.SetQuestion("example.com.", dns.TypeA)
105+ h.ServeDNS(TestResponseWriter{}, q)
106+ if dnsResult.Rcode != dns.RcodeServerFailure || len(dnsResult.Answer) != 0 {
107+ t.Fatal("expected SERVFAIL without static/host fallback")
108+ }
109+}