跳到主要内容

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

浏览文件

port.go

166 行 · 5.2 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package hostagent
5
6import (
7 "context"
8 "net"
9
10 "github.com/lima-vm/sshocker/pkg/ssh"
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
19type portForwarder struct {
20 sshConfig *ssh.SSHConfig
21 sshAddressPort func() (string, int)
22 rules []limatype.PortForward
23 ignore bool
24 vmType limatype.VMType
25 onEvent func(*events.PortForwardEvent)
26}
27
28const sshGuestPort = 22
29
30var IPv4loopback1 = limayaml.IPv4loopback1
31
32// portForwardRules returns the rules used by both forwarders: the SSH ports are
33// blocked on every guest IP, then the instance rules, then the default forward
34// for the remaining ports.
35func portForwardRules(instDir string, user limatype.User, param map[string]string, sshLocalPort int, instRules []limatype.PortForward) []limatype.PortForward {
36 rules := make([]limatype.PortForward, 0, 3+len(instRules))
37 for _, port := range []int{sshGuestPort, sshLocalPort} {
38 // GuestIPMustBeZero must be set explicitly. FillPortForwardDefaults would otherwise
39 // derive it from GuestIP and turn the rule into an exact 0.0.0.0 match, which leaves
40 // a guest listening on 127.0.0.1 or ::1 unblocked.
41 rule := limatype.PortForward{GuestIP: net.IPv4zero, GuestIPMustBeZero: new(false), GuestPort: port, Ignore: true}
42 limayaml.FillPortForwardDefaults(&rule, instDir, user, param)
43 rules = append(rules, rule)
44 }
45 rules = append(rules, instRules...)
46 // Default forwards for all non-privileged ports from "127.0.0.1" and "::1"
47 rule := limatype.PortForward{}
48 limayaml.FillPortForwardDefaults(&rule, instDir, user, param)
49 return append(rules, rule)
50}
51
52func newPortForwarder(sshConfig *ssh.SSHConfig, sshAddressPort func() (string, int), rules []limatype.PortForward, ignore bool, vmType limatype.VMType, onEvent func(*events.PortForwardEvent)) *portForwarder {
53 return &portForwarder{
54 sshConfig: sshConfig,
55 sshAddressPort: sshAddressPort,
56 rules: rules,
57 ignore: ignore,
58 vmType: vmType,
59 onEvent: onEvent,
60 }
61}
62
63func (pf *portForwarder) emitEvent(ev *events.PortForwardEvent) {
64 if pf.onEvent != nil {
65 pf.onEvent(ev)
66 }
67}
68
69func hostAddress(rule limatype.PortForward, guest *api.IPPort) string {
70 if rule.HostSocket != "" {
71 return rule.HostSocket
72 }
73 host := &api.IPPort{Ip: rule.HostIP.String()}
74 if guest.Port == 0 {
75 // guest is a socket
76 host.Port = int32(rule.HostPort)
77 } else {
78 host.Port = guest.Port + int32(rule.HostPortRange[0]-rule.GuestPortRange[0])
79 }
80 return host.HostString()
81}
82
83func (pf *portForwarder) forwardingAddresses(guest *api.IPPort) (hostAddr, guestAddr string) {
84 guestIP := net.ParseIP(guest.Ip)
85 for _, rule := range pf.rules {
86 if rule.GuestSocket != "" {
87 continue
88 }
89 switch rule.Proto {
90 case limatype.ProtoTCP, limatype.ProtoAny:
91 default:
92 continue
93 }
94 if guest.Port < int32(rule.GuestPortRange[0]) || guest.Port > int32(rule.GuestPortRange[1]) {
95 continue
96 }
97 switch {
98 case guestIP.IsUnspecified():
99 case guestIP.Equal(rule.GuestIP):
100 case guestIP.Equal(net.IPv6loopback) && rule.GuestIP.Equal(IPv4loopback1):
101 case rule.GuestIP.IsUnspecified() && !*rule.GuestIPMustBeZero:
102 // When GuestIPMustBeZero is true, then 0.0.0.0 must be an exact match, which is already
103 // handled above by the guest.IP.IsUnspecified() condition.
104 default:
105 continue
106 }
107 if rule.Ignore {
108 if guestIP.IsUnspecified() && !rule.GuestIP.IsUnspecified() {
109 continue
110 }
111 break
112 }
113 return hostAddress(rule, guest), guest.HostString()
114 }
115 return "", guest.HostString()
116}
117
118func (pf *portForwarder) OnEvent(ctx context.Context, ev *api.Event) {
119 sshAddress, sshPort := pf.sshAddressPort()
120 for _, f := range ev.RemovedLocalPorts {
121 if f.Protocol != "tcp" {
122 continue
123 }
124 local, remote := pf.forwardingAddresses(f)
125 if local == "" {
126 continue
127 }
128 logrus.Infof("Stopping forwarding TCP from %s to %s", remote, local)
129 pf.emitEvent(&events.PortForwardEvent{
130 Type: events.PortForwardEventStopping,
131 Protocol: "tcp",
132 GuestAddr: remote,
133 HostAddr: local,
134 })
135 if err := forwardTCP(ctx, pf.sshConfig, sshAddress, sshPort, local, remote, verbCancel); err != nil {
136 logrus.WithError(err).Warnf("failed to stop forwarding tcp port %d", f.Port)
137 }
138 }
139 for _, f := range ev.AddedLocalPorts {
140 if f.Protocol != "tcp" {
141 continue
142 }
143 local, remote := pf.forwardingAddresses(f)
144 if local == "" {
145 if !pf.ignore {
146 logrus.Infof("Not forwarding TCP %s", remote)
147 pf.emitEvent(&events.PortForwardEvent{
148 Type: events.PortForwardEventNotForwarding,
149 Protocol: "tcp",
150 GuestAddr: remote,
151 })
152 }
153 continue
154 }
155 logrus.Infof("Forwarding TCP from %s to %s", remote, local)
156 pf.emitEvent(&events.PortForwardEvent{
157 Type: events.PortForwardEventForwarding,
158 Protocol: "tcp",
159 GuestAddr: remote,
160 HostAddr: local,
161 })
162 if err := forwardTCP(ctx, pf.sshConfig, sshAddress, sshPort, local, remote, verbForward); err != nil {
163 logrus.WithError(err).Warnf("failed to set up forwarding tcp port %d (negligible if already forwarded)", f.Port)
164 }
165 }
166}