github.com/yaling888/clash@v1.53.0/transport/wireguard/option_bsd.go (about)

     1  //go:build (freebsd || openbsd) && !nogvisor
     2  
     3  package wireguard
     4  
     5  import (
     6  	"net/netip"
     7  
     8  	"github.com/yaling888/clash/component/dialer"
     9  	"github.com/yaling888/clash/component/iface"
    10  )
    11  
    12  func getListenIP(network string, interfaceName string) (string, error) {
    13  	if interfaceName == "" {
    14  		interfaceName = dialer.DefaultInterface.Load()
    15  		if interfaceName == "" {
    16  			return "", nil
    17  		}
    18  	}
    19  
    20  	ifaceObj, err := iface.ResolveInterface(interfaceName)
    21  	if err != nil {
    22  		return "", err
    23  	}
    24  
    25  	var addr *netip.Prefix
    26  	switch network {
    27  	case "udp4":
    28  		addr, err = ifaceObj.PickIPv4Addr(netip.Addr{})
    29  	case "udp6":
    30  		addr, err = ifaceObj.PickIPv6Addr(netip.Addr{})
    31  	default:
    32  		addr, err = ifaceObj.PickIPv4Addr(netip.Addr{})
    33  	}
    34  	if err != nil {
    35  		return "", err
    36  	}
    37  
    38  	return addr.String(), nil
    39  }