Skip to content

Commit f01757c

Browse files
committed
Suppress "Not forwarding …" messages when forwarding has been disabled
Signed-off-by: Jan Dubois <[email protected]>
1 parent 52f5ad3 commit f01757c

File tree

4 files changed

+40
-7
lines changed

4 files changed

+40
-7
lines changed

pkg/hostagent/hostagent.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,22 @@ func New(instName string, stdout io.Writer, signalCh chan os.Signal, opts ...Opt
153153
AdditionalArgs: sshutil.SSHArgsFromOpts(sshOpts),
154154
}
155155

156+
ignoreTCP := false
157+
ignoreUDP := false
158+
for _, rule := range y.PortForwards {
159+
if rule.Ignore && rule.GuestPortRange[0] == 1 && rule.GuestPortRange[1] == 65535 {
160+
switch rule.Proto {
161+
case limayaml.TCP:
162+
ignoreTCP = true
163+
logrus.Info("TCP port forwarding is disabled (except for SSH)")
164+
case limayaml.UDP:
165+
ignoreUDP = true
166+
logrus.Info("UDP port forwarding is disabled")
167+
}
168+
} else {
169+
break
170+
}
171+
}
156172
rules := make([]limayaml.PortForward, 0, 3+len(y.PortForwards))
157173
// Block ports 22 and sshLocalPort on all IPs
158174
for _, port := range []int{sshGuestPort, sshLocalPort} {
@@ -188,8 +204,8 @@ func New(instName string, stdout io.Writer, signalCh chan os.Signal, opts ...Opt
188204
instName: instName,
189205
instSSHAddress: inst.SSHAddress,
190206
sshConfig: sshConfig,
191-
portForwarder: newPortForwarder(sshConfig, sshLocalPort, rules, inst.VMType),
192-
grpcPortForwarder: portfwd.NewPortForwarder(rules),
207+
portForwarder: newPortForwarder(sshConfig, sshLocalPort, rules, ignoreTCP, inst.VMType),
208+
grpcPortForwarder: portfwd.NewPortForwarder(rules, ignoreTCP, ignoreUDP),
193209
driver: limaDriver,
194210
signalCh: signalCh,
195211
eventEnc: json.NewEncoder(stdout),

pkg/hostagent/port.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,20 @@ type portForwarder struct {
1414
sshConfig *ssh.SSHConfig
1515
sshHostPort int
1616
rules []limayaml.PortForward
17+
ignore bool
1718
vmType limayaml.VMType
1819
}
1920

2021
const sshGuestPort = 22
2122

2223
var IPv4loopback1 = limayaml.IPv4loopback1
2324

24-
func newPortForwarder(sshConfig *ssh.SSHConfig, sshHostPort int, rules []limayaml.PortForward, vmType limayaml.VMType) *portForwarder {
25+
func newPortForwarder(sshConfig *ssh.SSHConfig, sshHostPort int, rules []limayaml.PortForward, ignore bool, vmType limayaml.VMType) *portForwarder {
2526
return &portForwarder{
2627
sshConfig: sshConfig,
2728
sshHostPort: sshHostPort,
2829
rules: rules,
30+
ignore: ignore,
2931
vmType: vmType,
3032
}
3133
}
@@ -94,7 +96,9 @@ func (pf *portForwarder) OnEvent(ctx context.Context, ev *api.Event) {
9496
}
9597
local, remote := pf.forwardingAddresses(f)
9698
if local == "" {
97-
logrus.Infof("Not forwarding TCP %s", remote)
99+
if !pf.ignore {
100+
logrus.Infof("Not forwarding TCP %s", remote)
101+
}
98102
continue
99103
}
100104
logrus.Infof("Forwarding TCP from %s to %s", remote, local)

pkg/limayaml/limayaml.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ type Proto = string
214214

215215
const (
216216
TCP Proto = "tcp"
217+
UDP Proto = "udp"
217218
)
218219

219220
type PortForward struct {

pkg/portfwd/forward.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,30 @@ var IPv4loopback1 = limayaml.IPv4loopback1
1515

1616
type Forwarder struct {
1717
rules []limayaml.PortForward
18+
ignoreTCP bool
19+
ignoreUDP bool
1820
closableListeners *ClosableListeners
1921
}
2022

21-
func NewPortForwarder(rules []limayaml.PortForward) *Forwarder {
22-
return &Forwarder{rules: rules, closableListeners: NewClosableListener()}
23+
func NewPortForwarder(rules []limayaml.PortForward, ignoreTCP bool, ignoreUDP bool) *Forwarder {
24+
return &Forwarder{
25+
rules: rules,
26+
ignoreTCP: ignoreTCP,
27+
ignoreUDP: ignoreUDP,
28+
closableListeners: NewClosableListener(),
29+
}
2330
}
2431

2532
func (fw *Forwarder) OnEvent(ctx context.Context, client *guestagentclient.GuestAgentClient, ev *api.Event) {
2633
for _, f := range ev.LocalPortsAdded {
2734
local, remote := fw.forwardingAddresses(f)
2835
if local == "" {
29-
logrus.Infof("Not forwarding %s %s", strings.ToUpper(f.Protocol), remote)
36+
if !fw.ignoreTCP && f.Protocol == "tcp" {
37+
logrus.Infof("Not forwarding TCP %s", remote)
38+
}
39+
if !fw.ignoreTCP && f.Protocol == "udp" {
40+
logrus.Infof("Not forwarding UDP %s", remote)
41+
}
3042
continue
3143
}
3244
logrus.Infof("Forwarding %s from %s to %s", strings.ToUpper(f.Protocol), remote, local)

0 commit comments

Comments
 (0)