跳到主要内容

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

浏览文件

link.go

146 行 · 3.6 KB历史下载
1package tap
2
3import (
4 "net"
5
6 "github.com/google/gopacket"
7 "github.com/google/gopacket/layers"
8 log "github.com/sirupsen/logrus"
9 "gvisor.dev/gvisor/pkg/tcpip"
10 "gvisor.dev/gvisor/pkg/tcpip/header"
11 "gvisor.dev/gvisor/pkg/tcpip/stack"
12)
13
14type LinkEndpoint struct {
15 debug bool
16 mtu uint32
17 mac tcpip.LinkAddress
18 ip string
19 virtualIPs map[string]struct{}
20
21 dispatcher stack.NetworkDispatcher
22 networkSwitch NetworkSwitch
23}
24
25func NewLinkEndpoint(debug bool, mtu uint32, macAddress string, ip string, virtualIPs []string) (*LinkEndpoint, error) {
26 linkAddr, err := net.ParseMAC(macAddress)
27 if err != nil {
28 return nil, err
29 }
30 set := make(map[string]struct{})
31 for _, virtualIP := range virtualIPs {
32 set[virtualIP] = struct{}{}
33 }
34 return &LinkEndpoint{
35 debug: debug,
36 mtu: mtu,
37 mac: tcpip.LinkAddress(linkAddr),
38 ip: ip,
39 virtualIPs: set,
40 }, nil
41}
42
43func (e *LinkEndpoint) ARPHardwareType() header.ARPHardwareType {
44 return header.ARPHardwareEther
45}
46
47func (e *LinkEndpoint) Connect(networkSwitch NetworkSwitch) {
48 e.networkSwitch = networkSwitch
49}
50
51func (e *LinkEndpoint) Attach(dispatcher stack.NetworkDispatcher) {
52 e.dispatcher = dispatcher
53}
54
55func (e *LinkEndpoint) IsAttached() bool {
56 return e.dispatcher != nil
57}
58
59func (e *LinkEndpoint) DeliverNetworkPacket(protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) {
60 e.dispatcher.DeliverNetworkPacket(protocol, pkt)
61}
62
63func (e *LinkEndpoint) AddHeader(_ *stack.PacketBuffer) {
64}
65
66func (e *LinkEndpoint) ParseHeader(*stack.PacketBuffer) bool { return true }
67
68func (e *LinkEndpoint) Capabilities() stack.LinkEndpointCapabilities {
69 return stack.CapabilityResolutionRequired | stack.CapabilityRXChecksumOffload
70}
71
72func (e *LinkEndpoint) LinkAddress() tcpip.LinkAddress {
73 return e.mac
74}
75
76func (e *LinkEndpoint) SetLinkAddress(addr tcpip.LinkAddress) {
77 e.mac = addr
78}
79
80func (e *LinkEndpoint) MaxHeaderLength() uint16 {
81 return uint16(header.EthernetMinimumSize)
82}
83
84func (e *LinkEndpoint) MTU() uint32 {
85 return e.mtu
86}
87
88func (e *LinkEndpoint) SetMTU(mtu uint32) {
89 e.mtu = mtu
90}
91
92func (e *LinkEndpoint) Wait() {}
93func (e *LinkEndpoint) Close() {}
94func (e *LinkEndpoint) SetOnCloseAction(_ func()) {}
95
96func (e *LinkEndpoint) WritePackets(pkts stack.PacketBufferList) (int, tcpip.Error) {
97 n := 0
98 for _, p := range pkts.AsSlice() {
99 if err := e.writePacket(p.EgressRoute, p.NetworkProtocolNumber, p); err != nil {
100 return n, err
101 }
102 n++
103 }
104 return n, nil
105}
106
107func (e *LinkEndpoint) writePacket(r stack.RouteInfo, protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) tcpip.Error {
108 // Preserve the src address if it's set in the route.
109 srcAddr := e.LinkAddress()
110 if r.LocalLinkAddress != "" {
111 srcAddr = r.LocalLinkAddress
112 }
113 eth := header.Ethernet(pkt.LinkHeader().Push(header.EthernetMinimumSize))
114 eth.Encode(&header.EthernetFields{
115 Type: protocol,
116 SrcAddr: srcAddr,
117 DstAddr: r.RemoteLinkAddress,
118 })
119
120 h := header.ARP(pkt.NetworkHeader().Slice())
121 if h.IsValid() &&
122 h.Op() == header.ARPReply {
123 ip := tcpip.AddrFromSlice(h.ProtocolAddressSender()).String()
124 _, ok := e.virtualIPs[ip]
125 if ip != e.IP() && !ok {
126 log.Debugf("dropping spoofing packets from the gateway about IP %s", ip)
127 return nil
128 }
129 }
130
131 if e.debug {
132 packet := gopacket.NewPacket(pkt.ToView().AsSlice(), layers.LayerTypeEthernet, gopacket.Default)
133 log.Info(packet.String())
134 }
135
136 e.networkSwitch.DeliverNetworkPacket(protocol, pkt)
137 return nil
138}
139
140func (e *LinkEndpoint) WriteRawPacket(_ *stack.PacketBuffer) tcpip.Error {
141 return &tcpip.ErrNotSupported{}
142}
143
144func (e *LinkEndpoint) IP() string {
145 return e.ip
146}