65 lines
1.0 KiB
Go
65 lines
1.0 KiB
Go
package auth
|
|
|
|
import (
|
|
"bytes"
|
|
"net"
|
|
|
|
"git.loafle.net/commons_go/util/net/gateway"
|
|
)
|
|
|
|
type Network struct {
|
|
Name string `json:"name"`
|
|
Address string `json:"address"`
|
|
Gateway string `json:"gateway"`
|
|
MacAddress string `json:"macAddress"`
|
|
}
|
|
|
|
func getNetwork() (*Network, error) {
|
|
var ip net.IP
|
|
var iface string
|
|
var err error
|
|
if ip, iface, err = gateway.DiscoverGateway(); nil != err {
|
|
return nil, err
|
|
}
|
|
|
|
interfaces, err := net.Interfaces()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
idx := -1
|
|
|
|
for _idx, i := range interfaces {
|
|
if i.Name == iface {
|
|
idx = _idx
|
|
break
|
|
}
|
|
}
|
|
|
|
if -1 == idx {
|
|
return nil, nil
|
|
}
|
|
|
|
n := &Network{}
|
|
i := interfaces[idx]
|
|
|
|
n.Name = i.Name
|
|
n.MacAddress = i.HardwareAddr.String()
|
|
n.Gateway = ip.String()
|
|
|
|
if addrs, err := i.Addrs(); nil == err {
|
|
var buffer bytes.Buffer
|
|
for _idx, a := range addrs {
|
|
if 0 < _idx {
|
|
buffer.WriteString("|")
|
|
}
|
|
buffer.WriteString(a.String())
|
|
}
|
|
n.Address = buffer.String()
|
|
} else {
|
|
return nil, err
|
|
}
|
|
|
|
return n, nil
|
|
}
|