1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-10-30 15:57:35 -04:00
v2fly/proxy/vmess/io/writer.go

37 lines
634 B
Go
Raw Normal View History

2016-02-01 06:22:29 -05:00
package io
import (
"hash/fnv"
2016-08-20 14:55:45 -04:00
"v2ray.com/core/common/alloc"
v2io "v2ray.com/core/common/io"
2016-02-01 06:22:29 -05:00
)
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,
}
}
2016-11-27 15:39:09 -05:00
func (v *AuthChunkWriter) Write(buffer *alloc.Buffer) error {
2016-02-01 06:22:29 -05:00
Authenticate(buffer)
2016-11-27 15:39:09 -05:00
return v.writer.Write(buffer)
2016-02-01 06:22:29 -05:00
}
2016-11-27 15:39:09 -05:00
func (v *AuthChunkWriter) Release() {
v.writer.Release()
v.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)
2016-07-17 17:11:05 -04:00
buffer.PrependHash(fnvHash)
2016-02-01 06:22:29 -05:00
2016-06-26 16:34:48 -04:00
buffer.PrependUint16(uint16(buffer.Len()))
2016-02-01 06:22:29 -05:00
}