ota.go 3.0 KB

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