forked from thatguystone/cog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
40 lines (33 loc) · 902 Bytes
/
node.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Package node provides information about the local node
package node
import (
"fmt"
"net"
)
// ResolveListenAddress validates the given address and resolves any special
// hostnames to actual addresses.
//
// Currently, it resolves the following:
// - <ec2-priv>: resolves to the node's private IP address
func ResolveListenAddress(addr *string) error {
return resolveListenAddress(addr, &EC2)
}
func resolveListenAddress(addr *string, ec2 *EC2Metadata) error {
if len(*addr) == 0 {
return fmt.Errorf("no listen address given")
}
host, port, err := net.SplitHostPort(*addr)
if err != nil {
return fmt.Errorf("invalid listen address: %s: %v", *addr, err)
}
switch host {
case "<ec2-priv>":
host, err = ec2.GetPrivIP()
}
if err != nil {
err = fmt.Errorf("while resolving hostname %s: %v", *addr, err)
} else {
*addr = fmt.Sprintf("%s:%s", host, port)
}
return err
}