1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-07-05 13:35:23 +00:00
v2fly/proxy/shadowsocks/ota.go

139 lines
3.0 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-12-08 10:21:24 +00:00
2016-12-09 10:35:27 +00:00
"v2ray.com/core/common/buf"
2016-08-20 18:55:45 +00:00
"v2ray.com/core/common/serial"
)
const (
2016-12-21 22:07:35 +00:00
// AuthSize is the number of extra bytes for Shadowsocks OTA.
AuthSize = 10
)
type KeyGenerator func() []byte
type Authenticator struct {
key KeyGenerator
}
func NewAuthenticator(keygen KeyGenerator) *Authenticator {
return &Authenticator{
key: keygen,
}
}
2016-12-09 11:08:25 +00:00
func (v *Authenticator) Authenticate(data []byte) buf.Supplier {
2016-11-27 20:39:09 +00:00
hasher := hmac.New(sha1.New, v.key())
hasher.Write(data)
res := hasher.Sum(nil)
2016-12-09 11:08:25 +00:00
return func(b []byte) (int, error) {
return copy(b, res[:AuthSize]), nil
2016-12-06 10:03:42 +00:00
}
}
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 {
2016-12-21 22:07:35 +00:00
chunkID := 0
return func() []byte {
newKey := make([]byte, 0, len(iv)+4)
newKey = append(newKey, iv...)
2016-12-21 22:07:35 +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,
}
}
2017-04-15 19:07:23 +00:00
func (v *ChunkReader) Read() (buf.MultiBuffer, error) {
2016-12-09 11:08:25 +00:00
buffer := buf.New()
if err := buffer.AppendSupplier(buf.ReadFullFrom(v.reader, 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-12-05 14:19:14 +00:00
length := serial.BytesToUint16(buffer.BytesTo(2)) + AuthSize
2016-12-09 11:08:25 +00:00
if length > buf.Size {
2016-11-19 09:57:00 +00:00
// Theoretically the size of a chunk is 64K, but most Shadowsocks implementations used <4K buffer.
buffer.Release()
2016-12-09 11:08:25 +00:00
buffer = buf.NewLocal(int(length) + 128)
2016-11-19 09:57:00 +00:00
}
2016-12-05 16:05:47 +00:00
buffer.Clear()
2016-12-09 11:08:25 +00:00
if err := buffer.AppendSupplier(buf.ReadFullFrom(v.reader, int(length))); err != nil {
2016-02-01 11:22:29 +00:00
buffer.Release()
2016-01-29 19:54:06 +00:00
return nil, err
}
2016-12-05 14:19:14 +00:00
authBytes := buffer.BytesTo(AuthSize)
payload := buffer.BytesFrom(AuthSize)
2016-01-29 19:54:06 +00:00
2016-12-06 10:03:42 +00:00
actualAuthBytes := make([]byte, AuthSize)
v.auth.Authenticate(payload)(actualAuthBytes)
2016-05-24 20:09:22 +00:00
if !bytes.Equal(authBytes, actualAuthBytes) {
2016-02-01 11:22:29 +00:00
buffer.Release()
2017-04-08 23:43:25 +00:00
return nil, newError("invalid auth")
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
2017-04-16 11:17:35 +00:00
return buf.NewMultiBufferValue(buffer), nil
2016-01-29 19:54:06 +00:00
}
2016-10-20 22:33:23 +00:00
type ChunkWriter struct {
writer io.Writer
auth *Authenticator
2016-12-08 10:21:24 +00:00
buffer []byte
2016-10-20 22:33:23 +00:00
}
func NewChunkWriter(writer io.Writer, auth *Authenticator) *ChunkWriter {
return &ChunkWriter{
writer: writer,
auth: auth,
2016-12-08 10:21:24 +00:00
buffer: make([]byte, 32*1024),
2016-10-20 22:33:23 +00:00
}
}
2017-04-15 19:07:23 +00:00
func (w *ChunkWriter) Write(mb buf.MultiBuffer) error {
defer mb.Release()
2017-04-15 19:49:18 +00:00
for {
payloadLen, err := mb.Read(w.buffer[2+AuthSize:])
if err != nil {
2017-04-15 19:07:23 +00:00
return err
}
2017-04-15 19:49:18 +00:00
serial.Uint16ToBytes(uint16(payloadLen), w.buffer[:0])
w.auth.Authenticate(w.buffer[2+AuthSize : 2+AuthSize+payloadLen])(w.buffer[2:])
if _, err := w.writer.Write(w.buffer[:2+AuthSize+payloadLen]); err != nil {
return err
}
if mb.IsEmpty() {
break
}
2017-04-15 19:07:23 +00:00
}
return nil
}