Merge pull request #372 from philips/bind-addr-ports

fix(server): override port of bind
This commit is contained in:
Brandon Philips
2013-12-05 15:29:37 -08:00
3 changed files with 48 additions and 17 deletions

View File

@ -445,7 +445,7 @@ func (c *Config) PeerTLSConfig() (TLSConfig, error) {
return c.PeerTLSInfo().Config()
}
// sanitizeURL will cleanup a host string in the format hostname:port and
// sanitizeURL will cleanup a host string in the format hostname[:port] and
// attach a schema.
func sanitizeURL(host string, defaultScheme string) (string, error) {
// Blank URLs are fine input, just return it
@ -476,15 +476,23 @@ func sanitizeBindAddr(bindAddr string, addr string) (string, error) {
return "", err
}
ahost, aport, err := net.SplitHostPort(aurl.Host)
// If it is a valid host:port simply return with no further checks.
bhost, bport, err := net.SplitHostPort(bindAddr)
if err == nil && bhost != "" {
return bindAddr, nil
}
// SplitHostPort makes the host optional, but we don't want that.
if bhost == "" && bport != "" {
return "", fmt.Errorf("IP required can't use a port only")
}
// bindAddr doesn't have a port if we reach here so take the port from the
// advertised URL.
_, aport, err := net.SplitHostPort(aurl.Host)
if err != nil {
return "", err
}
// If the listen host isn't set use the advertised host
if bindAddr == "" {
bindAddr = ahost
}
return net.JoinHostPort(bindAddr, aport), nil
}