ota.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package shadowsocks
  2. import (
  3. "bytes"
  4. "crypto/hmac"
  5. "crypto/sha1"
  6. "io"
  7. "v2ray.com/core/common/alloc"
  8. "v2ray.com/core/common/errors"
  9. "v2ray.com/core/common/serial"
  10. )
  11. const (
  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(auth []byte, data []byte) []byte {
  24. hasher := hmac.New(sha1.New, v.key())
  25. hasher.Write(data)
  26. res := hasher.Sum(nil)
  27. return append(auth, res[:AuthSize]...)
  28. }
  29. func HeaderKeyGenerator(key []byte, iv []byte) func() []byte {
  30. return func() []byte {
  31. newKey := make([]byte, 0, len(key)+len(iv))
  32. newKey = append(newKey, iv...)
  33. newKey = append(newKey, key...)
  34. return newKey
  35. }
  36. }
  37. func ChunkKeyGenerator(iv []byte) func() []byte {
  38. chunkId := 0
  39. return func() []byte {
  40. newKey := make([]byte, 0, len(iv)+4)
  41. newKey = append(newKey, iv...)
  42. newKey = serial.IntToBytes(chunkId, newKey)
  43. chunkId++
  44. return newKey
  45. }
  46. }
  47. type ChunkReader struct {
  48. reader io.Reader
  49. auth *Authenticator
  50. }
  51. func NewChunkReader(reader io.Reader, auth *Authenticator) *ChunkReader {
  52. return &ChunkReader{
  53. reader: reader,
  54. auth: auth,
  55. }
  56. }
  57. func (v *ChunkReader) Release() {
  58. v.reader = nil
  59. v.auth = nil
  60. }
  61. func (v *ChunkReader) Read() (*alloc.Buffer, error) {
  62. buffer := alloc.NewBuffer()
  63. if _, err := io.ReadFull(v.reader, buffer.Value[:2]); err != nil {
  64. buffer.Release()
  65. return nil, err
  66. }
  67. // There is a potential buffer overflow here. Large buffer is 64K bytes,
  68. // while uin16 + 10 will be more than that
  69. length := serial.BytesToUint16(buffer.Value[:2]) + AuthSize
  70. if length > alloc.BufferSize {
  71. // Theoretically the size of a chunk is 64K, but most Shadowsocks implementations used <4K buffer.
  72. buffer.Release()
  73. buffer = alloc.NewLocalBuffer(int(length) + 128)
  74. }
  75. if _, err := io.ReadFull(v.reader, buffer.Value[:length]); err != nil {
  76. buffer.Release()
  77. return nil, err
  78. }
  79. buffer.Slice(0, int(length))
  80. authBytes := buffer.Value[:AuthSize]
  81. payload := buffer.Value[AuthSize:]
  82. actualAuthBytes := v.auth.Authenticate(nil, payload)
  83. if !bytes.Equal(authBytes, actualAuthBytes) {
  84. buffer.Release()
  85. return nil, errors.New("Shadowsocks|AuthenticationReader: Invalid auth.")
  86. }
  87. buffer.SliceFrom(AuthSize)
  88. return buffer, nil
  89. }
  90. type ChunkWriter struct {
  91. writer io.Writer
  92. auth *Authenticator
  93. }
  94. func NewChunkWriter(writer io.Writer, auth *Authenticator) *ChunkWriter {
  95. return &ChunkWriter{
  96. writer: writer,
  97. auth: auth,
  98. }
  99. }
  100. func (v *ChunkWriter) Release() {
  101. v.writer = nil
  102. v.auth = nil
  103. }
  104. func (v *ChunkWriter) Write(payload *alloc.Buffer) error {
  105. totalLength := payload.Len()
  106. payload.SliceBack(AuthSize)
  107. v.auth.Authenticate(payload.Value[:0], payload.Value[AuthSize:])
  108. payload.PrependUint16(uint16(totalLength))
  109. _, err := v.writer.Write(payload.Bytes())
  110. return err
  111. }