From c094d8f2e81ec4d1c95d983930cfe6eaaa5fe9e5 Mon Sep 17 00:00:00 2001 From: Jack Robison Date: Wed, 28 Apr 2021 15:28:00 -0400 Subject: [PATCH] add ALLOW_LAN_UDP hub setting --- lbry/utils.py | 6 ++++-- lbry/wallet/server/env.py | 1 + lbry/wallet/server/server.py | 2 +- lbry/wallet/server/udp.py | 12 ++++++++---- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/lbry/utils.py b/lbry/utils.py index de55504f2..e5d624991 100644 --- a/lbry/utils.py +++ b/lbry/utils.py @@ -380,13 +380,15 @@ CARRIER_GRADE_NAT_SUBNET = ipaddress.ip_network('100.64.0.0/10') IPV4_TO_6_RELAY_SUBNET = ipaddress.ip_network('192.88.99.0/24') -def is_valid_public_ipv4(address, allow_localhost: bool = False): +def is_valid_public_ipv4(address, allow_localhost: bool = False, allow_lan: bool = False): try: parsed_ip = ipaddress.ip_address(address) if parsed_ip.is_loopback and allow_localhost: return True + if allow_lan and parsed_ip.is_private: + return True if any((parsed_ip.version != 4, parsed_ip.is_unspecified, parsed_ip.is_link_local, parsed_ip.is_loopback, - parsed_ip.is_multicast, parsed_ip.is_reserved, parsed_ip.is_private, parsed_ip.is_reserved)): + parsed_ip.is_multicast, parsed_ip.is_reserved, parsed_ip.is_private)): return False else: return not any((CARRIER_GRADE_NAT_SUBNET.supernet_of(ipaddress.ip_network(f"{address}/32")), diff --git a/lbry/wallet/server/env.py b/lbry/wallet/server/env.py index 18f594a96..5477cf9e1 100644 --- a/lbry/wallet/server/env.py +++ b/lbry/wallet/server/env.py @@ -73,6 +73,7 @@ class Env: self.tor_banner_file = self.default('TOR_BANNER_FILE', self.banner_file) self.anon_logs = self.boolean('ANON_LOGS', False) self.log_sessions = self.integer('LOG_SESSIONS', 3600) + self.allow_lan_udp = self.boolean('ALLOW_LAN_UDP', False) # Peer discovery self.peer_discovery = self.peer_discovery_enum() self.peer_announce = self.boolean('PEER_ANNOUNCE', True) diff --git a/lbry/wallet/server/server.py b/lbry/wallet/server/server.py index fd85bd202..956f06ed2 100644 --- a/lbry/wallet/server/server.py +++ b/lbry/wallet/server/server.py @@ -115,7 +115,7 @@ class Server: if self.env.udp_port: await self.bp.status_server.start( 0, bytes.fromhex(self.bp.coin.GENESIS_HASH)[::-1], - self.env.host, self.env.udp_port + self.env.host, self.env.udp_port, self.env.allow_lan_udp ) await _start_cancellable(self.bp.fetch_and_process_blocks) diff --git a/lbry/wallet/server/udp.py b/lbry/wallet/server/udp.py index 2e44654d1..1357beb04 100644 --- a/lbry/wallet/server/udp.py +++ b/lbry/wallet/server/udp.py @@ -70,7 +70,7 @@ class SPVServerStatusProtocol(asyncio.DatagramProtocol): PROTOCOL_VERSION = 1 def __init__(self, height: int, tip: bytes, throttle_cache_size: int = 1024, throttle_reqs_per_sec: int = 10, - allow_localhost: bool = False): + allow_localhost: bool = False, allow_lan: bool = False): super().__init__() self.transport: Optional[asyncio.transports.DatagramTransport] = None self._height = height @@ -82,6 +82,7 @@ class SPVServerStatusProtocol(asyncio.DatagramProtocol): self._should_log = LRUCache(throttle_cache_size) self._min_delay = 1 / throttle_reqs_per_sec self._allow_localhost = allow_localhost + self._allow_lan = allow_lan def update_cached_response(self): self._cached_response = SPVPong.make(self._height, self._tip, self._flags, self.PROTOCOL_VERSION) @@ -121,7 +122,8 @@ class SPVServerStatusProtocol(asyncio.DatagramProtocol): except (ValueError, struct.error, AttributeError, TypeError): # log.exception("derp") return - if is_valid_public_ipv4(addr[0], allow_localhost=self._allow_localhost) and addr[1] >= 1024: + if addr[1] >= 1024 and is_valid_public_ipv4( + addr[0], allow_localhost=self._allow_localhost, allow_lan=self._allow_lan): self.transport.sendto(self.make_pong(addr[0]), addr) else: log.warning("odd packet from %s:%i", addr[0], addr[1]) @@ -142,12 +144,14 @@ class StatusServer: def __init__(self): self._protocol: Optional[SPVServerStatusProtocol] = None - async def start(self, height: int, tip: bytes, interface: str, port: int): + async def start(self, height: int, tip: bytes, interface: str, port: int, allow_lan: bool = False): if self.is_running: return loop = asyncio.get_event_loop() interface = interface if interface.lower() != 'localhost' else '127.0.0.1' - self._protocol = SPVServerStatusProtocol(height, tip, allow_localhost=interface == '127.0.0.1') + self._protocol = SPVServerStatusProtocol( + height, tip, allow_localhost=interface == '127.0.0.1', allow_lan=allow_lan + ) await loop.create_datagram_endpoint(lambda: self._protocol, (interface, port)) log.info("started udp status server on %s:%i", interface, port)