Skip to content

Commit 432be07

Browse files
fix: do not bind to IPv6 socket if 0.0.0.0 is passed
This fixes an odd default behaviour exhibited by the Go listener.
1 parent aec9219 commit 432be07

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

config/options.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"encoding/json"
55
"errors"
66
"fmt"
7-
"net"
87
"os"
98
"path"
109
"reflect"
@@ -77,7 +76,7 @@ type CloudbuildOpts struct {
7776
// General options:
7877
ConfigPath string `mapstructure:"config-path"`
7978
LogLevel LogLevel `mapstructure:"log-level"`
80-
HTTPBindAddress net.IP `mapstructure:"listen-ip"`
79+
HTTPBindAddress string `mapstructure:"listen-ip"`
8180
HTTPBindPort uint16 `mapstructure:"port"`
8281
TrustedPlatform string `mapstructure:"platform"`
8382

@@ -177,8 +176,8 @@ func (o *CloudbuildOpts) BindAPIOpts(c *cobra.Command) {
177176
c.Flags().Uint16VarP(
178177
&o.HTTPBindPort, "port", "p", o.HTTPBindPort, "HTTP listen port",
179178
)
180-
c.Flags().IPVarP(
181-
&o.HTTPBindAddress, "listen-ip", "l", net.IPv4zero, "HTTP listen IP",
179+
c.Flags().StringVarP(
180+
&o.HTTPBindAddress, "listen-ip", "l", o.HTTPBindAddress, "HTTP listen IP",
182181
)
183182
c.Flags().StringVar(
184183
&o.TargetsDef, "targets", o.TargetsDef, "Targets definition",

server/http.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package server
22

33
import (
44
"errors"
5+
"net"
56
"net/http"
67
"strings"
78

@@ -263,5 +264,22 @@ func (app *Application) Start(listen string, opts *config.CloudbuildOpts) error
263264
}
264265
})
265266

266-
return router.Run(listen)
267+
var network string
268+
269+
switch {
270+
case strings.HasPrefix(listen, "0.0.0.0"):
271+
network = "tcp4" // IPv4 only
272+
case listen == "" || strings.HasPrefix(listen, ":"):
273+
network = "tcp" // Dual-stack (default Go behavior)
274+
default:
275+
network = "tcp" // Let Go decide based on the address
276+
}
277+
278+
listener, err := net.Listen(network, listen)
279+
if err != nil {
280+
return err
281+
}
282+
defer listener.Close()
283+
284+
return router.RunListener(listener)
267285
}

0 commit comments

Comments
 (0)