2021-09-06 15:07:34 +00:00
|
|
|
package iface
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net"
|
2022-04-19 17:52:51 +00:00
|
|
|
"net/netip"
|
2023-06-04 15:43:54 +00:00
|
|
|
"strings"
|
2021-09-06 15:07:34 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Dreamacro/clash/common/singledo"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Interface struct {
|
|
|
|
Index int
|
|
|
|
Name string
|
2022-04-19 17:52:51 +00:00
|
|
|
Addrs []*netip.Prefix
|
2021-09-06 15:07:34 +00:00
|
|
|
HardwareAddr net.HardwareAddr
|
|
|
|
}
|
|
|
|
|
2021-10-10 15:44:09 +00:00
|
|
|
var (
|
|
|
|
ErrIfaceNotFound = errors.New("interface not found")
|
|
|
|
ErrAddrNotFound = errors.New("addr not found")
|
|
|
|
)
|
2021-09-06 15:07:34 +00:00
|
|
|
|
2022-04-23 18:07:57 +00:00
|
|
|
var interfaces = singledo.NewSingle[map[string]*Interface](time.Second * 20)
|
2021-09-06 15:07:34 +00:00
|
|
|
|
|
|
|
func ResolveInterface(name string) (*Interface, error) {
|
2022-04-23 18:07:57 +00:00
|
|
|
value, err, _ := interfaces.Do(func() (map[string]*Interface, error) {
|
2021-09-06 15:07:34 +00:00
|
|
|
ifaces, err := net.Interfaces()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
r := map[string]*Interface{}
|
|
|
|
|
|
|
|
for _, iface := range ifaces {
|
|
|
|
addrs, err := iface.Addrs()
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2023-06-04 15:43:54 +00:00
|
|
|
// if not available device like Meta, dummy0, docker0, etc.
|
2023-07-02 01:59:18 +00:00
|
|
|
if (iface.Flags&net.FlagMulticast == 0) || (iface.Flags&net.FlagPointToPoint != 0) || (iface.Flags&net.FlagRunning == 0) {
|
2023-06-04 15:43:54 +00:00
|
|
|
continue
|
|
|
|
}
|
2021-09-06 15:07:34 +00:00
|
|
|
|
2022-04-19 17:52:51 +00:00
|
|
|
ipNets := make([]*netip.Prefix, 0, len(addrs))
|
2021-09-06 15:07:34 +00:00
|
|
|
for _, addr := range addrs {
|
|
|
|
ipNet := addr.(*net.IPNet)
|
2022-04-19 17:52:51 +00:00
|
|
|
ip, _ := netip.AddrFromSlice(ipNet.IP)
|
|
|
|
|
2023-06-04 15:43:54 +00:00
|
|
|
//unavailable IPv6 Address
|
|
|
|
if ip.Is6() && strings.HasPrefix(ip.String(), "fe80") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-04-19 17:52:51 +00:00
|
|
|
ones, bits := ipNet.Mask.Size()
|
|
|
|
if bits == 32 {
|
|
|
|
ip = ip.Unmap()
|
2021-09-06 15:07:34 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 17:52:51 +00:00
|
|
|
pf := netip.PrefixFrom(ip, ones)
|
|
|
|
ipNets = append(ipNets, &pf)
|
2021-09-06 15:07:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
r[iface.Name] = &Interface{
|
|
|
|
Index: iface.Index,
|
|
|
|
Name: iface.Name,
|
|
|
|
Addrs: ipNets,
|
|
|
|
HardwareAddr: iface.HardwareAddr,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return r, nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-04-23 18:07:57 +00:00
|
|
|
ifaces := value
|
2021-09-06 15:07:34 +00:00
|
|
|
iface, ok := ifaces[name]
|
|
|
|
if !ok {
|
|
|
|
return nil, ErrIfaceNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return iface, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func FlushCache() {
|
|
|
|
interfaces.Reset()
|
|
|
|
}
|
|
|
|
|
2022-04-19 17:52:51 +00:00
|
|
|
func (iface *Interface) PickIPv4Addr(destination netip.Addr) (*netip.Prefix, error) {
|
|
|
|
return iface.pickIPAddr(destination, func(addr *netip.Prefix) bool {
|
|
|
|
return addr.Addr().Is4()
|
2021-09-06 15:07:34 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-19 17:52:51 +00:00
|
|
|
func (iface *Interface) PickIPv6Addr(destination netip.Addr) (*netip.Prefix, error) {
|
|
|
|
return iface.pickIPAddr(destination, func(addr *netip.Prefix) bool {
|
|
|
|
return addr.Addr().Is6()
|
2021-09-06 15:07:34 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-19 17:52:51 +00:00
|
|
|
func (iface *Interface) pickIPAddr(destination netip.Addr, accept func(addr *netip.Prefix) bool) (*netip.Prefix, error) {
|
|
|
|
var fallback *netip.Prefix
|
2021-09-06 15:07:34 +00:00
|
|
|
|
|
|
|
for _, addr := range iface.Addrs {
|
|
|
|
if !accept(addr) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-04-19 17:52:51 +00:00
|
|
|
if fallback == nil && !addr.Addr().IsLinkLocalUnicast() {
|
2021-09-06 15:07:34 +00:00
|
|
|
fallback = addr
|
|
|
|
|
2022-04-19 17:52:51 +00:00
|
|
|
if !destination.IsValid() {
|
2021-09-06 15:07:34 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-19 17:52:51 +00:00
|
|
|
if destination.IsValid() && addr.Contains(destination) {
|
2021-09-06 15:07:34 +00:00
|
|
|
return addr, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if fallback == nil {
|
|
|
|
return nil, ErrAddrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return fallback, nil
|
|
|
|
}
|