0

我有一个 DNS 服务器,我想在其上添加一些速率限制,因为我收到了一些突发的查询。

我通读了https://github.com/racker/python-twisted-core/blob/master/doc/examples/shaper.py并根据我的需要进行了调整:

from twisted.internet import reactor, protocol
from twisted.protocols import htb

udpBucket = htb.Bucket()
udpBucket.maxburst = 100000 # 1000 kb/s
udpBucket.rate     = 100000 # 1000 kb/s thereafter 

udpFilter = htb.HierarchicalBucketFilter()
udpFilter.buckets[None] = udpBucket

class DNSClientBucket(htb.Bucket):
  maxburst = 10000 # 10 kb/s
  rate     =  1000 #  1 kb/s thereafter

udpFilter2 = htb.FilterByHost(udpFilter)
udpFilter2.bucketFactory = DNSClientBucket

class DnsUdpProtocol(protocol.Protocol):
  def datagramReceived(self, data, client):
    print(data)

dns_udp_protocol = DnsUdpProtocol()
dns_udp_protocol = htb.ShapedProtocolFactory(dns_udp_protocol, udpFilter2)

dns_udp_interface   = socket.gethostbyname(socket.gethostname())
dns_udp_server_port = 53
reactor.listenUDP(port=dns_udp_server_port, protocol=dns_udp_protocol, interface=dns_udp_interface)

但我得到以下异常:

Traceback (most recent call last):
  File "server.py", line 263, in <module>
    reactor.listenUDP(port=dns_udp_server_port, protocol=dns_udp_protocol, interface=dns_udp_interface)
  File "/usr/lib/python3.7/site-packages/twisted/internet/posixbase.py", line 369, in listenUDP
    p.startListening()
  File "/usr/lib/python3.7/site-packages/twisted/internet/udp.py", line 179, in startListening
    self._connectToProtocol()
  File "/usr/lib/python3.7/site-packages/twisted/internet/udp.py", line 217, in _connectToProtocol
    self.protocol.makeConnection(self)
AttributeError: 'ShapedProtocolFactory' object has no attribute 'makeConnection'

甚至可以将twisted.protocols.htb与 UDP 协议一起使用吗?

4

1 回答 1

0

可能不是。Twisted 中的 HTB 用于面向流的套接字,而不是面向数据报的套接字,如 UDP。

此外,即使出于预期目的,Twisted 中的 HTB 也可能相当粗略。

于 2019-09-09T23:15:48.293 回答