1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-07-12 08:14:24 -04:00
v2fly/proxy/vmess/io/reader.go

122 lines
2.6 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"
2016-05-12 20:20:07 -04:00
"github.com/v2ray/v2ray-core/common/log"
2016-02-01 06:22:29 -05:00
"github.com/v2ray/v2ray-core/common/serial"
"github.com/v2ray/v2ray-core/transport"
)
2016-05-04 17:41:24 -04:00
// @Private
func AllocBuffer(size int) *alloc.Buffer {
if size < 8*1024-16 {
return alloc.NewBuffer()
}
return alloc.NewLargeBuffer()
}
// @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 {
2016-05-12 20:20:07 -04:00
log.Debug("VMess Reader: Expected auth ", this.expectedAuth, " actual auth: ", this.actualAuth.Sum32())
2016-05-04 17:41:24 -04:00
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 {
buffer = AllocBuffer(this.chunkLength).Clear()
}
if this.chunkLength == -1 {
for buffer.Len() < 6 {
_, err := buffer.FillFrom(this.reader)
if err != nil {
buffer.Release()
return nil, err
}
}
2016-05-12 20:20:07 -04:00
log.Debug("VMess Reader: raw buffer: ", buffer.Value)
2016-05-04 17:41:24 -04:00
length := serial.BytesLiteral(buffer.Value[:2]).Uint16Value()
this.chunkLength = int(length) - 4
this.validator = NewValidator(serial.BytesLiteral(buffer.Value[2:6]).Uint32Value())
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()
return nil, err
}
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-05-04 17:41:24 -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 {
this.last = AllocBuffer(leftLength).Clear()
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
}