github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/proxy/shadowsocks2022/requestsalt.go (about) 1 package shadowsocks2022 2 3 import ( 4 "encoding/hex" 5 "io" 6 7 "github.com/lunixbochs/struc" 8 ) 9 10 func newRequestSaltWithLength(length int) RequestSalt { 11 return &requestSaltWithLength{length: length} 12 } 13 14 type requestSaltWithLength struct { 15 length int 16 content []byte 17 } 18 19 func (r *requestSaltWithLength) isRequestSalt() {} 20 func (r *requestSaltWithLength) Pack(p []byte, opt *struc.Options) (int, error) { 21 n := copy(p, r.content) 22 if n != r.length { 23 return 0, newError("failed to pack request salt with length") 24 } 25 return n, nil 26 } 27 28 func (r *requestSaltWithLength) Unpack(reader io.Reader, length int, opt *struc.Options) error { 29 r.content = make([]byte, r.length) 30 n, err := io.ReadFull(reader, r.content) 31 if err != nil { 32 return newError("failed to unpack request salt with length").Base(err) 33 } 34 if n != r.length { 35 return newError("failed to unpack request salt with length") 36 } 37 return nil 38 } 39 40 func (r *requestSaltWithLength) Size(opt *struc.Options) int { 41 return r.length 42 } 43 44 func (r *requestSaltWithLength) String() string { 45 return hex.Dump(r.content) 46 } 47 48 func (r *requestSaltWithLength) Bytes() []byte { 49 return r.content 50 } 51 52 func (r *requestSaltWithLength) FillAllFrom(reader io.Reader) error { 53 r.content = make([]byte, r.length) 54 _, err := io.ReadFull(reader, r.content) 55 if err != nil { 56 return newError("failed to fill salt from reader").Base(err) 57 } 58 return nil 59 }