forked from LBRYCommunity/lbry-sdk
add ALLOW_LAN_UDP hub setting
This commit is contained in:
parent
c465d6a6c2
commit
c094d8f2e8
4 changed files with 14 additions and 7 deletions
|
@ -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')
|
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:
|
try:
|
||||||
parsed_ip = ipaddress.ip_address(address)
|
parsed_ip = ipaddress.ip_address(address)
|
||||||
if parsed_ip.is_loopback and allow_localhost:
|
if parsed_ip.is_loopback and allow_localhost:
|
||||||
return True
|
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,
|
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
|
return False
|
||||||
else:
|
else:
|
||||||
return not any((CARRIER_GRADE_NAT_SUBNET.supernet_of(ipaddress.ip_network(f"{address}/32")),
|
return not any((CARRIER_GRADE_NAT_SUBNET.supernet_of(ipaddress.ip_network(f"{address}/32")),
|
||||||
|
|
|
@ -73,6 +73,7 @@ class Env:
|
||||||
self.tor_banner_file = self.default('TOR_BANNER_FILE', self.banner_file)
|
self.tor_banner_file = self.default('TOR_BANNER_FILE', self.banner_file)
|
||||||
self.anon_logs = self.boolean('ANON_LOGS', False)
|
self.anon_logs = self.boolean('ANON_LOGS', False)
|
||||||
self.log_sessions = self.integer('LOG_SESSIONS', 3600)
|
self.log_sessions = self.integer('LOG_SESSIONS', 3600)
|
||||||
|
self.allow_lan_udp = self.boolean('ALLOW_LAN_UDP', False)
|
||||||
# Peer discovery
|
# Peer discovery
|
||||||
self.peer_discovery = self.peer_discovery_enum()
|
self.peer_discovery = self.peer_discovery_enum()
|
||||||
self.peer_announce = self.boolean('PEER_ANNOUNCE', True)
|
self.peer_announce = self.boolean('PEER_ANNOUNCE', True)
|
||||||
|
|
|
@ -115,7 +115,7 @@ class Server:
|
||||||
if self.env.udp_port:
|
if self.env.udp_port:
|
||||||
await self.bp.status_server.start(
|
await self.bp.status_server.start(
|
||||||
0, bytes.fromhex(self.bp.coin.GENESIS_HASH)[::-1],
|
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)
|
await _start_cancellable(self.bp.fetch_and_process_blocks)
|
||||||
|
|
||||||
|
|
|
@ -70,7 +70,7 @@ class SPVServerStatusProtocol(asyncio.DatagramProtocol):
|
||||||
PROTOCOL_VERSION = 1
|
PROTOCOL_VERSION = 1
|
||||||
|
|
||||||
def __init__(self, height: int, tip: bytes, throttle_cache_size: int = 1024, throttle_reqs_per_sec: int = 10,
|
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__()
|
super().__init__()
|
||||||
self.transport: Optional[asyncio.transports.DatagramTransport] = None
|
self.transport: Optional[asyncio.transports.DatagramTransport] = None
|
||||||
self._height = height
|
self._height = height
|
||||||
|
@ -82,6 +82,7 @@ class SPVServerStatusProtocol(asyncio.DatagramProtocol):
|
||||||
self._should_log = LRUCache(throttle_cache_size)
|
self._should_log = LRUCache(throttle_cache_size)
|
||||||
self._min_delay = 1 / throttle_reqs_per_sec
|
self._min_delay = 1 / throttle_reqs_per_sec
|
||||||
self._allow_localhost = allow_localhost
|
self._allow_localhost = allow_localhost
|
||||||
|
self._allow_lan = allow_lan
|
||||||
|
|
||||||
def update_cached_response(self):
|
def update_cached_response(self):
|
||||||
self._cached_response = SPVPong.make(self._height, self._tip, self._flags, self.PROTOCOL_VERSION)
|
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):
|
except (ValueError, struct.error, AttributeError, TypeError):
|
||||||
# log.exception("derp")
|
# log.exception("derp")
|
||||||
return
|
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)
|
self.transport.sendto(self.make_pong(addr[0]), addr)
|
||||||
else:
|
else:
|
||||||
log.warning("odd packet from %s:%i", addr[0], addr[1])
|
log.warning("odd packet from %s:%i", addr[0], addr[1])
|
||||||
|
@ -142,12 +144,14 @@ class StatusServer:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self._protocol: Optional[SPVServerStatusProtocol] = None
|
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:
|
if self.is_running:
|
||||||
return
|
return
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
interface = interface if interface.lower() != 'localhost' else '127.0.0.1'
|
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))
|
await loop.create_datagram_endpoint(lambda: self._protocol, (interface, port))
|
||||||
log.info("started udp status server on %s:%i", interface, port)
|
log.info("started udp status server on %s:%i", interface, port)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue