1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-01-04 16:37:12 -05:00

remove duplicated address type def

This commit is contained in:
Darien Raymond 2017-10-21 21:59:46 +02:00
parent f1a15e92f5
commit 24089bfad0
5 changed files with 23 additions and 26 deletions

View File

@ -4,6 +4,7 @@ import (
"v2ray.com/core/common/bitmask" "v2ray.com/core/common/bitmask"
"v2ray.com/core/common/buf" "v2ray.com/core/common/buf"
"v2ray.com/core/common/net" "v2ray.com/core/common/net"
"v2ray.com/core/common/protocol"
"v2ray.com/core/common/serial" "v2ray.com/core/common/serial"
) )
@ -27,14 +28,6 @@ const (
TargetNetworkUDP TargetNetwork = 0x02 TargetNetworkUDP TargetNetwork = 0x02
) )
type AddressType byte
const (
AddressTypeIPv4 AddressType = 0x01
AddressTypeDomain AddressType = 0x02
AddressTypeIPv6 AddressType = 0x03
)
/* /*
Frame format Frame format
2 bytes - length 2 bytes - length
@ -79,16 +72,16 @@ func (f FrameMetadata) AsSupplier() buf.Supplier {
addr := f.Target.Address addr := f.Target.Address
switch addr.Family() { switch addr.Family() {
case net.AddressFamilyIPv4: case net.AddressFamilyIPv4:
b = append(b, byte(AddressTypeIPv4)) b = append(b, byte(protocol.AddressTypeIPv4))
b = append(b, addr.IP()...) b = append(b, addr.IP()...)
length += 5 length += 5
case net.AddressFamilyIPv6: case net.AddressFamilyIPv6:
b = append(b, byte(AddressTypeIPv6)) b = append(b, byte(protocol.AddressTypeIPv6))
b = append(b, addr.IP()...) b = append(b, addr.IP()...)
length += 17 length += 17
case net.AddressFamilyDomain: case net.AddressFamilyDomain:
nDomain := len(addr.Domain()) nDomain := len(addr.Domain())
b = append(b, byte(AddressTypeDomain), byte(nDomain)) b = append(b, byte(protocol.AddressTypeDomain), byte(nDomain))
b = append(b, addr.Domain()...) b = append(b, addr.Domain()...)
length += nDomain + 2 length += nDomain + 2
} }
@ -115,18 +108,18 @@ func ReadFrameFrom(b []byte) (*FrameMetadata, error) {
if f.SessionStatus == SessionStatusNew { if f.SessionStatus == SessionStatusNew {
network := TargetNetwork(b[0]) network := TargetNetwork(b[0])
port := net.PortFromBytes(b[1:3]) port := net.PortFromBytes(b[1:3])
addrType := AddressType(b[3]) addrType := protocol.AddressType(b[3])
b = b[4:] b = b[4:]
var addr net.Address var addr net.Address
switch addrType { switch addrType {
case AddressTypeIPv4: case protocol.AddressTypeIPv4:
addr = net.IPAddress(b[0:4]) addr = net.IPAddress(b[0:4])
b = b[4:] b = b[4:]
case AddressTypeIPv6: case protocol.AddressTypeIPv6:
addr = net.IPAddress(b[0:16]) addr = net.IPAddress(b[0:16])
b = b[16:] b = b[16:]
case AddressTypeDomain: case protocol.AddressTypeDomain:
nDomain := int(b[0]) nDomain := int(b[0])
addr = net.DomainAddress(string(b[1 : 1+nDomain])) addr = net.DomainAddress(string(b[1 : 1+nDomain]))
b = b[nDomain+1:] b = b[nDomain+1:]

View File

@ -6,3 +6,11 @@ const (
TransferTypeStream TransferType = 0 TransferTypeStream TransferType = 0
TransferTypePacket TransferType = 1 TransferTypePacket TransferType = 1
) )
type AddressType byte
const (
AddressTypeIPv4 AddressType = 1
AddressTypeDomain AddressType = 2
AddressTypeIPv6 AddressType = 3
)

View File

@ -89,13 +89,13 @@ func (c *ClientSession) EncodeRequestHeader(header *protocol.RequestHeader, writ
switch header.Address.Family() { switch header.Address.Family() {
case net.AddressFamilyIPv4: case net.AddressFamilyIPv4:
buffer = append(buffer, AddrTypeIPv4) buffer = append(buffer, byte(protocol.AddressTypeIPv4))
buffer = append(buffer, header.Address.IP()...) buffer = append(buffer, header.Address.IP()...)
case net.AddressFamilyIPv6: case net.AddressFamilyIPv6:
buffer = append(buffer, AddrTypeIPv6) buffer = append(buffer, byte(protocol.AddressTypeIPv6))
buffer = append(buffer, header.Address.IP()...) buffer = append(buffer, header.Address.IP()...)
case net.AddressFamilyDomain: case net.AddressFamilyDomain:
buffer = append(buffer, AddrTypeDomain, byte(len(header.Address.Domain()))) buffer = append(buffer, byte(protocol.AddressTypeDomain), byte(len(header.Address.Domain())))
buffer = append(buffer, header.Address.Domain()...) buffer = append(buffer, header.Address.Domain()...)
} }
} }

View File

@ -2,8 +2,4 @@ package encoding
const ( const (
Version = byte(1) Version = byte(1)
AddrTypeIPv4 = byte(0x01)
AddrTypeIPv6 = byte(0x03)
AddrTypeDomain = byte(0x02)
) )

View File

@ -175,22 +175,22 @@ func (s *ServerSession) DecodeRequestHeader(reader io.Reader) (*protocol.Request
if request.Command != protocol.RequestCommandMux { if request.Command != protocol.RequestCommandMux {
request.Port = net.PortFromBytes(buffer[38:40]) request.Port = net.PortFromBytes(buffer[38:40])
switch buffer[40] { switch protocol.AddressType(buffer[40]) {
case AddrTypeIPv4: case protocol.AddressTypeIPv4:
_, err = io.ReadFull(decryptor, buffer[41:45]) // 4 bytes _, err = io.ReadFull(decryptor, buffer[41:45]) // 4 bytes
bufferLen += 4 bufferLen += 4
if err != nil { if err != nil {
return nil, newError("failed to read IPv4 address").Base(err) return nil, newError("failed to read IPv4 address").Base(err)
} }
request.Address = net.IPAddress(buffer[41:45]) request.Address = net.IPAddress(buffer[41:45])
case AddrTypeIPv6: case protocol.AddressTypeIPv6:
_, err = io.ReadFull(decryptor, buffer[41:57]) // 16 bytes _, err = io.ReadFull(decryptor, buffer[41:57]) // 16 bytes
bufferLen += 16 bufferLen += 16
if err != nil { if err != nil {
return nil, newError("failed to read IPv6 address").Base(err) return nil, newError("failed to read IPv6 address").Base(err)
} }
request.Address = net.IPAddress(buffer[41:57]) request.Address = net.IPAddress(buffer[41:57])
case AddrTypeDomain: case protocol.AddressTypeDomain:
_, err = io.ReadFull(decryptor, buffer[41:42]) _, err = io.ReadFull(decryptor, buffer[41:42])
if err != nil { if err != nil {
return nil, newError("failed to read domain address").Base(err) return nil, newError("failed to read domain address").Base(err)