This commit is contained in:
Robert Resch 2022-08-20 16:43:17 +02:00
parent e7f5d0846f
commit e7c03f8cf0
4 changed files with 20 additions and 16 deletions

View file

@ -8,4 +8,4 @@ def get_resolver_with_public_nameserver() -> AsyncResolver:
async def resolve(host: str) -> str: async def resolve(host: str) -> str:
hosts = await get_resolver_with_public_nameserver().resolve(host) hosts = await get_resolver_with_public_nameserver().resolve(host)
return hosts[0]["host"] return hosts[0]["host"] # type:ignore[no-any-return]

View file

@ -1,5 +1,6 @@
"""Web server middleware module.""" """Web server middleware module."""
import json import json
from typing import Any
from aiohttp import web from aiohttp import web
from aiohttp.typedefs import Handler from aiohttp.typedefs import Handler
@ -13,7 +14,7 @@ _LOGGER = get_logger("webserver_requests")
class CustomEncoder(json.JSONEncoder): class CustomEncoder(json.JSONEncoder):
def default(self, obj): def default(self, obj: Any) -> Any:
if isinstance(obj, set): if isinstance(obj, set):
return list(obj) return list(obj)
return json.JSONEncoder.default(self, obj) return json.JSONEncoder.default(self, obj)
@ -32,9 +33,14 @@ async def log_all_requests(request: Request, handler: Handler) -> StreamResponse
"path": request.path, "path": request.path,
"query_string": request.query_string, "query_string": request.query_string,
"headers": {h for h in request.headers.items()}, "headers": {h for h in request.headers.items()},
"route_resource": request.match_info.route.resource.canonical,
} }
} }
if request.match_info.route.resource:
to_log["request"][
"route_resource"
] = request.match_info.route.resource.canonical
try: try:
if request.content_length: if request.content_length:
if request.content_type == "application/json": if request.content_type == "application/json":
@ -44,7 +50,7 @@ async def log_all_requests(request: Request, handler: Handler) -> StreamResponse
response = await handler(request) response = await handler(request)
if response is None: if response is None:
confserverlog.warning( # type:ignore[unreachable] _LOGGER.warning( # type:ignore[unreachable]
"Response was null!" "Response was null!"
) )
_LOGGER.warning(json.dumps(to_log, cls=CustomEncoder)) _LOGGER.warning(json.dumps(to_log, cls=CustomEncoder))
@ -55,6 +61,7 @@ async def log_all_requests(request: Request, handler: Handler) -> StreamResponse
} }
if isinstance(response, Response) and response.body: if isinstance(response, Response) and response.body:
assert response.text
if response.content_type == "application/json": if response.content_type == "application/json":
to_log["response"]["body"] = json.loads(response.text) to_log["response"]["body"] = json.loads(response.text)
elif response.content_type.startswith("text"): elif response.content_type.startswith("text"):

View file

@ -319,7 +319,7 @@ class WebServer:
if request.content.total_bytes > 0: if request.content.total_bytes > 0:
read_body = await request.read() read_body = await request.read()
proxymodelog.info( proxymodelog.info(
f"HTTP Proxy Request to EcoVacs (body=true) (URL:{request.url}) - {read_body}" f"HTTP Proxy Request to EcoVacs (body=true) (URL:{request.url}) - {read_body.decode('utf-8')}"
) )
if request.content_type == "application/x-www-form-urlencoded": if request.content_type == "application/x-www-form-urlencoded":
# android apps use form # android apps use form
@ -349,10 +349,10 @@ class WebServer:
) )
async with session.request(request.method, request.url) as resp: async with session.request(request.method, request.url) as resp:
if resp.content_type == "application/octet-stream": if resp.content_type == "application/octet-stream":
response = await resp.read()
proxymodelog.info( proxymodelog.info(
f"HTTP Proxy Response from EcoVacs (URL: {request.url}) - (Status: {resp.status}) - <BYTES CONTENT>" f"HTTP Proxy Response from EcoVacs (URL: {request.url}) - (Status: {resp.status}) - <BYTES CONTENT>"
) )
return web.Response(body=await resp.read())
else: else:
response = await resp.text() response = await resp.text()
proxymodelog.info( proxymodelog.info(
@ -363,14 +363,10 @@ class WebServer:
if resp.content_type == "application/json": if resp.content_type == "application/json":
response = json.loads(response) response = json.loads(response)
return web.json_response(response) return web.json_response(response)
elif resp.content_type == "application/octet-stream": if resp.content_type == "application/octet-stream":
return web.Response(body=response) return web.Response(body=response)
else:
return web.Response(text=response)
else:
return web.Response(text=response)
return web.Response(text=response)
except asyncio.CancelledError: except asyncio.CancelledError:
proxymodelog.exception( proxymodelog.exception(
f"Request cancelled or timeout - {request.url}", exc_info=True f"Request cancelled or timeout - {request.url}", exc_info=True

View file

@ -230,7 +230,7 @@ class XMPPAsyncClient:
xmppserverlog.debug( xmppserverlog.debug(
"admin username received from bot: {}".format(ctl.get("admin")) "admin username received from bot: {}".format(ctl.get("admin"))
) )
#XMPPServer.client_id = ctl.get("admin") # XMPPServer.client_id = ctl.get("admin")
return return
# forward # forward
@ -314,8 +314,8 @@ class XMPPAsyncClient:
xmppserverlog.info( xmppserverlog.info(
"Bot reported user has no permissions, Bumper will attempt to add user to bot. This is typical if bot was last on Ecovacs Network." "Bot reported user has no permissions, Bumper will attempt to add user to bot. This is typical if bot was last on Ecovacs Network."
) )
xquery = xml.getchildren() xquery = list(xml)
ctl = xquery[0].getchildren() ctl = list(xquery[0])
if "error" in ctl[0].attrib: if "error" in ctl[0].attrib:
ctlerr = ctl[0].attrib["error"] ctlerr = ctl[0].attrib["error"]
adminuser = ctlerr.replace( adminuser = ctlerr.replace(
@ -328,7 +328,8 @@ class XMPPAsyncClient:
adminuser.startswith("fuid_") adminuser.startswith("fuid_")
or adminuser.startswith("fusername_") or adminuser.startswith("fusername_")
or bumper.use_auth or bumper.use_auth
): # if not fuid_ then its ecovacs OR ignore bumper auth ):
# if not fuid_ then its ecovacs OR ignore bumper auth
# TODO: Implement auth later, should this user have access to bot? # TODO: Implement auth later, should this user have access to bot?
# Add user jid to bot # Add user jid to bot