feature: revert miner_factory to use httpx, as it now seems to be the same speed, and aiohttp doesnt support digest auth.

This commit is contained in:
UpstreamData
2023-07-24 13:09:30 -06:00
parent 03f2a1f9ba
commit 4de950d8f4
2 changed files with 23 additions and 24 deletions

View File

@@ -22,7 +22,7 @@ import json
import re import re
from typing import Callable, List, Optional, Tuple, Union from typing import Callable, List, Optional, Tuple, Union
import aiohttp import httpx
from pyasic.logger import logger from pyasic.logger import logger
from pyasic.miners.antminer import * from pyasic.miners.antminer import *
@@ -455,7 +455,7 @@ class MinerFactory:
async def _get_miner_web(self, ip: str): async def _get_miner_web(self, ip: str):
urls = [f"http://{ip}/", f"https://{ip}/"] urls = [f"http://{ip}/", f"https://{ip}/"]
async with aiohttp.ClientSession() as session: async with httpx.AsyncClient() as session:
tasks = [asyncio.create_task(self._web_ping(session, url)) for url in urls] tasks = [asyncio.create_task(self._web_ping(session, url)) for url in urls]
text, resp = await concurrent_get_first_result( text, resp = await concurrent_get_first_result(
@@ -466,22 +466,22 @@ class MinerFactory:
@staticmethod @staticmethod
async def _web_ping( async def _web_ping(
session: aiohttp.ClientSession, url: str session: httpx.AsyncClient, url: str
) -> Tuple[Optional[str], Optional[aiohttp.ClientResponse]]: ) -> Tuple[Optional[str], Optional[httpx.Response]]:
try: try:
resp = await session.get(url, allow_redirects=False) resp = await session.get(url, follow_redirects=False)
return await resp.text(), resp return resp.text, resp
except (aiohttp.ClientError, asyncio.TimeoutError): except (httpx.HTTPError, asyncio.TimeoutError):
pass pass
return None, None return None, None
@staticmethod @staticmethod
def _parse_web_type(web_text: str, web_resp: aiohttp.ClientResponse) -> MinerTypes: def _parse_web_type(web_text: str, web_resp: httpx.Response) -> MinerTypes:
if web_resp.status == 401 and 'realm="antMiner' in web_resp.headers.get( if web_resp.status_code == 401 and 'realm="antMiner' in web_resp.headers.get(
"www-authenticate", "" "www-authenticate", ""
): ):
return MinerTypes.ANTMINER return MinerTypes.ANTMINER
if web_resp.status == 307 and "https://" in web_resp.headers.get( if web_resp.status_code == 307 and "https://" in web_resp.headers.get(
"location", "" "location", ""
): ):
return MinerTypes.WHATSMINER return MinerTypes.WHATSMINER
@@ -576,26 +576,26 @@ class MinerFactory:
self, self,
ip: Union[ipaddress.ip_address, str], ip: Union[ipaddress.ip_address, str],
location: str, location: str,
auth: Optional[aiohttp.BasicAuth] = None, auth: Optional[httpx.DigestAuth] = None,
) -> Optional[dict]: ) -> Optional[dict]:
async with aiohttp.ClientSession() as session: async with httpx.AsyncClient() as session:
try: try:
data = await session.get( data = await session.get(
f"http://{str(ip)}{location}", f"http://{str(ip)}{location}",
auth=auth, auth=auth,
timeout=30, timeout=30,
) )
except (aiohttp.ClientError, asyncio.TimeoutError): except (httpx.HTTPError, asyncio.TimeoutError):
logger.info(f"{ip}: Web command timeout.") logger.info(f"{ip}: Web command timeout.")
return return
if data is None: if data is None:
return return
try: try:
json_data = await data.json() json_data = data.json()
except (aiohttp.ContentTypeError, asyncio.TimeoutError): except (json.JSONDecodeError, asyncio.TimeoutError):
try: try:
return json.loads(await data.text()) return json.loads(data.text)
except (json.JSONDecodeError, aiohttp.ClientError): except (json.JSONDecodeError, httpx.HTTPError):
return return
else: else:
return json_data return json_data
@@ -716,7 +716,7 @@ class MinerFactory:
pass pass
# last resort, this is slow # last resort, this is slow
auth = aiohttp.BasicAuth("root", "root") auth = httpx.DigestAuth("root", "root")
web_json_data = await self.send_web_command( web_json_data = await self.send_web_command(
ip, "/cgi-bin/get_system_info.cgi", auth=auth ip, "/cgi-bin/get_system_info.cgi", auth=auth
) )
@@ -760,7 +760,7 @@ class MinerFactory:
async def get_miner_model_innosilicon(self, ip: str) -> Optional[str]: async def get_miner_model_innosilicon(self, ip: str) -> Optional[str]:
try: try:
async with aiohttp.ClientSession() as session: async with httpx.AsyncClient() as session:
auth_req = await session.post( auth_req = await session.post(
f"http://{ip}/api/auth", f"http://{ip}/api/auth",
data={"username": "admin", "password": "admin"}, data={"username": "admin", "password": "admin"},
@@ -775,7 +775,7 @@ class MinerFactory:
) )
).json() ).json()
return web_data["type"] return web_data["type"]
except (aiohttp.ClientError, LookupError): except (httpx.HTTPError, LookupError):
pass pass
async def get_miner_model_braiins_os(self, ip: str) -> Optional[str]: async def get_miner_model_braiins_os(self, ip: str) -> Optional[str]:
@@ -790,16 +790,16 @@ class MinerFactory:
pass pass
try: try:
async with aiohttp.ClientSession() as session: async with httpx.AsyncClient() as session:
d = await session.post( d = await session.post(
f"http://{ip}/graphql", f"http://{ip}/graphql",
json={"query": "{bosminer {info{modelName}}}"}, json={"query": "{bosminer {info{modelName}}}"},
) )
if d.status == 200: if d.status_code == 200:
json_data = await d.json() json_data = await d.json()
miner_model = json_data["data"]["bosminer"]["info"]["modelName"] miner_model = json_data["data"]["bosminer"]["info"]["modelName"]
return miner_model return miner_model
except (aiohttp.ClientError, LookupError): except (httpx.HTTPError, LookupError):
pass pass
async def get_miner_model_vnish(self, ip: str) -> Optional[str]: async def get_miner_model_vnish(self, ip: str) -> Optional[str]:

View File

@@ -14,7 +14,6 @@ httpx = "^0.24.0"
passlib = "^1.7.4" passlib = "^1.7.4"
pyaml = "^23.5.9" pyaml = "^23.5.9"
toml = "^0.10.2" toml = "^0.10.2"
aiohttp = "^3.8.4"
[tool.poetry.group.dev] [tool.poetry.group.dev]
optional = true optional = true