1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-06 02:07:29 -05:00
v2fly/proxy/vmess/io/reader.go

111 lines
2.3 KiB
Go
Raw Normal View History

2016-02-01 06:22:29 -05:00
package io
import (
2016-05-04 17:41:24 -04:00
"hash"
2016-02-01 06:22:29 -05: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 17:41:24 -04: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 06:22:29 -05:00
type AuthChunkReader struct {
2016-05-04 17:41:24 -04:00
reader io.Reader
last *alloc.Buffer
chunkLength int
validator *Validator
2016-02-01 06:22:29 -05:00
}
func NewAuthChunkReader(reader io.Reader) *AuthChunkReader {
return &AuthChunkReader{
2016-05-04 17:41:24 -04:00
reader: reader,
chunkLength: -1,
2016-02-01 06:22:29 -05:00
}
}
func (this *AuthChunkReader) Read() (*alloc.Buffer, error) {
2016-05-04 17:41:24 -04:00
var buffer *alloc.Buffer
if this.last != nil {
buffer = this.last
this.last = nil
} else {
2016-05-25 16:05:47 -04:00
buffer = alloc.NewBufferWithSize(4096).Clear()
2016-05-04 17:41:24 -04:00
}
if this.chunkLength == -1 {
for buffer.Len() < 6 {
_, err := buffer.FillFrom(this.reader)
if err != nil {
buffer.Release()
2016-06-02 15:20:58 -04:00
return nil, io.ErrUnexpectedEOF
2016-05-04 17:41:24 -04:00
}
}
2016-05-24 16:09:22 -04:00
length := serial.BytesToUint16(buffer.Value[:2])
2016-05-04 17:41:24 -04:00
this.chunkLength = int(length) - 4
2016-05-24 16:09:22 -04:00
this.validator = NewValidator(serial.BytesToUint32(buffer.Value[2:6]))
2016-05-04 17:41:24 -04:00
buffer.SliceFrom(6)
2016-05-04 18:24:18 -04:00
} else if buffer.Len() < this.chunkLength {
_, err := buffer.FillFrom(this.reader)
if err != nil {
buffer.Release()
2016-06-02 15:20:58 -04:00
return nil, io.ErrUnexpectedEOF
2016-05-04 18:24:18 -04:00
}
2016-04-28 15:14:00 -04:00
}
2016-05-04 17:41:24 -04:00
if this.chunkLength == 0 {
2016-02-01 06:22:29 -05:00
buffer.Release()
2016-06-01 20:20:53 -04:00
return nil, io.EOF
2016-02-01 06:22:29 -05:00
}
2016-05-12 20:20:07 -04:00
if buffer.Len() < this.chunkLength {
2016-05-04 17:41:24 -04:00
this.validator.Consume(buffer.Value)
this.chunkLength -= buffer.Len()
} else {
this.validator.Consume(buffer.Value[:this.chunkLength])
if !this.validator.Validate() {
buffer.Release()
return nil, transport.ErrorCorruptedPacket
}
leftLength := buffer.Len() - this.chunkLength
2016-05-12 20:20:07 -04:00
if leftLength > 0 {
2016-05-25 16:05:47 -04:00
this.last = alloc.NewBufferWithSize(leftLength + 4096).Clear()
2016-05-12 20:20:07 -04:00
this.last.Append(buffer.Value[this.chunkLength:])
buffer.Slice(0, this.chunkLength)
}
2016-05-04 17:41:24 -04:00
this.chunkLength = -1
this.validator = nil
2016-02-01 06:22:29 -05:00
}
2016-05-04 17:41:24 -04:00
2016-02-01 06:22:29 -05:00
return buffer, nil
}
2016-03-11 17:51:58 -05:00
func (this *AuthChunkReader) Release() {
this.reader = nil
2016-05-04 17:41:24 -04:00
this.last.Release()
this.last = nil
this.validator = nil
2016-03-11 17:51:58 -05:00
}