github.com/xmplusdev/xray-core@v1.8.10/common/singbridge/destination.go (about)

     1  package singbridge
     2  
     3  import (
     4  	M "github.com/sagernet/sing/common/metadata"
     5  	N "github.com/sagernet/sing/common/network"
     6  	"github.com/xmplusdev/xray-core/common/net"
     7  )
     8  
     9  func ToNetwork(network string) net.Network {
    10  	switch N.NetworkName(network) {
    11  	case N.NetworkTCP:
    12  		return net.Network_TCP
    13  	case N.NetworkUDP:
    14  		return net.Network_UDP
    15  	default:
    16  		return net.Network_Unknown
    17  	}
    18  }
    19  
    20  func ToDestination(socksaddr M.Socksaddr, network net.Network) net.Destination {
    21  	// IsFqdn() implicitly checks if the domain name is valid
    22  	if socksaddr.IsFqdn() {
    23  		return net.Destination{
    24  			Network: network,
    25  			Address: net.DomainAddress(socksaddr.Fqdn),
    26  			Port:    net.Port(socksaddr.Port),
    27  		}
    28  	}
    29  
    30  	// IsIP() implicitly checks if the IP address is valid
    31  	if socksaddr.IsIP() {
    32  		return net.Destination{
    33  			Network: network,
    34  			Address: net.IPAddress(socksaddr.Addr.AsSlice()),
    35  			Port:    net.Port(socksaddr.Port),
    36  		}
    37  	}
    38  
    39  	return net.Destination{}
    40  }
    41  
    42  func ToSocksaddr(destination net.Destination) M.Socksaddr {
    43  	var addr M.Socksaddr
    44  	switch destination.Address.Family() {
    45  	case net.AddressFamilyDomain:
    46  		addr.Fqdn = destination.Address.Domain()
    47  	default:
    48  		addr.Addr = M.AddrFromIP(destination.Address.IP())
    49  	}
    50  	addr.Port = uint16(destination.Port)
    51  	return addr
    52  }