v2ray-core/proxy/vmess/io/reader.go

118 lines
2.5 KiB
Go
Raw Normal View History

2016-02-01 11:22:29 +00:00
package io
import (
2016-05-04 21:41:24 +00:00
"hash"
2016-02-01 11:22:29 +00:00
"hash/fnv"
"io"
"github.com/v2ray/v2ray-core/common/alloc"
"github.com/v2ray/v2ray-core/common/serial"
"github.com/v2ray/v2ray-core/transport"
)
2016-05-04 21:41:24 +00:00
// @Private
type Validator struct {
actualAuth hash.Hash32
expectedAuth uint32
}
func NewValidator(expectedAuth uint32) *Validator {
return &Validator{
actualAuth: fnv.New32a(),
expectedAuth: expectedAuth,
}
}
func (this *Validator) Consume(b []byte) {
this.actualAuth.Write(b)
}
func (this *Validator) Validate() bool {
return this.actualAuth.Sum32() == this.expectedAuth
}
2016-02-01 11:22:29 +00:00
type AuthChunkReader struct {
2016-05-04 21:41:24 +00:00
reader io.Reader
last *alloc.Buffer
chunkLength int
validator *Validator
2016-02-01 11:22:29 +00:00
}
func NewAuthChunkReader(reader io.Reader) *AuthChunkReader {
return &AuthChunkReader{
2016-05-04 21:41:24 +00:00
reader: reader,
chunkLength: -1,
2016-02-01 11:22:29 +00:00
}
}
func (this *AuthChunkReader) Read() (*alloc.Buffer, error) {
2016-05-04 21:41:24 +00:00
var buffer *alloc.Buffer
if this.last != nil {
buffer = this.last
this.last = nil
} else {
2016-05-25 20:05:47 +00:00
buffer = alloc.NewBufferWithSize(4096).Clear()
2016-05-04 21:41:24 +00:00
}
if this.chunkLength == -1 {
for buffer.Len() < 6 {
_, err := buffer.FillFrom(this.reader)
if err != nil {
buffer.Release()
2016-06-02 19:20:58 +00:00
return nil, io.ErrUnexpectedEOF
2016-05-04 21:41:24 +00:00
}
}
2016-05-24 20:09:22 +00:00
length := serial.BytesToUint16(buffer.Value[:2])
2016-05-04 21:41:24 +00:00
this.chunkLength = int(length) - 4
2016-05-24 20:09:22 +00:00
this.validator = NewValidator(serial.BytesToUint32(buffer.Value[2:6]))
2016-05-04 21:41:24 +00:00
buffer.SliceFrom(6)
2016-07-03 20:32:28 +00:00
if buffer.Len() < this.chunkLength && this.chunkLength <= 2048 {
_, err := buffer.FillFrom(this.reader)
if err != nil {
buffer.Release()
return nil, io.ErrUnexpectedEOF
}
}
2016-05-04 22:24:18 +00:00
} else if buffer.Len() < this.chunkLength {
_, err := buffer.FillFrom(this.reader)
if err != nil {
buffer.Release()
2016-06-02 19:20:58 +00:00
return nil, io.ErrUnexpectedEOF
2016-05-04 22:24:18 +00:00
}
2016-04-28 19:14:00 +00:00
}
2016-05-04 21:41:24 +00:00
if this.chunkLength == 0 {
2016-02-01 11:22:29 +00:00
buffer.Release()
2016-06-02 00:20:53 +00:00
return nil, io.EOF
2016-02-01 11:22:29 +00:00
}
2016-05-13 00:20:07 +00:00
if buffer.Len() < this.chunkLength {
2016-05-04 21:41:24 +00:00
this.validator.Consume(buffer.Value)
this.chunkLength -= buffer.Len()
} else {
this.validator.Consume(buffer.Value[:this.chunkLength])
if !this.validator.Validate() {
buffer.Release()
2016-06-27 06:53:35 +00:00
return nil, transport.ErrCorruptedPacket
2016-05-04 21:41:24 +00:00
}
leftLength := buffer.Len() - this.chunkLength
2016-05-13 00:20:07 +00:00
if leftLength > 0 {
2016-05-25 20:05:47 +00:00
this.last = alloc.NewBufferWithSize(leftLength + 4096).Clear()
2016-05-13 00:20:07 +00:00
this.last.Append(buffer.Value[this.chunkLength:])
buffer.Slice(0, this.chunkLength)
}
2016-05-04 21:41:24 +00:00
this.chunkLength = -1
this.validator = nil
2016-02-01 11:22:29 +00:00
}
2016-05-04 21:41:24 +00:00
2016-02-01 11:22:29 +00:00
return buffer, nil
}
2016-03-11 22:51:58 +00:00
func (this *AuthChunkReader) Release() {
this.reader = nil
2016-05-04 21:41:24 +00:00
this.last.Release()
this.last = nil
this.validator = nil
2016-03-11 22:51:58 +00:00
}