Skip to content

Commit 930ba81

Browse files
committed
p2p: change how the score and confidence is retreived from redis
1 parent 2cf5e45 commit 930ba81

File tree

2 files changed

+15
-40
lines changed

2 files changed

+15
-40
lines changed

modules/p2ptrust/p2ptrust.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class Trust(IModule):
7373
authors = ["Dita", "Alya Gomaa"]
7474
pigeon_port = 6668
7575
rename_with_port = False
76-
slips_update_channel = "ip_info_change"
76+
# slips_update_channel = "ip_info_change"
7777
p2p_data_request_channel = "p2p_data_request"
7878
gopy_channel_raw = "p2p_gopy"
7979
pygo_channel_raw = "p2p_pygo"
@@ -101,7 +101,7 @@ def init(self, *args, **kwargs):
101101

102102
self.gopy_channel = self.gopy_channel_raw + str_port
103103
self.pygo_channel = self.pygo_channel_raw + str_port
104-
self.storage_name = "IPsInfo"
104+
self.storage_name = self.db.constants.IPS_INFO
105105
if self.rename_redis_ip_info:
106106
self.storage_name += str(self.port)
107107
self.c1 = self.db.subscribe("report_to_peers")

modules/p2ptrust/utils/utils.py

Lines changed: 13 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,21 @@ def get_ip_info_from_slips(ip_address: str, db) -> (float, float):
9999
"""
100100

101101
# poll new info from redis
102-
ip_info = db.get_ip_info(ip_address)
103-
104-
# There is a bug in the database where sometimes False is returned when key is not found. Correctly, dictionary
105-
# should be always returned, even if it is empty. This check cannot be simplified to `if not ip_info`, because I
106-
# want the empty dictionary to be handled by the read data function.
107-
# TODO: when database is fixed and doesn't return booleans, remove this IF statement
108-
if ip_info is False:
102+
# convert it to float
103+
# the higher the score, the more malicious this ip
104+
try:
105+
if threat_level := db.get_ip_info(ip_address, "threat_level"):
106+
slips_score: float = threat_levels[threat_level]
107+
else:
108+
slips_score = float(db.get_ip_info(ip_address, "score"))
109+
except (KeyError, ValueError):
109110
return None, None
110111

111-
slips_score, slips_confidence = read_data_from_ip_info(ip_info)
112+
try:
113+
slips_confidence = float(db.get_ip_info(ip_address, "confidence"))
114+
except ValueError:
115+
pass
116+
112117
# check that both values were provided
113118
# TODO by Martin: Dita does not handle scenario when only confidence is None, is it intentional?
114119
return (
@@ -118,36 +123,6 @@ def get_ip_info_from_slips(ip_address: str, db) -> (float, float):
118123
)
119124

120125

121-
# parse data from redis
122-
def read_data_from_ip_info(ip_info: dict) -> (float, float):
123-
"""
124-
Get score and confidence from the data that is saved in Redis.
125-
126-
:param ip_info: The redis data for one IP address
127-
:return: Tuple with score and confidence. If data is not there,
128-
(None, None) is returned instead.
129-
"""
130-
# the higher the score, the more malicious this ip
131-
try:
132-
if "threat_level" in ip_info:
133-
score = threat_levels[ip_info["threat_level"]]
134-
else:
135-
score = ip_info["score"]
136-
137-
confidence = ip_info["confidence"]
138-
try:
139-
confidence = float(confidence)
140-
except ValueError:
141-
# sometimes the confidence is stored as a float,
142-
# and sometimes it's stored like this 'confidence: 0.6'
143-
# #TODO see what stores it in the second format instead of this try except
144-
confidence = float(confidence.split()[-1])
145-
146-
return float(score), confidence
147-
except (KeyError, TypeError):
148-
return None, None
149-
150-
151126
#
152127
# SEND COMMUNICATION TO GO
153128
#

0 commit comments

Comments
 (0)