Module: HTTPX::Plugins::Proxy::Socks4::Packet

Defined in:
lib/httpx/plugins/proxy/socks4.rb

Class Method Summary collapse

Class Method Details

.connect(parameters, uri) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/httpx/plugins/proxy/socks4.rb', line 95

def connect(parameters, uri)
  packet = [VERSION, CONNECT, uri.port].pack("CCn")
  begin
    ip = IPAddr.new(uri.host)
    raise Error, "Socks4 connection to #{ip} not supported" unless ip.ipv4?

    packet << [ip.to_i].pack("N")
  rescue IPAddr::InvalidAddressError
    if parameters.uri.scheme == "socks4"
      # resolv defaults to IPv4, and socks4 doesn't support IPv6 otherwise
      ip = IPAddr.new(Resolv.getaddress(uri.host))
      packet << [ip.to_i].pack("N")
    else
      packet << "\x0\x0\x0\x1" << "\x7\x0" << uri.host
    end
  end
  packet << [parameters.username].pack("Z*")
  packet
end