forked from rai-project/dlframework
/
utils.go
62 lines (52 loc) · 1.24 KB
/
utils.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
package client
import (
"net"
"strings"
externalip "github.com/glendc/go-external-ip"
)
func cleanNames() {
frameworkName = strings.ToLower(frameworkName)
frameworkVersion = strings.ToLower(frameworkVersion)
modelName = strings.ToLower(modelName)
modelVersion = strings.ToLower(modelVersion)
}
func getTracerServerAddress(addr string) string {
trimPrefix := func(s string) string {
s = strings.TrimSpace(s)
if strings.HasPrefix(s, "http://") {
return strings.TrimPrefix(s, "http://")
}
if strings.HasPrefix(s, "https://") {
return strings.TrimPrefix(s, "https://")
}
return s
}
host, _, err := net.SplitHostPort(addr)
if err != nil {
return trimPrefix(addr)
}
host = trimPrefix(host)
if host == "localhost" {
return GetOutboundIP().String()
}
return host
}
// This does not give the outbound ip of this machine
func getHostIP() string {
consensus := externalip.DefaultConsensus(nil, nil)
ip, err := consensus.ExternalIP()
if err != nil {
return ""
}
return ip.String()
}
// Get preferred outbound ip of this machine
func GetOutboundIP() net.IP {
conn, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
log.Fatal(err)
}
defer conn.Close()
localAddr := conn.LocalAddr().(*net.UDPAddr)
return localAddr.IP
}