-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnet.go
52 lines (45 loc) · 942 Bytes
/
net.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
// Copyright (c) 2020 Xelaj Software
//
// This file is a part of go-dry package.
// See https://github.com/xelaj/go-dry/blob/master/LICENSE for details
package dry
import (
"fmt"
"net"
"os"
)
// NetIP returns the primary IP address of the system or an empty string.
func NetIP() string {
addrs, err := net.InterfaceAddrs()
if err != nil {
return ""
}
for _, addr := range addrs {
ip := addr.String()
if ip != "127.0.0.1" {
return ip
}
}
return ""
}
// RealNetIP returns the real local IP of the system or an empty string.
func RealNetIP() string {
addrs, err := net.InterfaceAddrs()
if err != nil {
fmt.Println(err)
return ""
}
// get real local IP
for _, address := range addrs {
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet.IP.String()
}
}
}
return ""
}
func NetHostname() string {
name, _ := os.Hostname()
return name
}