| 1 | |
| 2 | |
| 3 | |
| 4 | package portfwd |
| 5 | |
| 6 | import ( |
| 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 | |
| 19 | var IPv4loopback1 = limayaml.IPv4loopback1 |
| 20 | |
| 21 | type Forwarder struct { |
| 22 | rules []limatype.PortForward |
| 23 | ignoreTCP bool |
| 24 | ignoreUDP bool |
| 25 | closableListeners *ClosableListeners |
| 26 | onEvent func(*events.PortForwardEvent) |
| 27 | } |
| 28 | |
| 29 | func 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 | |
| 39 | func (fw *Forwarder) emitEvent(ev *events.PortForwardEvent) { |
| 40 | if fw.onEvent != nil { |
| 41 | fw.onEvent(ev) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | func (fw *Forwarder) Close() error { |
| 46 | return fw.closableListeners.Close() |
| 47 | } |
| 48 | |
| 49 | func (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 | |
| 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 | |
| 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 | |
| 114 | func (fw *Forwarder) forwardingAddresses(guest *api.IPPort) (hostAddr, guestAddr string) { |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 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 | |
| 140 | |
| 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 | |
| 155 | func (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 | |
| 167 | func 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 | |
| 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 | } |