1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-01-17 23:06:30 -05:00

Add Port as a type

This commit is contained in:
V2Ray 2015-12-02 12:47:54 +01:00
parent e8e6cf1429
commit cee85bdf26
16 changed files with 125 additions and 98 deletions

View File

@ -85,7 +85,7 @@ func (this *FieldRule) Apply(dest v2net.Destination) bool {
if this.Port != nil { if this.Port != nil {
port := address.Port() port := address.Port()
if port < this.Port.From() || port > this.Port.To() { if port.Value() < this.Port.From().Value() || port.Value() > this.Port.To().Value() {
return false return false
} }
} }

View File

@ -2,7 +2,6 @@ package net
import ( import (
"net" "net"
"strconv"
"github.com/v2ray/v2ray-core/common/log" "github.com/v2ray/v2ray-core/common/log"
) )
@ -12,8 +11,7 @@ import (
type Address interface { type Address interface {
IP() net.IP // IP of this Address IP() net.IP // IP of this Address
Domain() string // Domain of this Address Domain() string // Domain of this Address
Port() uint16 // Port of this Address Port() Port // Port of this Address
PortBytes() []byte // Port in bytes, network byte order
IsIPv4() bool // True if this Address is an IPv4 address IsIPv4() bool // True if this Address is an IPv4 address
IsIPv6() bool // True if this Address is an IPv6 address IsIPv6() bool // True if this Address is an IPv6 address
@ -35,16 +33,16 @@ func allZeros(data []byte) bool {
func IPAddress(ip []byte, port uint16) Address { func IPAddress(ip []byte, port uint16) Address {
switch len(ip) { switch len(ip) {
case net.IPv4len: case net.IPv4len:
return IPv4Address{ return &IPv4Address{
PortAddress: PortAddress{port: port}, port: 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: case net.IPv6len:
if allZeros(ip[0:10]) && ip[10] == 0xff && ip[11] == 0xff { if allZeros(ip[0:10]) && ip[10] == 0xff && ip[11] == 0xff {
return IPAddress(ip[12:16], port) return IPAddress(ip[12:16], port)
} }
return IPv6Address{ return &IPv6Address{
PortAddress: PortAddress{port: port}, port: Port(port),
ip: [16]byte{ ip: [16]byte{
ip[0], ip[1], ip[2], ip[3], ip[0], ip[1], ip[2], ip[3],
ip[4], ip[5], ip[6], ip[7], ip[4], ip[5], ip[6], ip[7],
@ -60,107 +58,107 @@ func IPAddress(ip []byte, port uint16) Address {
// DomainAddress creates an Address with given domain and port. // DomainAddress creates an Address with given domain and port.
func DomainAddress(domain string, port uint16) Address { func DomainAddress(domain string, port uint16) Address {
return DomainAddressImpl{ return &DomainAddressImpl{
domain: domain, domain: domain,
PortAddress: PortAddress{port: port}, port: Port(port),
} }
} }
type PortAddress struct {
port uint16
}
func (addr PortAddress) Port() uint16 {
return addr.port
}
func (addr PortAddress) PortBytes() []byte {
return []byte{byte(addr.port >> 8), byte(addr.port)}
}
type IPv4Address struct { type IPv4Address struct {
PortAddress port Port
ip [4]byte ip [4]byte
} }
func (addr IPv4Address) IP() net.IP { func (addr *IPv4Address) IP() net.IP {
return net.IP(addr.ip[:]) return net.IP(addr.ip[:])
} }
func (addr IPv4Address) Domain() string { func (this *IPv4Address) Port() Port {
return this.port
}
func (addr *IPv4Address) Domain() string {
panic("Calling Domain() on an IPv4Address.") panic("Calling Domain() on an IPv4Address.")
} }
func (addr IPv4Address) IsIPv4() bool { func (addr *IPv4Address) IsIPv4() bool {
return true return true
} }
func (addr IPv4Address) IsIPv6() bool { func (addr *IPv4Address) IsIPv6() bool {
return false return false
} }
func (addr IPv4Address) IsDomain() bool { func (addr *IPv4Address) IsDomain() bool {
return false return false
} }
func (addr IPv4Address) String() string { func (this *IPv4Address) String() string {
return addr.IP().String() + ":" + strconv.Itoa(int(addr.PortAddress.port)) return this.IP().String() + ":" + this.port.String()
} }
type IPv6Address struct { type IPv6Address struct {
PortAddress port Port
ip [16]byte ip [16]byte
} }
func (addr IPv6Address) IP() net.IP { func (addr *IPv6Address) IP() net.IP {
return net.IP(addr.ip[:]) return net.IP(addr.ip[:])
} }
func (addr IPv6Address) Domain() string { func (this *IPv6Address) Port() Port {
return this.port
}
func (addr *IPv6Address) Domain() string {
panic("Calling Domain() on an IPv6Address.") panic("Calling Domain() on an IPv6Address.")
} }
func (addr IPv6Address) IsIPv4() bool { func (addr *IPv6Address) IsIPv4() bool {
return false return false
} }
func (addr IPv6Address) IsIPv6() bool { func (addr *IPv6Address) IsIPv6() bool {
return true return true
} }
func (addr IPv6Address) IsDomain() bool { func (addr *IPv6Address) IsDomain() bool {
return false return false
} }
func (addr IPv6Address) String() string { func (this *IPv6Address) String() string {
return "[" + addr.IP().String() + "]:" + strconv.Itoa(int(addr.PortAddress.port)) return "[" + this.IP().String() + "]:" + this.port.String()
} }
type DomainAddressImpl struct { type DomainAddressImpl struct {
PortAddress port Port
domain string domain string
} }
func (addr DomainAddressImpl) IP() net.IP { func (addr *DomainAddressImpl) IP() net.IP {
panic("Calling IP() on a DomainAddress.") panic("Calling IP() on a DomainAddress.")
} }
func (addr DomainAddressImpl) Domain() string { func (this *DomainAddressImpl) Port() Port {
return this.port
}
func (addr *DomainAddressImpl) Domain() string {
return addr.domain return addr.domain
} }
func (addr DomainAddressImpl) IsIPv4() bool { func (addr *DomainAddressImpl) IsIPv4() bool {
return false return false
} }
func (addr DomainAddressImpl) IsIPv6() bool { func (addr *DomainAddressImpl) IsIPv6() bool {
return false return false
} }
func (addr DomainAddressImpl) IsDomain() bool { func (addr *DomainAddressImpl) IsDomain() bool {
return true return true
} }
func (addr DomainAddressImpl) String() string { func (this *DomainAddressImpl) String() string {
return addr.domain + ":" + strconv.Itoa(int(addr.PortAddress.port)) return this.domain + ":" + this.port.String()
} }

View File

@ -11,14 +11,14 @@ func TestIPv4Address(t *testing.T) {
assert := unit.Assert(t) assert := unit.Assert(t)
ip := []byte{byte(1), byte(2), byte(3), byte(4)} ip := []byte{byte(1), byte(2), byte(3), byte(4)}
port := uint16(80) port := NewPort(80)
addr := IPAddress(ip, port) addr := IPAddress(ip, port.Value())
assert.Bool(addr.IsIPv4()).IsTrue() assert.Bool(addr.IsIPv4()).IsTrue()
assert.Bool(addr.IsIPv6()).IsFalse() assert.Bool(addr.IsIPv6()).IsFalse()
assert.Bool(addr.IsDomain()).IsFalse() assert.Bool(addr.IsDomain()).IsFalse()
assert.Bytes(addr.IP()).Equals(ip) assert.Bytes(addr.IP()).Equals(ip)
assert.Uint16(addr.Port()).Equals(port) assert.Uint16(addr.Port().Value()).Equals(port.Value())
assert.String(addr.String()).Equals("1.2.3.4:80") assert.String(addr.String()).Equals("1.2.3.4:80")
} }
@ -31,14 +31,14 @@ func TestIPv6Address(t *testing.T) {
byte(1), byte(2), byte(3), byte(4), byte(1), byte(2), byte(3), byte(4),
byte(1), byte(2), byte(3), byte(4), byte(1), byte(2), byte(3), byte(4),
} }
port := uint16(443) port := NewPort(443)
addr := IPAddress(ip, port) addr := IPAddress(ip, port.Value())
assert.Bool(addr.IsIPv6()).IsTrue() assert.Bool(addr.IsIPv6()).IsTrue()
assert.Bool(addr.IsIPv4()).IsFalse() assert.Bool(addr.IsIPv4()).IsFalse()
assert.Bool(addr.IsDomain()).IsFalse() assert.Bool(addr.IsDomain()).IsFalse()
assert.Bytes(addr.IP()).Equals(ip) assert.Bytes(addr.IP()).Equals(ip)
assert.Uint16(addr.Port()).Equals(port) assert.Uint16(addr.Port().Value()).Equals(port.Value())
assert.String(addr.String()).Equals("[102:304:102:304:102:304:102:304]:443") assert.String(addr.String()).Equals("[102:304:102:304:102:304:102:304]:443")
} }
@ -46,14 +46,14 @@ func TestDomainAddress(t *testing.T) {
assert := unit.Assert(t) assert := unit.Assert(t)
domain := "v2ray.com" domain := "v2ray.com"
port := uint16(443) port := NewPort(443)
addr := DomainAddress(domain, port) addr := DomainAddress(domain, port.Value())
assert.Bool(addr.IsDomain()).IsTrue() assert.Bool(addr.IsDomain()).IsTrue()
assert.Bool(addr.IsIPv4()).IsFalse() assert.Bool(addr.IsIPv4()).IsFalse()
assert.Bool(addr.IsIPv6()).IsFalse() assert.Bool(addr.IsIPv6()).IsFalse()
assert.String(addr.Domain()).Equals(domain) assert.String(addr.Domain()).Equals(domain)
assert.Uint16(addr.Port()).Equals(port) assert.Uint16(addr.Port().Value()).Equals(port.Value())
assert.String(addr.String()).Equals("v2ray.com:443") assert.String(addr.String()).Equals("v2ray.com:443")
} }
@ -61,8 +61,8 @@ func TestNetIPv4Address(t *testing.T) {
assert := unit.Assert(t) assert := unit.Assert(t)
ip := net.IPv4(1, 2, 3, 4) ip := net.IPv4(1, 2, 3, 4)
port := uint16(80) port := NewPort(80)
addr := IPAddress(ip, port) addr := IPAddress(ip, port.Value())
assert.Bool(addr.IsIPv4()).IsTrue() assert.Bool(addr.IsIPv4()).IsTrue()
assert.String(addr.String()).Equals("1.2.3.4:80") assert.String(addr.String()).Equals("1.2.3.4:80")
} }

View File

@ -7,6 +7,7 @@ import (
"strings" "strings"
"github.com/v2ray/v2ray-core/common/log" "github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
) )
var ( var (
@ -14,15 +15,15 @@ var (
) )
type PortRange struct { type PortRange struct {
from uint16 from v2net.Port
to uint16 to v2net.Port
} }
func (this *PortRange) From() uint16 { func (this *PortRange) From() v2net.Port {
return this.from return this.from
} }
func (this *PortRange) To() uint16 { func (this *PortRange) To() v2net.Port {
return this.to return this.to
} }
@ -34,8 +35,8 @@ func (this *PortRange) UnmarshalJSON(data []byte) error {
log.Error("Invalid port [%s]", string(data)) log.Error("Invalid port [%s]", string(data))
return InvalidPortRange return InvalidPortRange
} }
this.from = uint16(maybeint) this.from = v2net.Port(uint16(maybeint))
this.to = uint16(maybeint) this.to = v2net.Port(uint16(maybeint))
return nil return nil
} }
@ -49,8 +50,8 @@ func (this *PortRange) UnmarshalJSON(data []byte) error {
log.Error("Invalid from port %s", pair[0]) log.Error("Invalid from port %s", pair[0])
return InvalidPortRange return InvalidPortRange
} }
this.from = uint16(value) this.from = v2net.Port(uint16(value))
this.to = uint16(value) this.to = v2net.Port(uint16(value))
return nil return nil
} else if len(pair) == 2 { } else if len(pair) == 2 {
from, err := strconv.Atoi(pair[0]) from, err := strconv.Atoi(pair[0])
@ -58,14 +59,14 @@ func (this *PortRange) UnmarshalJSON(data []byte) error {
log.Error("Invalid from port %s", pair[0]) log.Error("Invalid from port %s", pair[0])
return InvalidPortRange return InvalidPortRange
} }
this.from = uint16(from) this.from = v2net.Port(uint16(from))
to, err := strconv.Atoi(pair[1]) to, err := strconv.Atoi(pair[1])
if err != nil || to <= 0 || to >= 65535 { if err != nil || to <= 0 || to >= 65535 {
log.Error("Invalid to port %s", pair[1]) log.Error("Invalid to port %s", pair[1])
return InvalidPortRange return InvalidPortRange
} }
this.to = uint16(to) this.to = v2net.Port(uint16(to))
if this.from > this.to { if this.from > this.to {
log.Error("Invalid port range %d -> %d", this.from, this.to) log.Error("Invalid port range %d -> %d", this.from, this.to)

View File

@ -14,8 +14,8 @@ func TestIntPort(t *testing.T) {
err := json.Unmarshal([]byte("1234"), &portRange) err := json.Unmarshal([]byte("1234"), &portRange)
assert.Error(err).IsNil() assert.Error(err).IsNil()
assert.Uint16(portRange.from).Equals(uint16(1234)) assert.Uint16(portRange.from.Value()).Equals(uint16(1234))
assert.Uint16(portRange.to).Equals(uint16(1234)) assert.Uint16(portRange.to.Value()).Equals(uint16(1234))
} }
func TestOverRangeIntPort(t *testing.T) { func TestOverRangeIntPort(t *testing.T) {
@ -36,8 +36,8 @@ func TestSingleStringPort(t *testing.T) {
err := json.Unmarshal([]byte("\"1234\""), &portRange) err := json.Unmarshal([]byte("\"1234\""), &portRange)
assert.Error(err).IsNil() assert.Error(err).IsNil()
assert.Uint16(portRange.from).Equals(uint16(1234)) assert.Uint16(portRange.from.Value()).Equals(uint16(1234))
assert.Uint16(portRange.to).Equals(uint16(1234)) assert.Uint16(portRange.to.Value()).Equals(uint16(1234))
} }
func TestStringPairPort(t *testing.T) { func TestStringPairPort(t *testing.T) {
@ -47,8 +47,8 @@ func TestStringPairPort(t *testing.T) {
err := json.Unmarshal([]byte("\"1234-5678\""), &portRange) err := json.Unmarshal([]byte("\"1234-5678\""), &portRange)
assert.Error(err).IsNil() assert.Error(err).IsNil()
assert.Uint16(portRange.from).Equals(uint16(1234)) assert.Uint16(portRange.from.Value()).Equals(uint16(1234))
assert.Uint16(portRange.to).Equals(uint16(5678)) assert.Uint16(portRange.to.Value()).Equals(uint16(5678))
} }
func TestOverRangeStringPort(t *testing.T) { func TestOverRangeStringPort(t *testing.T) {

23
common/net/port.go Normal file
View File

@ -0,0 +1,23 @@
package net
import (
"strconv"
)
type Port uint16
func NewPort(port int) Port {
return Port(uint16(port))
}
func (this Port) Value() uint16 {
return uint16(this)
}
func (this Port) Bytes() []byte {
return []byte{byte(this >> 8), byte(this)}
}
func (this Port) String() string {
return strconv.Itoa(int(this))
}

View File

@ -1,6 +1,6 @@
package net package net
type PortRange interface { type PortRange interface {
From() uint16 From() Port
To() uint16 To() Port
} }

View File

@ -1,14 +1,18 @@
package testing package testing
import (
v2net "github.com/v2ray/v2ray-core/common/net"
)
type PortRange struct { type PortRange struct {
FromValue uint16 FromValue v2net.Port
ToValue uint16 ToValue v2net.Port
} }
func (this *PortRange) From() uint16 { func (this *PortRange) From() v2net.Port {
return this.FromValue return this.FromValue
} }
func (this *PortRange) To() uint16 { func (this *PortRange) To() v2net.Port {
return this.ToValue return this.ToValue
} }

View File

@ -34,7 +34,7 @@ func (request *Socks5UDPRequest) Write(buffer *alloc.Buffer) {
case request.Address.IsDomain(): case request.Address.IsDomain():
buffer.AppendBytes(AddrTypeDomain, byte(len(request.Address.Domain()))).Append([]byte(request.Address.Domain())) buffer.AppendBytes(AddrTypeDomain, byte(len(request.Address.Domain()))).Append([]byte(request.Address.Domain()))
} }
buffer.Append(request.Address.PortBytes()) buffer.Append(request.Address.Port().Bytes())
buffer.Append(request.Data.Value) buffer.Append(request.Data.Value)
} }

View File

@ -193,7 +193,7 @@ func (this *SocksServer) handleUDP(reader *v2net.TimeOutReader, writer io.Writer
udpAddr := this.getUDPAddr() udpAddr := this.getUDPAddr()
response.Port = udpAddr.Port() response.Port = udpAddr.Port().Value()
switch { switch {
case udpAddr.IsIPv4(): case udpAddr.IsIPv4():
response.SetIPv4(udpAddr.IP()) response.SetIPv4(udpAddr.IP())

View File

@ -172,7 +172,7 @@ func (this *VMessRequest) ToBytes(idHash user.CounterHash, randomRangeInt64 user
buffer.Append(this.RequestKey) buffer.Append(this.RequestKey)
buffer.Append(this.ResponseHeader) buffer.Append(this.ResponseHeader)
buffer.AppendBytes(this.Command) buffer.AppendBytes(this.Command)
buffer.Append(this.Address.PortBytes()) buffer.Append(this.Address.Port().Bytes())
switch { switch {
case this.Address.IsIPv4(): case this.Address.IsIPv4():

View File

@ -67,7 +67,7 @@ func TestDetourConfig(t *testing.T) {
detour := detours[0] detour := detours[0]
assert.String(detour.Protocol()).Equals("dokodemo-door") assert.String(detour.Protocol()).Equals("dokodemo-door")
assert.Uint16(detour.PortRange().From()).Equals(uint16(28394)) assert.Uint16(detour.PortRange().From().Value()).Equals(uint16(28394))
assert.Uint16(detour.PortRange().To()).Equals(uint16(28394)) assert.Uint16(detour.PortRange().To().Value()).Equals(uint16(28394))
assert.Pointer(detour.Settings()).IsNotNil() assert.Pointer(detour.Settings()).IsNotNil()
} }

View File

@ -24,15 +24,15 @@ type LogConfig struct {
} }
type PortRange struct { type PortRange struct {
FromValue uint16 FromValue v2net.Port
ToValue uint16 ToValue v2net.Port
} }
func (this *PortRange) From() uint16 { func (this *PortRange) From() v2net.Port {
return this.FromValue return this.FromValue
} }
func (this *PortRange) To() uint16 { func (this *PortRange) To() v2net.Port {
return this.ToValue return this.ToValue
} }

View File

@ -2,13 +2,14 @@ package point
import ( import (
"github.com/v2ray/v2ray-core/common/log" "github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/common/retry" "github.com/v2ray/v2ray-core/common/retry"
"github.com/v2ray/v2ray-core/proxy/common/connhandler" "github.com/v2ray/v2ray-core/proxy/common/connhandler"
"github.com/v2ray/v2ray-core/shell/point/config" "github.com/v2ray/v2ray-core/shell/point/config"
) )
type InboundConnectionHandlerWithPort struct { type InboundConnectionHandlerWithPort struct {
port uint16 port v2net.Port
handler connhandler.InboundConnectionHandler handler connhandler.InboundConnectionHandler
} }
@ -45,7 +46,7 @@ func (this *InboundDetourHandler) Initialize() error {
func (this *InboundDetourHandler) Start() error { func (this *InboundDetourHandler) Start() error {
for _, ich := range this.ich { for _, ich := range this.ich {
return retry.Timed(100 /* times */, 100 /* ms */).On(func() error { return retry.Timed(100 /* times */, 100 /* ms */).On(func() error {
err := ich.handler.Listen(ich.port) err := ich.handler.Listen(ich.port.Value())
if err != nil { if err != nil {
return err return err
} }

View File

@ -40,7 +40,7 @@ func appendAddress(request []byte, address v2net.Address) []byte {
request = append(request, []byte(address.Domain())...) request = append(request, []byte(address.Domain())...)
} }
request = append(request, address.PortBytes()...) request = append(request, address.Port().Bytes()...)
return request return request
} }