跳到主要内容

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

浏览文件

forward.go

179 行 · 5.5 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package portfwd
5
6import (
7 "context"
8 "net"
9 "strings"
10
11 "github.com/sirupsen/logrus"
12
13 "github.com/lima-vm/lima/v2/pkg/guestagent/api"
14 "github.com/lima-vm/lima/v2/pkg/hostagent/events"
15 "github.com/lima-vm/lima/v2/pkg/limatype"
16 "github.com/lima-vm/lima/v2/pkg/limayaml"
17)
18
19var IPv4loopback1 = limayaml.IPv4loopback1
20
21type Forwarder struct {
22 rules []limatype.PortForward
23 ignoreTCP bool
24 ignoreUDP bool
25 closableListeners *ClosableListeners
26 onEvent func(*events.PortForwardEvent)
27}
28
29func NewPortForwarder(rules []limatype.PortForward, ignoreTCP, ignoreUDP bool, onEvent func(*events.PortForwardEvent)) *Forwarder {
30 return &Forwarder{
31 rules: rules,
32 ignoreTCP: ignoreTCP,
33 ignoreUDP: ignoreUDP,
34 closableListeners: NewClosableListener(),
35 onEvent: onEvent,
36 }
37}
38
39func (fw *Forwarder) emitEvent(ev *events.PortForwardEvent) {
40 if fw.onEvent != nil {
41 fw.onEvent(ev)
42 }
43}
44
45func (fw *Forwarder) Close() error {
46 return fw.closableListeners.Close()
47}
48
49func (fw *Forwarder) OnEvent(ctx context.Context, dialContext func(ctx context.Context, network string, addr string) (net.Conn, error), ev *api.Event) {
50 for _, f := range ev.AddedLocalPorts {
51 // Before forwarding, check if any static rule matches this port otherwise it will be forwarded twice and cause a port conflict
52 if fw.isPortStaticallyForwarded(f) {
53 continue
54 }
55 local, remote := fw.forwardingAddresses(f)
56 if local == "" {
57 if !fw.ignoreTCP && f.Protocol == "tcp" {
58 logrus.Infof("Not forwarding TCP %s", remote)
59 fw.emitEvent(&events.PortForwardEvent{
60 Type: events.PortForwardEventNotForwarding,
61 Protocol: f.Protocol,
62 GuestAddr: remote,
63 })
64 }
65 if !fw.ignoreUDP && f.Protocol == "udp" {
66 logrus.Infof("Not forwarding UDP %s", remote)
67 fw.emitEvent(&events.PortForwardEvent{
68 Type: events.PortForwardEventNotForwarding,
69 Protocol: f.Protocol,
70 GuestAddr: remote,
71 })
72 }
73 continue
74 }
75 // Never next to a program of the host on the same port (see inUse).
76 if !fw.closableListeners.has(f.Protocol, local, remote) {
77 if err := inUse(ctx, f.Protocol, local); err != nil {
78 logrus.Warnf("Not forwarding %s from %s to %s: %v", strings.ToUpper(f.Protocol), remote, local, err)
79 fw.emitEvent(&events.PortForwardEvent{
80 Type: events.PortForwardEventFailed,
81 Protocol: f.Protocol,
82 GuestAddr: remote,
83 HostAddr: local,
84 Error: err.Error(),
85 })
86 continue
87 }
88 }
89 logrus.Infof("Forwarding %s from %s to %s", strings.ToUpper(f.Protocol), remote, local)
90 fw.emitEvent(&events.PortForwardEvent{
91 Type: events.PortForwardEventForwarding,
92 Protocol: f.Protocol,
93 GuestAddr: remote,
94 HostAddr: local,
95 })
96 fw.closableListeners.Forward(ctx, dialContext, f.Protocol, local, remote)
97 }
98 for _, f := range ev.RemovedLocalPorts {
99 local, remote := fw.forwardingAddresses(f)
100 if local == "" {
101 continue
102 }
103 fw.emitEvent(&events.PortForwardEvent{
104 Type: events.PortForwardEventStopping,
105 Protocol: f.Protocol,
106 GuestAddr: remote,
107 HostAddr: local,
108 })
109 fw.closableListeners.Remove(ctx, f.Protocol, local, remote)
110 logrus.Debugf("Port forwarding closed proto:%s host:%s guest:%s", f.Protocol, local, remote)
111 }
112}
113
114func (fw *Forwarder) forwardingAddresses(guest *api.IPPort) (hostAddr, guestAddr string) {
115 // rule.Proto is limited to tcp, udp and any by limayaml validation, and the guest
116 // agent only ever reports tcp or udp. Without this check any other value would
117 // match none of the rules that name a protocol, so it would skip an `ignore` rule
118 // and then be forwarded by the catch-all rule the caller appends last.
119 if guest.Protocol != limatype.ProtoTCP && guest.Protocol != limatype.ProtoUDP {
120 logrus.Debugf("Not forwarding %s: unsupported protocol %#q", guest.HostString(), guest.Protocol)
121 return "", guest.HostString()
122 }
123 guestIP := net.ParseIP(guest.Ip)
124 for _, rule := range fw.rules {
125 if rule.GuestSocket != "" {
126 continue
127 }
128 if rule.Proto != limatype.ProtoAny && rule.Proto != guest.Protocol {
129 continue
130 }
131 if guest.Port < int32(rule.GuestPortRange[0]) || guest.Port > int32(rule.GuestPortRange[1]) {
132 continue
133 }
134 switch {
135 case guestIP.IsUnspecified():
136 case guestIP.Equal(rule.GuestIP):
137 case guestIP.Equal(net.IPv6loopback) && rule.GuestIP.Equal(IPv4loopback1):
138 case rule.GuestIP.IsUnspecified() && !*rule.GuestIPMustBeZero:
139 // When GuestIPMustBeZero is true, then 0.0.0.0 must be an exact match, which is already
140 // handled above by the guestIP.IsUnspecified() condition.
141 default:
142 continue
143 }
144 if rule.Ignore {
145 if guestIP.IsUnspecified() && !rule.GuestIP.IsUnspecified() {
146 continue
147 }
148 break
149 }
150 return hostAddress(rule, guest), guest.HostString()
151 }
152 return "", guest.HostString()
153}
154
155func (fw *Forwarder) isPortStaticallyForwarded(guest *api.IPPort) bool {
156 for _, rule := range fw.rules {
157 if !rule.Static {
158 continue
159 }
160 if guest.Port >= int32(rule.GuestPortRange[0]) && guest.Port <= int32(rule.GuestPortRange[1]) {
161 return true
162 }
163 }
164 return false
165}
166
167func hostAddress(rule limatype.PortForward, guest *api.IPPort) string {
168 if rule.HostSocket != "" {
169 return rule.HostSocket
170 }
171 host := &api.IPPort{Ip: rule.HostIP.String()}
172 if guest.Port == 0 {
173 // guest is a socket
174 host.Port = int32(rule.HostPort)
175 } else {
176 host.Port = guest.Port + int32(rule.HostPortRange[0]-rule.GuestPortRange[0])
177 }
178 return host.HostString()
179}