2018-08-23 04:21:46 +00:00
|
|
|
package nic
|
|
|
|
|
|
|
|
import (
|
2018-09-12 05:25:14 +00:00
|
|
|
"log"
|
2018-08-23 04:21:46 +00:00
|
|
|
"net"
|
2018-10-22 17:45:28 +00:00
|
|
|
"strconv"
|
2018-09-13 03:11:37 +00:00
|
|
|
"strings"
|
2018-08-23 04:21:46 +00:00
|
|
|
|
|
|
|
omm "git.loafle.net/overflow/model/meta"
|
|
|
|
omn "git.loafle.net/overflow/model/net"
|
|
|
|
oun "git.loafle.net/overflow/util-go/net"
|
2018-09-13 03:11:37 +00:00
|
|
|
"github.com/google/gopacket/pcap"
|
2018-08-23 04:21:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func DiscoverInterfaces() ([]*omn.Interface, error) {
|
|
|
|
gwIP, gwIface, err := DiscoverGateway()
|
|
|
|
if nil != err {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
netIfaces, err := net.Interfaces()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ifaces := make([]*omn.Interface, 0)
|
|
|
|
|
|
|
|
for _, netIface := range netIfaces {
|
|
|
|
if netIface.Flags&net.FlagLoopback != 0 || netIface.Flags&net.FlagUp == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
iface := &omn.Interface{}
|
|
|
|
ifaces = append(ifaces, iface)
|
|
|
|
|
2018-09-13 03:11:37 +00:00
|
|
|
iface.FriendlyName = netIface.Name
|
2018-08-23 04:21:46 +00:00
|
|
|
iface.Mac = netIface.HardwareAddr.String()
|
|
|
|
iface.Addresses = make([]*omn.InterfaceAddress, 0)
|
|
|
|
|
2018-10-22 17:45:28 +00:00
|
|
|
log.Print(strconv.ParseInt(string(netIface.HardwareAddr), 10, 64))
|
|
|
|
|
2018-08-23 04:21:46 +00:00
|
|
|
netAddrs, err := netIface.Addrs()
|
|
|
|
if nil != err {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
LOOP_ADDR:
|
|
|
|
for _, netAddr := range netAddrs {
|
|
|
|
_, ipNet, err := oun.ParseCIDR(netAddr.String())
|
|
|
|
if nil != err {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-09-12 05:25:14 +00:00
|
|
|
var ip net.IP
|
|
|
|
switch v := netAddr.(type) {
|
|
|
|
case *net.IPNet:
|
|
|
|
ip = v.IP
|
|
|
|
case *net.IPAddr:
|
|
|
|
ip = v.IP
|
|
|
|
default:
|
|
|
|
log.Print("Addr is not *net.IPNet or *net.IPAddr", netAddr)
|
|
|
|
}
|
|
|
|
|
2018-08-23 04:21:46 +00:00
|
|
|
addr := &omn.InterfaceAddress{}
|
|
|
|
iface.Addresses = append(iface.Addresses, addr)
|
2018-09-12 05:25:14 +00:00
|
|
|
addr.Address = ip.String()
|
2018-08-23 04:21:46 +00:00
|
|
|
addr.Netmask = ipNet.Mask.String()
|
|
|
|
addr.Network = ipNet.Network().String()
|
|
|
|
|
2018-09-13 03:11:37 +00:00
|
|
|
if gwIface == ip.String() {
|
2018-08-23 04:21:46 +00:00
|
|
|
addr.Gateway = gwIP.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ipNet.Version() {
|
|
|
|
case 4:
|
|
|
|
addr.MetaIPType = omm.ToMetaIPType(omm.MetaIPTypeEnumV4)
|
|
|
|
case 6:
|
|
|
|
addr.MetaIPType = omm.ToMetaIPType(omm.MetaIPTypeEnumV6)
|
|
|
|
default:
|
|
|
|
continue LOOP_ADDR
|
|
|
|
}
|
2018-09-13 03:11:37 +00:00
|
|
|
iface.Iface = ifaceName(ip.String())
|
2018-08-23 04:21:46 +00:00
|
|
|
}
|
2018-09-13 03:11:37 +00:00
|
|
|
|
2018-08-23 04:21:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ifaces, nil
|
|
|
|
}
|
2018-09-13 03:11:37 +00:00
|
|
|
|
|
|
|
func ifaceName(ip string) string {
|
|
|
|
devices, err := pcap.FindAllDevs()
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
for _, d := range devices {
|
|
|
|
for _, address := range d.Addresses {
|
|
|
|
if strings.Compare(address.IP.String(), ip) == 0 {
|
|
|
|
return d.Name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|