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

117 lines
2.4 KiB
Go
Raw Normal View History

2016-02-01 12:22:29 +01:00
package io
import (
2016-11-19 14:38:13 +01:00
"errors"
2016-05-04 23:41:24 +02:00
"hash"
2016-02-01 12:22:29 +01:00
"hash/fnv"
"io"
2016-08-20 20:55:45 +02:00
"v2ray.com/core/common/alloc"
"v2ray.com/core/common/serial"
2016-02-01 12:22:29 +01:00
)
2016-08-24 11:17:42 +02:00
// Private: Visible for testing.
2016-05-04 23:41:24 +02:00
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 12:22:29 +01:00
type AuthChunkReader struct {
2016-05-04 23:41:24 +02:00
reader io.Reader
last *alloc.Buffer
chunkLength int
validator *Validator
2016-02-01 12:22:29 +01:00
}
func NewAuthChunkReader(reader io.Reader) *AuthChunkReader {
return &AuthChunkReader{
2016-05-04 23:41:24 +02:00
reader: reader,
chunkLength: -1,
2016-02-01 12:22:29 +01:00
}
}
func (this *AuthChunkReader) Read() (*alloc.Buffer, error) {
2016-05-04 23:41:24 +02:00
var buffer *alloc.Buffer
if this.last != nil {
buffer = this.last
this.last = nil
} else {
2016-11-19 01:50:09 +01:00
buffer = alloc.NewBuffer().Clear()
2016-05-04 23:41:24 +02:00
}
if this.chunkLength == -1 {
for buffer.Len() < 6 {
_, err := buffer.FillFrom(this.reader)
if err != nil {
buffer.Release()
2016-06-02 21:20:58 +02:00
return nil, io.ErrUnexpectedEOF
2016-05-04 23:41:24 +02:00
}
}
2016-05-24 22:09:22 +02:00
length := serial.BytesToUint16(buffer.Value[:2])
2016-05-04 23:41:24 +02:00
this.chunkLength = int(length) - 4
2016-05-24 22:09:22 +02:00
this.validator = NewValidator(serial.BytesToUint32(buffer.Value[2:6]))
2016-05-04 23:41:24 +02:00
buffer.SliceFrom(6)
2016-07-03 22:32:28 +02: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-05 00:24:18 +02:00
} else if buffer.Len() < this.chunkLength {
_, err := buffer.FillFrom(this.reader)
if err != nil {
buffer.Release()
2016-06-02 21:20:58 +02:00
return nil, io.ErrUnexpectedEOF
2016-05-05 00:24:18 +02:00
}
2016-04-28 21:14:00 +02:00
}
2016-05-04 23:41:24 +02:00
if this.chunkLength == 0 {
2016-02-01 12:22:29 +01:00
buffer.Release()
2016-06-02 02:20:53 +02:00
return nil, io.EOF
2016-02-01 12:22:29 +01:00
}
2016-05-12 17:20:07 -07:00
if buffer.Len() < this.chunkLength {
2016-05-04 23:41:24 +02: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-11-19 14:38:13 +01:00
return nil, errors.New("VMess|AuthChunkReader: Invalid auth.")
2016-05-04 23:41:24 +02:00
}
leftLength := buffer.Len() - this.chunkLength
2016-05-12 17:20:07 -07:00
if leftLength > 0 {
2016-11-19 01:50:09 +01:00
this.last = alloc.NewBuffer().Clear()
2016-05-12 17:20:07 -07:00
this.last.Append(buffer.Value[this.chunkLength:])
buffer.Slice(0, this.chunkLength)
}
2016-05-04 23:41:24 +02:00
this.chunkLength = -1
this.validator = nil
2016-02-01 12:22:29 +01:00
}
2016-05-04 23:41:24 +02:00
2016-02-01 12:22:29 +01:00
return buffer, nil
}
2016-03-11 23:51:58 +01:00
func (this *AuthChunkReader) Release() {
this.reader = nil
2016-05-04 23:41:24 +02:00
this.last.Release()
this.last = nil
this.validator = nil
2016-03-11 23:51:58 +01:00
}