github.com/zooyer/miskit@v1.0.71/device/interface_darwin.go (about)

     1  /**
     2   * @Author: zzy
     3   * @Email: zhangzhongyuan@didiglobal.com
     4   * @Description:
     5   * @File: interface_darwin.go
     6   * @Package: device
     7   * @Version: 1.0.0
     8   * @Date: 2022/10/5 15:18
     9   */
    10  
    11  package device
    12  
    13  import (
    14  	"encoding/json"
    15  	"syscall"
    16  
    17  	"golang.org/x/net/route"
    18  )
    19  
    20  func interfaceMessages(index int) ([]route.Message, error) {
    21  	rib, err := route.FetchRIB(syscall.AF_UNSPEC, syscall.NET_RT_IFLIST, index)
    22  	if err != nil {
    23  		return nil, err
    24  	}
    25  	return route.ParseRIB(syscall.NET_RT_IFLIST, rib)
    26  }
    27  
    28  func isPhysicalEthernet(index int) (is bool, err error) {
    29  	msg, err := interfaceMessages(index)
    30  	if err != nil {
    31  		return
    32  	}
    33  
    34  	for _, m := range msg {
    35  		m, ok := m.(*route.InterfaceMessage)
    36  		if !ok {
    37  			continue
    38  		}
    39  
    40  		if m.Index != index {
    41  			continue
    42  		}
    43  
    44  		if sys := m.Sys(); len(sys) == 1 {
    45  			if im, ok := sys[0].(*route.InterfaceMetrics); ok && im.Type == 6 {
    46  				//fmt.Println(fmt.Sprintf("name: %s, flags:%b", m.Name, m.Flags))
    47  				//fmt.Println("name:", m.Name, "version:", m.Version, "type:", m.Type, "flags:", m.Flags, "index:", m.Index, "sys:", marshalJSON(m.Sys()))
    48  				_ = im
    49  				return true, nil
    50  			}
    51  		}
    52  	}
    53  
    54  	return
    55  }
    56  
    57  func marshalJSON(v any) string {
    58  	data, _ := json.Marshal(v)
    59  	return string(data)
    60  }