1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-20 10:56:07 -04:00
v2fly/proxy/shadowsocks2022/requestsalt.go

60 lines
1.3 KiB
Go
Raw Normal View History

2023-11-03 16:10:11 -04:00
package shadowsocks2022
import (
"encoding/hex"
"io"
2023-11-18 19:42:20 -05:00
"github.com/lunixbochs/struc"
2023-11-03 16:10:11 -04:00
)
func newRequestSaltWithLength(length int) RequestSalt {
return &requestSaltWithLength{length: length}
}
type requestSaltWithLength struct {
length int
content []byte
}
func (r *requestSaltWithLength) isRequestSalt() {}
func (r *requestSaltWithLength) Pack(p []byte, opt *struc.Options) (int, error) {
n := copy(p, r.content)
if n != r.length {
return 0, newError("failed to pack request salt with length")
}
return n, nil
}
func (r *requestSaltWithLength) Unpack(reader io.Reader, length int, opt *struc.Options) error {
r.content = make([]byte, r.length)
n, err := io.ReadFull(reader, r.content)
if err != nil {
return newError("failed to unpack request salt with length").Base(err)
}
if n != r.length {
return newError("failed to unpack request salt with length")
}
return nil
}
func (r *requestSaltWithLength) Size(opt *struc.Options) int {
return r.length
}
func (r *requestSaltWithLength) String() string {
return hex.Dump(r.content)
}
func (r *requestSaltWithLength) Bytes() []byte {
return r.content
}
func (r *requestSaltWithLength) FillAllFrom(reader io.Reader) error {
r.content = make([]byte, r.length)
_, err := io.ReadFull(reader, r.content)
if err != nil {
return newError("failed to fill salt from reader").Base(err)
}
return nil
}