Skip to content

Commit

Permalink
config: Host support IPv4/IPv6 (#128)
Browse files Browse the repository at this point in the history
* config: Host support IPv4/IPv6

* config: Add QINGSTOR_ENABLE_DUAL_STACK env variable

* config: Modify error message
  • Loading branch information
zhaohuxing authored Jan 28, 2021
1 parent e326d0b commit cb30a5d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
31 changes: 28 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package config
import (
"errors"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -51,6 +52,8 @@ type Config struct {

EnableVirtualHostStyle bool `yaml:"enable_virtual_host_style"`

EnableDualStack bool `yaml:"enable_dual_stack"`

HTTPSettings HTTPClientSettings

Connection *http.Client
Expand Down Expand Up @@ -160,6 +163,20 @@ func (c *Config) Check() (err error) {
}
}

ip := net.ParseIP(c.Host)
if c.EnableVirtualHostStyle {
if ip != nil {
err = errors.New("Host is ip, cannot virtual host style")
return
}
}

if !c.EnableDualStack {
if ip != nil && ip.To4() == nil {
err = errors.New("Host is IPv6 address, enable_dual_stack should be true")
return
}
}
return
}

Expand Down Expand Up @@ -227,12 +244,12 @@ func (c *Config) LoadConfigFromContent(content []byte) (err error) {
return
}

err = c.Check()
err = c.parseEndpoint()
if err != nil {
return
}

err = c.parseEndpoint()
err = c.Check()
if err != nil {
return
}
Expand Down Expand Up @@ -270,6 +287,10 @@ func (c *Config) readCredentialFromEnv() (err error) {
if err != nil {
return
}
c.EnableDualStack, err = strconv.ParseBool(os.Getenv(EnvEnableDualStack))
if err != nil {
return
}
return nil
}

Expand Down Expand Up @@ -301,7 +322,11 @@ func (c *Config) InitHTTPClient() {
// when the network is "tcp" and the destination is a host name
// with both IPv4 and IPv6 addresses. This allows a client to
// tolerate networks where one address family is silently broken
dialer.DualStack = c.HTTPSettings.DualStack
dialer.DualStack = c.EnableDualStack
if !dialer.DualStack {
negativeValue, _ := time.ParseDuration("-300ms")
dialer.FallbackDelay = negativeValue
}
c.Connection = &http.Client{
// We do not use the timeout in http client,
// because this timeout is for the whole http body read/write,
Expand Down
26 changes: 26 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,29 @@ func TestParseEndpoint(t *testing.T) {
})
}
}

func TestSupportIP(t *testing.T) {
// Host is ip address, enable virtual host style
c := Config{
AccessKeyID: "AccessKeyID",
SecretAccessKey: "SecretAccessKey",
Endpoint: "https://192.168.0.1:443",
EnableVirtualHostStyle: true,
}
err := c.parseEndpoint()
assert.Nil(t, err)
err = c.Check()
assert.NotNil(t, err)

//
c = Config{
AccessKeyID: "AccessKeyID",
SecretAccessKey: "SecretAccessKey",
Endpoint: "https://2001:db8::68:443",
EnableDualStack: false,
}
err = c.parseEndpoint()
assert.Nil(t, err)
err = c.Check()
assert.NotNil(t, err)
}
3 changes: 3 additions & 0 deletions config/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ const (

// EnvEnableVirtualHostStyle is config envrionment variable.
EnvEnableVirtualHostStyle = "QINGSTOR_ENABLE_VIRTUAL_HOST_STYLE"

// EnvEnableDualStack is config envrionment variable.
EnvEnableDualStack = "QINGSTOR_ENABLE_DUAL_STACK"
)

// GetUserConfigFilePath returns the user config file path.
Expand Down

0 comments on commit cb30a5d

Please sign in to comment.