| 1 | |
| 2 | |
| 3 | |
| 4 | package hostagent |
| 5 | |
| 6 | import ( |
| 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 | |
| 19 | type 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 | |
| 28 | const sshGuestPort = 22 |
| 29 | |
| 30 | var IPv4loopback1 = limayaml.IPv4loopback1 |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | func 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 | |
| 39 | |
| 40 | |
| 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 | |
| 47 | rule := limatype.PortForward{} |
| 48 | limayaml.FillPortForwardDefaults(&rule, instDir, user, param) |
| 49 | return append(rules, rule) |
| 50 | } |
| 51 | |
| 52 | func 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 | |
| 63 | func (pf *portForwarder) emitEvent(ev *events.PortForwardEvent) { |
| 64 | if pf.onEvent != nil { |
| 65 | pf.onEvent(ev) |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | func 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 | |
| 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 | |
| 83 | func (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 | |
| 103 | |
| 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 | |
| 118 | func (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 | } |