func TestUDPEncoding(t *testing.T) { assert := assert.On(t) request := &protocol.RequestHeader{ Version: Version, Command: protocol.RequestCommandUDP, Address: v2net.LocalHostIP, Port: 1234, User: &protocol.User{ Email: "*****@*****.**", Account: serial.ToTypedMessage(&Account{ Password: "******", CipherType: CipherType_AES_128_CFB, Ota: Account_Disabled, }), }, } data := buf.NewLocal(256) data.AppendSupplier(serial.WriteString("test string")) encodedData, err := EncodeUDPPacket(request, data) assert.Error(err).IsNil() decodedRequest, decodedData, err := DecodeUDPPacket(request.User, encodedData) assert.Error(err).IsNil() assert.Bytes(decodedData.Bytes()).Equals(data.Bytes()) assert.Address(decodedRequest.Address).Equals(request.Address) assert.Port(decodedRequest.Port).Equals(request.Port) }
func (v HttpAuthenticator) GetClientWriter() *HeaderWriter { header := buf.NewSmall() config := v.config.Request header.AppendSupplier(serial.WriteString(strings.Join([]string{config.GetMethodValue(), config.PickUri(), config.GetFullVersion()}, " "))) header.AppendSupplier(writeCRLF) headers := config.PickHeaders() for _, h := range headers { header.AppendSupplier(serial.WriteString(h)) header.AppendSupplier(writeCRLF) } header.AppendSupplier(writeCRLF) return &HeaderWriter{ header: header, } }
func TestBufferString(t *testing.T) { assert := assert.On(t) buffer := New() defer buffer.Release() buffer.AppendSupplier(serial.WriteString("Test String")) assert.String(buffer.String()).Equals("Test String") }
func TestUDPReaderWriter(t *testing.T) { assert := assert.On(t) user := &protocol.User{ Account: serial.ToTypedMessage(&Account{ Password: "******", CipherType: CipherType_CHACHA20_IEFT, }), } cache := buf.New() writer := &UDPWriter{ Writer: cache, Request: &protocol.RequestHeader{ Version: Version, Address: v2net.DomainAddress("v2ray.com"), Port: 123, User: user, Option: RequestOptionOneTimeAuth, }, } reader := &UDPReader{ Reader: cache, User: user, } b := buf.New() b.AppendSupplier(serial.WriteString("test payload")) err := writer.Write(b) assert.Error(err).IsNil() payload, err := reader.Read() assert.Error(err).IsNil() assert.String(payload.String()).Equals("test payload") b = buf.New() b.AppendSupplier(serial.WriteString("test payload 2")) err = writer.Write(b) assert.Error(err).IsNil() payload, err = reader.Read() assert.Error(err).IsNil() assert.String(payload.String()).Equals("test payload 2") }
func formResponseHeader(config *ResponseConfig) *HeaderWriter { header := buf.NewSmall() header.AppendSupplier(serial.WriteString(strings.Join([]string{config.GetFullVersion(), config.GetStatusValue().Code, config.GetStatusValue().Reason}, " "))) header.AppendSupplier(writeCRLF) headers := config.PickHeaders() for _, h := range headers { header.AppendSupplier(serial.WriteString(h)) header.AppendSupplier(writeCRLF) } if !config.HasHeader("Date") { header.AppendSupplier(serial.WriteString("Date: ")) header.AppendSupplier(serial.WriteString(time.Now().Format(http.TimeFormat))) header.AppendSupplier(writeCRLF) } header.AppendSupplier(writeCRLF) return &HeaderWriter{ header: header, } }
func appendAddress(buffer *buf.Buffer, address v2net.Address, port v2net.Port) { switch address.Family() { case v2net.AddressFamilyIPv4: buffer.AppendBytes(0x01) buffer.Append(address.IP()) case v2net.AddressFamilyIPv6: buffer.AppendBytes(0x04) buffer.Append(address.IP()) case v2net.AddressFamilyDomain: buffer.AppendBytes(0x03, byte(len(address.Domain()))) buffer.AppendSupplier(serial.WriteString(address.Domain())) } buffer.AppendSupplier(serial.WriteUint16(port.Value())) }
func TestReaderWriter(t *testing.T) { assert := assert.On(t) cache := buf.New() b := buf.NewLocal(256) b.AppendSupplier(serial.WriteString("abcd" + ENDING)) writer := NewHeaderWriter(b) err := writer.Write(cache) assert.Error(err).IsNil() assert.Int(cache.Len()).Equals(8) _, err = cache.Write([]byte{'e', 'f', 'g'}) assert.Error(err).IsNil() reader := &HeaderReader{} buffer, err := reader.Read(cache) assert.Error(err).IsNil() assert.Bytes(buffer.Bytes()).Equals([]byte{'e', 'f', 'g'}) }
func TestTCPRequest(t *testing.T) { assert := assert.On(t) request := &protocol.RequestHeader{ Version: Version, Command: protocol.RequestCommandTCP, Address: v2net.LocalHostIP, Option: RequestOptionOneTimeAuth, Port: 1234, User: &protocol.User{ Email: "*****@*****.**", Account: serial.ToTypedMessage(&Account{ Password: "******", CipherType: CipherType_CHACHA20, }), }, } data := buf.NewLocal(256) data.AppendSupplier(serial.WriteString("test string")) cache := buf.New() writer, err := WriteTCPRequest(request, cache) assert.Error(err).IsNil() writer.Write(data) decodedRequest, reader, err := ReadTCPSession(request.User, cache) assert.Error(err).IsNil() assert.Address(decodedRequest.Address).Equals(request.Address) assert.Port(decodedRequest.Port).Equals(request.Port) decodedData, err := reader.Read() assert.Error(err).IsNil() assert.String(decodedData.String()).Equals("test string") }
func (v *HTTPResponse) WriteTo(writer buf.Writer) { b := buf.NewLocal(512) b.AppendSupplier(serial.WriteString(http403response)) writer.Write(b) }
"v2ray.com/core/common" "v2ray.com/core/common/buf" "v2ray.com/core/common/serial" ) const ( CRLF = "\r\n" ENDING = CRLF + CRLF // max length of HTTP header. Safety precaution for DDoS attack. maxHeaderLength = 8192 ) var ( ErrHeaderToLong = errors.New("Header too long.") writeCRLF = serial.WriteString(CRLF) ) type Reader interface { Read(io.Reader) (*buf.Buffer, error) } type Writer interface { Write(io.Writer) error } type NoOpReader struct{} func (v *NoOpReader) Read(io.Reader) (*buf.Buffer, error) { return nil, nil }