1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2026-01-01 23:15:21 -05:00

proto for destination

This commit is contained in:
Darien Raymond
2016-09-20 11:53:05 +02:00
parent c518726910
commit 7de7588ec2
35 changed files with 170 additions and 195 deletions

View File

@@ -5,16 +5,10 @@ import (
)
// Destination represents a network destination including address and protocol (tcp / udp).
type Destination interface {
Network() Network // Protocol of communication (tcp / udp)
Address() Address // Address of destination
Port() Port
String() string // String representation of the destination
NetAddr() string
Equals(Destination) bool
IsTCP() bool // True if destination is reachable via TCP
IsUDP() bool // True if destination is reachable via UDP
type Destination struct {
Network Network
Address Address
Port Port
}
func DestinationFromAddr(addr net.Addr) Destination {
@@ -30,102 +24,30 @@ func DestinationFromAddr(addr net.Addr) Destination {
// TCPDestination creates a TCP destination with given address
func TCPDestination(address Address, port Port) Destination {
return &tcpDestination{address: address, port: port}
return Destination{
Network: Network_TCP,
Address: address,
Port: port,
}
}
// UDPDestination creates a UDP destination with given address
func UDPDestination(address Address, port Port) Destination {
return &udpDestination{address: address, port: port}
}
type tcpDestination struct {
address Address
port Port
}
func (dest *tcpDestination) Network() Network {
return Network_TCP
}
func (dest *tcpDestination) Address() Address {
return dest.address
}
func (dest *tcpDestination) NetAddr() string {
return dest.address.String() + ":" + dest.port.String()
}
func (dest *tcpDestination) String() string {
return "tcp:" + dest.NetAddr()
}
func (dest *tcpDestination) IsTCP() bool {
return true
}
func (dest *tcpDestination) IsUDP() bool {
return false
}
func (dest *tcpDestination) Port() Port {
return dest.port
}
func (dest *tcpDestination) Equals(another Destination) bool {
if dest == nil && another == nil {
return true
return Destination{
Network: Network_UDP,
Address: address,
Port: port,
}
if dest == nil || another == nil {
return false
}
if another.Network() != Network_TCP {
return false
}
return dest.Port() == another.Port() && dest.Address().Equals(another.Address())
}
type udpDestination struct {
address Address
port Port
func (this Destination) NetAddr() string {
return this.Address.String() + ":" + this.Port.String()
}
func (dest *udpDestination) Network() Network {
return Network_UDP
func (this Destination) String() string {
return this.Network.UrlPrefix() + ":" + this.NetAddr()
}
func (dest *udpDestination) Address() Address {
return dest.address
}
func (dest *udpDestination) NetAddr() string {
return dest.address.String() + ":" + dest.port.String()
}
func (dest *udpDestination) String() string {
return "udp:" + dest.NetAddr()
}
func (dest *udpDestination) IsTCP() bool {
return false
}
func (dest *udpDestination) IsUDP() bool {
return true
}
func (dest *udpDestination) Port() Port {
return dest.port
}
func (dest *udpDestination) Equals(another Destination) bool {
if dest == nil && another == nil {
return true
}
if dest == nil || another == nil {
return false
}
if another.Network() != Network_UDP {
return false
}
return dest.Port() == another.Port() && dest.Address().Equals(another.Address())
func (this Destination) Equals(another Destination) bool {
return this.Network == another.Network && this.Port == another.Port && this.Address.Equals(another.Address)
}