From 8992370eb83b8f120beb5a240ad267c86204ac5c Mon Sep 17 00:00:00 2001 From: Christophe Fergeau Date: Wed, 8 Jan 2025 12:21:54 +0100 Subject: [PATCH] forwarder: Use distinct type for `proxies` map key This allows the go compiler to catch the bug fixed in the previous commit: ``` GOARCH=amd64 GOOS=freebsd go build -ldflags "-s -w -X github.com/containers/gvisor-tap-vsock/pkg/types.gitVersion=v0.8.1-5-g9df1d587" -o bin/gvproxy-freebsd-amd64 ./cmd/gvproxy # github.com/containers/gvisor-tap-vsock/pkg/services/forwarder pkg/services/forwarder/ports.go:73:24: cannot use local (variable of type string) as ProxyKey value in map index make: *** [Makefile:57: cross] Error 1 ``` Signed-off-by: Christophe Fergeau --- pkg/services/forwarder/ports.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/pkg/services/forwarder/ports.go b/pkg/services/forwarder/ports.go index 6b92a005..a1de5401 100644 --- a/pkg/services/forwarder/ports.go +++ b/pkg/services/forwarder/ports.go @@ -25,11 +25,13 @@ import ( "gvisor.dev/gvisor/pkg/tcpip/stack" ) +type ProxyKey string + type PortsForwarder struct { stack *stack.Stack proxiesLock sync.Mutex - proxies map[string]proxy + proxies map[ProxyKey]proxy } type proxy struct { @@ -61,7 +63,7 @@ func (w CloseWrapper) Close() error { func NewPortsForwarder(s *stack.Stack) *PortsForwarder { return &PortsForwarder{ stack: s, - proxies: make(map[string]proxy), + proxies: make(map[ProxyKey]proxy), } } @@ -256,8 +258,8 @@ func (f *PortsForwarder) Expose(protocol types.TransportProtocol, local, remote return nil } -func key(protocol types.TransportProtocol, local string) string { - return fmt.Sprintf("%s/%s", protocol, local) +func key(protocol types.TransportProtocol, local string) ProxyKey { + return ProxyKey(fmt.Sprintf("%s/%s", protocol, local)) } func (f *PortsForwarder) Unexpose(protocol types.TransportProtocol, local string) error {