Skip to content
This repository was archived by the owner on May 16, 2019. It is now read-only.

Commit bc5a91e

Browse files
committed
Merge branch 'stats-collection'
2 parents 5ba23b6 + 9b75da4 commit bc5a91e

File tree

3 files changed

+7
-65
lines changed

3 files changed

+7
-65
lines changed

net/dos.py

Lines changed: 5 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,8 @@
88
MALFORMATED = 110
99

1010
SCORES = {
11-
PING: 0,
12-
STUN: 0,
13-
STORE: 0,
14-
INV: 0,
15-
VALUES: 0,
16-
GET_LISTINGS: 0,
1711
FOLLOW: 0,
18-
UNFOLLOW: 0,
19-
RECONNECTIONS: 0,
20-
MALFORMATED: 0
12+
UNFOLLOW: 0
2113
}
2214

2315

@@ -28,54 +20,15 @@ def __init__(self, multiplexer, ban_time=86400):
2820
self.ban_time = ban_time
2921
self.peers = {}
3022
self.scoring_loop = task.LoopingCall(self.adjust_scores)
31-
#self.scoring_loop.start(30, now=False)
23+
self.scoring_loop.start(30, now=False)
3224
self.log = Logger(system=self)
3325

3426
def process_message(self, peer, message):
35-
if peer:
36-
# disabled for now
37-
return
3827
if peer[0] not in self.peers:
3928
self.peers[peer[0]] = SCORES.copy()
4029

4130
try:
42-
if message == 100:
43-
self.peers[peer[0]][RECONNECTIONS] += 1
44-
if self.peers[peer[0]][RECONNECTIONS] > 10:
45-
self.ban(peer, RECONNECTIONS)
46-
return
47-
elif message == 110:
48-
self.peers[peer[0]][MALFORMATED] += 1
49-
if self.peers[peer[0]][MALFORMATED] > 10:
50-
self.ban(peer, MALFORMATED)
51-
return
52-
if message.command == PING:
53-
self.peers[peer[0]][PING] += 0.5
54-
if self.peers[peer[0]][PING] > 10:
55-
self.ban(peer, PING)
56-
elif message.command == STUN:
57-
self.peers[peer[0]][STUN] += 1
58-
if self.peers[peer[0]][STUN] > 1:
59-
self.ban(peer, STUN)
60-
elif message.command == STORE:
61-
args = tuple(message.arguments)
62-
for arg in args:
63-
self.peers[peer[0]][STORE] += len(arg)
64-
if self.peers[peer[0]][STORE] > 1000000:
65-
self.ban(peer, STORE)
66-
elif message.command == INV:
67-
self.peers[peer[0]][INV] += 30
68-
if self.peers[peer[0]][INV] > 150:
69-
self.ban(peer, INV)
70-
elif message.command == VALUES:
71-
self.peers[peer[0]][VALUES] += 30
72-
if self.peers[peer[0]][VALUES] > 150:
73-
self.ban(peer, VALUES)
74-
elif message.command == GET_LISTINGS:
75-
self.peers[peer[0]][GET_LISTINGS] += 5
76-
if self.peers[peer[0]][GET_LISTINGS] > 250:
77-
self.ban(peer, GET_LISTINGS)
78-
elif message.command == FOLLOW:
31+
if message.command == FOLLOW:
7932
self.peers[peer[0]][FOLLOW] += 1
8033
if self.peers[peer[0]][FOLLOW] > 3:
8134
self.ban(peer, FOLLOW)
@@ -88,12 +41,7 @@ def process_message(self, peer, message):
8841
self.log.warning("Exception processing banscore")
8942

9043
def ban(self, peer, message_type):
91-
if message_type == 100:
92-
reason = "RECONNECTIONS"
93-
elif message_type == 110:
94-
reason = "MALFORMATTED"
95-
else:
96-
reason = Command.Name(message_type)
44+
reason = Command.Name(message_type)
9745
self.log.warning("Banned %s. Reason: too many %s messages." %
9846
(peer[0], reason))
9947
self.multiplexer.ban_ip(peer[0])
@@ -107,9 +55,6 @@ def adjust_scores(self):
10755
for k, v in self.peers[peer].items():
10856
if v > 0:
10957
remove = False
110-
if k == STORE:
111-
self.peers[peer][k] = v - 350
112-
else:
113-
self.peers[peer][k] = v - 1
58+
self.peers[peer][k] = v - 1
11459
if remove:
11560
del self.peers[peer]

net/rpcudp.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ def receive_message(self, message, sender, connection, ban_score):
6161
if msgID in self._outstanding:
6262
self._acceptResponse(msgID, data, sender)
6363
elif message.command != NOT_FOUND:
64-
#ban_score.process_message(connection.dest_addr, message)
64+
ban_score.process_message(connection.dest_addr, message)
6565
self._acceptRequest(msgID, str(Command.Name(message.command)).lower(), data, sender, connection)
66-
#else: ban_score.process_message(connection.dest_addr, message)
66+
6767

6868
def _acceptResponse(self, msgID, data, sender):
6969
if data is not None:

net/wireprotocol.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,10 @@ def on_connection_made(self):
7777
if self.connection.state == State.CONNECTED:
7878
self.addr = str(self.connection.dest_addr[0]) + ":" + str(self.connection.dest_addr[1])
7979
self.log.info("connected to %s" % self.addr)
80-
#self.ban_score.process_message(self.connection.dest_addr, 100)
8180

8281
def receive_message(self, datagram):
8382
if len(datagram) < 166:
8483
self.log.warning("received datagram too small from %s, ignoring" % self.addr)
85-
#self.ban_score.process_message(self.connection.dest_addr, 110)
8684
return False
8785
try:
8886
m = Message()
@@ -109,7 +107,6 @@ def receive_message(self, datagram):
109107
except Exception:
110108
# If message isn't formatted property then ignore
111109
self.log.warning("received an invalid message from %s, ignoring" % self.addr)
112-
#self.ban_score.process_message(self.connection.dest_addr, 110)
113110
return False
114111

115112
def handle_shutdown(self):

0 commit comments

Comments
 (0)