From 24089bfad0caeba876f6bacf0f6b947c19d2e7ba Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Sat, 21 Oct 2017 21:59:46 +0200 Subject: [PATCH] remove duplicated address type def --- app/proxyman/mux/frame.go | 23 ++++++++--------------- common/protocol/payload.go | 8 ++++++++ proxy/vmess/encoding/client.go | 6 +++--- proxy/vmess/encoding/const.go | 4 ---- proxy/vmess/encoding/server.go | 8 ++++---- 5 files changed, 23 insertions(+), 26 deletions(-) diff --git a/app/proxyman/mux/frame.go b/app/proxyman/mux/frame.go index b31ffed4f..f742c885c 100644 --- a/app/proxyman/mux/frame.go +++ b/app/proxyman/mux/frame.go @@ -4,6 +4,7 @@ import ( "v2ray.com/core/common/bitmask" "v2ray.com/core/common/buf" "v2ray.com/core/common/net" + "v2ray.com/core/common/protocol" "v2ray.com/core/common/serial" ) @@ -27,14 +28,6 @@ const ( TargetNetworkUDP TargetNetwork = 0x02 ) -type AddressType byte - -const ( - AddressTypeIPv4 AddressType = 0x01 - AddressTypeDomain AddressType = 0x02 - AddressTypeIPv6 AddressType = 0x03 -) - /* Frame format 2 bytes - length @@ -79,16 +72,16 @@ func (f FrameMetadata) AsSupplier() buf.Supplier { addr := f.Target.Address switch addr.Family() { case net.AddressFamilyIPv4: - b = append(b, byte(AddressTypeIPv4)) + b = append(b, byte(protocol.AddressTypeIPv4)) b = append(b, addr.IP()...) length += 5 case net.AddressFamilyIPv6: - b = append(b, byte(AddressTypeIPv6)) + b = append(b, byte(protocol.AddressTypeIPv6)) b = append(b, addr.IP()...) length += 17 case net.AddressFamilyDomain: nDomain := len(addr.Domain()) - b = append(b, byte(AddressTypeDomain), byte(nDomain)) + b = append(b, byte(protocol.AddressTypeDomain), byte(nDomain)) b = append(b, addr.Domain()...) length += nDomain + 2 } @@ -115,18 +108,18 @@ func ReadFrameFrom(b []byte) (*FrameMetadata, error) { if f.SessionStatus == SessionStatusNew { network := TargetNetwork(b[0]) port := net.PortFromBytes(b[1:3]) - addrType := AddressType(b[3]) + addrType := protocol.AddressType(b[3]) b = b[4:] var addr net.Address switch addrType { - case AddressTypeIPv4: + case protocol.AddressTypeIPv4: addr = net.IPAddress(b[0:4]) b = b[4:] - case AddressTypeIPv6: + case protocol.AddressTypeIPv6: addr = net.IPAddress(b[0:16]) b = b[16:] - case AddressTypeDomain: + case protocol.AddressTypeDomain: nDomain := int(b[0]) addr = net.DomainAddress(string(b[1 : 1+nDomain])) b = b[nDomain+1:] diff --git a/common/protocol/payload.go b/common/protocol/payload.go index 822143014..03eced677 100644 --- a/common/protocol/payload.go +++ b/common/protocol/payload.go @@ -6,3 +6,11 @@ const ( TransferTypeStream TransferType = 0 TransferTypePacket TransferType = 1 ) + +type AddressType byte + +const ( + AddressTypeIPv4 AddressType = 1 + AddressTypeDomain AddressType = 2 + AddressTypeIPv6 AddressType = 3 +) diff --git a/proxy/vmess/encoding/client.go b/proxy/vmess/encoding/client.go index d3eab9eb6..fff486514 100644 --- a/proxy/vmess/encoding/client.go +++ b/proxy/vmess/encoding/client.go @@ -89,13 +89,13 @@ func (c *ClientSession) EncodeRequestHeader(header *protocol.RequestHeader, writ switch header.Address.Family() { case net.AddressFamilyIPv4: - buffer = append(buffer, AddrTypeIPv4) + buffer = append(buffer, byte(protocol.AddressTypeIPv4)) buffer = append(buffer, header.Address.IP()...) case net.AddressFamilyIPv6: - buffer = append(buffer, AddrTypeIPv6) + buffer = append(buffer, byte(protocol.AddressTypeIPv6)) buffer = append(buffer, header.Address.IP()...) 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()...) } } diff --git a/proxy/vmess/encoding/const.go b/proxy/vmess/encoding/const.go index 3f2a15031..d9d858787 100644 --- a/proxy/vmess/encoding/const.go +++ b/proxy/vmess/encoding/const.go @@ -2,8 +2,4 @@ package encoding const ( Version = byte(1) - - AddrTypeIPv4 = byte(0x01) - AddrTypeIPv6 = byte(0x03) - AddrTypeDomain = byte(0x02) ) diff --git a/proxy/vmess/encoding/server.go b/proxy/vmess/encoding/server.go index eefa9aaaa..fc0a95b6b 100644 --- a/proxy/vmess/encoding/server.go +++ b/proxy/vmess/encoding/server.go @@ -175,22 +175,22 @@ func (s *ServerSession) DecodeRequestHeader(reader io.Reader) (*protocol.Request if request.Command != protocol.RequestCommandMux { request.Port = net.PortFromBytes(buffer[38:40]) - switch buffer[40] { - case AddrTypeIPv4: + switch protocol.AddressType(buffer[40]) { + case protocol.AddressTypeIPv4: _, err = io.ReadFull(decryptor, buffer[41:45]) // 4 bytes bufferLen += 4 if err != nil { return nil, newError("failed to read IPv4 address").Base(err) } request.Address = net.IPAddress(buffer[41:45]) - case AddrTypeIPv6: + case protocol.AddressTypeIPv6: _, err = io.ReadFull(decryptor, buffer[41:57]) // 16 bytes bufferLen += 16 if err != nil { return nil, newError("failed to read IPv6 address").Base(err) } request.Address = net.IPAddress(buffer[41:57]) - case AddrTypeDomain: + case protocol.AddressTypeDomain: _, err = io.ReadFull(decryptor, buffer[41:42]) if err != nil { return nil, newError("failed to read domain address").Base(err)