mirror of
https://github.com/markqvist/LXMF.git
synced 2025-02-24 17:09:52 -05:00
Added PN announce data validation to announce handler
This commit is contained in:
parent
61331b58d7
commit
b172c7fcd4
@ -2,8 +2,7 @@ import time
|
|||||||
import RNS
|
import RNS
|
||||||
import RNS.vendor.umsgpack as msgpack
|
import RNS.vendor.umsgpack as msgpack
|
||||||
|
|
||||||
from .LXMF import APP_NAME, stamp_cost_from_app_data
|
from .LXMF import APP_NAME, stamp_cost_from_app_data, pn_announce_data_is_valid
|
||||||
|
|
||||||
from .LXMessage import LXMessage
|
from .LXMessage import LXMessage
|
||||||
|
|
||||||
class LXMFDeliveryAnnounceHandler:
|
class LXMFDeliveryAnnounceHandler:
|
||||||
@ -40,23 +39,24 @@ class LXMFPropagationAnnounceHandler:
|
|||||||
def received_announce(self, destination_hash, announced_identity, app_data):
|
def received_announce(self, destination_hash, announced_identity, app_data):
|
||||||
try:
|
try:
|
||||||
if type(app_data) == bytes:
|
if type(app_data) == bytes:
|
||||||
data = msgpack.unpackb(app_data)
|
|
||||||
|
|
||||||
if self.lxmrouter.propagation_node and self.lxmrouter.autopeer:
|
if self.lxmrouter.propagation_node and self.lxmrouter.autopeer:
|
||||||
node_timebase = data[1]
|
data = msgpack.unpackb(app_data)
|
||||||
propagation_transfer_limit = None
|
|
||||||
if len(data) >= 3:
|
|
||||||
try:
|
|
||||||
propagation_transfer_limit = float(data[2])
|
|
||||||
except:
|
|
||||||
propagation_transfer_limit = None
|
|
||||||
|
|
||||||
if data[0] == True:
|
if pn_announce_data_is_valid(data):
|
||||||
if RNS.Transport.hops_to(destination_hash) <= self.lxmrouter.autopeer_maxdepth:
|
node_timebase = data[1]
|
||||||
self.lxmrouter.peer(destination_hash, node_timebase, propagation_transfer_limit)
|
propagation_transfer_limit = None
|
||||||
|
if len(data) >= 3:
|
||||||
|
try:
|
||||||
|
propagation_transfer_limit = float(data[2])
|
||||||
|
except:
|
||||||
|
propagation_transfer_limit = None
|
||||||
|
|
||||||
elif data[0] == False:
|
if data[0] == True:
|
||||||
self.lxmrouter.unpeer(destination_hash, node_timebase)
|
if RNS.Transport.hops_to(destination_hash) <= self.lxmrouter.autopeer_maxdepth:
|
||||||
|
self.lxmrouter.peer(destination_hash, node_timebase, propagation_transfer_limit)
|
||||||
|
|
||||||
|
elif data[0] == False:
|
||||||
|
self.lxmrouter.unpeer(destination_hash, node_timebase)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
RNS.log("Error while evaluating propagation node announce, ignoring announce.", RNS.LOG_DEBUG)
|
RNS.log("Error while evaluating propagation node announce, ignoring announce.", RNS.LOG_DEBUG)
|
||||||
|
26
LXMF/LXMF.py
26
LXMF/LXMF.py
@ -83,6 +83,7 @@ AM_CUSTOM = 0xFF
|
|||||||
# handle and operate on LXMF data in client programs #
|
# handle and operate on LXMF data in client programs #
|
||||||
##########################################################
|
##########################################################
|
||||||
|
|
||||||
|
import RNS
|
||||||
import RNS.vendor.umsgpack as msgpack
|
import RNS.vendor.umsgpack as msgpack
|
||||||
def display_name_from_app_data(app_data=None):
|
def display_name_from_app_data(app_data=None):
|
||||||
if app_data == None:
|
if app_data == None:
|
||||||
@ -104,8 +105,8 @@ def display_name_from_app_data(app_data=None):
|
|||||||
try:
|
try:
|
||||||
decoded = dn.decode("utf-8")
|
decoded = dn.decode("utf-8")
|
||||||
return decoded
|
return decoded
|
||||||
except:
|
except Exception as e:
|
||||||
RNS.log("Could not decode display name in included announce data. The contained exception was: {e}", RNS.LOG_ERROR)
|
RNS.log(f"Could not decode display name in included announce data. The contained exception was: {e}", RNS.LOG_ERROR)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# Original announce format
|
# Original announce format
|
||||||
@ -128,3 +129,24 @@ def stamp_cost_from_app_data(app_data=None):
|
|||||||
# Original announce format
|
# Original announce format
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
def pn_announce_data_is_valid(data):
|
||||||
|
try:
|
||||||
|
if type(data) == bytes:
|
||||||
|
data = msgpack.unpackb(data)
|
||||||
|
|
||||||
|
if len(data) < 3:
|
||||||
|
raise ValueError("Invalid announce data: Insufficient peer data")
|
||||||
|
else:
|
||||||
|
if data[0] != True and data[0] != False:
|
||||||
|
raise ValueError("Invalid announce data: Indeterminate propagation node status")
|
||||||
|
try:
|
||||||
|
int(data[1])
|
||||||
|
except:
|
||||||
|
raise ValueError("Invalid announce data: Could not decode peer timebase")
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
RNS.log(f"Could not validate propagation node announce data: {e}", RNS.LOG_DEBUG)
|
||||||
|
return False
|
||||||
|
|
||||||
|
return True
|
Loading…
x
Reference in New Issue
Block a user