From c3972f952432f76aae37e0a095e9191f326ce329 Mon Sep 17 00:00:00 2001 From: Upstream Data Date: Sat, 25 Nov 2023 01:08:04 -0700 Subject: [PATCH] feature: add default ssl ctx to all httpx clients to speed up initialization. --- pyasic/miners/miner_factory.py | 8 ++++---- pyasic/settings/__init__.py | 4 ++++ pyasic/web/antminer.py | 8 ++++---- pyasic/web/bosminer/__init__.py | 4 ++-- pyasic/web/goldshell.py | 6 +++--- pyasic/web/inno.py | 6 +++--- pyasic/web/vnish.py | 4 ++-- 7 files changed, 22 insertions(+), 18 deletions(-) diff --git a/pyasic/miners/miner_factory.py b/pyasic/miners/miner_factory.py index ca5ea033..934f0316 100644 --- a/pyasic/miners/miner_factory.py +++ b/pyasic/miners/miner_factory.py @@ -474,7 +474,7 @@ class MinerFactory: async def _get_miner_web(self, ip: str): urls = [f"http://{ip}/", f"https://{ip}/"] - async with httpx.AsyncClient(verify=False) as session: + async with httpx.AsyncClient(verify=settings.ssl_cxt) as session: tasks = [asyncio.create_task(self._web_ping(session, url)) for url in urls] text, resp = await concurrent_get_first_result( @@ -602,7 +602,7 @@ class MinerFactory: location: str, auth: Optional[httpx.DigestAuth] = None, ) -> Optional[dict]: - async with httpx.AsyncClient(verify=False) as session: + async with httpx.AsyncClient(verify=settings.ssl_cxt) as session: try: data = await session.get( f"http://{str(ip)}{location}", @@ -798,7 +798,7 @@ class MinerFactory: async def get_miner_model_innosilicon(self, ip: str) -> Optional[str]: try: - async with httpx.AsyncClient(verify=False) as session: + async with httpx.AsyncClient(verify=settings.ssl_cxt) as session: auth_req = await session.post( f"http://{ip}/api/auth", data={"username": "admin", "password": "admin"}, @@ -828,7 +828,7 @@ class MinerFactory: pass try: - async with httpx.AsyncClient(verify=False) as session: + async with httpx.AsyncClient(verify=settings.ssl_cxt) as session: d = await session.post( f"http://{ip}/graphql", json={"query": "{bosminer {info{modelName}}}"}, diff --git a/pyasic/settings/__init__.py b/pyasic/settings/__init__.py index ee566915..275b4920 100644 --- a/pyasic/settings/__init__.py +++ b/pyasic/settings/__init__.py @@ -16,6 +16,8 @@ from typing import Any +import httpx + _settings = { # defaults "network_ping_retries": 1, "network_ping_timeout": 3, @@ -39,3 +41,5 @@ def get(key: str, other: Any = None) -> Any: def update(key: str, val: Any) -> Any: _settings[key] = val + +ssl_cxt = httpx.create_ssl_context() \ No newline at end of file diff --git a/pyasic/web/antminer.py b/pyasic/web/antminer.py index 32249e5e..0eaf4113 100644 --- a/pyasic/web/antminer.py +++ b/pyasic/web/antminer.py @@ -38,7 +38,7 @@ class AntminerModernWebAPI(BaseWebAPI): url = f"http://{self.ip}/cgi-bin/{command}.cgi" auth = httpx.DigestAuth(self.username, self.pwd) try: - async with httpx.AsyncClient() as client: + async with httpx.AsyncClient(verify=settings.ssl_cxt) as client: if parameters: data = await client.post( url, data=json.dumps(parameters), auth=auth, timeout=settings.get("api_function_timeout", 3) # noqa @@ -57,7 +57,7 @@ class AntminerModernWebAPI(BaseWebAPI): async def multicommand( self, *commands: str, ignore_errors: bool = False, allow_warning: bool = True ) -> dict: - async with httpx.AsyncClient() as client: + async with httpx.AsyncClient(verify=settings.ssl_cxt) as client: tasks = [ asyncio.create_task(self._handle_multicommand(client, command)) for command in commands @@ -149,7 +149,7 @@ class AntminerOldWebAPI(BaseWebAPI): url = f"http://{self.ip}/cgi-bin/{command}.cgi" auth = httpx.DigestAuth(self.username, self.pwd) try: - async with httpx.AsyncClient() as client: + async with httpx.AsyncClient(verify=settings.ssl_cxt) as client: if parameters: data = await client.post( url, data=parameters, auth=auth, timeout=settings.get("api_function_timeout", 3) @@ -170,7 +170,7 @@ class AntminerOldWebAPI(BaseWebAPI): ) -> dict: data = {k: None for k in commands} auth = httpx.DigestAuth(self.username, self.pwd) - async with httpx.AsyncClient() as client: + async with httpx.AsyncClient(verify=settings.ssl_cxt) as client: for command in commands: try: url = f"http://{self.ip}/cgi-bin/{command}.cgi" diff --git a/pyasic/web/bosminer/__init__.py b/pyasic/web/bosminer/__init__.py index 9aa13d92..78206514 100644 --- a/pyasic/web/bosminer/__init__.py +++ b/pyasic/web/bosminer/__init__.py @@ -186,7 +186,7 @@ class BOSMinerGQLAPI: if command.get("query") is None: query = {"query": self.parse_command(command)} try: - async with httpx.AsyncClient() as client: + async with httpx.AsyncClient(verify=settings.ssl_cxt) as client: await self.auth(client) data = await client.post(url, json=query) except httpx.HTTPError: @@ -239,7 +239,7 @@ class BOSMinerLuCIAPI: async def send_command(self, path: str, ignore_errors: bool = False) -> dict: try: - async with httpx.AsyncClient() as client: + async with httpx.AsyncClient(verify=settings.ssl_cxt) as client: await self.auth(client) data = await client.get( f"http://{self.ip}{path}", headers={"User-Agent": "BTC Tools v0.1"} diff --git a/pyasic/web/goldshell.py b/pyasic/web/goldshell.py index d8517bcf..47cf2fdf 100644 --- a/pyasic/web/goldshell.py +++ b/pyasic/web/goldshell.py @@ -31,7 +31,7 @@ class GoldshellWebAPI(BaseWebAPI): self.jwt = None async def auth(self): - async with httpx.AsyncClient() as client: + async with httpx.AsyncClient(verify=settings.ssl_cxt) as client: try: await client.get(f"http://{self.ip}/user/logout") auth = ( @@ -71,7 +71,7 @@ class GoldshellWebAPI(BaseWebAPI): parameters.pop("pool_pwd") if not self.jwt: await self.auth() - async with httpx.AsyncClient() as client: + async with httpx.AsyncClient(verify=settings.ssl_cxt) as client: for i in range(settings.get("get_data_retries", 1)): try: if parameters: @@ -102,7 +102,7 @@ class GoldshellWebAPI(BaseWebAPI): data = {k: None for k in commands} data["multicommand"] = True await self.auth() - async with httpx.AsyncClient() as client: + async with httpx.AsyncClient(verify=settings.ssl_cxt) as client: for command in commands: try: response = await client.get( diff --git a/pyasic/web/inno.py b/pyasic/web/inno.py index 679af1eb..3b691614 100644 --- a/pyasic/web/inno.py +++ b/pyasic/web/inno.py @@ -32,7 +32,7 @@ class InnosiliconWebAPI(BaseWebAPI): self.jwt = None async def auth(self): - async with httpx.AsyncClient() as client: + async with httpx.AsyncClient(verify=settings.ssl_cxt) as client: try: auth = await client.post( f"http://{self.ip}/api/auth", @@ -54,7 +54,7 @@ class InnosiliconWebAPI(BaseWebAPI): ) -> dict: if not self.jwt: await self.auth() - async with httpx.AsyncClient() as client: + async with httpx.AsyncClient(verify=settings.ssl_cxt) as client: for i in range(settings.get("get_data_retries", 1)): try: response = await client.post( @@ -90,7 +90,7 @@ class InnosiliconWebAPI(BaseWebAPI): data = {k: None for k in commands} data["multicommand"] = True await self.auth() - async with httpx.AsyncClient() as client: + async with httpx.AsyncClient(verify=settings.ssl_cxt) as client: for command in commands: try: response = await client.post( diff --git a/pyasic/web/vnish.py b/pyasic/web/vnish.py index c2381b7e..b756d68a 100644 --- a/pyasic/web/vnish.py +++ b/pyasic/web/vnish.py @@ -31,7 +31,7 @@ class VNishWebAPI(BaseWebAPI): self.token = None async def auth(self): - async with httpx.AsyncClient() as client: + async with httpx.AsyncClient(verify=settings.ssl_cxt) as client: try: auth = await client.post( f"http://{self.ip}/api/v1/unlock", @@ -58,7 +58,7 @@ class VNishWebAPI(BaseWebAPI): ) -> dict: if not self.token: await self.auth() - async with httpx.AsyncClient() as client: + async with httpx.AsyncClient(verify=settings.ssl_cxt) as client: for i in range(settings.get("get_data_retries", 1)): try: auth = self.token