v2ray-core/proxy/shadowsocks/ota.go

133 lines
2.7 KiB
Go
Raw Normal View History

2019-02-01 19:08:21 +00:00
// +build !confonly
package shadowsocks
import (
2016-05-24 20:09:22 +00:00
"bytes"
"crypto/hmac"
"crypto/sha1"
2018-11-02 14:47:58 +00:00
"encoding/binary"
2016-01-29 19:54:06 +00:00
"io"
2016-12-08 10:21:24 +00:00
2017-11-29 21:57:18 +00:00
"v2ray.com/core/common"
2016-12-09 10:35:27 +00:00
"v2ray.com/core/common/buf"
2018-08-16 10:05:33 +00:00
"v2ray.com/core/common/bytespool"
2018-11-03 12:03:02 +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,
}
}
2018-11-02 20:34:04 +00:00
func (v *Authenticator) Authenticate(data []byte, dest []byte) {
2016-11-27 20:39:09 +00:00
hasher := hmac.New(sha1.New, v.key())
2017-11-29 21:57:18 +00:00
common.Must2(hasher.Write(data))
res := hasher.Sum(nil)
2018-11-02 20:34:04 +00:00
copy(dest, 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 {
2018-11-02 14:47:58 +00:00
chunkID := uint32(0)
return func() []byte {
2018-11-02 14:47:58 +00:00
newKey := make([]byte, len(iv)+4)
copy(newKey, iv)
2018-11-02 17:20:02 +00:00
binary.BigEndian.PutUint32(newKey[len(iv):], chunkID)
2016-12-21 22:07:35 +00:00
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-11-09 21:33:15 +00:00
func (v *ChunkReader) ReadMultiBuffer() (buf.MultiBuffer, error) {
2018-11-03 12:03:02 +00:00
size, err := serial.ReadUint16(v.reader)
2018-03-12 21:10:13 +00:00
if err != nil {
2018-11-14 18:17:11 +00:00
return nil, newError("failed to read size").Base(err)
2016-11-19 09:57:00 +00:00
}
2018-03-12 21:10:13 +00:00
size += AuthSize
2016-12-05 16:05:47 +00:00
2018-08-16 10:05:33 +00:00
buffer := bytespool.Alloc(int32(size))
defer bytespool.Free(buffer)
if _, err := io.ReadFull(v.reader, buffer[:size]); err != nil {
2016-01-29 19:54:06 +00:00
return nil, err
}
2018-08-16 10:05:33 +00:00
authBytes := buffer[:AuthSize]
payload := buffer[AuthSize:size]
2016-01-29 19:54:06 +00:00
2016-12-06 10:03:42 +00:00
actualAuthBytes := make([]byte, AuthSize)
2018-11-02 20:34:04 +00:00
v.auth.Authenticate(payload, actualAuthBytes)
2016-05-24 20:09:22 +00:00
if !bytes.Equal(authBytes, actualAuthBytes) {
2017-04-08 23:43:25 +00:00
return nil, newError("invalid auth")
2016-01-29 19:54:06 +00:00
}
2018-11-18 18:36:36 +00:00
mb := buf.MergeBytes(nil, payload)
2018-08-16 10:05:33 +00:00
return mb, 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-11-09 21:33:15 +00:00
// WriteMultiBuffer implements buf.Writer.
func (w *ChunkWriter) WriteMultiBuffer(mb buf.MultiBuffer) error {
2018-11-17 21:45:07 +00:00
defer buf.ReleaseMulti(mb)
2017-04-15 19:07:23 +00:00
2017-04-15 19:49:18 +00:00
for {
mb, payloadLen := buf.SplitBytes(mb, w.buffer[2+AuthSize:])
2018-11-02 17:20:02 +00:00
binary.BigEndian.PutUint16(w.buffer, uint16(payloadLen))
2018-11-02 20:34:04 +00:00
w.auth.Authenticate(w.buffer[2+AuthSize:2+AuthSize+payloadLen], w.buffer[2:])
2018-07-28 13:03:40 +00:00
if err := buf.WriteAllBytes(w.writer, w.buffer[:2+AuthSize+payloadLen]); err != nil {
2017-04-15 19:49:18 +00:00
return err
}
if mb.IsEmpty() {
break
}
2017-04-15 19:07:23 +00:00
}
return nil
}