writer.go 745 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package io
  2. import (
  3. "hash/fnv"
  4. "github.com/v2ray/v2ray-core/common/alloc"
  5. v2io "github.com/v2ray/v2ray-core/common/io"
  6. "github.com/v2ray/v2ray-core/common/serial"
  7. )
  8. type AuthChunkWriter struct {
  9. writer v2io.Writer
  10. }
  11. func NewAuthChunkWriter(writer v2io.Writer) *AuthChunkWriter {
  12. return &AuthChunkWriter{
  13. writer: writer,
  14. }
  15. }
  16. func (this *AuthChunkWriter) Write(buffer *alloc.Buffer) error {
  17. Authenticate(buffer)
  18. return this.writer.Write(buffer)
  19. }
  20. func (this *AuthChunkWriter) Release() {
  21. this.writer = nil
  22. }
  23. func Authenticate(buffer *alloc.Buffer) {
  24. fnvHash := fnv.New32a()
  25. fnvHash.Write(buffer.Value)
  26. buffer.SliceBack(4)
  27. fnvHash.Sum(buffer.Value[:0])
  28. buffer.Prepend(serial.Uint16Literal(uint16(buffer.Len())).Bytes())
  29. }