From dde162622d0787fae282d96914cc038d0e236386 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Fri, 8 May 2020 10:20:47 -0700 Subject: [PATCH] use local port in listener config, when set --- builder/googlecompute/step_start_tunnel.go | 40 +++++++++++++--------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/builder/googlecompute/step_start_tunnel.go b/builder/googlecompute/step_start_tunnel.go index 739263351..f8c0bc571 100644 --- a/builder/googlecompute/step_start_tunnel.go +++ b/builder/googlecompute/step_start_tunnel.go @@ -138,25 +138,33 @@ type StepStartTunnel struct { } func (s *StepStartTunnel) ConfigureLocalHostPort(ctx context.Context) error { + minPortNumber, maxPortNumber := 8000, 9000 + if s.IAPConf.IAPLocalhostPort == 0 { + minPortNumber = s.IAPConf.IAPLocalhostPort + maxPortNumber = minPortNumber + log.Printf("Using TCP port for %d IAP proxy", s.IAPConf.IAPLocalhostPort) + } else { log.Printf("Finding an available TCP port for IAP proxy") - l, err := net.ListenRangeConfig{ - Min: 8000, - Max: 9000, - Addr: "0.0.0.0", - Network: "tcp", - }.Listen(ctx) - - if err != nil { - err := fmt.Errorf("error finding an available port to initiate a session tunnel: %s", err) - return err - } - - s.IAPConf.IAPLocalhostPort = l.Port - l.Close() - log.Printf("Setting up proxy to listen on localhost at %d", - s.IAPConf.IAPLocalhostPort) } + + l, err := net.ListenRangeConfig{ + Min: minPortNumber, + Max: maxPortNumber, + Addr: "0.0.0.0", + Network: "tcp", + }.Listen(ctx) + + if err != nil { + err := fmt.Errorf("error finding an available port to initiate a session tunnel: %s", err) + return err + } + + s.IAPConf.IAPLocalhostPort = l.Port + l.Close() + log.Printf("Setting up proxy to listen on localhost at %d", + s.IAPConf.IAPLocalhostPort) + return nil }