requestsalt.go 1.3 KB

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