Class: SocksHandler::UDP

Inherits:
Object
  • Object
show all
Extended by:
SocksHandler
Defined in:
lib/socks_handler/udp.rb

Constant Summary

Constants included from SocksHandler

PROTOCOL_VERSION, VERSION

Class Method Summary collapse

Class Method Details

.associate_udp(socket, bind_host, bind_port, username = nil, password = nil) ⇒ SocksHandler::UDPSocket

Associates a TCP socket with a UDP connection

Examples:

tcp_socket = TCPSocket.new("127.0.0.1", 1080) # or Socket.tcp("127.0.0.1", 1080)
udp_socket = SocksHandler::UDP.associate_udp(tcp_socket, "0.0.0.0", 0)

"echo" is a UDP echo server that only the socks server can access
udp_socket.send("hello", 0, "echo", 7)
puts udp_socket.gets #=> hello

Parameters:

  • socket (Socket, TCPSocket)

    a socket that has connected to a socks server

  • bind_host (String)

    host for UDPSocket#bind

  • bind_port (Integer, String)

    port for UDPSocket#bind

  • username (String, nil) (defaults to: nil)
  • password (String, nil) (defaults to: nil)

Returns:



27
28
29
30
31
32
33
34
35
36
# File 'lib/socks_handler/udp.rb', line 27

def associate_udp(socket, bind_host, bind_port, username = nil, password = nil)
  negotiate(socket, username, password)
  address, port = send_details(socket,  Command::UDP_ASSOCIATE, bind_host, bind_port)
  SocksHandler::UDPSocket.new.tap do |s|
    # Use peeraddr instead of address
    # because we might not be able to access the address directly
    s.bind(bind_host, bind_port)
    s.connect_socks_server(socket.peeraddr[3], port)
  end
end