UDP代理实现

时间:2011-03-23 09:46:29

标签: delphi proxy udp pascal synapse

我正在尝试构建一个简单的TCP和UDP代理。 TCP代理没有问题,但UDP处理起来要复杂一些。经典的代理方案就是这样。

  1. 侦听来自客户端的传入数据包
  2. 收到数据包后,将其发送到目的地(服务器)
  3. 侦听来自服务器的可能响应数据包
  4. 将响应数据包发送回客户端
  5. 好的,这应该很简单。但是当试图使用Synapse或Indy实现这个时我遇到了问题。当我从客户端收到数据包时,我建立了一个内部UDP客户端,将数据包转发到目的地。然后我必须听取目的地的可能回复。现在问题是什么是最好的实现?在TCP中没有单一的请求/响应。目的地可以随着时间的推移响应多个答案,或根本不响应。如果我继续通过一个客户端数据包侦听响应,那么我将错过来自此客户端或其他客户端的其他未来数据包。

    我正在为这个问题寻找一个好的设计。以下是供参考的示例通信。请注意目标位置的多个响应

    - bind UDP port 40222 on interface 0.0.0.0
    - ready
    - add 127.0.0.1:4569
    
    127.0.0.1:4569 -> 192.168.90.10:4569
    c3 ef 00 00 00 00 00 03 00 00 06 01 0b 02 00 02   ................
    02 0a 37 30 30 35 35 35 31 32 31 32 04 0d 4e 6f   ..7005551212..No
    74 20 41 76 61 69 6c 61 62 6c 65 09 04 00 00 00   t Available.....
    08 08 04 00 00 00 08 06 06 31 36 31 34 30 31 01   .........161401.
    08 34 31 33 31 33 39 34 37 0d 08 34 31 33 31 33   .41313947..41313
    39 34 37                                          947
    
    192.168.90.10:4569 -> 127.0.0.1:4569
    a9 e7 43 ef 00 00 00 09 00 01 06 08 0e 02 00 03   ..C.............
    0f 09 34 31 38 32 32 31 37 38 33 06 06 31 36 31   ..418221783..161
    34 30 31                                          401
    
    127.0.0.1:4569 -> 192.168.90.10:4569
    c3 ef 29 e7 00 00 00 4f 01 01 06 09 10 20 39 36   ..)....O..... 96
    64 66 37 31 32 38 61 62 35 39 39 37 65 36 37 36   df7128ab5997e676
    65 62 38 63 61 30 33 39 38 66 33 34 30 65         eb8ca0398f340e
    
    192.168.90.10:4569 -> 127.0.0.1:4569
    a9 e7 43 ef 00 00 00 56 01 02 06 07 09 04 00 00   ..C....V........
    00 08                                             ..
    
    127.0.0.1:4569 -> 192.168.90.10:4569
    c3 ef 29 e7 00 00 00 56 02 02 06 04               ..)....V....
    
    192.168.90.10:4569 -> 127.0.0.1:4569
    a9 e7 43 ef 00 00 02 85 02 02 04 0e               ..C.........
    
    192.168.90.10:4569 -> 127.0.0.1:4569
    a9 e7 43 ef 00 00 02 96 03 02 02 08 54 54 54 54   ..C.........TTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54               TTTTTTTTTTTT
    
    127.0.0.1:4569 -> 192.168.90.10:4569
    c3 ef 29 e7 00 00 02 96 02 04 06 04               ..).........
    
    192.168.90.10:4569 -> 127.0.0.1:4569
    29 e7 02 aa 54 54 54 54 54 54 54 54 54 54 54 54   )...TTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54                                       TTTT
    
    192.168.90.10:4569 -> 127.0.0.1:4569
    29 e7 02 be 54 54 54 54 54 54 54 54 54 54 54 54   )...TTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54                                       TTTT
    
    192.168.90.10:4569 -> 127.0.0.1:4569
    29 e7 02 d2 54 54 54 54 54 54 54 54 54 54 54 54   )...TTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54                                       TTTT
    
    192.168.90.10:4569 -> 127.0.0.1:4569
    29 e7 02 e6 54 54 54 54 54 54 54 54 54 54 54 54   )...TTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54                                       TTTT
    
    192.168.90.10:4569 -> 127.0.0.1:4569
    29 e7 02 fa 54 54 54 54 54 54 54 54 54 54 54 54   )...TTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54   TTTTTTTTTTTTTTTT
    54 54 54 54                                       TTTT
    

    修改

    记录。也许UDP代理实现起来很麻烦,因为它可以使用。这是一个很大的概率,而理论上它肯定是可行的。但我会尝试只是为了它的乐趣。如果我得到一个稳定的工作解决方案,那就更好了。否则我会学到新的东西并承认失败。

    我决不会顽固地走在墙上。我仍然希望有人有个好主意:))

2 个答案:

答案 0 :(得分:1)

不只是代理服务器遇到UDP问题 - 网状设备(如状态防火墙和NAT设备)也会遇到此问题。

它处理的典型方式是超时。一旦在超时的长度内没有观察到代理客户端和服务器之间的UDP流量,则“连接”被拆除。当看到流量时,重置超时。

此外,拥有一个当前代理连接不应该阻止同时打开另一个连接 - 您的代理应该能够处理它。

答案 1 :(得分:0)

典型的UDP客户端 - 服务器通信可能如下所示:

  • 客户端将UDP数据包发送到端口1000
  • 服务器接收数据包并将响应发送回端口1001(或端口1000!)

您的代理应该只监听端口1000和1001上的数据包。当数据包进入端口1000时,将其发送到服务器上的端口1000。当数据包到达端口1001时,它来自服务器,需要在端口1001上发送到适当的客户端。这几乎是乐趣结束的地方。 UDP在会话或连接管理方面没有提供任何内容:这完全取决于您尝试代理的特定UDP协议。如果您收到来自2个不同客户端的UDP数据包,并且您从服务器收到“响应”数据包,则UDP本身没有任何内容可以告诉您转发该数据包的位置。在UDP之上构建的协议可能会或可能不会有某种维护状态的方式。

无法使用通用解决方案,您可以阅读RFC,并且可以为要支持的每个UDP协议实现特定的帮助程序。