Use net.JoinHostPort instead of fmt.Sprintf

Using fmt.Sprintf breaks IPv6 addresses.
This commit is contained in:
Michael Schurter 2016-11-28 10:38:54 -08:00
parent 1198a6fc88
commit b3ede6a5b7
3 changed files with 14 additions and 12 deletions

View File

@ -2,6 +2,7 @@ package env
import (
"fmt"
"net"
"os"
"strconv"
"strings"
@ -153,8 +154,8 @@ func (t *TaskEnvironment) Build() *TaskEnvironment {
if forwardedPort, ok := t.PortMap[label]; ok {
value = forwardedPort
}
t.TaskEnv[fmt.Sprintf("%s%s", PortPrefix, label)] = fmt.Sprintf("%d", value)
IPPort := fmt.Sprintf("%s:%d", network.IP, value)
t.TaskEnv[fmt.Sprintf("%s%s", PortPrefix, label)] = strconv.Itoa(value)
IPPort := net.JoinHostPort(network.IP, strconv.Itoa(value))
t.TaskEnv[fmt.Sprintf("%s%s", AddrPrefix, label)] = IPPort
}

View File

@ -529,7 +529,7 @@ func (c *Config) Listener(proto, addr string, port int) (net.Listener, error) {
Err: &net.AddrError{Err: "invalid port", Addr: fmt.Sprint(port)},
}
}
return net.Listen(proto, fmt.Sprintf("%s:%d", addr, port))
return net.Listen(proto, net.JoinHostPort(addr, strconv.Itoa(port)))
}
// Merge merges two configurations.
@ -677,9 +677,9 @@ func (c *Config) normalizeAddrs() error {
c.Addresses.RPC = normalizeBind(c.Addresses.RPC, c.BindAddr)
c.Addresses.Serf = normalizeBind(c.Addresses.Serf, c.BindAddr)
c.normalizedAddrs = &Addresses{
HTTP: fmt.Sprintf("%s:%d", c.Addresses.HTTP, c.Ports.HTTP),
RPC: fmt.Sprintf("%s:%d", c.Addresses.RPC, c.Ports.RPC),
Serf: fmt.Sprintf("%s:%d", c.Addresses.Serf, c.Ports.Serf),
HTTP: net.JoinHostPort(c.Addresses.HTTP, strconv.Itoa(c.Ports.HTTP)),
RPC: net.JoinHostPort(c.Addresses.RPC, strconv.Itoa(c.Ports.RPC)),
Serf: net.JoinHostPort(c.Addresses.Serf, strconv.Itoa(c.Ports.Serf)),
}
addr, err := normalizeAdvertise(c.AdvertiseAddrs.HTTP, c.Addresses.HTTP, c.Ports.HTTP, c.DevMode)
@ -735,7 +735,7 @@ func normalizeAdvertise(addr string, bind string, defport int, dev bool) (string
}
// missing port, append the default
return fmt.Sprintf("%s:%d", addr, defport), nil
return net.JoinHostPort(addr, strconv.Itoa(defport)), nil
}
return addr, nil
}
@ -749,11 +749,11 @@ func normalizeAdvertise(addr string, bind string, defport int, dev bool) (string
// Return the first unicast address
for _, ip := range ips {
if ip.IsLinkLocalUnicast() || ip.IsGlobalUnicast() {
return fmt.Sprintf("%s:%d", ip, defport), nil
return net.JoinHostPort(ip.String(), strconv.Itoa(defport)), nil
}
if ip.IsLoopback() && dev {
// loopback is fine for dev mode
return fmt.Sprintf("%s:%d", ip, defport), nil
return net.JoinHostPort(ip.String(), strconv.Itoa(defport)), nil
}
}
@ -772,11 +772,11 @@ func normalizeAdvertise(addr string, bind string, defport int, dev bool) (string
// Return the first unicast address
for _, ip := range ips {
if ip.IsLinkLocalUnicast() || ip.IsGlobalUnicast() {
return fmt.Sprintf("%s:%d", ip, defport), nil
return net.JoinHostPort(ip.String(), strconv.Itoa(defport)), nil
}
if ip.IsLoopback() && dev {
// loopback is fine for dev mode
return fmt.Sprintf("%s:%d", ip, defport), nil
return net.JoinHostPort(ip.String(), strconv.Itoa(defport)), nil
}
}
return "", fmt.Errorf("No valid advertise addresses, please set `advertise` manually")

View File

@ -2,6 +2,7 @@ package command
import (
"fmt"
"net"
"sort"
"strings"
@ -107,7 +108,7 @@ func standardOutput(mem []*api.AgentMember, leaders map[string]string) []string
regLeader, ok := leaders[reg]
isLeader := false
if ok {
if regLeader == fmt.Sprintf("%s:%s", member.Addr, member.Tags["port"]) {
if regLeader == net.JoinHostPort(member.Addr, member.Tags["port"]) {
isLeader = true
}