2016-02-01 06:22:29 -05:00
|
|
|
package io
|
|
|
|
|
|
|
|
import (
|
|
|
|
"hash/fnv"
|
|
|
|
|
|
|
|
"github.com/v2ray/v2ray-core/common/alloc"
|
|
|
|
v2io "github.com/v2ray/v2ray-core/common/io"
|
|
|
|
"github.com/v2ray/v2ray-core/common/serial"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AuthChunkWriter struct {
|
2016-04-12 15:43:13 -04:00
|
|
|
writer v2io.Writer
|
2016-02-01 06:22:29 -05:00
|
|
|
}
|
|
|
|
|
2016-04-12 15:43:13 -04:00
|
|
|
func NewAuthChunkWriter(writer v2io.Writer) *AuthChunkWriter {
|
2016-02-01 06:22:29 -05:00
|
|
|
return &AuthChunkWriter{
|
|
|
|
writer: writer,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *AuthChunkWriter) Write(buffer *alloc.Buffer) error {
|
|
|
|
Authenticate(buffer)
|
|
|
|
return this.writer.Write(buffer)
|
|
|
|
}
|
|
|
|
|
2016-03-11 17:51:58 -05:00
|
|
|
func (this *AuthChunkWriter) Release() {
|
2016-03-24 11:36:18 -04:00
|
|
|
this.writer.Release()
|
2016-04-12 15:43:13 -04:00
|
|
|
this.writer = nil
|
2016-03-11 17:51:58 -05:00
|
|
|
}
|
|
|
|
|
2016-02-01 06:22:29 -05:00
|
|
|
func Authenticate(buffer *alloc.Buffer) {
|
|
|
|
fnvHash := fnv.New32a()
|
|
|
|
fnvHash.Write(buffer.Value)
|
|
|
|
|
|
|
|
buffer.SliceBack(4)
|
|
|
|
fnvHash.Sum(buffer.Value[:0])
|
|
|
|
|
|
|
|
buffer.Prepend(serial.Uint16Literal(uint16(buffer.Len())).Bytes())
|
|
|
|
}
|