Skip to content

Commit

Permalink
Use net.JoinHostPort
Browse files Browse the repository at this point in the history
  • Loading branch information
42wim committed Jul 25, 2016
1 parent b3df64c commit c576c23
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 36 deletions.
32 changes: 16 additions & 16 deletions command/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,49 +175,49 @@ func (a *Agent) serverConfig() (*nomad.Config, error) {
if a.config.AdvertiseAddrs.HTTP != "" {
a.serverHTTPAddr = a.config.AdvertiseAddrs.HTTP
} else if a.config.Addresses.HTTP != "" {
a.serverHTTPAddr = joinIPPort(a.config.Addresses.HTTP, a.config.Ports.HTTP)
a.serverHTTPAddr = net.JoinHostPort(a.config.Addresses.HTTP, strconv.Itoa(a.config.Ports.HTTP))
} else if a.config.BindAddr != "" {
a.serverHTTPAddr = joinIPPort(a.config.BindAddr, a.config.Ports.HTTP)
a.serverHTTPAddr = net.JoinHostPort(a.config.BindAddr, strconv.Itoa(a.config.Ports.HTTP))
} else {
a.serverHTTPAddr = joinIPPort("127.0.0.1", a.config.Ports.HTTP)
a.serverHTTPAddr = net.JoinHostPort("127.0.0.1", strconv.Itoa(a.config.Ports.HTTP))
}
addr, err := net.ResolveTCPAddr("tcp", a.serverHTTPAddr)
if err != nil {
return nil, fmt.Errorf("error resolving HTTP addr %+q: %v", a.serverHTTPAddr, err)
}
a.serverHTTPAddr = joinIPPort(addr.IP.String(), addr.Port)
a.serverHTTPAddr = net.JoinHostPort(addr.IP.String(), strconv.Itoa(addr.Port))

// Resolve the Server's RPC Address
if a.config.AdvertiseAddrs.RPC != "" {
a.serverRPCAddr = a.config.AdvertiseAddrs.RPC
} else if a.config.Addresses.RPC != "" {
a.serverRPCAddr = joinIPPort(a.config.Addresses.RPC, a.config.Ports.RPC)
a.serverRPCAddr = net.JoinHostPort(a.config.Addresses.RPC, strconv.Itoa(a.config.Ports.RPC))
} else if a.config.BindAddr != "" {
a.serverRPCAddr = joinIPPort(a.config.BindAddr, a.config.Ports.RPC)
a.serverRPCAddr = net.JoinHostPort(a.config.BindAddr, strconv.Itoa(a.config.Ports.RPC))
} else {
a.serverRPCAddr = joinIPPort("127.0.0.1", a.config.Ports.RPC)
a.serverRPCAddr = net.JoinHostPort("127.0.0.1", strconv.Itoa(a.config.Ports.RPC))
}
addr, err = net.ResolveTCPAddr("tcp", a.serverRPCAddr)
if err != nil {
return nil, fmt.Errorf("error resolving RPC addr %+q: %v", a.serverRPCAddr, err)
}
a.serverRPCAddr = joinIPPort(addr.IP.String(), addr.Port)
a.serverRPCAddr = net.JoinHostPort(addr.IP.String(), strconv.Itoa(addr.Port))

// Resolve the Server's Serf Address
if a.config.AdvertiseAddrs.Serf != "" {
a.serverSerfAddr = a.config.AdvertiseAddrs.Serf
} else if a.config.Addresses.Serf != "" {
a.serverSerfAddr = joinIPPort(a.config.Addresses.Serf, a.config.Ports.Serf)
a.serverSerfAddr = net.JoinHostPort(a.config.Addresses.Serf, strconv.Itoa(a.config.Ports.Serf))
} else if a.config.BindAddr != "" {
a.serverSerfAddr = joinIPPort(a.config.BindAddr, a.config.Ports.Serf)
a.serverSerfAddr = net.JoinHostPort(a.config.BindAddr, strconv.Itoa(a.config.Ports.Serf))
} else {
a.serverSerfAddr = joinIPPort("127.0.0.1", a.config.Ports.Serf)
a.serverSerfAddr = net.JoinHostPort("127.0.0.1", strconv.Itoa(a.config.Ports.Serf))
}
addr, err = net.ResolveTCPAddr("tcp", a.serverSerfAddr)
if err != nil {
return nil, fmt.Errorf("error resolving Serf addr %+q: %v", a.serverSerfAddr, err)
}
a.serverSerfAddr = joinIPPort(addr.IP.String(), addr.Port)
a.serverSerfAddr = net.JoinHostPort(addr.IP.String(), strconv.Itoa(addr.Port))

if gcThreshold := a.config.Server.NodeGCThreshold; gcThreshold != "" {
dur, err := time.ParseDuration(gcThreshold)
Expand Down Expand Up @@ -314,17 +314,17 @@ func (a *Agent) clientConfig() (*clientconfig.Config, error) {
if a.config.AdvertiseAddrs.HTTP != "" {
a.clientHTTPAddr = a.config.AdvertiseAddrs.HTTP
} else if a.config.Addresses.HTTP != "" {
a.clientHTTPAddr = joinIPPort(a.config.Addresses.HTTP, a.config.Ports.HTTP)
a.clientHTTPAddr = net.JoinHostPort(a.config.Addresses.HTTP, strconv.Itoa(a.config.Ports.HTTP))
} else if a.config.BindAddr != "" {
a.clientHTTPAddr = joinIPPort(a.config.BindAddr, a.config.Ports.HTTP)
a.clientHTTPAddr = net.JoinHostPort(a.config.BindAddr, strconv.Itoa(a.config.Ports.HTTP))
} else {
a.clientHTTPAddr = joinIPPort("127.0.0.1", a.config.Ports.HTTP)
a.clientHTTPAddr = net.JoinHostPort("127.0.0.1", strconv.Itoa(a.config.Ports.HTTP))
}
addr, err := net.ResolveTCPAddr("tcp", a.clientHTTPAddr)
if err != nil {
return nil, fmt.Errorf("error resolving HTTP addr %+q: %v", a.clientHTTPAddr, err)
}
httpAddr := joinIPPort(addr.IP.String(), addr.Port)
httpAddr := net.JoinHostPort(addr.IP.String(), strconv.Itoa(addr.Port))

conf.Node.HTTPAddr = httpAddr
a.clientHTTPAddr = httpAddr
Expand Down
20 changes: 0 additions & 20 deletions command/agent/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,3 @@ func ipOfDevice(name string) (net.IP, error) {
}
return nil, fmt.Errorf("no ips were detected on the interface: %v", name)
}

// isIPV6 checks if the IP address is an IPv6 address
func isIPV6(ip string) bool {
addr := net.ParseIP(ip)
if addr != nil {
// ipv6
if addr.To4() == nil {
return true
}
}
return false
}

// joinIPPort joins ip and port correctly for IPv4 (ip:port) or IPv6 ([ip]:port)
func joinIPPort(ip string, port int) string {
if isIPV6(ip) {
return fmt.Sprintf("[%s]:%d", ip, port)
}
return fmt.Sprintf("%s:%d", ip, port)
}

0 comments on commit c576c23

Please sign in to comment.