ruby - Forwarding requests UDPSocket -
i have basic ruby program, listens on port (53), receives data , sends location (google dns server - 8.8.8.8). responses not going original destination, or i'm not forwarding them correctly.
here code. nb i'm using eventmachine
require 'rubygems' require 'eventmachine' module dnsserver def post_init puts 'connected' end def receive_data(data) # forward data conn = udpsocket.new conn.connect '8.8.8.8', 53 conn.send data, 0 conn.close p data.unpack("h*") end def unbind puts 'disconnected' end end em.run em.open_datagram_socket '0.0.0.0', 53, dnsserver end
any thoughts why or tips debug, appreciated.
the obvious problems are:
- udp comms connectionless, use 4 argument version of
send
instead ofconnect
- you're not receiving data socket talking 8.8.8.8
- you're not sending data (
#send_data
) original client
this seems work:
require 'socket' require 'rubygems' require 'eventmachine' module dnsserver def receive_data(data) # forward data conn = udpsocket.new conn.send data, 0, '8.8.8.8', 53 send_data conn.recv 4096 end end em.run em.open_datagram_socket '0.0.0.0', 53, dnsserver end
Comments
Post a Comment