mirror of
https://github.com/v2ray/v2ray-core.git
synced 2026-11-25 07:20:44 +00:00
move port out of address
This commit is contained in:
1 parent
4be7cd7908
commit
34a0cb0b70
34 files changed
+194
-172
No files matched your search
+12
-28
@@ -11,7 +11,6 @@ import (
|
||||
type Address interface {
|
||||
IP() net.IP // IP of this Address
|
||||
Domain() string // Domain of this Address
|
||||
Port() Port // Port of this Address
|
||||
|
||||
IsIPv4() bool // True if this Address is an IPv4 address
|
||||
IsIPv6() bool // True if this Address is an IPv6 address
|
||||
@@ -30,19 +29,17 @@ func allZeros(data []byte) bool {
|
||||
}
|
||||
|
||||
// IPAddress creates an Address with given IP and port.
|
||||
func IPAddress(ip []byte, port Port) Address {
|
||||
func IPAddress(ip []byte) Address {
|
||||
switch len(ip) {
|
||||
case net.IPv4len:
|
||||
return &IPv4Address{
|
||||
port: port,
|
||||
ip: [4]byte{ip[0], ip[1], ip[2], ip[3]},
|
||||
ip: [4]byte{ip[0], ip[1], ip[2], ip[3]},
|
||||
}
|
||||
case net.IPv6len:
|
||||
if allZeros(ip[0:10]) && ip[10] == 0xff && ip[11] == 0xff {
|
||||
return IPAddress(ip[12:16], port)
|
||||
return IPAddress(ip[12:16])
|
||||
}
|
||||
return &IPv6Address{
|
||||
port: Port(port),
|
||||
ip: [16]byte{
|
||||
ip[0], ip[1], ip[2], ip[3],
|
||||
ip[4], ip[5], ip[6], ip[7],
|
||||
@@ -57,26 +54,23 @@ func IPAddress(ip []byte, port Port) Address {
|
||||
}
|
||||
|
||||
// DomainAddress creates an Address with given domain and port.
|
||||
func DomainAddress(domain string, port Port) Address {
|
||||
func DomainAddress(domain string) Address {
|
||||
return &DomainAddressImpl{
|
||||
domain: domain,
|
||||
port: port,
|
||||
}
|
||||
}
|
||||
|
||||
type address struct {
|
||||
}
|
||||
|
||||
type IPv4Address struct {
|
||||
port Port
|
||||
ip [4]byte
|
||||
ip [4]byte
|
||||
}
|
||||
|
||||
func (addr *IPv4Address) IP() net.IP {
|
||||
return net.IP(addr.ip[:])
|
||||
}
|
||||
|
||||
func (this *IPv4Address) Port() Port {
|
||||
return this.port
|
||||
}
|
||||
|
||||
func (addr *IPv4Address) Domain() string {
|
||||
panic("Calling Domain() on an IPv4Address.")
|
||||
}
|
||||
@@ -94,22 +88,17 @@ func (addr *IPv4Address) IsDomain() bool {
|
||||
}
|
||||
|
||||
func (this *IPv4Address) String() string {
|
||||
return this.IP().String() + ":" + this.port.String()
|
||||
return this.IP().String()
|
||||
}
|
||||
|
||||
type IPv6Address struct {
|
||||
port Port
|
||||
ip [16]byte
|
||||
ip [16]byte
|
||||
}
|
||||
|
||||
func (addr *IPv6Address) IP() net.IP {
|
||||
return net.IP(addr.ip[:])
|
||||
}
|
||||
|
||||
func (this *IPv6Address) Port() Port {
|
||||
return this.port
|
||||
}
|
||||
|
||||
func (addr *IPv6Address) Domain() string {
|
||||
panic("Calling Domain() on an IPv6Address.")
|
||||
}
|
||||
@@ -127,11 +116,10 @@ func (addr *IPv6Address) IsDomain() bool {
|
||||
}
|
||||
|
||||
func (this *IPv6Address) String() string {
|
||||
return "[" + this.IP().String() + "]:" + this.port.String()
|
||||
return "[" + this.IP().String() + "]"
|
||||
}
|
||||
|
||||
type DomainAddressImpl struct {
|
||||
port Port
|
||||
domain string
|
||||
}
|
||||
|
||||
@@ -139,10 +127,6 @@ func (addr *DomainAddressImpl) IP() net.IP {
|
||||
panic("Calling IP() on a DomainAddress.")
|
||||
}
|
||||
|
||||
func (this *DomainAddressImpl) Port() Port {
|
||||
return this.port
|
||||
}
|
||||
|
||||
func (addr *DomainAddressImpl) Domain() string {
|
||||
return addr.domain
|
||||
}
|
||||
@@ -160,5 +144,5 @@ func (addr *DomainAddressImpl) IsDomain() bool {
|
||||
}
|
||||
|
||||
func (this *DomainAddressImpl) String() string {
|
||||
return this.domain + ":" + this.port.String()
|
||||
return this.domain
|
||||
}
|
||||
@@ -14,15 +14,13 @@ func TestIPv4Address(t *testing.T) {
|
||||
v2testing.Current(t)
|
||||
|
||||
ip := []byte{byte(1), byte(2), byte(3), byte(4)}
|
||||
port := v2net.Port(80)
|
||||
addr := v2net.IPAddress(ip, port)
|
||||
addr := v2net.IPAddress(ip)
|
||||
|
||||
v2netassert.Address(addr).IsIPv4()
|
||||
v2netassert.Address(addr).IsNotIPv6()
|
||||
v2netassert.Address(addr).IsNotDomain()
|
||||
assert.Bytes(addr.IP()).Equals(ip)
|
||||
v2netassert.Port(addr.Port()).Equals(port)
|
||||
assert.String(addr).Equals("1.2.3.4:80")
|
||||
assert.String(addr).Equals("1.2.3.4")
|
||||
}
|
||||
|
||||
func TestIPv6Address(t *testing.T) {
|
||||
@@ -34,38 +32,33 @@ func TestIPv6Address(t *testing.T) {
|
||||
byte(1), byte(2), byte(3), byte(4),
|
||||
byte(1), byte(2), byte(3), byte(4),
|
||||
}
|
||||
port := v2net.Port(443)
|
||||
addr := v2net.IPAddress(ip, port)
|
||||
addr := v2net.IPAddress(ip)
|
||||
|
||||
v2netassert.Address(addr).IsIPv6()
|
||||
v2netassert.Address(addr).IsNotIPv4()
|
||||
v2netassert.Address(addr).IsNotDomain()
|
||||
assert.Bytes(addr.IP()).Equals(ip)
|
||||
v2netassert.Port(addr.Port()).Equals(port)
|
||||
assert.String(addr).Equals("[102:304:102:304:102:304:102:304]:443")
|
||||
assert.String(addr).Equals("[102:304:102:304:102:304:102:304]")
|
||||
}
|
||||
|
||||
func TestDomainAddress(t *testing.T) {
|
||||
v2testing.Current(t)
|
||||
|
||||
domain := "v2ray.com"
|
||||
port := v2net.Port(443)
|
||||
addr := v2net.DomainAddress(domain, port)
|
||||
addr := v2net.DomainAddress(domain)
|
||||
|
||||
v2netassert.Address(addr).IsDomain()
|
||||
v2netassert.Address(addr).IsNotIPv6()
|
||||
v2netassert.Address(addr).IsNotIPv4()
|
||||
assert.StringLiteral(addr.Domain()).Equals(domain)
|
||||
v2netassert.Port(addr.Port()).Equals(port)
|
||||
assert.String(addr).Equals("v2ray.com:443")
|
||||
assert.String(addr).Equals("v2ray.com")
|
||||
}
|
||||
|
||||
func TestNetIPv4Address(t *testing.T) {
|
||||
v2testing.Current(t)
|
||||
|
||||
ip := net.IPv4(1, 2, 3, 4)
|
||||
port := v2net.Port(80)
|
||||
addr := v2net.IPAddress(ip, port)
|
||||
addr := v2net.IPAddress(ip)
|
||||
v2netassert.Address(addr).IsIPv4()
|
||||
assert.String(addr).Equals("1.2.3.4:80")
|
||||
assert.String(addr).Equals("1.2.3.4")
|
||||
}
|
||||
+42
-22
@@ -4,66 +4,86 @@ package net
|
||||
type Destination interface {
|
||||
Network() string // Protocol of communication (tcp / udp)
|
||||
Address() Address // Address of destination
|
||||
String() string // String representation of the destination
|
||||
Port() Port
|
||||
String() string // String representation of the destination
|
||||
NetAddr() string
|
||||
|
||||
IsTCP() bool // True if destination is reachable via TCP
|
||||
IsUDP() bool // True if destination is reachable via UDP
|
||||
}
|
||||
|
||||
// NewTCPDestination creates a TCP destination with given address
|
||||
func NewTCPDestination(address Address) Destination {
|
||||
return TCPDestination{address: address}
|
||||
// TCPDestination creates a TCP destination with given address
|
||||
func TCPDestination(address Address, port Port) Destination {
|
||||
return &tcpDestination{address: address, port: port}
|
||||
}
|
||||
|
||||
// NewUDPDestination creates a UDP destination with given address
|
||||
func NewUDPDestination(address Address) Destination {
|
||||
return UDPDestination{address: address}
|
||||
// UDPDestination creates a UDP destination with given address
|
||||
func UDPDestination(address Address, port Port) Destination {
|
||||
return &udpDestination{address: address, port: port}
|
||||
}
|
||||
|
||||
type TCPDestination struct {
|
||||
type tcpDestination struct {
|
||||
address Address
|
||||
port Port
|
||||
}
|
||||
|
||||
func (dest TCPDestination) Network() string {
|
||||
func (dest *tcpDestination) Network() string {
|
||||
return "tcp"
|
||||
}
|
||||
|
||||
func (dest TCPDestination) Address() Address {
|
||||
func (dest *tcpDestination) Address() Address {
|
||||
return dest.address
|
||||
}
|
||||
|
||||
func (dest TCPDestination) String() string {
|
||||
return "tcp:" + dest.address.String()
|
||||
func (dest *tcpDestination) NetAddr() string {
|
||||
return dest.address.String() + ":" + dest.port.String()
|
||||
}
|
||||
|
||||
func (dest TCPDestination) IsTCP() bool {
|
||||
func (dest *tcpDestination) String() string {
|
||||
return "tcp:" + dest.NetAddr()
|
||||
}
|
||||
|
||||
func (dest *tcpDestination) IsTCP() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (dest TCPDestination) IsUDP() bool {
|
||||
func (dest *tcpDestination) IsUDP() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
type UDPDestination struct {
|
||||
address Address
|
||||
func (dest *tcpDestination) Port() Port {
|
||||
return dest.port
|
||||
}
|
||||
|
||||
func (dest UDPDestination) Network() string {
|
||||
type udpDestination struct {
|
||||
address Address
|
||||
port Port
|
||||
}
|
||||
|
||||
func (dest *udpDestination) Network() string {
|
||||
return "udp"
|
||||
}
|
||||
|
||||
func (dest UDPDestination) Address() Address {
|
||||
func (dest *udpDestination) Address() Address {
|
||||
return dest.address
|
||||
}
|
||||
|
||||
func (dest UDPDestination) String() string {
|
||||
return "udp:" + dest.address.String()
|
||||
func (dest *udpDestination) NetAddr() string {
|
||||
return dest.address.String() + ":" + dest.port.String()
|
||||
}
|
||||
|
||||
func (dest UDPDestination) IsTCP() bool {
|
||||
func (dest *udpDestination) String() string {
|
||||
return "udp:" + dest.NetAddr()
|
||||
}
|
||||
|
||||
func (dest *udpDestination) IsTCP() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (dest UDPDestination) IsUDP() bool {
|
||||
func (dest *udpDestination) IsUDP() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (dest *udpDestination) Port() Port {
|
||||
return dest.port
|
||||
}
|
||||
@@ -12,7 +12,7 @@ import (
|
||||
func TestTCPDestination(t *testing.T) {
|
||||
v2testing.Current(t)
|
||||
|
||||
dest := v2net.NewTCPDestination(v2net.IPAddress([]byte{1, 2, 3, 4}, 80))
|
||||
dest := v2net.TCPDestination(v2net.IPAddress([]byte{1, 2, 3, 4}), 80)
|
||||
v2netassert.Destination(dest).IsTCP()
|
||||
v2netassert.Destination(dest).IsNotUDP()
|
||||
assert.String(dest).Equals("tcp:1.2.3.4:80")
|
||||
@@ -21,7 +21,7 @@ func TestTCPDestination(t *testing.T) {
|
||||
func TestUDPDestination(t *testing.T) {
|
||||
v2testing.Current(t)
|
||||
|
||||
dest := v2net.NewUDPDestination(v2net.IPAddress([]byte{0x20, 0x01, 0x48, 0x60, 0x48, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88, 0x88}, 53))
|
||||
dest := v2net.UDPDestination(v2net.IPAddress([]byte{0x20, 0x01, 0x48, 0x60, 0x48, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88, 0x88}), 53)
|
||||
v2netassert.Destination(dest).IsNotTCP()
|
||||
v2netassert.Destination(dest).IsUDP()
|
||||
assert.String(dest).Equals("udp:[2001:4860:4860::8888]:53")
|
||||
|
||||
Reference in new issue
Block a user