ota.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package shadowsocks
  2. import (
  3. "bytes"
  4. "crypto/hmac"
  5. "crypto/sha1"
  6. "io"
  7. "v2ray.com/core/common/buf"
  8. "v2ray.com/core/common/serial"
  9. )
  10. const (
  11. // AuthSize is the number of extra bytes for Shadowsocks OTA.
  12. AuthSize = 10
  13. )
  14. type KeyGenerator func() []byte
  15. type Authenticator struct {
  16. key KeyGenerator
  17. }
  18. func NewAuthenticator(keygen KeyGenerator) *Authenticator {
  19. return &Authenticator{
  20. key: keygen,
  21. }
  22. }
  23. func (v *Authenticator) Authenticate(data []byte) buf.Supplier {
  24. hasher := hmac.New(sha1.New, v.key())
  25. hasher.Write(data)
  26. res := hasher.Sum(nil)
  27. return func(b []byte) (int, error) {
  28. return copy(b, res[:AuthSize]), nil
  29. }
  30. }
  31. func HeaderKeyGenerator(key []byte, iv []byte) func() []byte {
  32. return func() []byte {
  33. newKey := make([]byte, 0, len(key)+len(iv))
  34. newKey = append(newKey, iv...)
  35. newKey = append(newKey, key...)
  36. return newKey
  37. }
  38. }
  39. func ChunkKeyGenerator(iv []byte) func() []byte {
  40. chunkID := 0
  41. return func() []byte {
  42. newKey := make([]byte, 0, len(iv)+4)
  43. newKey = append(newKey, iv...)
  44. newKey = serial.IntToBytes(chunkID, newKey)
  45. chunkID++
  46. return newKey
  47. }
  48. }
  49. type ChunkReader struct {
  50. reader io.Reader
  51. auth *Authenticator
  52. }
  53. func NewChunkReader(reader io.Reader, auth *Authenticator) *ChunkReader {
  54. return &ChunkReader{
  55. reader: reader,
  56. auth: auth,
  57. }
  58. }
  59. func (v *ChunkReader) Read() (buf.MultiBuffer, error) {
  60. buffer := buf.New()
  61. if err := buffer.AppendSupplier(buf.ReadFullFrom(v.reader, 2)); err != nil {
  62. buffer.Release()
  63. return nil, err
  64. }
  65. // There is a potential buffer overflow here. Large buffer is 64K bytes,
  66. // while uin16 + 10 will be more than that
  67. length := serial.BytesToUint16(buffer.BytesTo(2)) + AuthSize
  68. if length > buf.Size {
  69. // Theoretically the size of a chunk is 64K, but most Shadowsocks implementations used <4K buffer.
  70. buffer.Release()
  71. buffer = buf.NewLocal(int(length) + 128)
  72. }
  73. buffer.Clear()
  74. if err := buffer.AppendSupplier(buf.ReadFullFrom(v.reader, int(length))); err != nil {
  75. buffer.Release()
  76. return nil, err
  77. }
  78. authBytes := buffer.BytesTo(AuthSize)
  79. payload := buffer.BytesFrom(AuthSize)
  80. actualAuthBytes := make([]byte, AuthSize)
  81. v.auth.Authenticate(payload)(actualAuthBytes)
  82. if !bytes.Equal(authBytes, actualAuthBytes) {
  83. buffer.Release()
  84. return nil, newError("invalid auth")
  85. }
  86. buffer.SliceFrom(AuthSize)
  87. mb := buf.NewMultiBuffer()
  88. mb.Append(buffer)
  89. return mb, nil
  90. }
  91. type ChunkWriter struct {
  92. writer io.Writer
  93. auth *Authenticator
  94. buffer []byte
  95. }
  96. func NewChunkWriter(writer io.Writer, auth *Authenticator) *ChunkWriter {
  97. return &ChunkWriter{
  98. writer: writer,
  99. auth: auth,
  100. buffer: make([]byte, 32*1024),
  101. }
  102. }
  103. func (w *ChunkWriter) Write(mb buf.MultiBuffer) error {
  104. defer mb.Release()
  105. for _, b := range mb {
  106. if err := w.writeInternal(b); err != nil {
  107. return err
  108. }
  109. }
  110. return nil
  111. }
  112. func (w *ChunkWriter) writeInternal(payload *buf.Buffer) error {
  113. totalLength := payload.Len()
  114. serial.Uint16ToBytes(uint16(totalLength), w.buffer[:0])
  115. w.auth.Authenticate(payload.Bytes())(w.buffer[2:])
  116. copy(w.buffer[2+AuthSize:], payload.Bytes())
  117. _, err := w.writer.Write(w.buffer[:2+AuthSize+payload.Len()])
  118. return err
  119. }