socks4.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package protocol
  2. import (
  3. "github.com/v2ray/v2ray-core/common/errors"
  4. )
  5. type SocksVersion4Error struct {
  6. errors.ErrorCode
  7. }
  8. var socksVersion4ErrorInstance = SocksVersion4Error{ErrorCode: 1000}
  9. func NewSocksVersion4Error() SocksVersion4Error {
  10. return socksVersion4ErrorInstance
  11. }
  12. func (err SocksVersion4Error) Error() string {
  13. return err.Prefix() + "Request is socks version 4."
  14. }
  15. type Socks4AuthenticationRequest struct {
  16. Version byte
  17. Command byte
  18. Port uint16
  19. IP [4]byte
  20. }
  21. type Socks4AuthenticationResponse struct {
  22. result byte
  23. port uint16
  24. ip []byte
  25. }
  26. func NewSocks4AuthenticationResponse(result byte, port uint16, ip []byte) *Socks4AuthenticationResponse {
  27. return &Socks4AuthenticationResponse{
  28. result: result,
  29. port: port,
  30. ip: ip,
  31. }
  32. }
  33. func (r *Socks4AuthenticationResponse) ToBytes(buffer []byte) []byte {
  34. if buffer == nil {
  35. buffer = make([]byte, 8)
  36. }
  37. buffer[1] = r.result
  38. buffer[2] = byte(r.port >> 8)
  39. buffer[3] = byte(r.port)
  40. copy(buffer[4:], r.ip)
  41. return buffer
  42. }