Skip to content

Commit

Permalink
Added property to force http server to bind only to IPv4 address
Browse files Browse the repository at this point in the history
  • Loading branch information
ejdre-vestas committed Jul 10, 2024
1 parent 3c943cc commit 4933a3f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,6 @@
- `http_bind_address` (string) - This is the bind address for the HTTP server. Defaults to 0.0.0.0 so that
it will work with any network interface.

- `http_only_ipv4` (bool) - If true the HTTP server will only be bound to an IPv4 interface

<!-- End of code generated from the comments of the HTTPConfig struct in multistep/commonsteps/http_config.go; -->
2 changes: 2 additions & 0 deletions multistep/commonsteps/http_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ type HTTPConfig struct {
// interface with a non-loopback address. Either `http_bind_address` or
// `http_interface` can be specified.
HTTPInterface string `mapstructure:"http_interface" undocumented:"true"`
// If true the HTTP server will only be bound to an IPv4 interface
HTTPOnlyIPv4 bool `mapstructure:"http_only_ipv4"`
}

func (c *HTTPConfig) Prepare(ctx *interpolate.Context) []error {
Expand Down
28 changes: 17 additions & 11 deletions multistep/commonsteps/step_http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ import (

func HTTPServerFromHTTPConfig(cfg *HTTPConfig) *StepHTTPServer {
return &StepHTTPServer{
HTTPDir: cfg.HTTPDir,
HTTPContent: cfg.HTTPContent,
HTTPPortMin: cfg.HTTPPortMin,
HTTPPortMax: cfg.HTTPPortMax,
HTTPAddress: cfg.HTTPAddress,
HTTPDir: cfg.HTTPDir,
HTTPContent: cfg.HTTPContent,
HTTPPortMin: cfg.HTTPPortMin,
HTTPPortMax: cfg.HTTPPortMax,
HTTPAddress: cfg.HTTPAddress,
HTTPOnlyIPv4: cfg.HTTPOnlyIPv4,
}
}

Expand All @@ -40,11 +41,12 @@ func HTTPServerFromHTTPConfig(cfg *HTTPConfig) *StepHTTPServer {
//
// http_port int - The port the HTTP server started on.
type StepHTTPServer struct {
HTTPDir string
HTTPContent map[string]string
HTTPPortMin int
HTTPPortMax int
HTTPAddress string
HTTPDir string
HTTPContent map[string]string
HTTPPortMin int
HTTPPortMax int
HTTPAddress string
HTTPOnlyIPv4 bool

l *net.Listener
}
Expand Down Expand Up @@ -102,11 +104,15 @@ func (s *StepHTTPServer) Run(ctx context.Context, state multistep.StateBag) mult

// Find an available TCP port for our HTTP server
var err error
network := "tcp"
if s.HTTPOnlyIPv4 {
network = "tcp4"
}
s.l, err = net.ListenRangeConfig{
Min: s.HTTPPortMin,
Max: s.HTTPPortMax,
Addr: s.HTTPAddress,
Network: "tcp",
Network: network,
}.Listen(ctx)

if err != nil {
Expand Down

0 comments on commit 4933a3f

Please sign in to comment.