Skip to content

Commit

Permalink
Suppress "Not forwarding …" messages when forwarding has been disabled
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Dubois <jan.dubois@suse.com>
  • Loading branch information
jandubois committed Sep 9, 2024
1 parent 52f5ad3 commit a053653
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
20 changes: 18 additions & 2 deletions pkg/hostagent/hostagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,22 @@ func New(instName string, stdout io.Writer, signalCh chan os.Signal, opts ...Opt
AdditionalArgs: sshutil.SSHArgsFromOpts(sshOpts),
}

ignoreTCP := false
ignoreUDP := false
for _, rule := range y.PortForwards {
if rule.Ignore && rule.GuestPortRange[0] == 1 && rule.GuestPortRange[1] == 65535 {
switch rule.Proto {
case limayaml.TCP:
ignoreTCP = true
logrus.Info("TCP port forwarding is disabled (except for SSH)")
case limayaml.UDP:
ignoreUDP = true
logrus.Info("UDP port forwarding is disabled")
}
} else {
break
}
}
rules := make([]limayaml.PortForward, 0, 3+len(y.PortForwards))
// Block ports 22 and sshLocalPort on all IPs
for _, port := range []int{sshGuestPort, sshLocalPort} {
Expand Down Expand Up @@ -188,8 +204,8 @@ func New(instName string, stdout io.Writer, signalCh chan os.Signal, opts ...Opt
instName: instName,
instSSHAddress: inst.SSHAddress,
sshConfig: sshConfig,
portForwarder: newPortForwarder(sshConfig, sshLocalPort, rules, inst.VMType),
grpcPortForwarder: portfwd.NewPortForwarder(rules),
portForwarder: newPortForwarder(sshConfig, sshLocalPort, rules, ignoreTCP, inst.VMType),
grpcPortForwarder: portfwd.NewPortForwarder(rules, ignoreTCP, ignoreUDP),
driver: limaDriver,
signalCh: signalCh,
eventEnc: json.NewEncoder(stdout),
Expand Down
8 changes: 6 additions & 2 deletions pkg/hostagent/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,20 @@ type portForwarder struct {
sshConfig *ssh.SSHConfig
sshHostPort int
rules []limayaml.PortForward
ignore bool
vmType limayaml.VMType
}

const sshGuestPort = 22

var IPv4loopback1 = limayaml.IPv4loopback1

func newPortForwarder(sshConfig *ssh.SSHConfig, sshHostPort int, rules []limayaml.PortForward, vmType limayaml.VMType) *portForwarder {
func newPortForwarder(sshConfig *ssh.SSHConfig, sshHostPort int, rules []limayaml.PortForward, ignore bool, vmType limayaml.VMType) *portForwarder {
return &portForwarder{
sshConfig: sshConfig,
sshHostPort: sshHostPort,
rules: rules,
ignore: ignore,
vmType: vmType,
}
}
Expand Down Expand Up @@ -94,7 +96,9 @@ func (pf *portForwarder) OnEvent(ctx context.Context, ev *api.Event) {
}
local, remote := pf.forwardingAddresses(f)
if local == "" {
logrus.Infof("Not forwarding TCP %s", remote)
if !pf.ignore {
logrus.Infof("Not forwarding TCP %s", remote)
}
continue
}
logrus.Infof("Forwarding TCP from %s to %s", remote, local)
Expand Down
1 change: 1 addition & 0 deletions pkg/limayaml/limayaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ type Proto = string

const (
TCP Proto = "tcp"
UDP Proto = "udp"
)

type PortForward struct {
Expand Down
18 changes: 15 additions & 3 deletions pkg/portfwd/forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,30 @@ var IPv4loopback1 = limayaml.IPv4loopback1

type Forwarder struct {
rules []limayaml.PortForward
ignoreTCP bool
ignoreUDP bool
closableListeners *ClosableListeners
}

func NewPortForwarder(rules []limayaml.PortForward) *Forwarder {
return &Forwarder{rules: rules, closableListeners: NewClosableListener()}
func NewPortForwarder(rules []limayaml.PortForward, ignoreTCP, ignoreUDP bool) *Forwarder {
return &Forwarder{
rules: rules,
ignoreTCP: ignoreTCP,
ignoreUDP: ignoreUDP,
closableListeners: NewClosableListener(),
}
}

func (fw *Forwarder) OnEvent(ctx context.Context, client *guestagentclient.GuestAgentClient, ev *api.Event) {
for _, f := range ev.LocalPortsAdded {
local, remote := fw.forwardingAddresses(f)
if local == "" {
logrus.Infof("Not forwarding %s %s", strings.ToUpper(f.Protocol), remote)
if !fw.ignoreTCP && f.Protocol == "tcp" {
logrus.Infof("Not forwarding TCP %s", remote)
}
if !fw.ignoreUDP && f.Protocol == "udp" {
logrus.Infof("Not forwarding UDP %s", remote)
}
continue
}
logrus.Infof("Forwarding %s from %s to %s", strings.ToUpper(f.Protocol), remote, local)
Expand Down

0 comments on commit a053653

Please sign in to comment.