转发请求 UDPSocket

发布于 2024-10-05 10:54:30 字数 628 浏览 0 评论 0原文

我有一个基本的 ruby​​ 程序,它监听端口 (53),接收数据,然后发送到另一个位置(Google DNS 服务器 - 8.8.8.8)。回复不会返回到原来的目的地,或者我没有正确转发它们。

这是代码。注意我正在使用 EventMachine

require 'rubygems'
require 'eventmachine'

module DNSServer
    def post_init
        puts 'connected'
    end

    def receive_data(data)
        # Forward all 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 do
    EM.open_datagram_socket '0.0.0.0', 53, DNSServer
end

任何关于原因或调试技巧的想法将不胜感激。

I have a basic ruby program, that listens on a port (53), receives the data and then sends to another location (Google DNS server - 8.8.8.8). The responses are not going back to their original destination, or I'm not forwarding them correctly.

Here is the code. NB I'm using EventMachine

require 'rubygems'
require 'eventmachine'

module DNSServer
    def post_init
        puts 'connected'
    end

    def receive_data(data)
        # Forward all 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 do
    EM.open_datagram_socket '0.0.0.0', 53, DNSServer
end

Any thoughts as to why or tips to debug, would be most appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫瑾少女 2024-10-12 10:54:30

明显的问题是:

  1. UDP 通信通常是无连接的,使用 4 个参数版本的 send 而不是 connect
  2. 您没有从与 8.8.8.8 通信的套接字接收到任何数据
  3. 您没有将任何数据发送回(#send_data)到原始客户端

这似乎有效:

require 'socket'
require 'rubygems'
require 'eventmachine'

module DNSServer
    def receive_data(data)
        # Forward all data
        conn = UDPSocket.new
        conn.send data, 0, '8.8.8.8', 53
        send_data conn.recv 4096
    end
end

EM.run do
    EM.open_datagram_socket '0.0.0.0', 53, DNSServer
end

The obvious problems are:

  1. UDP comms are usually connectionless, use the 4 argument version of send instead of connect
  2. You're not receiving any data from the socket talking to 8.8.8.8
  3. You're not sending any data back (#send_data) to the original client

This seems to work:

require 'socket'
require 'rubygems'
require 'eventmachine'

module DNSServer
    def receive_data(data)
        # Forward all data
        conn = UDPSocket.new
        conn.send data, 0, '8.8.8.8', 53
        send_data conn.recv 4096
    end
end

EM.run do
    EM.open_datagram_socket '0.0.0.0', 53, DNSServer
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文