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

117 lines
2.4 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"
2016-08-20 14:55:45 -04:00
"v2ray.com/core/common/alloc"
2016-12-04 03:10:47 -05:00
"v2ray.com/core/common/errors"
2016-08-20 14:55:45 -04:00
"v2ray.com/core/common/serial"
2016-02-01 06:22:29 -05:00
)
2016-08-24 05:17:42 -04:00
// Private: Visible for testing.
2016-05-04 17:41:24 -04:00
type Validator struct {
actualAuth hash.Hash32
expectedAuth uint32
}
func NewValidator(expectedAuth uint32) *Validator {
return &Validator{
actualAuth: fnv.New32a(),
expectedAuth: expectedAuth,
}
}
2016-11-27 15:39:09 -05:00
func (v *Validator) Consume(b []byte) {
v.actualAuth.Write(b)
2016-05-04 17:41:24 -04:00
}
2016-11-27 15:39:09 -05:00
func (v *Validator) Validate() bool {
return v.actualAuth.Sum32() == v.expectedAuth
2016-05-04 17:41:24 -04:00
}
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
}
}
2016-11-27 15:39:09 -05:00
func (v *AuthChunkReader) Read() (*alloc.Buffer, error) {
2016-05-04 17:41:24 -04:00
var buffer *alloc.Buffer
2016-11-27 15:39:09 -05:00
if v.last != nil {
buffer = v.last
v.last = nil
2016-05-04 17:41:24 -04:00
} else {
2016-11-18 19:50:09 -05:00
buffer = alloc.NewBuffer().Clear()
2016-05-04 17:41:24 -04:00
}
2016-11-27 15:39:09 -05:00
if v.chunkLength == -1 {
2016-05-04 17:41:24 -04:00
for buffer.Len() < 6 {
2016-11-27 15:39:09 -05:00
_, err := buffer.FillFrom(v.reader)
2016-05-04 17:41:24 -04:00
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-11-27 15:39:09 -05:00
v.chunkLength = int(length) - 4
v.validator = NewValidator(serial.BytesToUint32(buffer.Value[2:6]))
2016-05-04 17:41:24 -04:00
buffer.SliceFrom(6)
2016-11-27 15:39:09 -05:00
if buffer.Len() < v.chunkLength && v.chunkLength <= 2048 {
_, err := buffer.FillFrom(v.reader)
2016-07-03 16:32:28 -04:00
if err != nil {
buffer.Release()
return nil, io.ErrUnexpectedEOF
}
}
2016-11-27 15:39:09 -05:00
} else if buffer.Len() < v.chunkLength {
_, err := buffer.FillFrom(v.reader)
2016-05-04 18:24:18 -04:00
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
2016-11-27 15:39:09 -05:00
if v.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-11-27 15:39:09 -05:00
if buffer.Len() < v.chunkLength {
v.validator.Consume(buffer.Value)
v.chunkLength -= buffer.Len()
2016-05-04 17:41:24 -04:00
} else {
2016-11-27 15:39:09 -05:00
v.validator.Consume(buffer.Value[:v.chunkLength])
if !v.validator.Validate() {
2016-05-04 17:41:24 -04:00
buffer.Release()
2016-11-19 08:38:13 -05:00
return nil, errors.New("VMess|AuthChunkReader: Invalid auth.")
2016-05-04 17:41:24 -04:00
}
2016-11-27 15:39:09 -05:00
leftLength := buffer.Len() - v.chunkLength
2016-05-12 20:20:07 -04:00
if leftLength > 0 {
2016-11-27 15:39:09 -05:00
v.last = alloc.NewBuffer().Clear()
v.last.Append(buffer.Value[v.chunkLength:])
buffer.Slice(0, v.chunkLength)
2016-05-12 20:20:07 -04:00
}
2016-05-04 17:41:24 -04:00
2016-11-27 15:39:09 -05:00
v.chunkLength = -1
v.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
2016-11-27 15:39:09 -05:00
func (v *AuthChunkReader) Release() {
v.reader = nil
v.last.Release()
v.last = nil
v.validator = nil
2016-03-11 17:51:58 -05:00
}