github.com/Uhtred009/v2ray-core-1@v4.31.2+incompatible/proxy/vless/encoding/encoding.go (about) 1 // +build !confonly 2 3 package encoding 4 5 //go:generate go run v2ray.com/core/common/errors/errorgen 6 7 import ( 8 "io" 9 10 "v2ray.com/core/common/buf" 11 "v2ray.com/core/common/net" 12 "v2ray.com/core/common/protocol" 13 "v2ray.com/core/proxy/vless" 14 ) 15 16 const ( 17 Version = byte(0) 18 ) 19 20 var addrParser = protocol.NewAddressParser( 21 protocol.AddressFamilyByte(byte(protocol.AddressTypeIPv4), net.AddressFamilyIPv4), 22 protocol.AddressFamilyByte(byte(protocol.AddressTypeDomain), net.AddressFamilyDomain), 23 protocol.AddressFamilyByte(byte(protocol.AddressTypeIPv6), net.AddressFamilyIPv6), 24 protocol.PortThenAddress(), 25 ) 26 27 // EncodeRequestHeader writes encoded request header into the given writer. 28 func EncodeRequestHeader(writer io.Writer, request *protocol.RequestHeader, requestAddons *Addons) error { 29 30 buffer := buf.StackNew() 31 defer buffer.Release() 32 33 if err := buffer.WriteByte(request.Version); err != nil { 34 return newError("failed to write request version").Base(err) 35 } 36 37 if _, err := buffer.Write(request.User.Account.(*vless.MemoryAccount).ID.Bytes()); err != nil { 38 return newError("failed to write request user id").Base(err) 39 } 40 41 if err := EncodeHeaderAddons(&buffer, requestAddons); err != nil { 42 return newError("failed to encode request header addons").Base(err) 43 } 44 45 if err := buffer.WriteByte(byte(request.Command)); err != nil { 46 return newError("failed to write request command").Base(err) 47 } 48 49 if request.Command != protocol.RequestCommandMux { 50 if err := addrParser.WriteAddressPort(&buffer, request.Address, request.Port); err != nil { 51 return newError("failed to write request address and port").Base(err) 52 } 53 } 54 55 if _, err := writer.Write(buffer.Bytes()); err != nil { 56 return newError("failed to write request header").Base(err) 57 } 58 59 return nil 60 } 61 62 // DecodeRequestHeader decodes and returns (if successful) a RequestHeader from an input stream. 63 func DecodeRequestHeader(isfb bool, first *buf.Buffer, reader io.Reader, validator *vless.Validator) (*protocol.RequestHeader, *Addons, error, bool) { 64 65 buffer := buf.StackNew() 66 defer buffer.Release() 67 68 request := new(protocol.RequestHeader) 69 70 if isfb { 71 request.Version = first.Byte(0) 72 } else { 73 if _, err := buffer.ReadFullFrom(reader, 1); err != nil { 74 return nil, nil, newError("failed to read request version").Base(err), false 75 } 76 request.Version = buffer.Byte(0) 77 } 78 79 switch request.Version { 80 case 0: 81 82 var id [16]byte 83 84 if isfb { 85 copy(id[:], first.BytesRange(1, 17)) 86 } else { 87 buffer.Clear() 88 if _, err := buffer.ReadFullFrom(reader, 16); err != nil { 89 return nil, nil, newError("failed to read request user id").Base(err), false 90 } 91 copy(id[:], buffer.Bytes()) 92 } 93 94 if request.User = validator.Get(id); request.User == nil { 95 return nil, nil, newError("invalid request user id"), isfb 96 } 97 98 if isfb { 99 first.Advance(17) 100 } 101 102 requestAddons, err := DecodeHeaderAddons(&buffer, reader) 103 if err != nil { 104 return nil, nil, newError("failed to decode request header addons").Base(err), false 105 } 106 107 buffer.Clear() 108 if _, err := buffer.ReadFullFrom(reader, 1); err != nil { 109 return nil, nil, newError("failed to read request command").Base(err), false 110 } 111 112 request.Command = protocol.RequestCommand(buffer.Byte(0)) 113 switch request.Command { 114 case protocol.RequestCommandMux: 115 request.Address = net.DomainAddress("v1.mux.cool") 116 request.Port = 0 117 case protocol.RequestCommandTCP, protocol.RequestCommandUDP: 118 if addr, port, err := addrParser.ReadAddressPort(&buffer, reader); err == nil { 119 request.Address = addr 120 request.Port = port 121 } 122 } 123 124 if request.Address == nil { 125 return nil, nil, newError("invalid request address"), false 126 } 127 128 return request, requestAddons, nil, false 129 130 default: 131 132 return nil, nil, newError("invalid request version"), isfb 133 134 } 135 136 } 137 138 // EncodeResponseHeader writes encoded response header into the given writer. 139 func EncodeResponseHeader(writer io.Writer, request *protocol.RequestHeader, responseAddons *Addons) error { 140 141 buffer := buf.StackNew() 142 defer buffer.Release() 143 144 if err := buffer.WriteByte(request.Version); err != nil { 145 return newError("failed to write response version").Base(err) 146 } 147 148 if err := EncodeHeaderAddons(&buffer, responseAddons); err != nil { 149 return newError("failed to encode response header addons").Base(err) 150 } 151 152 if _, err := writer.Write(buffer.Bytes()); err != nil { 153 return newError("failed to write response header").Base(err) 154 } 155 156 return nil 157 } 158 159 // DecodeResponseHeader decodes and returns (if successful) a ResponseHeader from an input stream. 160 func DecodeResponseHeader(reader io.Reader, request *protocol.RequestHeader) (*Addons, error) { 161 162 buffer := buf.StackNew() 163 defer buffer.Release() 164 165 if _, err := buffer.ReadFullFrom(reader, 1); err != nil { 166 return nil, newError("failed to read response version").Base(err) 167 } 168 169 if buffer.Byte(0) != request.Version { 170 return nil, newError("unexpected response version. Expecting ", int(request.Version), " but actually ", int(buffer.Byte(0))) 171 } 172 173 responseAddons, err := DecodeHeaderAddons(&buffer, reader) 174 if err != nil { 175 return nil, newError("failed to decode response header addons").Base(err) 176 } 177 178 return responseAddons, nil 179 }