1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-07-04 21:15:24 +00:00
v2fly/proxy/shadowsocks/ota.go

129 lines
2.8 KiB
Go
Raw Normal View History

package shadowsocks
import (
2016-05-24 20:09:22 +00:00
"bytes"
"crypto/hmac"
"crypto/sha1"
2016-01-29 19:54:06 +00:00
"io"
2016-08-20 18:55:45 +00:00
"v2ray.com/core/common/alloc"
"v2ray.com/core/common/log"
"v2ray.com/core/common/serial"
"v2ray.com/core/transport"
)
const (
AuthSize = 10
)
type KeyGenerator func() []byte
type Authenticator struct {
key KeyGenerator
}
func NewAuthenticator(keygen KeyGenerator) *Authenticator {
return &Authenticator{
key: keygen,
}
}
func (this *Authenticator) Authenticate(auth []byte, data []byte) []byte {
hasher := hmac.New(sha1.New, this.key())
hasher.Write(data)
res := hasher.Sum(nil)
return append(auth, res[:AuthSize]...)
}
func HeaderKeyGenerator(key []byte, iv []byte) func() []byte {
return func() []byte {
newKey := make([]byte, 0, len(key)+len(iv))
newKey = append(newKey, iv...)
2016-02-28 20:00:53 +00:00
newKey = append(newKey, key...)
return newKey
}
}
func ChunkKeyGenerator(iv []byte) func() []byte {
chunkId := 0
return func() []byte {
newKey := make([]byte, 0, len(iv)+4)
newKey = append(newKey, iv...)
2016-07-04 07:47:14 +00:00
newKey = serial.IntToBytes(chunkId, newKey)
chunkId++
return newKey
}
}
2016-01-29 19:54:06 +00:00
type ChunkReader struct {
reader io.Reader
auth *Authenticator
}
func NewChunkReader(reader io.Reader, auth *Authenticator) *ChunkReader {
return &ChunkReader{
reader: reader,
auth: auth,
}
}
2016-04-12 19:43:13 +00:00
func (this *ChunkReader) Release() {
this.reader = nil
this.auth = nil
}
2016-01-29 19:54:06 +00:00
func (this *ChunkReader) Read() (*alloc.Buffer, error) {
buffer := alloc.NewLargeBuffer()
if _, err := io.ReadFull(this.reader, buffer.Value[:2]); err != nil {
2016-02-01 11:22:29 +00:00
buffer.Release()
2016-01-29 19:54:06 +00:00
return nil, err
}
// There is a potential buffer overflow here. Large buffer is 64K bytes,
// while uin16 + 10 will be more than that
2016-05-24 20:09:22 +00:00
length := serial.BytesToUint16(buffer.Value[:2]) + AuthSize
2016-01-29 19:54:06 +00:00
if _, err := io.ReadFull(this.reader, buffer.Value[:length]); err != nil {
2016-02-01 11:22:29 +00:00
buffer.Release()
2016-01-29 19:54:06 +00:00
return nil, err
}
buffer.Slice(0, int(length))
authBytes := buffer.Value[:AuthSize]
payload := buffer.Value[AuthSize:]
actualAuthBytes := this.auth.Authenticate(nil, payload)
2016-05-24 20:09:22 +00:00
if !bytes.Equal(authBytes, actualAuthBytes) {
2016-02-01 11:22:29 +00:00
buffer.Release()
2016-01-29 19:54:06 +00:00
log.Debug("AuthenticationReader: Unexpected auth: ", authBytes)
2016-06-27 06:53:35 +00:00
return nil, transport.ErrCorruptedPacket
2016-01-29 19:54:06 +00:00
}
2016-07-13 09:38:14 +00:00
buffer.SliceFrom(AuthSize)
2016-01-29 19:54:06 +00:00
return buffer, nil
}
2016-10-20 22:33:23 +00:00
type ChunkWriter struct {
writer io.Writer
auth *Authenticator
}
func NewChunkWriter(writer io.Writer, auth *Authenticator) *ChunkWriter {
return &ChunkWriter{
writer: writer,
auth: auth,
}
}
func (this *ChunkWriter) Release() {
this.writer = nil
this.auth = nil
}
2016-10-31 14:24:28 +00:00
func (this *ChunkWriter) Write(payload *alloc.Buffer) error {
2016-10-20 22:33:23 +00:00
totalLength := payload.Len()
2016-10-31 14:24:28 +00:00
payload.SliceBack(AuthSize)
this.auth.Authenticate(payload.Value[:0], payload.Value[AuthSize:])
2016-10-20 22:33:23 +00:00
payload.PrependUint16(uint16(totalLength))
2016-10-31 14:24:28 +00:00
_, err := this.writer.Write(payload.Bytes())
return err
2016-10-20 22:33:23 +00:00
}