Skip to content

Commit c6c3c1e

Browse files
committed
pkg/hostagent: Support static port forwarding to direct guest IP
Since port forwarding to the guest IP directly does not require guest agent, we can support static port forwarding if `DialContextToGuestIP()` returns non-nil. pkg/portfwd: - Add `Forwarder.Forward()` pkg/hostagent: - Use `a.grpcPortForwarder.Forward()` if `DialContextToGuestIP()` returns non-nil. Signed-off-by: Norio Nomura <norio.nomura@gmail.com>
1 parent 4954852 commit c6c3c1e

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

pkg/hostagent/hostagent.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,9 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
772772
}
773773

774774
func (a *HostAgent) addStaticPortForwardsFromList(ctx context.Context, staticPortForwards []limatype.PortForward) {
775+
// Since port forwarding to the guest IP directly does not require guest agent,
776+
// we can support static port forwarding if DialContextToGuestIP() returns non-nil.
777+
dialContext := a.DialContextToGuestIP()
775778
sshAddress, sshPort := a.sshAddressPort()
776779
for _, rule := range staticPortForwards {
777780
if rule.GuestSocket == "" {
@@ -783,8 +786,21 @@ func (a *HostAgent) addStaticPortForwardsFromList(ctx context.Context, staticPor
783786
local, remote := a.portForwarder.forwardingAddresses(guest)
784787
if local != "" {
785788
logrus.Infof("Setting up static TCP forwarding from %s to %s", remote, local)
786-
if err := forwardTCP(ctx, a.sshConfig, sshAddress, sshPort, local, remote, verbForward); err != nil {
787-
logrus.WithError(err).Warnf("failed to set up static TCP forwarding %s -> %s", remote, local)
789+
if dialContext != nil {
790+
switch rule.Proto {
791+
case "tcp":
792+
a.grpcPortForwarder.Forward(ctx, dialContext, rule.Proto, local, remote)
793+
case "udp":
794+
a.grpcPortForwarder.Forward(ctx, dialContext, rule.Proto, local, remote)
795+
case "any":
796+
a.grpcPortForwarder.Forward(ctx, dialContext, "tcp", local, remote)
797+
a.grpcPortForwarder.Forward(ctx, dialContext, "udp", local, remote)
798+
}
799+
} else {
800+
// fallback to SSH-based port forwarding
801+
if err := forwardTCP(ctx, a.sshConfig, sshAddress, sshPort, local, remote, verbForward); err != nil {
802+
logrus.WithError(err).Warnf("failed to set up static TCP forwarding %s -> %s", remote, local)
803+
}
788804
}
789805
}
790806
}

pkg/portfwd/forward.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ func (fw *Forwarder) OnEvent(ctx context.Context, dialContext func(ctx context.C
5353
}
5454
continue
5555
}
56-
logrus.Infof("Forwarding %s from %s to %s", strings.ToUpper(f.Protocol), remote, local)
57-
fw.closableListeners.Forward(ctx, dialContext, f.Protocol, local, remote)
56+
fw.Forward(ctx, dialContext, f.Protocol, local, remote)
5857
}
5958
for _, f := range ev.RemovedLocalPorts {
6059
local, remote := fw.forwardingAddresses(f)
@@ -66,6 +65,11 @@ func (fw *Forwarder) OnEvent(ctx context.Context, dialContext func(ctx context.C
6665
}
6766
}
6867

68+
func (fw *Forwarder) Forward(ctx context.Context, dialContext func(ctx context.Context, network string, addr string) (net.Conn, error), protocol, local, remote string) {
69+
logrus.Infof("Forwarding %s from %s to %s", strings.ToUpper(protocol), remote, local)
70+
fw.closableListeners.Forward(ctx, dialContext, protocol, local, remote)
71+
}
72+
6973
func (fw *Forwarder) forwardingAddresses(guest *api.IPPort) (hostAddr, guestAddr string) {
7074
guestIP := net.ParseIP(guest.Ip)
7175
for _, rule := range fw.rules {

0 commit comments

Comments
 (0)