feature: update scan method to use port 80 when possible, and add .scan() method.

This commit is contained in:
Upstream Data
2023-11-24 23:13:56 -07:00
parent e68f188e8f
commit 78f689eb2c

View File

@@ -107,12 +107,15 @@ class MinerNetwork:
""" """
return cls(list(ipaddress.ip_network(subnet, strict=False).hosts())) return cls(list(ipaddress.ip_network(subnet, strict=False).hosts()))
async def scan_network_for_miners(self) -> List[AnyMiner]: async def scan(self) -> List[AnyMiner]:
"""Scan the network for miners, and return found miners as a list. """Scan the network for miners.
Returns: Returns:
A list of found miners. A list of found miners.
""" """
return await self.scan_network_for_miners()
async def scan_network_for_miners(self) -> List[AnyMiner]:
logging.debug(f"{self} - (Scan Network For Miners) - Scanning") logging.debug(f"{self} - (Scan Network For Miners) - Scanning")
# clear cached miners # clear cached miners
@@ -156,76 +159,18 @@ class MinerNetwork:
except TimeoutError: except TimeoutError:
yield None yield None
@staticmethod
async def ping_miner(
ip: ipaddress.ip_address, semaphore: asyncio.Semaphore
) -> Union[None, ipaddress.ip_address]:
async with semaphore:
try:
miner = await ping_miner(ip)
if miner:
return miner
except ConnectionRefusedError:
tasks = [ping_miner(ip, port=port) for port in [4029, 8889]]
for miner in asyncio.as_completed(tasks):
try:
miner = await miner
if miner:
return miner
except ConnectionRefusedError:
pass
@staticmethod @staticmethod
async def ping_and_get_miner( async def ping_and_get_miner(
ip: ipaddress.ip_address, semaphore: asyncio.Semaphore ip: ipaddress.ip_address, semaphore: asyncio.Semaphore
) -> Union[None, AnyMiner]: ) -> Union[None, AnyMiner]:
async with semaphore: async with semaphore:
try: miner = await ping_and_get_miner(ip)
miner = await ping_and_get_miner(ip) if miner:
if miner: return miner
return miner
except ConnectionRefusedError:
tasks = [ping_and_get_miner(ip, port=port) for port in [4029, 8889]]
for miner in asyncio.as_completed(tasks):
try:
miner = await miner
if miner:
return miner
except ConnectionRefusedError:
pass
async def ping_miner(
ip: ipaddress.ip_address, port=4028
) -> Union[None, ipaddress.ip_address]:
for i in range(settings.get("network_ping_retries", 1)):
try:
connection_fut = asyncio.open_connection(str(ip), port)
# get the read and write streams from the connection
reader, writer = await asyncio.wait_for(
connection_fut, timeout=settings.get("network_ping_timeout", 3)
)
# immediately close connection, we know connection happened
writer.close()
# make sure the writer is closed
await writer.wait_closed()
# ping was successful
return ip
except asyncio.exceptions.TimeoutError:
# ping failed if we time out
continue
except (ConnectionRefusedError, OSError):
# handle for other connection errors
logging.debug(f"{str(ip)}: Connection Refused.")
raise ConnectionRefusedError
except Exception as e:
logging.warning(f"{str(ip)}: Ping And Get Miner Exception: {e}")
raise ConnectionRefusedError
return
async def ping_and_get_miner( async def ping_and_get_miner(
ip: ipaddress.ip_address, port=4028 ip: ipaddress.ip_address, port=80
) -> Union[None, AnyMiner]: ) -> Union[None, AnyMiner]:
for i in range(settings.get("network_ping_retries", 1)): for i in range(settings.get("network_ping_retries", 1)):
try: try:
@@ -243,13 +188,9 @@ async def ping_and_get_miner(
except asyncio.exceptions.TimeoutError: except asyncio.exceptions.TimeoutError:
# ping failed if we time out # ping failed if we time out
continue continue
except (ConnectionRefusedError, OSError):
# handle for other connection errors
logging.debug(f"{str(ip)}: Connection Refused.")
raise ConnectionRefusedError
except Exception as e: except Exception as e:
logging.warning(f"{str(ip)}: Ping And Get Miner Exception: {e}") logging.warning(f"{str(ip)}: Unhandled ping exception: {e}")
raise ConnectionRefusedError return
return return