Skip to content

Commit

Permalink
session: disable resource-limits for private IP sessions
Browse files Browse the repository at this point in the history
When hosting a server over .onion, all resource usage was accounted against
the private IP of the Tor gateway (e.g. localhost).

related: kyuupichan/electrumx#817 (comment)
  • Loading branch information
SomberNight authored and DonPharoah committed Feb 3, 2021
1 parent e0b8bf0 commit 7370e9a
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions electrumx/server/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from collections import defaultdict
from functools import partial
from ipaddress import IPv4Address, IPv6Address
from typing import Optional

import attr
from aiorpcx import (
Expand Down Expand Up @@ -769,15 +770,21 @@ async def _notify_sessions(self, height, touched):
def _ip_addr_group_name(self, session):
host = session.remote_address().host
if isinstance(host, IPv4Address):
return '.'.join(str(host).split('.')[:3])
if host.is_private: # exempt private addresses
return None
return '.'.join(str(host).split('.')[:3]) # /24
if isinstance(host, IPv6Address):
return ':'.join(host.exploded.split(':')[:3])
if host.is_private:
return None
return ':'.join(host.exploded.split(':')[:3]) # /48
return 'unknown_addr'

def _timeslice_name(self, session):
return f't{int(session.start_time - self.start_time) // 300}'

def _session_group(self, name, weight):
if name is None:
return None
group = self.session_groups.get(name)
if not group:
group = SessionGroup(name, weight, set(), 0)
Expand All @@ -791,6 +798,7 @@ def add_session(self, session):
self._session_group(self._timeslice_name(session), 0.03),
self._session_group(self._ip_addr_group_name(session), 1.0),
)
groups = [group for group in groups if group is not None]
self.sessions[session] = groups
for group in groups:
group.sessions.add(session)
Expand Down

0 comments on commit 7370e9a

Please sign in to comment.