github.com/igoogolx/clash@v1.19.8/component/dialer/bind_others.go (about)

     1  //go:build !linux && !darwin && !windows
     2  
     3  package dialer
     4  
     5  import (
     6  	"net"
     7  	"strconv"
     8  )
     9  
    10  func bindIfaceToDialer(ifaceName string, dialer *net.Dialer, network string, destination net.IP) error {
    11  	if !destination.IsGlobalUnicast() {
    12  		return nil
    13  	}
    14  
    15  	local := uint64(0)
    16  	if dialer.LocalAddr != nil {
    17  		_, port, err := net.SplitHostPort(dialer.LocalAddr.String())
    18  		if err == nil {
    19  			local, _ = strconv.ParseUint(port, 10, 16)
    20  		}
    21  	}
    22  
    23  	addr, err := lookupLocalAddr(ifaceName, network, destination, int(local))
    24  	if err != nil {
    25  		return err
    26  	}
    27  
    28  	dialer.LocalAddr = addr
    29  
    30  	return nil
    31  }
    32  
    33  func bindIfaceToListenConfig(ifaceName string, _ *net.ListenConfig, network, address string) (string, error) {
    34  	_, port, err := net.SplitHostPort(address)
    35  	if err != nil {
    36  		port = "0"
    37  	}
    38  
    39  	local, _ := strconv.ParseUint(port, 10, 16)
    40  
    41  	addr, err := lookupLocalAddr(ifaceName, network, nil, int(local))
    42  	if err != nil {
    43  		return "", err
    44  	}
    45  
    46  	return addr.String(), nil
    47  }