Skip to content

Commit 392f700

Browse files
committed
Allow separate rules for UDP port forwarding
Additionally a Proto value of "any" would match both TCP and UDP ports. Signed-off-by: Jan Dubois <[email protected]>
1 parent a053653 commit 392f700

File tree

8 files changed

+26
-11
lines changed

8 files changed

+26
-11
lines changed

examples/default.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ networks:
356356
# hostIP: "0.0.0.0" # overrides the default value "127.0.0.1"; allows privileged port forwarding
357357
# # default: hostPort: 443 (same as guestPort)
358358
# # default: guestIP: "127.0.0.1" (also matches bind addresses "0.0.0.0", "::", and "::1")
359-
# # default: proto: "tcp" (only valid value right now)
359+
# # default: proto: "tcp" (other options: "udp, "any")
360360
#
361361
# - guestPortRange: [4000, 4999]
362362
# hostIP: "0.0.0.0" # overrides the default value "127.0.0.1"

pkg/hostagent/hostagent.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,16 @@ func New(instName string, stdout io.Writer, signalCh chan os.Signal, opts ...Opt
158158
for _, rule := range y.PortForwards {
159159
if rule.Ignore && rule.GuestPortRange[0] == 1 && rule.GuestPortRange[1] == 65535 {
160160
switch rule.Proto {
161-
case limayaml.TCP:
161+
case limayaml.ProtoTCP:
162162
ignoreTCP = true
163163
logrus.Info("TCP port forwarding is disabled (except for SSH)")
164-
case limayaml.UDP:
164+
case limayaml.ProtoUDP:
165165
ignoreUDP = true
166166
logrus.Info("UDP port forwarding is disabled")
167+
case limayaml.ProtoAny:
168+
ignoreTCP = true
169+
ignoreUDP = true
170+
logrus.Info("TCP (except for SSH) and UDP port forwarding is disabled")
167171
}
168172
} else {
169173
break

pkg/hostagent/port.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ func (pf *portForwarder) forwardingAddresses(guest *api.IPPort) (hostAddr, guest
5252
if rule.GuestSocket != "" {
5353
continue
5454
}
55+
switch rule.Proto {
56+
case limayaml.ProtoTCP, limayaml.ProtoAny:
57+
default:
58+
continue
59+
}
5560
if guest.Port < int32(rule.GuestPortRange[0]) || guest.Port > int32(rule.GuestPortRange[1]) {
5661
continue
5762
}

pkg/limayaml/defaults.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,7 @@ func executeHostTemplate(format, instDir string, param map[string]string) (bytes
807807

808808
func FillPortForwardDefaults(rule *PortForward, instDir string, param map[string]string) {
809809
if rule.Proto == "" {
810-
rule.Proto = TCP
810+
rule.Proto = ProtoTCP
811811
}
812812
if rule.GuestIP == nil {
813813
if rule.GuestIPMustBeZero {

pkg/limayaml/defaults_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func TestFillDefault(t *testing.T) {
114114
GuestPortRange: [2]int{1, 65535},
115115
HostIP: IPv4loopback1,
116116
HostPortRange: [2]int{1, 65535},
117-
Proto: TCP,
117+
Proto: ProtoTCP,
118118
Reverse: false,
119119
}
120120

@@ -399,7 +399,7 @@ func TestFillDefault(t *testing.T) {
399399
HostIP: IPv4loopback1,
400400
HostPort: 80,
401401
HostPortRange: [2]int{80, 80},
402-
Proto: TCP,
402+
Proto: ProtoTCP,
403403
}},
404404
CopyToHost: []CopyToHost{{}},
405405
Env: map[string]string{
@@ -616,7 +616,7 @@ func TestFillDefault(t *testing.T) {
616616
HostIP: IPv4loopback1,
617617
HostPort: 8080,
618618
HostPortRange: [2]int{8080, 8080},
619-
Proto: TCP,
619+
Proto: ProtoTCP,
620620
}},
621621
CopyToHost: []CopyToHost{{}},
622622
Env: map[string]string{

pkg/limayaml/limayaml.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,9 @@ type Probe struct {
213213
type Proto = string
214214

215215
const (
216-
TCP Proto = "tcp"
217-
UDP Proto = "udp"
216+
ProtoTCP Proto = "tcp"
217+
ProtoUDP Proto = "udp"
218+
ProtoAny Proto = "any"
218219
)
219220

220221
type PortForward struct {

pkg/limayaml/validate.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,10 @@ func Validate(y *LimaYAML, warn bool) error {
282282
return fmt.Errorf("field `%s.hostSocket` must be less than UNIX_PATH_MAX=%d characters, but is %d",
283283
field, osutil.UnixPathMax, len(rule.HostSocket))
284284
}
285-
if rule.Proto != TCP {
286-
return fmt.Errorf("field `%s.proto` must be %q", field, TCP)
285+
switch rule.Proto {
286+
case ProtoTCP, ProtoUDP, ProtoAny:
287+
default:
288+
return fmt.Errorf("field `%s.proto` must be %q, %q, or %q", field, ProtoTCP, ProtoUDP, ProtoAny)
287289
}
288290
if rule.Reverse && rule.GuestSocket == "" {
289291
return fmt.Errorf("field `%s.reverse` must be %t", field, false)

pkg/portfwd/forward.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ func (fw *Forwarder) forwardingAddresses(guest *api.IPPort) (hostAddr, guestAddr
6060
if rule.GuestSocket != "" {
6161
continue
6262
}
63+
if rule.Proto != limayaml.ProtoAny && rule.Proto != guest.Protocol {
64+
continue
65+
}
6366
if guest.Port < int32(rule.GuestPortRange[0]) || guest.Port > int32(rule.GuestPortRange[1]) {
6467
continue
6568
}

0 commit comments

Comments
 (0)