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:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user