2016-02-01 11:22:29 +00:00
|
|
|
package io
|
|
|
|
|
|
|
|
import (
|
|
|
|
"hash/fnv"
|
|
|
|
|
2016-08-20 18:55:45 +00:00
|
|
|
"v2ray.com/core/common/alloc"
|
|
|
|
v2io "v2ray.com/core/common/io"
|
2016-02-01 11:22:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type AuthChunkWriter struct {
|
2016-04-12 19:43:13 +00:00
|
|
|
writer v2io.Writer
|
2016-02-01 11:22:29 +00:00
|
|
|
}
|
|
|
|
|
2016-04-12 19:43:13 +00:00
|
|
|
func NewAuthChunkWriter(writer v2io.Writer) *AuthChunkWriter {
|
2016-02-01 11:22:29 +00:00
|
|
|
return &AuthChunkWriter{
|
|
|
|
writer: writer,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *AuthChunkWriter) Write(buffer *alloc.Buffer) error {
|
|
|
|
Authenticate(buffer)
|
|
|
|
return this.writer.Write(buffer)
|
|
|
|
}
|
|
|
|
|
2016-03-11 22:51:58 +00:00
|
|
|
func (this *AuthChunkWriter) Release() {
|
2016-03-24 15:36:18 +00:00
|
|
|
this.writer.Release()
|
2016-04-12 19:43:13 +00:00
|
|
|
this.writer = nil
|
2016-03-11 22:51:58 +00:00
|
|
|
}
|
|
|
|
|
2016-02-01 11:22:29 +00:00
|
|
|
func Authenticate(buffer *alloc.Buffer) {
|
|
|
|
fnvHash := fnv.New32a()
|
|
|
|
fnvHash.Write(buffer.Value)
|
2016-07-17 21:11:05 +00:00
|
|
|
buffer.PrependHash(fnvHash)
|
2016-02-01 11:22:29 +00:00
|
|
|
|
2016-06-26 20:34:48 +00:00
|
|
|
buffer.PrependUint16(uint16(buffer.Len()))
|
2016-02-01 11:22:29 +00:00
|
|
|
}
|