github.com/moqsien/xraycore@v1.8.5/common/net/destination.go (about)

     1  package net
     2  
     3  import (
     4  	"net"
     5  	"strings"
     6  )
     7  
     8  // Destination represents a network destination including address and protocol (tcp / udp).
     9  type Destination struct {
    10  	Address Address
    11  	Port    Port
    12  	Network Network
    13  }
    14  
    15  // DestinationFromAddr generates a Destination from a net address.
    16  func DestinationFromAddr(addr net.Addr) Destination {
    17  	switch addr := addr.(type) {
    18  	case *net.TCPAddr:
    19  		return TCPDestination(IPAddress(addr.IP), Port(addr.Port))
    20  	case *net.UDPAddr:
    21  		return UDPDestination(IPAddress(addr.IP), Port(addr.Port))
    22  	case *net.UnixAddr:
    23  		return UnixDestination(DomainAddress(addr.Name))
    24  	default:
    25  		panic("Net: Unknown address type.")
    26  	}
    27  }
    28  
    29  // ParseDestination converts a destination from its string presentation.
    30  func ParseDestination(dest string) (Destination, error) {
    31  	d := Destination{
    32  		Address: AnyIP,
    33  		Port:    Port(0),
    34  	}
    35  	if strings.HasPrefix(dest, "tcp:") {
    36  		d.Network = Network_TCP
    37  		dest = dest[4:]
    38  	} else if strings.HasPrefix(dest, "udp:") {
    39  		d.Network = Network_UDP
    40  		dest = dest[4:]
    41  	} else if strings.HasPrefix(dest, "unix:") {
    42  		d = UnixDestination(DomainAddress(dest[5:]))
    43  		return d, nil
    44  	}
    45  
    46  	hstr, pstr, err := SplitHostPort(dest)
    47  	if err != nil {
    48  		return d, err
    49  	}
    50  	if len(hstr) > 0 {
    51  		d.Address = ParseAddress(hstr)
    52  	}
    53  	if len(pstr) > 0 {
    54  		port, err := PortFromString(pstr)
    55  		if err != nil {
    56  			return d, err
    57  		}
    58  		d.Port = port
    59  	}
    60  	return d, nil
    61  }
    62  
    63  // TCPDestination creates a TCP destination with given address
    64  func TCPDestination(address Address, port Port) Destination {
    65  	return Destination{
    66  		Network: Network_TCP,
    67  		Address: address,
    68  		Port:    port,
    69  	}
    70  }
    71  
    72  // UDPDestination creates a UDP destination with given address
    73  func UDPDestination(address Address, port Port) Destination {
    74  	return Destination{
    75  		Network: Network_UDP,
    76  		Address: address,
    77  		Port:    port,
    78  	}
    79  }
    80  
    81  // UnixDestination creates a Unix destination with given address
    82  func UnixDestination(address Address) Destination {
    83  	return Destination{
    84  		Network: Network_UNIX,
    85  		Address: address,
    86  	}
    87  }
    88  
    89  // NetAddr returns the network address in this Destination in string form.
    90  func (d Destination) NetAddr() string {
    91  	addr := ""
    92  	if d.Network == Network_TCP || d.Network == Network_UDP {
    93  		addr = d.Address.String() + ":" + d.Port.String()
    94  	} else if d.Network == Network_UNIX {
    95  		addr = d.Address.String()
    96  	}
    97  	return addr
    98  }
    99  
   100  // String returns the strings form of this Destination.
   101  func (d Destination) String() string {
   102  	prefix := "unknown:"
   103  	switch d.Network {
   104  	case Network_TCP:
   105  		prefix = "tcp:"
   106  	case Network_UDP:
   107  		prefix = "udp:"
   108  	case Network_UNIX:
   109  		prefix = "unix:"
   110  	}
   111  	return prefix + d.NetAddr()
   112  }
   113  
   114  // IsValid returns true if this Destination is valid.
   115  func (d Destination) IsValid() bool {
   116  	return d.Network != Network_Unknown
   117  }
   118  
   119  // AsDestination converts current Endpoint into Destination.
   120  func (p *Endpoint) AsDestination() Destination {
   121  	return Destination{
   122  		Network: p.Network,
   123  		Address: p.Address.AsAddress(),
   124  		Port:    Port(p.Port),
   125  	}
   126  }