1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2026-04-06 22:05:27 -04:00

refactor shadowsocks

This commit is contained in:
v2ray
2016-01-29 20:54:06 +01:00
parent c9554546db
commit e9c784d4bd
5 changed files with 48 additions and 81 deletions

View File

@@ -4,9 +4,6 @@ import (
"io"
"github.com/v2ray/v2ray-core/common/alloc"
"github.com/v2ray/v2ray-core/common/crypto"
"github.com/v2ray/v2ray-core/common/serial"
"github.com/v2ray/v2ray-core/transport"
)
// ReadFrom reads from a reader and put all content to a buffer.
@@ -55,68 +52,3 @@ func (this *AdaptiveReader) Read() (*alloc.Buffer, error) {
}
return buffer, nil
}
type ChunkReader struct {
reader io.Reader
}
func NewChunkReader(reader io.Reader) *ChunkReader {
return &ChunkReader{
reader: reader,
}
}
func (this *ChunkReader) Read() (*alloc.Buffer, error) {
buffer := alloc.NewLargeBuffer()
if _, err := io.ReadFull(this.reader, buffer.Value[:2]); err != nil {
alloc.Release(buffer)
return nil, err
}
length := serial.BytesLiteral(buffer.Value[:2]).Uint16Value()
if _, err := io.ReadFull(this.reader, buffer.Value[:length]); err != nil {
alloc.Release(buffer)
return nil, err
}
buffer.Slice(0, int(length))
return buffer, nil
}
type AuthenticationReader struct {
reader Reader
authenticator crypto.Authenticator
authBeforePayload bool
}
func NewAuthenticationReader(reader Reader, auth crypto.Authenticator, authBeforePayload bool) *AuthenticationReader {
return &AuthenticationReader{
reader: reader,
authenticator: auth,
authBeforePayload: authBeforePayload,
}
}
func (this *AuthenticationReader) Read() (*alloc.Buffer, error) {
buffer, err := this.reader.Read()
if err != nil {
alloc.Release(buffer)
return nil, err
}
authSize := this.authenticator.AuthSize()
var authBytes, payloadBytes []byte
if this.authBeforePayload {
authBytes = buffer.Value[:authSize]
payloadBytes = buffer.Value[authSize:]
} else {
payloadBytes = buffer.Value[:authSize]
authBytes = buffer.Value[authSize:]
}
actualAuthBytes := this.authenticator.Authenticate(nil, payloadBytes)
if !serial.BytesLiteral(authBytes).Equals(serial.BytesLiteral(actualAuthBytes)) {
alloc.Release(buffer)
return nil, transport.CorruptedPacket
}
buffer.Value = payloadBytes
return buffer, nil
}