refactor common/net.Port

This commit is contained in:
Darien Raymond 2015-12-02 20:44:01 +00:00
parent fa7c1069bc
commit ae056714db
33 changed files with 86 additions and 70 deletions

View File

@ -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,
}
}

View File

@ -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")
}

View File

@ -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 {

View File

@ -11,7 +11,7 @@ func Address(value v2net.Address) *AddressSubject {
}
type AddressSubject struct {
*assert.Subject
assert.Subject
value v2net.Address
}

View File

@ -11,7 +11,7 @@ func Destination(value v2net.Destination) *DestinationSubject {
}
type DestinationSubject struct {
*assert.Subject
assert.Subject
value v2net.Destination
}

View File

@ -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"))
}
}

View File

@ -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)))
}

View File

@ -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
}

View File

@ -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"`
}

View File

@ -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),

View File

@ -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,
},

View File

@ -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),

View File

@ -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())
}

View File

@ -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,
}
}

View File

@ -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})
}

View File

@ -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()

View File

@ -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:

View File

@ -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())

View File

@ -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),

View File

@ -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
}

View File

@ -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"`
}

View File

@ -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),

View File

@ -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:

View File

@ -27,7 +27,7 @@ type OutboundDetourConfig interface {
}
type PointConfig interface {
Port() uint16
Port() v2net.Port
LogConfig() LogConfig
RouterConfig() routerconfig.RouterConfig
InboundConfig() ConnectionConfig

View File

@ -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
}

View File

@ -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()

View File

@ -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
}

View File

@ -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
}

View File

@ -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

View File

@ -9,7 +9,7 @@ func Int(value int) *IntSubject {
}
type IntSubject struct {
*Subject
Subject
value int
}

View File

@ -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

View File

@ -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) {

View File

@ -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) {