Skip to content

Commit

Permalink
Added incoming resource transfer size limits
Browse files Browse the repository at this point in the history
  • Loading branch information
markqvist committed Mar 1, 2024
1 parent 64050d3 commit 7ecd3c0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
26 changes: 23 additions & 3 deletions LXMF/LXMRouter.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class LXMRouter:
FASTEST_N_RANDOM_POOL = 2

PROPAGATION_LIMIT = 256
DELIVERY_LIMIT = 1024
DELIVERY_LIMIT = 1000

PR_PATH_TIMEOUT = 10

Expand Down Expand Up @@ -970,7 +970,8 @@ def delivery_packet(self, data, packet):
def delivery_link_established(self, link):
link.track_phy_stats(True)
link.set_packet_callback(self.delivery_packet)
link.set_resource_strategy(RNS.Link.ACCEPT_ALL)
link.set_resource_strategy(RNS.Link.ACCEPT_APP)
link.set_resource_callback(self.delivery_resource_advertised)
link.set_resource_started_callback(self.resource_transfer_began)
link.set_resource_concluded_callback(self.delivery_resource_concluded)

Expand All @@ -980,6 +981,15 @@ def delivery_link_closed(self, link):
def resource_transfer_began(self, resource):
RNS.log("Transfer began for LXMF delivery resource "+str(resource), RNS.LOG_DEBUG)

def delivery_resource_advertised(self, resource):
size = resource.get_data_size()
limit = self.delivery_per_transfer_limit*1000
if limit != None and size > limit:
RNS.log("Rejecting "+RNS.prettysize(size)+" incoming LXMF delivery resource, since it exceeds the limit of "+RNS.prettysize(limit), RNS.LOG_DEBUG)
return False
else:
return True

def delivery_resource_concluded(self, resource):
RNS.log("Transfer concluded for LXMF delivery resource "+str(resource), RNS.LOG_DEBUG)
if resource.status == RNS.Resource.COMPLETE:
Expand Down Expand Up @@ -1080,11 +1090,21 @@ def sync_peers(self):

def propagation_link_established(self, link):
link.set_packet_callback(self.propagation_packet)
link.set_resource_strategy(RNS.Link.ACCEPT_ALL)
link.set_resource_strategy(RNS.Link.ACCEPT_APP)
link.set_resource_callback(self.propagation_resource_advertised)
link.set_resource_started_callback(self.resource_transfer_began)
link.set_resource_concluded_callback(self.propagation_resource_concluded)
self.active_propagation_links.append(link)

def propagation_resource_advertised(self, resource):
size = resource.get_data_size()
limit = self.propagation_per_transfer_limit*1000
if limit != None and size > limit:
RNS.log("Rejecting "+RNS.prettysize(size)+" incoming LXMF propagation resource, since it exceeds the limit of "+RNS.prettysize(limit), RNS.LOG_DEBUG)
return False
else:
return True

def propagation_packet(self, data, packet):
try:
if packet.destination_type != RNS.Destination.LINK:
Expand Down
4 changes: 2 additions & 2 deletions LXMF/Utilities/lxmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def apply_config():
if active_configuration["delivery_transfer_max_accepted_size"] < 0.38:
active_configuration["delivery_transfer_max_accepted_size"] = 0.38
else:
active_configuration["delivery_transfer_max_accepted_size"] = 1024
active_configuration["delivery_transfer_max_accepted_size"] = 1000

if "lxmf" in lxmd_config and "on_inbound" in lxmd_config["lxmf"]:
active_configuration["on_inbound"] = lxmd_config["lxmf"]["on_inbound"]
Expand Down Expand Up @@ -526,7 +526,7 @@ def main():
# this will be rejected before the transfer
# begins.
delivery_transfer_max_accepted_size = 1024
delivery_transfer_max_accepted_size = 1000
# You can configure an external program to be run
# every time a message is received. The program
Expand Down

0 comments on commit 7ecd3c0

Please sign in to comment.