-
Notifications
You must be signed in to change notification settings - Fork 469
/
ip.go
executable file
·74 lines (66 loc) · 1.8 KB
/
ip.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package iputil
import (
"fmt"
"net"
"net/url"
"strings"
"github.com/ServiceComb/go-chassis/core/common"
)
//Localhost is a function which returns localhost IP address
func Localhost() string { return "127.0.0.1" }
//GetLocalIP 获得本机IP
func GetLocalIP() string {
addresses, err := net.InterfaceAddrs()
if err != nil {
return ""
}
for _, address := range addresses {
// Parse IP
var ip net.IP
if ip, _, err = net.ParseCIDR(address.String()); err != nil {
return ""
}
// Check if Isloopback and IPV4
if ip != nil && !ip.IsLoopback() && ip.To4() != nil {
return ip.String()
}
}
return ""
}
// DefaultEndpoint4Protocol : To ensure consistency, we generate default addr for listenAddress and advertiseAddress by one method. To avoid unnecessary port allocation work, we allocate fixed port for user defined protocol.
func DefaultEndpoint4Protocol(proto string) string {
return strings.Join([]string{Localhost(), DefaultPort4Protocol(proto)}, ":")
}
//DefaultPort4Protocol returns the default port for different protocols
func DefaultPort4Protocol(proto string) string {
switch proto {
case common.ProtocolRest:
return "5000"
case common.ProtocolHighway:
return "6000"
default:
return "7000"
}
}
// URIs2Hosts returns hosts and schema
func URIs2Hosts(uris []string) ([]string, string, error) {
hosts := make([]string, 0, len(uris))
var scheme string
for _, addr := range uris {
u, e := url.Parse(addr)
if e != nil {
//not uri. but still permitted, like zookeeper,file system
hosts = append(hosts, u.Host)
continue
}
if len(u.Host) == 0 {
continue
}
if len(scheme) != 0 && u.Scheme != scheme {
return nil, "", fmt.Errorf("inconsistent scheme found in registry address")
}
scheme = u.Scheme
hosts = append(hosts, u.Host)
}
return hosts, scheme, nil
}