1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-27 01:45:23 +00:00
v2fly/common/net/destination.go

129 lines
2.9 KiB
Go
Raw Normal View History

package net
2016-08-14 15:08:01 +00:00
import (
"net"
"strings"
2016-08-14 15:08:01 +00:00
)
2015-09-21 10:15:25 +00:00
// Destination represents a network destination including address and protocol (tcp / udp).
2016-09-20 09:53:05 +00:00
type Destination struct {
2016-12-21 14:37:16 +00:00
Address Address
Port Port
Network Network
2015-09-20 16:22:29 +00:00
}
2017-02-20 10:25:05 +00:00
// DestinationFromAddr generates a Destination from a net address.
2016-08-14 21:20:23 +00:00
func DestinationFromAddr(addr net.Addr) Destination {
switch addr := addr.(type) {
case *net.TCPAddr:
return TCPDestination(IPAddress(addr.IP), Port(addr.Port))
case *net.UDPAddr:
return UDPDestination(IPAddress(addr.IP), Port(addr.Port))
2018-04-09 18:45:23 +00:00
case *net.UnixAddr:
2020-10-29 07:30:38 +00:00
return UnixDestination(DomainAddress(addr.Name))
2016-08-14 21:20:23 +00:00
default:
2017-02-08 09:01:22 +00:00
panic("Net: Unknown address type.")
2016-08-14 21:20:23 +00:00
}
2016-08-14 15:08:01 +00:00
}
// ParseDestination converts a destination from its string presentation.
func ParseDestination(dest string) (Destination, error) {
d := Destination{
Address: AnyIP,
Port: Port(0),
}
switch {
case strings.HasPrefix(dest, "tcp:"):
d.Network = Network_TCP
dest = dest[4:]
case strings.HasPrefix(dest, "udp:"):
d.Network = Network_UDP
dest = dest[4:]
case strings.HasPrefix(dest, "unix:"):
2020-10-29 07:30:38 +00:00
d = UnixDestination(DomainAddress(dest[5:]))
return d, nil
}
hstr, pstr, err := SplitHostPort(dest)
if err != nil {
return d, err
}
if len(hstr) > 0 {
d.Address = ParseAddress(hstr)
}
if len(pstr) > 0 {
port, err := PortFromString(pstr)
if err != nil {
return d, err
}
d.Port = port
}
return d, nil
}
2015-12-16 22:53:38 +00:00
// TCPDestination creates a TCP destination with given address
func TCPDestination(address Address, port Port) Destination {
2016-09-20 09:53:05 +00:00
return Destination{
Network: Network_TCP,
Address: address,
Port: port,
}
2015-09-20 16:22:29 +00:00
}
2015-12-16 22:53:38 +00:00
// UDPDestination creates a UDP destination with given address
func UDPDestination(address Address, port Port) Destination {
2016-09-20 09:53:05 +00:00
return Destination{
Network: Network_UDP,
Address: address,
Port: port,
}
}
2020-10-29 07:30:38 +00:00
// UnixDestination creates a Unix destination with given address
func UnixDestination(address Address) Destination {
return Destination{
Network: Network_UNIX,
Address: address,
}
}
2017-11-06 21:42:16 +00:00
// NetAddr returns the network address in this Destination in string form.
2017-08-22 13:15:09 +00:00
func (d Destination) NetAddr() string {
2020-10-29 07:30:38 +00:00
addr := ""
if d.Network == Network_TCP || d.Network == Network_UDP {
addr = d.Address.String() + ":" + d.Port.String()
} else if d.Network == Network_UNIX {
addr = d.Address.String()
}
return addr
2015-09-20 16:22:29 +00:00
}
2017-11-06 21:42:16 +00:00
// String returns the strings form of this Destination.
2017-08-22 13:15:09 +00:00
func (d Destination) String() string {
2018-11-20 15:11:55 +00:00
prefix := "unknown:"
switch d.Network {
case Network_TCP:
prefix = "tcp:"
case Network_UDP:
prefix = "udp:"
2020-10-29 07:30:38 +00:00
case Network_UNIX:
prefix = "unix:"
2018-11-20 15:11:55 +00:00
}
return prefix + d.NetAddr()
2015-12-16 22:53:38 +00:00
}
2017-11-06 21:42:16 +00:00
// IsValid returns true if this Destination is valid.
2017-08-22 13:15:09 +00:00
func (d Destination) IsValid() bool {
return d.Network != Network_Unknown
}
// AsDestination converts current Endpoint into Destination.
2017-08-22 13:15:09 +00:00
func (p *Endpoint) AsDestination() Destination {
2016-09-20 14:05:35 +00:00
return Destination{
2017-08-22 13:15:09 +00:00
Network: p.Network,
Address: p.Address.AsAddress(),
Port: Port(p.Port),
2016-09-20 14:05:35 +00:00
}
}