From: Forrest Voight Date: Sun, 12 Feb 2012 02:58:42 +0000 (-0500) Subject: only allow three incoming connections per /16 X-Git-Tag: 0.8.6~19 X-Git-Url: https://git.novaco.in/?a=commitdiff_plain;h=b2132b4bd7c34390283af6719098899ccca32735;p=p2pool.git only allow three incoming connections per /16 --- diff --git a/p2pool/p2p.py b/p2pool/p2p.py index b46d378..cf90a78 100644 --- a/p2pool/p2p.py +++ b/p2pool/p2p.py @@ -230,20 +230,28 @@ class ServerFactory(protocol.ServerFactory): self.node = node self.max_conns = max_conns - self.conns = set() + self.conns = {} self.running = False def buildProtocol(self, addr): - if len(self.conns) >= self.max_conns: + if sum(self.conns.itervalues()) >= self.max_conns or self.conns.get(self._host_to_ident(addr.host), 0) >= 3: return None p = Protocol(self.node, True) p.factory = self return p + def _host_to_ident(self, host): + a, b, c, d = host.split('.') + return a, b + def proto_made_connection(self, proto): - self.conns.add(proto) + ident = self._host_to_ident(proto.transport.getPeer().host) + self.conns[ident] = self.conns.get(ident, 0) + 1 def proto_lost_connection(self, proto, reason): - self.conns.remove(proto) + ident = self._host_to_ident(proto.transport.getPeer().host) + self.conns[ident] -= 1 + if not self.conns[ident]: + del self.conns[ident] def proto_connected(self, proto): self.node.got_conn(proto)