From 341fa376a1cb104168f0284522b63a1c53be3895 Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Tue, 6 Dec 2016 11:31:19 +0100 Subject: [PATCH] Buffer.SetByte --- common/alloc/buffer.go | 4 ++++ proxy/shadowsocks/protocol.go | 10 +++++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/common/alloc/buffer.go b/common/alloc/buffer.go index ad486b1a..9acb42c1 100644 --- a/common/alloc/buffer.go +++ b/common/alloc/buffer.go @@ -92,6 +92,10 @@ func (b *Buffer) Byte(index int) byte { return b.head[b.start+index] } +func (b *Buffer) SetByte(index int, value byte) { + b.head[b.start+index] = value +} + // Bytes returns the content bytes of this Buffer. func (b *Buffer) Bytes() []byte { return b.head[b.start:b.end] diff --git a/proxy/shadowsocks/protocol.go b/proxy/shadowsocks/protocol.go index 9f38942f..3d807507 100644 --- a/proxy/shadowsocks/protocol.go +++ b/proxy/shadowsocks/protocol.go @@ -38,7 +38,7 @@ func ReadTCPSession(user *protocol.User, reader io.Reader) (*protocol.RequestHea return nil, nil, errors.Base(err).Message("Shadowsocks|TCP: Failed to read IV.") } - iv := append([]byte(nil), buffer.Bytes()[:ivLen]...) + iv := append([]byte(nil), buffer.BytesTo(ivLen)...) stream, err := account.Cipher.NewDecodingStream(account.Key, iv) if err != nil { @@ -59,8 +59,8 @@ func ReadTCPSession(user *protocol.User, reader io.Reader) (*protocol.RequestHea return nil, nil, errors.Base(err).Message("Shadowsocks|TCP: Failed to read address type.") } - addrType := (buffer.Bytes()[0] & 0x0F) - if (buffer.Bytes()[0] & 0x10) == 0x10 { + addrType := (buffer.Byte(0) & 0x0F) + if (buffer.Byte(0) & 0x10) == 0x10 { request.Option |= RequestOptionOneTimeAuth } @@ -171,7 +171,7 @@ func WriteTCPRequest(request *protocol.RequestHeader, writer io.Writer) (v2io.Wr header.AppendFunc(serial.WriteUint16(uint16(request.Port))) if request.Option.Has(RequestOptionOneTimeAuth) { - header.Bytes()[0] |= 0x10 + header.SetByte(0, header.Byte(0)|0x10) authenticator := NewAuthenticator(HeaderKeyGenerator(account.Key, iv)) header.AppendFunc(authenticator.Authenticate(header.Bytes())) @@ -267,7 +267,7 @@ func EncodeUDPPacket(request *protocol.RequestHeader, payload *alloc.Buffer) (*a if request.Option.Has(RequestOptionOneTimeAuth) { authenticator := NewAuthenticator(HeaderKeyGenerator(account.Key, iv)) - buffer.Bytes()[ivLen] |= 0x10 + buffer.SetByte(ivLen, buffer.Byte(ivLen)|0x10) buffer.AppendFunc(authenticator.Authenticate(buffer.BytesFrom(ivLen))) }