-
Notifications
You must be signed in to change notification settings - Fork 193
/
util.go
60 lines (51 loc) · 1.11 KB
/
util.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
package main
import (
"log"
"net"
"strings"
)
func getInterfaces() []string {
var inter []string
uniq := make(map[string]bool)
for _, host := range strings.Split(*flaginter, ",") {
ip, port, err := net.SplitHostPort(host)
if err != nil {
switch {
case strings.Contains(err.Error(), "missing port in address"):
// 127.0.0.1
ip = host
case strings.Contains(err.Error(), "too many colons in address") &&
// [a:b::c]
strings.LastIndex(host, "]") == len(host)-1:
ip = host[1 : len(host)-1]
port = ""
case strings.Contains(err.Error(), "too many colons in address"):
// a:b::c
ip = host
port = ""
default:
log.Fatalf("Could not parse %s: %s\n", host, err)
}
}
if len(port) == 0 {
port = *flagport
}
host = net.JoinHostPort(ip, port)
if uniq[host] {
continue
}
uniq[host] = true
// default to the first interfaces
// todo: skip 127.0.0.1 and ::1 ?
if ip != "127.0.0.1" {
if len(serverInfo.ID) == 0 {
serverInfo.ID = ip
}
if len(serverInfo.IP) == 0 {
serverInfo.IP = ip
}
}
inter = append(inter, host)
}
return inter
}