socks4.go 1.1 KB

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