diff --git a/common/net/address.go b/common/net/address.go index e210d477f..891fd5c9c 100644 --- a/common/net/address.go +++ b/common/net/address.go @@ -30,11 +30,11 @@ func allZeros(data []byte) bool { } // IPAddress creates an Address with given IP and port. -func IPAddress(ip []byte, port uint16) Address { +func IPAddress(ip []byte, port Port) Address { switch len(ip) { case net.IPv4len: return &IPv4Address{ - port: Port(port), + port: port, ip: [4]byte{ip[0], ip[1], ip[2], ip[3]}, } case net.IPv6len: @@ -57,10 +57,10 @@ func IPAddress(ip []byte, port uint16) Address { } // DomainAddress creates an Address with given domain and port. -func DomainAddress(domain string, port uint16) Address { +func DomainAddress(domain string, port Port) Address { return &DomainAddressImpl{ domain: domain, - port: Port(port), + port: port, } } diff --git a/common/net/address_test.go b/common/net/address_test.go index 6319acb9e..d40e0c14b 100644 --- a/common/net/address_test.go +++ b/common/net/address_test.go @@ -14,8 +14,8 @@ func TestIPv4Address(t *testing.T) { v2testing.Current(t) ip := []byte{byte(1), byte(2), byte(3), byte(4)} - port := v2net.NewPort(80) - addr := v2net.IPAddress(ip, port.Value()) + port := v2net.Port(80) + addr := v2net.IPAddress(ip, port) v2netassert.Address(addr).IsIPv4() v2netassert.Address(addr).IsNotIPv6() @@ -34,8 +34,8 @@ func TestIPv6Address(t *testing.T) { byte(1), byte(2), byte(3), byte(4), byte(1), byte(2), byte(3), byte(4), } - port := v2net.NewPort(443) - addr := v2net.IPAddress(ip, port.Value()) + port := v2net.Port(443) + addr := v2net.IPAddress(ip, port) v2netassert.Address(addr).IsIPv6() v2netassert.Address(addr).IsNotIPv4() @@ -49,8 +49,8 @@ func TestDomainAddress(t *testing.T) { v2testing.Current(t) domain := "v2ray.com" - port := v2net.NewPort(443) - addr := v2net.DomainAddress(domain, port.Value()) + port := v2net.Port(443) + addr := v2net.DomainAddress(domain, port) v2netassert.Address(addr).IsDomain() v2netassert.Address(addr).IsNotIPv6() @@ -64,8 +64,8 @@ func TestNetIPv4Address(t *testing.T) { v2testing.Current(t) ip := net.IPv4(1, 2, 3, 4) - port := v2net.NewPort(80) - addr := v2net.IPAddress(ip, port.Value()) + port := v2net.Port(80) + addr := v2net.IPAddress(ip, port) v2netassert.Address(addr).IsIPv4() assert.String(addr).Equals("1.2.3.4:80") } diff --git a/common/net/port.go b/common/net/port.go index a2edf2719..eb62704f2 100644 --- a/common/net/port.go +++ b/common/net/port.go @@ -6,8 +6,8 @@ import ( type Port uint16 -func NewPort(port int) Port { - return Port(uint16(port)) +func PortFromBytes(port []byte) Port { + return Port(uint16(port[0])<<8 + uint16(port[1])) } func (this Port) Value() uint16 { diff --git a/common/net/testing/assert/address.go b/common/net/testing/assert/address.go index b0d22652d..2c82f205f 100644 --- a/common/net/testing/assert/address.go +++ b/common/net/testing/assert/address.go @@ -11,7 +11,7 @@ func Address(value v2net.Address) *AddressSubject { } type AddressSubject struct { - *assert.Subject + assert.Subject value v2net.Address } diff --git a/common/net/testing/assert/destination.go b/common/net/testing/assert/destination.go index 054ca3520..5e0184555 100644 --- a/common/net/testing/assert/destination.go +++ b/common/net/testing/assert/destination.go @@ -11,7 +11,7 @@ func Destination(value v2net.Destination) *DestinationSubject { } type DestinationSubject struct { - *assert.Subject + assert.Subject value v2net.Destination } diff --git a/common/net/testing/assert/port.go b/common/net/testing/assert/port.go index 6a697bcaa..6c0e16e00 100644 --- a/common/net/testing/assert/port.go +++ b/common/net/testing/assert/port.go @@ -2,6 +2,7 @@ package assert import ( v2net "github.com/v2ray/v2ray-core/common/net" + "github.com/v2ray/v2ray-core/common/serial" "github.com/v2ray/v2ray-core/testing/assert" ) @@ -10,7 +11,7 @@ func Port(value v2net.Port) *PortSubject { } type PortSubject struct { - *assert.Subject + assert.Subject value v2net.Port } @@ -40,3 +41,9 @@ func (subject *PortSubject) LessThan(expectation v2net.Port) { subject.Fail(subject.DisplayString(), "is less than", expectation) } } + +func (subject *PortSubject) IsValid() { + if subject.value == 0 { + subject.Fail(subject.DisplayString(), "is", serial.StringLiteral("a valid port")) + } +} diff --git a/common/net/testing/port.go b/common/net/testing/port.go index 7bf6a0842..ebdf370da 100644 --- a/common/net/testing/port.go +++ b/common/net/testing/port.go @@ -2,12 +2,14 @@ package testing import ( "sync/atomic" + + v2net "github.com/v2ray/v2ray-core/common/net" ) var ( port = int32(30000) ) -func PickPort() uint16 { - return uint16(atomic.AddInt32(&port, 1)) +func PickPort() v2net.Port { + return v2net.Port(uint16(atomic.AddInt32(&port, 1))) } diff --git a/proxy/common/connhandler/inbound_connection.go b/proxy/common/connhandler/inbound_connection.go index 6afb3bb52..7aaa997ac 100644 --- a/proxy/common/connhandler/inbound_connection.go +++ b/proxy/common/connhandler/inbound_connection.go @@ -2,6 +2,7 @@ package connhandler import ( "github.com/v2ray/v2ray-core/app" + v2net "github.com/v2ray/v2ray-core/common/net" ) // A InboundConnectionHandlerFactory creates InboundConnectionHandler on demand. @@ -14,5 +15,5 @@ type InboundConnectionHandlerFactory interface { type InboundConnectionHandler interface { // Listen starts a InboundConnectionHandler by listen on a specific port. This method is called // exactly once during runtime. - Listen(port uint16) error + Listen(port v2net.Port) error } diff --git a/proxy/dokodemo/config/json/json.go b/proxy/dokodemo/config/json/json.go index 6015dd6b6..04416b9ad 100644 --- a/proxy/dokodemo/config/json/json.go +++ b/proxy/dokodemo/config/json/json.go @@ -1,13 +1,14 @@ package json import ( + v2net "github.com/v2ray/v2ray-core/common/net" v2netjson "github.com/v2ray/v2ray-core/common/net/json" "github.com/v2ray/v2ray-core/proxy/common/config/json" ) type DokodemoConfig struct { Host string `json:"address"` - Port int `json:"port"` + Port v2net.Port `json:"port"` Network *v2netjson.NetworkList `json:"network"` Timeout int `json:"timeout"` } diff --git a/proxy/dokodemo/dokodemo.go b/proxy/dokodemo/dokodemo.go index ff8488bc0..b96fa0268 100644 --- a/proxy/dokodemo/dokodemo.go +++ b/proxy/dokodemo/dokodemo.go @@ -27,14 +27,14 @@ func NewDokodemoDoor(dispatcher app.PacketDispatcher, config *json.DokodemoConfi } ip := net.ParseIP(config.Host) if ip != nil { - d.address = v2net.IPAddress(ip, uint16(config.Port)) + d.address = v2net.IPAddress(ip, config.Port) } else { - d.address = v2net.DomainAddress(config.Host, uint16(config.Port)) + d.address = v2net.DomainAddress(config.Host, config.Port) } return d } -func (this *DokodemoDoor) Listen(port uint16) error { +func (this *DokodemoDoor) Listen(port v2net.Port) error { this.accepting = true if this.config.Network.HasNetwork(v2net.TCPNetwork) { @@ -52,7 +52,7 @@ func (this *DokodemoDoor) Listen(port uint16) error { return nil } -func (this *DokodemoDoor) ListenUDP(port uint16) error { +func (this *DokodemoDoor) ListenUDP(port v2net.Port) error { udpConn, err := net.ListenUDP("udp", &net.UDPAddr{ IP: []byte{0, 0, 0, 0}, Port: int(port), @@ -88,7 +88,7 @@ func (this *DokodemoDoor) handleUDPPackets(udpConn *net.UDPConn) { } } -func (this *DokodemoDoor) ListenTCP(port uint16) error { +func (this *DokodemoDoor) ListenTCP(port v2net.Port) error { tcpListener, err := net.ListenTCP("tcp", &net.TCPAddr{ IP: []byte{0, 0, 0, 0}, Port: int(port), diff --git a/proxy/dokodemo/dokodemo_test.go b/proxy/dokodemo/dokodemo_test.go index ab1a60ddd..baae4401f 100644 --- a/proxy/dokodemo/dokodemo_test.go +++ b/proxy/dokodemo/dokodemo_test.go @@ -43,7 +43,7 @@ func TestDokodemoTCP(t *testing.T) { ProtocolValue: "dokodemo-door", SettingsValue: &json.DokodemoConfig{ Host: "127.0.0.1", - Port: int(port), + Port: port, Network: &networkList, Timeout: 0, }, @@ -105,7 +105,7 @@ func TestDokodemoUDP(t *testing.T) { ProtocolValue: "dokodemo-door", SettingsValue: &json.DokodemoConfig{ Host: "127.0.0.1", - Port: int(port), + Port: port, Network: &networkList, Timeout: 0, }, diff --git a/proxy/http/http.go b/proxy/http/http.go index 881c5d27a..e527846b5 100644 --- a/proxy/http/http.go +++ b/proxy/http/http.go @@ -6,6 +6,7 @@ import ( "github.com/v2ray/v2ray-core/app" "github.com/v2ray/v2ray-core/common/log" + v2net "github.com/v2ray/v2ray-core/common/net" jsonconfig "github.com/v2ray/v2ray-core/proxy/http/config/json" ) @@ -22,7 +23,7 @@ func NewHttpProxyServer(dispatcher app.PacketDispatcher, config *jsonconfig.Http } } -func (server *HttpProxyServer) Listen(port uint16) error { +func (server *HttpProxyServer) Listen(port v2net.Port) error { _, err := net.ListenTCP("tcp", &net.TCPAddr{ IP: []byte{0, 0, 0, 0}, Port: int(port), diff --git a/proxy/socks/protocol/socks.go b/proxy/socks/protocol/socks.go index a1da8ec0d..8210b2308 100644 --- a/proxy/socks/protocol/socks.go +++ b/proxy/socks/protocol/socks.go @@ -1,7 +1,6 @@ package protocol import ( - "encoding/binary" "io" "github.com/v2ray/v2ray-core/common/alloc" @@ -57,7 +56,7 @@ func ReadAuthentication(reader io.Reader) (auth Socks5AuthenticationRequest, aut if buffer.Value[0] == socks4Version { auth4.Version = buffer.Value[0] auth4.Command = buffer.Value[1] - auth4.Port = binary.BigEndian.Uint16(buffer.Value[2:4]) + auth4.Port = v2net.PortFromBytes(buffer.Value[2:4]) copy(auth4.IP[:], buffer.Value[4:8]) err = Socks4Downgrade return @@ -184,7 +183,7 @@ type Socks5Request struct { IPv4 [4]byte Domain string IPv6 [16]byte - Port uint16 + Port v2net.Port } func ReadRequest(reader io.Reader) (request *Socks5Request, err error) { @@ -256,7 +255,7 @@ func ReadRequest(reader io.Reader) (request *Socks5Request, err error) { return } - request.Port = binary.BigEndian.Uint16(buffer.Value[:2]) + request.Port = v2net.PortFromBytes(buffer.Value[:2]) return } @@ -294,7 +293,7 @@ type Socks5Response struct { IPv4 [4]byte Domain string IPv6 [16]byte - Port uint16 + Port v2net.Port } func NewSocks5Response() *Socks5Response { @@ -329,5 +328,5 @@ func (r *Socks5Response) Write(buffer *alloc.Buffer) { case 0x04: buffer.Append(r.IPv6[:]) } - buffer.AppendBytes(byte(r.Port>>8), byte(r.Port)) + buffer.Append(r.Port.Bytes()) } diff --git a/proxy/socks/protocol/socks4.go b/proxy/socks/protocol/socks4.go index a1c42217b..5ebb00717 100644 --- a/proxy/socks/protocol/socks4.go +++ b/proxy/socks/protocol/socks4.go @@ -4,6 +4,7 @@ import ( "errors" "github.com/v2ray/v2ray-core/common/alloc" + v2net "github.com/v2ray/v2ray-core/common/net" ) var ( @@ -13,7 +14,7 @@ var ( type Socks4AuthenticationRequest struct { Version byte Command byte - Port uint16 + Port v2net.Port IP [4]byte } @@ -23,10 +24,10 @@ type Socks4AuthenticationResponse struct { ip []byte } -func NewSocks4AuthenticationResponse(result byte, port uint16, ip []byte) *Socks4AuthenticationResponse { +func NewSocks4AuthenticationResponse(result byte, port v2net.Port, ip []byte) *Socks4AuthenticationResponse { return &Socks4AuthenticationResponse{ result: result, - port: port, + port: port.Value(), ip: ip, } } diff --git a/proxy/socks/protocol/socks4_test.go b/proxy/socks/protocol/socks4_test.go index 88572b544..325e859db 100644 --- a/proxy/socks/protocol/socks4_test.go +++ b/proxy/socks/protocol/socks4_test.go @@ -5,6 +5,8 @@ import ( "testing" "github.com/v2ray/v2ray-core/common/alloc" + v2net "github.com/v2ray/v2ray-core/common/net" + v2netassert "github.com/v2ray/v2ray-core/common/net/testing/assert" v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) @@ -22,7 +24,7 @@ func TestSocks4AuthenticationRequestRead(t *testing.T) { assert.Error(err).Equals(Socks4Downgrade) assert.Byte(request4.Version).Named("Version").Equals(0x04) assert.Byte(request4.Command).Named("Command").Equals(0x01) - assert.Uint16(request4.Port).Named("Port").Equals(53) + v2netassert.Port(request4.Port).Named("Port").Equals(v2net.Port(53)) assert.Bytes(request4.IP[:]).Named("IP").Equals([]byte{0x72, 0x72, 0x72, 0x72}) } diff --git a/proxy/socks/protocol/socks_test.go b/proxy/socks/protocol/socks_test.go index 759edaf7d..d904849e0 100644 --- a/proxy/socks/protocol/socks_test.go +++ b/proxy/socks/protocol/socks_test.go @@ -6,6 +6,8 @@ import ( "testing" "github.com/v2ray/v2ray-core/common/alloc" + v2net "github.com/v2ray/v2ray-core/common/net" + v2netassert "github.com/v2ray/v2ray-core/common/net/testing/assert" v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" "github.com/v2ray/v2ray-core/transport" @@ -68,7 +70,7 @@ func TestRequestRead(t *testing.T) { assert.Byte(request.Command).Named("Command").Equals(0x01) assert.Byte(request.AddrType).Named("Address Type").Equals(0x01) assert.Bytes(request.IPv4[:]).Named("IPv4").Equals([]byte{0x72, 0x72, 0x72, 0x72}) - assert.Uint16(request.Port).Named("Port").Equals(53) + v2netassert.Port(request.Port).Named("Port").Equals(v2net.Port(53)) } func TestResponseWrite(t *testing.T) { @@ -81,7 +83,7 @@ func TestResponseWrite(t *testing.T) { [4]byte{0x72, 0x72, 0x72, 0x72}, "", [16]byte{}, - uint16(53), + v2net.Port(53), } buffer := alloc.NewSmallBuffer().Clear() defer buffer.Release() diff --git a/proxy/socks/protocol/udp.go b/proxy/socks/protocol/udp.go index 6f331cfd1..82af3c4f4 100644 --- a/proxy/socks/protocol/udp.go +++ b/proxy/socks/protocol/udp.go @@ -1,7 +1,6 @@ package protocol import ( - "encoding/binary" "errors" "github.com/v2ray/v2ray-core/common/alloc" @@ -56,7 +55,7 @@ func ReadUDPRequest(packet []byte) (*Socks5UDPRequest, error) { return nil, transport.CorruptedPacket } ip := packet[4:8] - port := binary.BigEndian.Uint16(packet[8:10]) + port := v2net.PortFromBytes(packet[8:10]) request.Address = v2net.IPAddress(ip, port) dataBegin = 10 case AddrTypeIPv6: @@ -64,7 +63,7 @@ func ReadUDPRequest(packet []byte) (*Socks5UDPRequest, error) { return nil, transport.CorruptedPacket } ip := packet[4:20] - port := binary.BigEndian.Uint16(packet[20:22]) + port := v2net.PortFromBytes(packet[20:22]) request.Address = v2net.IPAddress(ip, port) dataBegin = 22 case AddrTypeDomain: @@ -73,7 +72,7 @@ func ReadUDPRequest(packet []byte) (*Socks5UDPRequest, error) { return nil, transport.CorruptedPacket } domain := string(packet[5 : 5+domainLength]) - port := binary.BigEndian.Uint16(packet[5+domainLength : 5+domainLength+2]) + port := v2net.PortFromBytes(packet[5+domainLength : 5+domainLength+2]) request.Address = v2net.DomainAddress(domain, port) dataBegin = 5 + domainLength + 2 default: diff --git a/proxy/socks/socks.go b/proxy/socks/socks.go index 202b1fe78..768a6bf84 100644 --- a/proxy/socks/socks.go +++ b/proxy/socks/socks.go @@ -36,7 +36,7 @@ func NewSocksServer(dispatcher app.PacketDispatcher, config *jsonconfig.SocksCon } } -func (this *SocksServer) Listen(port uint16) error { +func (this *SocksServer) Listen(port v2net.Port) error { listener, err := net.ListenTCP("tcp", &net.TCPAddr{ IP: []byte{0, 0, 0, 0}, Port: int(port), @@ -145,7 +145,7 @@ func (this *SocksServer) handleSocks5(reader *v2net.TimeOutReader, writer io.Wri if request.Command == protocol.CmdBind || request.Command == protocol.CmdUdpAssociate { response := protocol.NewSocks5Response() response.Error = protocol.ErrorCommandNotSupported - response.Port = uint16(0) + response.Port = v2net.Port(0) response.SetIPv4([]byte{0, 0, 0, 0}) responseBuffer := alloc.NewSmallBuffer().Clear() @@ -164,7 +164,7 @@ func (this *SocksServer) handleSocks5(reader *v2net.TimeOutReader, writer io.Wri response.Error = protocol.ErrorSuccess // Some SOCKS software requires a value other than dest. Let's fake one: - response.Port = uint16(1717) + response.Port = v2net.Port(1717) response.SetIPv4([]byte{0, 0, 0, 0}) responseBuffer := alloc.NewSmallBuffer().Clear() @@ -193,7 +193,7 @@ func (this *SocksServer) handleUDP(reader *v2net.TimeOutReader, writer io.Writer udpAddr := this.getUDPAddr() - response.Port = udpAddr.Port().Value() + response.Port = udpAddr.Port() switch { case udpAddr.IsIPv4(): response.SetIPv4(udpAddr.IP()) diff --git a/proxy/socks/udp.go b/proxy/socks/udp.go index efeee1dfb..0c215d689 100644 --- a/proxy/socks/udp.go +++ b/proxy/socks/udp.go @@ -11,7 +11,7 @@ import ( var udpAddress v2net.Address -func (this *SocksServer) ListenUDP(port uint16) error { +func (this *SocksServer) ListenUDP(port v2net.Port) error { addr := &net.UDPAddr{ IP: net.IP{0, 0, 0, 0}, Port: int(port), diff --git a/proxy/testing/mocks/inboundhandler.go b/proxy/testing/mocks/inboundhandler.go index 6b9741147..920659a02 100644 --- a/proxy/testing/mocks/inboundhandler.go +++ b/proxy/testing/mocks/inboundhandler.go @@ -10,13 +10,13 @@ import ( ) type InboundConnectionHandler struct { - Port uint16 + Port v2net.Port Dispatcher app.PacketDispatcher ConnInput io.Reader ConnOutput io.Writer } -func (this *InboundConnectionHandler) Listen(port uint16) error { +func (this *InboundConnectionHandler) Listen(port v2net.Port) error { this.Port = port return nil } diff --git a/proxy/vmess/config/json/outbound.go b/proxy/vmess/config/json/outbound.go index 5dcf48f99..d13eb6fd7 100644 --- a/proxy/vmess/config/json/outbound.go +++ b/proxy/vmess/config/json/outbound.go @@ -13,7 +13,7 @@ import ( type RawConfigTarget struct { Address string `json:"address"` - Port uint16 `json:"port"` + Port v2net.Port `json:"port"` Users []*ConfigUser `json:"users"` } diff --git a/proxy/vmess/inbound.go b/proxy/vmess/inbound.go index 67dd77c40..eabafcf01 100644 --- a/proxy/vmess/inbound.go +++ b/proxy/vmess/inbound.go @@ -32,7 +32,7 @@ func NewVMessInboundHandler(dispatcher app.PacketDispatcher, clients user.UserSe } } -func (this *VMessInboundHandler) Listen(port uint16) error { +func (this *VMessInboundHandler) Listen(port v2net.Port) error { listener, err := net.ListenTCP("tcp", &net.TCPAddr{ IP: []byte{0, 0, 0, 0}, Port: int(port), diff --git a/proxy/vmess/protocol/vmess.go b/proxy/vmess/protocol/vmess.go index 7e30ca657..b9b371d57 100644 --- a/proxy/vmess/protocol/vmess.go +++ b/proxy/vmess/protocol/vmess.go @@ -106,7 +106,7 @@ func (this *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) { request.ResponseHeader = buffer.Value[33:37] // 4 bytes request.Command = buffer.Value[37] - port := binary.BigEndian.Uint16(buffer.Value[38:40]) + port := v2net.PortFromBytes(buffer.Value[38:40]) switch buffer.Value[40] { case addrTypeIPv4: diff --git a/shell/point/config/config.go b/shell/point/config/config.go index 2e524ecd8..0490ed4f3 100644 --- a/shell/point/config/config.go +++ b/shell/point/config/config.go @@ -27,7 +27,7 @@ type OutboundDetourConfig interface { } type PointConfig interface { - Port() uint16 + Port() v2net.Port LogConfig() LogConfig RouterConfig() routerconfig.RouterConfig InboundConfig() ConnectionConfig diff --git a/shell/point/config/json/json.go b/shell/point/config/json/json.go index ba6dfd0e7..f965ce0fe 100644 --- a/shell/point/config/json/json.go +++ b/shell/point/config/json/json.go @@ -8,13 +8,14 @@ import ( routerconfig "github.com/v2ray/v2ray-core/app/router/config" routerconfigjson "github.com/v2ray/v2ray-core/app/router/config/json" "github.com/v2ray/v2ray-core/common/log" + v2net "github.com/v2ray/v2ray-core/common/net" proxyconfig "github.com/v2ray/v2ray-core/proxy/common/config" "github.com/v2ray/v2ray-core/shell/point/config" ) // Config is the config for Point server. type Config struct { - PortValue uint16 `json:"port"` // Port of this Point server. + PortValue v2net.Port `json:"port"` // Port of this Point server. LogConfigValue *LogConfig `json:"log"` RouterConfigValue *routerconfigjson.RouterConfig `json:"routing"` InboundConfigValue *ConnectionConfig `json:"inbound"` @@ -23,7 +24,7 @@ type Config struct { OutboundDetoursValue []*OutboundDetourConfig `json:"outboundDetour"` } -func (config *Config) Port() uint16 { +func (config *Config) Port() v2net.Port { return config.PortValue } diff --git a/shell/point/config/json/json_test.go b/shell/point/config/json/json_test.go index 331a9073e..4b55a918b 100644 --- a/shell/point/config/json/json_test.go +++ b/shell/point/config/json/json_test.go @@ -23,7 +23,7 @@ func TestClientSampleConfig(t *testing.T) { pointConfig, err := json.LoadConfig(filepath.Join(baseDir, "vpoint_socks_vmess.json")) assert.Error(err).IsNil() - assert.Uint16(pointConfig.Port()).Positive() + assert.Uint16(pointConfig.Port().Value()).Positive() assert.Pointer(pointConfig.InboundConfig()).IsNotNil() assert.Pointer(pointConfig.OutboundConfig()).IsNotNil() @@ -43,7 +43,7 @@ func TestServerSampleConfig(t *testing.T) { pointConfig, err := json.LoadConfig(filepath.Join(baseDir, "vpoint_vmess_freedom.json")) assert.Error(err).IsNil() - assert.Uint16(pointConfig.Port()).Positive() + assert.Uint16(pointConfig.Port().Value()).Positive() assert.Pointer(pointConfig.InboundConfig()).IsNotNil() assert.Pointer(pointConfig.OutboundConfig()).IsNotNil() diff --git a/shell/point/config/testing/mocks/config.go b/shell/point/config/testing/mocks/config.go index 7246b7bfb..144694df8 100644 --- a/shell/point/config/testing/mocks/config.go +++ b/shell/point/config/testing/mocks/config.go @@ -59,7 +59,7 @@ func (config *LogConfig) AccessLog() string { } type Config struct { - PortValue uint16 + PortValue v2net.Port LogConfigValue *LogConfig RouterConfigValue routerconfig.RouterConfig InboundConfigValue *ConnectionConfig @@ -68,7 +68,7 @@ type Config struct { OutboundDetoursValue []*OutboundDetourConfig } -func (config *Config) Port() uint16 { +func (config *Config) Port() v2net.Port { return config.PortValue } diff --git a/shell/point/inbound_detour.go b/shell/point/inbound_detour.go index 3aa7d41b6..2ee0bbf16 100644 --- a/shell/point/inbound_detour.go +++ b/shell/point/inbound_detour.go @@ -46,7 +46,7 @@ func (this *InboundDetourHandler) Initialize() error { func (this *InboundDetourHandler) Start() error { for _, ich := range this.ich { return retry.Timed(100 /* times */, 100 /* ms */).On(func() error { - err := ich.handler.Listen(ich.port.Value()) + err := ich.handler.Listen(ich.port) if err != nil { return err } diff --git a/shell/point/point.go b/shell/point/point.go index 41e24f9bb..5617b27dd 100644 --- a/shell/point/point.go +++ b/shell/point/point.go @@ -12,7 +12,7 @@ import ( // Point is an single server in V2Ray system. type Point struct { - port uint16 + port v2net.Port ich connhandler.InboundConnectionHandler och connhandler.OutboundConnectionHandler idh []*InboundDetourHandler diff --git a/testing/assert/intsubject.go b/testing/assert/intsubject.go index fe52adcec..6b3c27238 100644 --- a/testing/assert/intsubject.go +++ b/testing/assert/intsubject.go @@ -9,7 +9,7 @@ func Int(value int) *IntSubject { } type IntSubject struct { - *Subject + Subject value int } diff --git a/testing/scenarios/socks5_helper.go b/testing/scenarios/socks5_helper.go index 7b09ece6b..91f69be6b 100644 --- a/testing/scenarios/socks5_helper.go +++ b/testing/scenarios/socks5_helper.go @@ -58,7 +58,7 @@ func socks5UDPRequest(address v2net.Address, payload []byte) []byte { return request } -func setUpV2Ray() (uint16, error) { +func setUpV2Ray() (v2net.Port, error) { id1, err := config.NewID("ad937d9d-6e23-4a5a-ba23-bce5092a7c51") if err != nil { return 0, err diff --git a/testing/servers/tcp/tcp.go b/testing/servers/tcp/tcp.go index d9693e5e2..924454807 100644 --- a/testing/servers/tcp/tcp.go +++ b/testing/servers/tcp/tcp.go @@ -9,7 +9,7 @@ import ( ) type Server struct { - Port uint16 + Port v2net.Port MsgProcessor func(msg []byte) []byte } @@ -24,7 +24,7 @@ func (server *Server) Start() (v2net.Address, error) { } go server.acceptConnections(listener) localAddr := listener.Addr().(*net.TCPAddr) - return v2net.IPAddress(localAddr.IP, uint16(localAddr.Port)), nil + return v2net.IPAddress(localAddr.IP, v2net.Port(localAddr.Port)), nil } func (server *Server) acceptConnections(listener *net.TCPListener) { diff --git a/testing/servers/udp/udp.go b/testing/servers/udp/udp.go index 76bf3bf49..ecfe59757 100644 --- a/testing/servers/udp/udp.go +++ b/testing/servers/udp/udp.go @@ -8,7 +8,7 @@ import ( ) type Server struct { - Port uint16 + Port v2net.Port MsgProcessor func(msg []byte) []byte } @@ -23,7 +23,7 @@ func (server *Server) Start() (v2net.Address, error) { } go server.handleConnection(conn) localAddr := conn.LocalAddr().(*net.UDPAddr) - return v2net.IPAddress(localAddr.IP, uint16(localAddr.Port)), nil + return v2net.IPAddress(localAddr.IP, v2net.Port(localAddr.Port)), nil } func (server *Server) handleConnection(conn *net.UDPConn) {