socks4.go 763 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package protocol
  2. import (
  3. "io"
  4. "v2ray.com/core/common/errors"
  5. v2net "v2ray.com/core/common/net"
  6. )
  7. var (
  8. Socks4Downgrade = errors.New("Downgraded to Socks 4.")
  9. )
  10. type Socks4AuthenticationRequest struct {
  11. Version byte
  12. Command byte
  13. Port v2net.Port
  14. IP [4]byte
  15. }
  16. type Socks4AuthenticationResponse struct {
  17. result byte
  18. port uint16
  19. ip []byte
  20. }
  21. func NewSocks4AuthenticationResponse(result byte, port v2net.Port, ip []byte) *Socks4AuthenticationResponse {
  22. return &Socks4AuthenticationResponse{
  23. result: result,
  24. port: port.Value(),
  25. ip: ip,
  26. }
  27. }
  28. func (r *Socks4AuthenticationResponse) Write(writer io.Writer) {
  29. writer.Write([]byte{
  30. byte(0x00), r.result, byte(r.port >> 8), byte(r.port),
  31. r.ip[0], r.ip[1], r.ip[2], r.ip[3]})
  32. }