-
Notifications
You must be signed in to change notification settings - Fork 1
/
Interfaces.go
49 lines (42 loc) · 999 Bytes
/
Interfaces.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
package main
import (
"net"
"strings"
)
type InterfaceInfo struct {
Name string `json:"name"`
IP string `json:"ip"`
QrData string `json:"qr"`
}
func getInterfaceInfo() ([]InterfaceInfo, error) {
interfaces, err := net.Interfaces()
if err != nil {
return nil, err
}
var interfaceInfos []InterfaceInfo
ipOverride := GetString("ipOverride")
for _, iface := range interfaces {
addrs, err := iface.Addrs()
if err != nil {
return nil, err
}
for _, addr := range addrs {
switch v := addr.(type) {
case *net.IPNet:
if strings.Contains(v.IP.String(), "169.254") || v.IP.To4() == nil || v.IP.String() == "127.0.0.1" {
continue
}
if len(ipOverride) > 0 && v.IP.String() != ipOverride {
continue
}
interfaceInfo := InterfaceInfo{
Name: iface.Name,
IP: v.IP.String(),
QrData: qrServerInfoString(v.IP.String()),
}
interfaceInfos = append(interfaceInfos, interfaceInfo)
}
}
}
return interfaceInfos, nil
}