-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathdata.go
More file actions
38 lines (34 loc) · 856 Bytes
/
data.go
File metadata and controls
38 lines (34 loc) · 856 Bytes
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
package machine_code
import (
"github.com/super-l/machine-code/types"
"net"
"strings"
)
var Machine types.MachineInformation
var MachineErr []error
// Obtain accurate export traffic IP information
func GetIpAddr() (string, error) {
conn, err := net.Dial("udp", "8.8.8.8:53")
if err != nil {
return "", err
}
localAddr := conn.LocalAddr().(*net.UDPAddr)
ip := strings.Split(localAddr.String(), ":")[0]
return ip, nil
}
// Get all IP addresses
func GetIpAddrAll() ([]string, error) {
var ipList []string
addrList, err := net.InterfaceAddrs()
if err != nil {
return ipList, err
}
for _, address := range addrList {
if ipNet, ok := address.(*net.IPNet); ok && !ipNet.IP.IsLoopback() && !ipNet.IP.IsLinkLocalUnicast() {
if ipNet.IP.To4() != nil {
ipList = append(ipList, ipNet.IP.To4().String())
}
}
}
return ipList, nil
}