rename logers

This commit is contained in:
Robert Resch 2022-08-24 23:25:58 +02:00
parent 2da065924e
commit bfba75f0ad
10 changed files with 98 additions and 122 deletions

View file

@ -54,7 +54,8 @@ bumper_debug = strtobool(os.environ.get("BUMPER_DEBUG")) or False
use_auth = False use_auth = False
token_validity_seconds = 3600 # 1 hour token_validity_seconds = 3600 # 1 hour
oauth_validity_days = 15 oauth_validity_days = 15
bumper_proxy_mode = strtobool(os.environ.get("BUMPER_PROXY_MODE")) or False bumper_proxy_mqtt = strtobool(os.environ.get("BUMPER_PROXY_MQTT")) or False
bumper_proxy_web = strtobool(os.environ.get("BUMPER_PROXY_WEB")) or False
mqtt_server: MQTTServer mqtt_server: MQTTServer
mqtt_helperbot: HelperBot mqtt_helperbot: HelperBot
@ -65,7 +66,6 @@ shutting_down = False
bumperlog = get_logger("bumper") bumperlog = get_logger("bumper")
logging.getLogger("asyncio").setLevel(logging.CRITICAL + 1) # Ignore this logger logging.getLogger("asyncio").setLevel(logging.CRITICAL + 1) # Ignore this logger
proxymodelog = get_logger("proxymode")
web_server_https_port = os.environ.get("WEB_SERVER_HTTPS_PORT") or 443 web_server_https_port = os.environ.get("WEB_SERVER_HTTPS_PORT") or 443
mqtt_listen_port = 8883 mqtt_listen_port = 8883
@ -113,15 +113,17 @@ async def start() -> None:
bumperlog.info("Starting Bumper") bumperlog.info("Starting Bumper")
if bumper_proxy_mode: if bumper_proxy_mqtt:
bumperlog.info("Proxy Mode Enabled") bumperlog.info("Proxy MQTT Enabled")
if bumper_proxy_web:
bumperlog.info("Proxy Web Enabled")
global mqtt_server global mqtt_server
mqtt_server = MQTTServer(bumper_listen, mqtt_listen_port) mqtt_server = MQTTServer(bumper_listen, mqtt_listen_port)
global mqtt_helperbot global mqtt_helperbot
mqtt_helperbot = HelperBot(bumper_listen, mqtt_listen_port) mqtt_helperbot = HelperBot(bumper_listen, mqtt_listen_port)
global web_server global web_server
web_server = WebServer(web_server_bindings, bumper_proxy_mode) web_server = WebServer(web_server_bindings, bumper_proxy_web)
global xmpp_server global xmpp_server
xmpp_server = XMPPServer(bumper_listen, xmpp_listen_port) xmpp_server = XMPPServer(bumper_listen, xmpp_listen_port)
@ -208,19 +210,12 @@ def main(argv: None | list[str] = None) -> None:
help="announce address to bots on checkin", help="announce address to bots on checkin",
) )
parser.add_argument("--debug", action="store_true", help="enable debug logs") parser.add_argument("--debug", action="store_true", help="enable debug logs")
parser.add_argument(
"--proxy-mode", action="store_true", help="enable proxy mode"
)
args = parser.parse_args(args=argv) args = parser.parse_args(args=argv)
if args.debug: if args.debug:
bumper_debug = True bumper_debug = True
if args.proxy_mode:
global bumper_proxy_mode
bumper_proxy_mode = True
if args.listen: if args.listen:
bumper_listen = args.listen bumper_listen = args.listen

View file

@ -10,7 +10,7 @@ from bumper.models import BumperUser, OAuth, VacBotClient, VacBotDevice
from .util import get_logger from .util import get_logger
bumperlog = get_logger("bumper") _LOGGER = get_logger("db")
def db_file() -> str: def db_file() -> str:
@ -41,7 +41,7 @@ def user_add(userid: str) -> None:
user = user_get(userid) user = user_get(userid)
if not user: if not user:
bumperlog.info(f"Adding new user with userid: {newuser.userid}") _LOGGER.info(f"Adding new user with userid: {newuser.userid}")
user_full_upsert(newuser.asdict()) user_full_upsert(newuser.asdict())
@ -137,7 +137,7 @@ def user_add_token(userid: str, token: str) -> None:
tokens = opendb.table("tokens") tokens = opendb.table("tokens")
tmptoken = tokens.get((Query().userid == userid) & (Query().token == token)) tmptoken = tokens.get((Query().userid == userid) & (Query().token == token))
if not tmptoken: if not tmptoken:
bumperlog.debug(f"Adding token {token} for userid {userid}") _LOGGER.debug(f"Adding token {token} for userid {userid}")
tokens.insert( tokens.insert(
{ {
"userid": userid, "userid": userid,
@ -166,9 +166,7 @@ def user_revoke_expired_tokens(userid: str) -> None:
tsearch = tokens.search(Query().userid == userid) tsearch = tokens.search(Query().userid == userid)
for i in tsearch: for i in tsearch:
if datetime.now() >= datetime.fromisoformat(i["expiration"]): if datetime.now() >= datetime.fromisoformat(i["expiration"]):
bumperlog.debug( _LOGGER.debug("Removing token {} due to expiration".format(i["token"]))
"Removing token {} due to expiration".format(i["token"])
)
tokens.remove(doc_ids=[i.doc_id]) tokens.remove(doc_ids=[i.doc_id])
@ -214,9 +212,7 @@ def revoke_expired_oauths() -> None:
for i in entries: for i in entries:
oauth = OAuth(**i) oauth = OAuth(**i)
if datetime.now() >= datetime.fromisoformat(oauth.expire_at): if datetime.now() >= datetime.fromisoformat(oauth.expire_at):
bumperlog.debug( _LOGGER.debug(f"Removing oauth {oauth.access_token} due to expiration")
f"Removing oauth {oauth.access_token} due to expiration"
)
table.remove(doc_ids=[i.doc_id]) table.remove(doc_ids=[i.doc_id])
@ -228,9 +224,7 @@ def user_revoke_expired_oauths(userid: str) -> None:
for i in search: for i in search:
oauth = OAuth(**i) oauth = OAuth(**i)
if datetime.now() >= datetime.fromisoformat(oauth.expire_at): if datetime.now() >= datetime.fromisoformat(oauth.expire_at):
bumperlog.debug( _LOGGER.debug(f"Removing oauth {oauth.access_token} due to expiration")
f"Removing oauth {oauth.access_token} due to expiration"
)
table.remove(doc_ids=[i.doc_id]) table.remove(doc_ids=[i.doc_id])
@ -244,7 +238,7 @@ def user_add_oauth(userid: str) -> OAuth:
return OAuth(**entry) return OAuth(**entry)
else: else:
oauth = OAuth.create_new(userid) oauth = OAuth.create_new(userid)
bumperlog.debug(f"Adding oauth {oauth.access_token} for userid {userid}") _LOGGER.debug(f"Adding oauth {oauth.access_token} for userid {userid}")
table.insert(oauth.toDB()) table.insert(oauth.toDB())
return oauth return oauth
@ -261,7 +255,7 @@ def get_disconnected_xmpp_clients() -> list[Document]:
def check_authcode(uid: str, authcode: str) -> bool: def check_authcode(uid: str, authcode: str) -> bool:
bumperlog.debug(f"Checking for authcode: {authcode}") _LOGGER.debug(f"Checking for authcode: {authcode}")
tokens = db_get().table("tokens") tokens = db_get().table("tokens")
tmpauth = tokens.get( tmpauth = tokens.get(
(Query().authcode == authcode) (Query().authcode == authcode)
@ -277,7 +271,7 @@ def check_authcode(uid: str, authcode: str) -> bool:
def loginByItToken(authcode: str) -> dict[str, str]: def loginByItToken(authcode: str) -> dict[str, str]:
bumperlog.debug(f"Checking for authcode: {authcode}") _LOGGER.debug(f"Checking for authcode: {authcode}")
tokens = db_get().table("tokens") tokens = db_get().table("tokens")
tmpauth = tokens.get( tmpauth = tokens.get(
Query().authcode Query().authcode
@ -294,7 +288,7 @@ def loginByItToken(authcode: str) -> dict[str, str]:
def check_token(uid: str, token: str) -> bool: def check_token(uid: str, token: str) -> bool:
bumperlog.debug(f"Checking for token: {token}") _LOGGER.debug(f"Checking for token: {token}")
tokens = db_get().table("tokens") tokens = db_get().table("tokens")
tmpauth = tokens.get( tmpauth = tokens.get(
(Query().token == token) (Query().token == token)
@ -313,7 +307,7 @@ def revoke_expired_tokens() -> None:
tokens = db_get().table("tokens").all() tokens = db_get().table("tokens").all()
for i in tokens: for i in tokens:
if datetime.now() >= datetime.fromisoformat(i["expiration"]): if datetime.now() >= datetime.fromisoformat(i["expiration"]):
bumperlog.debug("Removing token {} due to expiration".format(i["token"])) _LOGGER.debug("Removing token {} due to expiration".format(i["token"]))
db_get().table("tokens").remove(doc_ids=[i.doc_id]) db_get().table("tokens").remove(doc_ids=[i.doc_id])
@ -330,7 +324,7 @@ def bot_add(sn: str, did: str, devclass: str, resource: str, company: str) -> No
if ( if (
not devclass == "" or "@" not in sn or "tmp" not in sn not devclass == "" or "@" not in sn or "tmp" not in sn
): # try to prevent bad additions to the bot list ): # try to prevent bad additions to the bot list
bumperlog.info(f"Adding new bot with SN: {newbot.name} DID: {newbot.did}") _LOGGER.info(f"Adding new bot with SN: {newbot.name} DID: {newbot.did}")
bot_full_upsert(newbot.asdict()) bot_full_upsert(newbot.asdict())
@ -353,7 +347,7 @@ def bot_full_upsert(vacbot: dict[str, Any]) -> None:
if "did" in vacbot: if "did" in vacbot:
bots.upsert(vacbot, Bot.did == vacbot["did"]) bots.upsert(vacbot, Bot.did == vacbot["did"])
else: else:
bumperlog.error(f"No DID in vacbot: {vacbot}") _LOGGER.error(f"No DID in vacbot: {vacbot}")
def bot_set_nick(did: str, nick: str) -> None: def bot_set_nick(did: str, nick: str) -> None:
@ -382,7 +376,7 @@ def client_add(userid: str, realm: str, resource: str) -> None:
client = client_get(resource) client = client_get(resource)
if not client: if not client:
bumperlog.info(f"Adding new client with resource {newclient.resource}") _LOGGER.info(f"Adding new client with resource {newclient.resource}")
client_full_upsert(newclient.asdict()) client_full_upsert(newclient.asdict())

View file

@ -10,7 +10,7 @@ from gmqtt.mqtt.constants import MQTTv311
from bumper.util import get_logger from bumper.util import get_logger
_LOGGER = get_logger("helperbot") _LOGGER = get_logger("helper_bot")
class CommandDto: class CommandDto:

View file

@ -25,7 +25,7 @@ import bumper
from ..util import get_logger from ..util import get_logger
_LOGGER = get_logger("proxymode") _LOGGER = get_logger("mqtt_proxy")
# iot/p2p/[command]]/[sender did]/[sender class]]/[sender resource] # iot/p2p/[command]]/[sender did]/[sender class]]/[sender resource]
# /[receiver did]/[receiver class]]/[receiver resource]/[q|p/[request id/j # /[receiver did]/[receiver class]]/[receiver resource]/[q|p/[request id/j

View file

@ -21,13 +21,12 @@ from bumper.db import (
client_set_mqtt, client_set_mqtt,
) )
from bumper.mqtt.helper_bot import HELPER_BOT_CLIENT_ID from bumper.mqtt.helper_bot import HELPER_BOT_CLIENT_ID
from bumper.mqtt.proxy import _LOGGER as _LOGGER_PROXY
from bumper.mqtt.proxy import ProxyClient from bumper.mqtt.proxy import ProxyClient
from bumper.util import get_logger from bumper.util import get_logger
mqttserverlog = get_logger("mqttserver") _LOGGER = get_logger("mqtt_server")
helperbotlog = get_logger("helperbot") _LOGGER_MESSAGES = get_logger("mqtt_messages")
boterrorlog = get_logger("boterror")
proxymodelog = get_logger("proxymode")
class MQTTServer: class MQTTServer:
@ -81,9 +80,7 @@ class MQTTServer:
self._broker = amqtt.broker.Broker(config=config) self._broker = amqtt.broker.Broker(config=config)
except Exception: except Exception:
mqttserverlog.exception( _LOGGER.exception("An exception occurred during initialize", exc_info=True)
"An exception occurred during initialize", exc_info=True
)
raise raise
@property @property
@ -98,13 +95,11 @@ class MQTTServer:
async def start(self) -> None: async def start(self) -> None:
"""Start MQTT server.""" """Start MQTT server."""
mqttserverlog.info("Starting MQTT Server at %s:%d", self._host, self._port) _LOGGER.info("Starting MQTT Server at %s:%d", self._host, self._port)
try: try:
await self._broker.start() await self._broker.start()
except Exception: except Exception:
mqttserverlog.exception( _LOGGER.exception("An exception occurred during startup", exc_info=True)
"An exception occurred during startup", exc_info=True
)
raise raise
async def shutdown(self) -> None: async def shutdown(self) -> None:
@ -119,7 +114,9 @@ class MQTTServer:
def _log__helperbot_message(custom_log_message: str, topic: str, data: str) -> None: def _log__helperbot_message(custom_log_message: str, topic: str, data: str) -> None:
helperbotlog.debug("%s - Topic: %s - Message: %s", custom_log_message, topic, data) _LOGGER_MESSAGES.debug(
"%s - Topic: %s - Message: %s", custom_log_message, topic, data
)
class BumperMQTTServerPlugin: class BumperMQTTServerPlugin:
@ -137,7 +134,7 @@ class BumperMQTTServerPlugin:
"'bumper' section not found in context configuration" "'bumper' section not found in context configuration"
) )
except Exception: except Exception:
mqttserverlog.exception( _LOGGER.exception(
"An exception occurred during plugin initialization", exc_info=True "An exception occurred during plugin initialization", exc_info=True
) )
raise raise
@ -150,7 +147,7 @@ class BumperMQTTServerPlugin:
try: try:
if client_id == HELPER_BOT_CLIENT_ID: if client_id == HELPER_BOT_CLIENT_ID:
mqttserverlog.info("Bumper Authentication Success - Helperbot") _LOGGER.info("Bumper Authentication Success - Helperbot")
return True return True
if "@" in client_id: if "@" in client_id:
@ -165,16 +162,16 @@ class BumperMQTTServerPlugin:
tmpbotdetail[1], tmpbotdetail[1],
"eco-ng", "eco-ng",
) )
mqttserverlog.info( _LOGGER.info(
"Bumper Authentication Success - Bot - SN: %s - DID: %s - Class: %s", "Bumper Authentication Success - Bot - SN: %s - DID: %s - Class: %s",
username, username,
didsplit[0], didsplit[0],
tmpbotdetail[0], tmpbotdetail[0],
) )
if bumper.bumper_proxy_mode: if bumper.bumper_proxy_mqtt:
mqtt_server = await dns.resolve("mq-ww.ecouser.net") mqtt_server = await dns.resolve("mq-ww.ecouser.net")
proxymodelog.info( _LOGGER_PROXY.info(
f"MQTT Proxy Mode - Using server {mqtt_server} for client {client_id}" f"MQTT Proxy Mode - Using server {mqtt_server} for client {client_id}"
) )
proxy = ProxyClient( proxy = ProxyClient(
@ -192,7 +189,7 @@ class BumperMQTTServerPlugin:
if check_authcode(didsplit[0], password) or not bumper.use_auth: if check_authcode(didsplit[0], password) or not bumper.use_auth:
client_add(userid, realm, resource) client_add(userid, realm, resource)
mqttserverlog.info( _LOGGER.info(
"Bumper Authentication Success - Client - Username: %s - ClientID: %s", "Bumper Authentication Success - Client - Username: %s - ClientID: %s",
username, username,
client_id, client_id,
@ -206,27 +203,23 @@ class BumperMQTTServerPlugin:
message_suffix = f"- Username: {username} - ClientID: {client_id}" message_suffix = f"- Username: {username} - ClientID: {client_id}"
if password_hash: # If there is a matching entry in passwd, check hash if password_hash: # If there is a matching entry in passwd, check hash
if pwd_context.verify(password, password_hash): if pwd_context.verify(password, password_hash):
mqttserverlog.info( _LOGGER.info("File Authentication Success %s", message_suffix)
"File Authentication Success %s", message_suffix
)
return True return True
mqttserverlog.info("File Authentication Failed %s", message_suffix) _LOGGER.info("File Authentication Failed %s", message_suffix)
else: else:
mqttserverlog.info( _LOGGER.info(
"File Authentication Failed - No Entry %s", message_suffix "File Authentication Failed - No Entry %s", message_suffix
) )
except Exception: # pylint: disable=broad-except except Exception: # pylint: disable=broad-except
mqttserverlog.exception( _LOGGER.exception("Session: %s", kwargs.get("session", ""), exc_info=True)
"Session: %s", kwargs.get("session", ""), exc_info=True
)
# Check for allow anonymous # Check for allow anonymous
if self.auth_config.get("allow-anonymous", True): if self.auth_config.get("allow-anonymous", True):
message = f"Anonymous Authentication Success: config allows anonymous - Username: {username}" message = f"Anonymous Authentication Success: config allows anonymous - Username: {username}"
self.context.logger.debug(message) self.context.logger.debug(message)
mqttserverlog.info(message) _LOGGER.info(message)
return True return True
return False return False
@ -260,15 +253,15 @@ class BumperMQTTServerPlugin:
async def on_broker_client_subscribed( async def on_broker_client_subscribed(
self, client_id: str, topic: str, qos: QOS_0 | QOS_1 | QOS_2 self, client_id: str, topic: str, qos: QOS_0 | QOS_1 | QOS_2
) -> None: ) -> None:
if bumper.bumper_proxy_mode: if bumper.bumper_proxy_mqtt:
# if proxy mode, also subscribe on ecovacs server # if proxy mode, also subscribe on ecovacs server
if client_id in self._proxy_clients: if client_id in self._proxy_clients:
await self._proxy_clients[client_id].subscribe(topic, qos) await self._proxy_clients[client_id].subscribe(topic, qos)
proxymodelog.info( _LOGGER_PROXY.info(
f"MQTT Proxy Mode - New MQTT Topic Subscription - Client: {client_id} - Topic: {topic}" f"MQTT Proxy Mode - New MQTT Topic Subscription - Client: {client_id} - Topic: {topic}"
) )
elif client_id != HELPER_BOT_CLIENT_ID: elif client_id != HELPER_BOT_CLIENT_ID:
proxymodelog.warning( _LOGGER_PROXY.warning(
f"MQTT Proxy Mode - No proxy client found! - Client: {client_id} - Topic: {topic}" f"MQTT Proxy Mode - No proxy client found! - Client: {client_id} - Topic: {topic}"
) )
@ -307,16 +300,11 @@ class BumperMQTTServerPlugin:
_log__helperbot_message("Send Command", topic, data_decoded) _log__helperbot_message("Send Command", topic, data_decoded)
elif topic_split[1] == "atr": elif topic_split[1] == "atr":
# Broadcast message received on atr # Broadcast message received on atr
if topic_split[2] == "errors":
boterrorlog.error(
"Received Error - Topic: %s - Message: %s", topic, data_decoded
)
else:
_log__helperbot_message("Received Broadcast", topic, data_decoded) _log__helperbot_message("Received Broadcast", topic, data_decoded)
else: else:
_log__helperbot_message("Received Message", topic, data_decoded) _log__helperbot_message("Received Message", topic, data_decoded)
if bumper.bumper_proxy_mode and client_id in self._proxy_clients: if bumper.bumper_proxy_mqtt and client_id in self._proxy_clients:
if not topic_split[3] == "proxyhelper": if not topic_split[3] == "proxyhelper":
# if from proxyhelper, don't send back to ecovacs...yet # if from proxyhelper, don't send back to ecovacs...yet
if topic_split[6] == "proxyhelper": if topic_split[6] == "proxyhelper":
@ -325,7 +313,7 @@ class BumperMQTTServerPlugin:
ttopic[10], "" ttopic[10], ""
) )
if ttopic[6] == "": if ttopic[6] == "":
proxymodelog.warning( _LOGGER_PROXY.warning(
"Request mapper is missing entry, " "Request mapper is missing entry, "
f"probably request took to long... Client_id: {client_id}" f"probably request took to long... Client_id: {client_id}"
f" - Request_id: {ttopic[10]}" f" - Request_id: {ttopic[10]}"
@ -333,19 +321,19 @@ class BumperMQTTServerPlugin:
return return
ttopic_join = "/".join(ttopic) ttopic_join = "/".join(ttopic)
proxymodelog.info( _LOGGER_PROXY.info(
f"Bot Message Converted Topic From {message.topic} TO {ttopic_join} " f"Bot Message Converted Topic From {message.topic} TO {ttopic_join} "
f"with message: {data_decoded}" f"with message: {data_decoded}"
) )
else: else:
ttopic_join = message.topic ttopic_join = message.topic
proxymodelog.info( _LOGGER_PROXY.info(
f"Bot Message From {ttopic_join} with message: {data_decoded}" f"Bot Message From {ttopic_join} with message: {data_decoded}"
) )
try: try:
# Send back to ecovacs # Send back to ecovacs
proxymodelog.info( _LOGGER_PROXY.info(
"Proxy Forward Message to Ecovacs - Topic:" "Proxy Forward Message to Ecovacs - Topic:"
f" {ttopic_join} - Message: {data_decoded}" f" {ttopic_join} - Message: {data_decoded}"
) )
@ -353,13 +341,13 @@ class BumperMQTTServerPlugin:
ttopic_join, data_decoded.encode(), message.qos ttopic_join, data_decoded.encode(), message.qos
) )
except Exception: # pylint: disable=broad-except except Exception: # pylint: disable=broad-except
proxymodelog.error( _LOGGER_PROXY.error(
"Forwarding to Ecovacs - Exception", "Forwarding to Ecovacs - Exception",
exc_info=True, exc_info=True,
) )
async def on_broker_client_disconnected(self, client_id: str) -> None: async def on_broker_client_disconnected(self, client_id: str) -> None:
"""On client disconnect.""" """On client disconnect."""
if bumper.bumper_proxy_mode and client_id in self._proxy_clients: if bumper.bumper_proxy_mqtt and client_id in self._proxy_clients:
await self._proxy_clients.pop(client_id).disconnect() await self._proxy_clients.pop(client_id).disconnect()
self._set_client_connected(client_id, False) self._set_client_connected(client_id, False)

View file

@ -32,7 +32,7 @@ def get_logger(name: str, rotate: RotatingFileHandler | None = None) -> logging.
__loggers[name] = logger __loggers[name] = logger
if name == "mqttserver": if name == "mqtt_server":
get_logger("transitions", rotate).setLevel( get_logger("transitions", rotate).setLevel(
logging.CRITICAL + 1 logging.CRITICAL + 1
) # Ignore this logger ) # Ignore this logger

View file

@ -30,10 +30,9 @@ from bumper.models import (
EcoVacs_Login, EcoVacs_Login,
EcoVacsHome_Login, EcoVacsHome_Login,
) )
from bumper.util import get_current_time_as_millis, get_logger from bumper.util import get_current_time_as_millis
from bumper.web.plugins import get_success_response from bumper.web.plugins import get_success_response
from bumper.web.server import _LOGGER
_logger = get_logger("confserver")
def _generate_token(user: dict[str, Any]) -> str: def _generate_token(user: dict[str, Any]) -> str:
@ -56,7 +55,7 @@ async def login(request: Request) -> Response:
user_devid = request.match_info.get("devid", "") user_devid = request.match_info.get("devid", "")
countrycode = request.match_info.get("country", "us") countrycode = request.match_info.get("country", "us")
apptype = request.match_info.get("apptype", "") apptype = request.match_info.get("apptype", "")
_logger.info(f"client with devid {user_devid} attempting login") _LOGGER.info(f"client with devid {user_devid} attempting login")
if use_auth: if use_auth:
if ( if (
not user_devid == "" not user_devid == ""
@ -120,7 +119,7 @@ async def login(request: Request) -> Response:
) )
except Exception as e: except Exception as e:
_logger.exception(f"{e}") _LOGGER.exception(f"{e}")
raise HTTPInternalServerError raise HTTPInternalServerError
@ -214,7 +213,7 @@ def _check_token(
return web.json_response(body) return web.json_response(body)
except Exception as e: except Exception as e:
_logger.exception(f"{e}") _LOGGER.exception(f"{e}")
raise HTTPInternalServerError raise HTTPInternalServerError
@ -269,7 +268,7 @@ def _auth_any(
if "did" in bot: if "did" in bot:
user_add_bot(tmpuser["userid"], bot["did"]) user_add_bot(tmpuser["userid"], bot["did"])
else: else:
_logger.error(f"No DID for bot: {bot}") _LOGGER.error(f"No DID for bot: {bot}")
if "checkLogin" in request.path: # If request was to check a token do so if "checkLogin" in request.path: # If request was to check a token do so
checkToken = _check_token( checkToken = _check_token(
@ -300,5 +299,5 @@ def _auth_any(
return body return body
except Exception as e: except Exception as e:
_logger.exception(f"{e}") _LOGGER.exception(f"{e}")
return {} return {}

View file

@ -35,10 +35,10 @@ class _aiohttp_filter(logging.Filter):
return False return False
confserverlog = get_logger("confserver") _LOGGER = get_logger("webserver")
# Add logging filter above to aiohttp.access # Add logging filter above to aiohttp.access
logging.getLogger("aiohttp.access").addFilter(_aiohttp_filter()) logging.getLogger("aiohttp.access").addFilter(_aiohttp_filter())
proxymodelog = logging.getLogger("proxymode") _LOGGER_PROXY = logging.getLogger("web_proxy")
@dataclasses.dataclass(frozen=True) @dataclasses.dataclass(frozen=True)
@ -110,7 +110,7 @@ class WebServer:
async def start(self) -> None: async def start(self) -> None:
"""Start server.""" """Start server."""
try: try:
confserverlog.info("Starting ConfServer") _LOGGER.info("Starting ConfServer")
for binding in self._bindings: for binding in self._bindings:
runner = web.AppRunner(self._app) runner = web.AppRunner(self._app)
self._runners.append(runner) self._runners.append(runner)
@ -130,13 +130,13 @@ class WebServer:
await site.start() await site.start()
except Exception as e: except Exception as e:
confserverlog.exception(f"{e}") _LOGGER.exception(f"{e}")
raise e raise e
async def shutdown(self) -> None: async def shutdown(self) -> None:
"""Shutdown server.""" """Shutdown server."""
try: try:
confserverlog.info("Shutting down") _LOGGER.info("Shutting down")
for runner in self._runners: for runner in self._runners:
await runner.shutdown() await runner.shutdown()
@ -144,7 +144,7 @@ class WebServer:
await self._app.shutdown() await self._app.shutdown()
except Exception as e: except Exception as e:
confserverlog.exception(f"{e}") _LOGGER.exception(f"{e}")
async def _handle_base(self, request: Request) -> Response: async def _handle_base(self, request: Request) -> Response:
try: try:
@ -174,7 +174,7 @@ class WebServer:
} }
return aiohttp_jinja2.render_template("home.jinja2", request, context=all) return aiohttp_jinja2.render_template("home.jinja2", request, context=all)
except Exception as e: except Exception as e:
confserverlog.exception(f"{e}") _LOGGER.exception(f"{e}")
raise HTTPInternalServerError raise HTTPInternalServerError
@ -209,7 +209,7 @@ class WebServer:
return web.json_response({"status": "invalid service"}) return web.json_response({"status": "invalid service"})
except Exception as e: except Exception as e:
confserverlog.exception(f"{e}") _LOGGER.exception(f"{e}")
raise raise
async def _handle_remove_bot(self, request: Request) -> Response: async def _handle_remove_bot(self, request: Request) -> Response:
@ -222,7 +222,7 @@ class WebServer:
return web.json_response({"status": "successfully removed bot"}) return web.json_response({"status": "successfully removed bot"})
except Exception as e: except Exception as e:
confserverlog.exception(f"{e}") _LOGGER.exception(f"{e}")
raise HTTPInternalServerError raise HTTPInternalServerError
@ -236,7 +236,7 @@ class WebServer:
return web.json_response({"status": "successfully removed client"}) return web.json_response({"status": "successfully removed client"})
except Exception as e: except Exception as e:
confserverlog.exception(f"{e}") _LOGGER.exception(f"{e}")
raise HTTPInternalServerError raise HTTPInternalServerError
@ -247,14 +247,14 @@ class WebServer:
else: else:
body = json.loads(await request.text()) body = json.loads(await request.text())
confserverlog.debug(body) _LOGGER.debug(body)
if body["todo"] == "FindBest": if body["todo"] == "FindBest":
service = body["service"] service = body["service"]
if service == "EcoMsgNew": if service == "EcoMsgNew":
srvip = bumper.bumper_announce_ip srvip = bumper.bumper_announce_ip
srvport = 5223 srvport = 5223
confserverlog.info( _LOGGER.info(
"Announcing EcoMsgNew Server to bot as: {}:{}".format( "Announcing EcoMsgNew Server to bot as: {}:{}".format(
srvip, srvport srvip, srvport
) )
@ -267,7 +267,7 @@ class WebServer:
elif service == "EcoUpdate": elif service == "EcoUpdate":
srvip = "47.88.66.164" # EcoVacs Server srvip = "47.88.66.164" # EcoVacs Server
srvport = 8005 srvport = 8005
confserverlog.info( _LOGGER.info(
"Announcing EcoUpdate Server to bot as: {}:{}".format( "Announcing EcoUpdate Server to bot as: {}:{}".format(
srvip, srvport srvip, srvport
) )
@ -279,7 +279,7 @@ class WebServer:
return web.json_response({}) return web.json_response({})
except Exception as e: except Exception as e:
confserverlog.exception(f"{e}") _LOGGER.exception(f"{e}")
raise HTTPInternalServerError raise HTTPInternalServerError
@ -291,14 +291,14 @@ class WebServer:
else: else:
postbody = json.loads(await request.text()) postbody = json.loads(await request.text())
confserverlog.debug(postbody) _LOGGER.debug(postbody)
body = {"authCode": postbody["itToken"], "result": "ok", "todo": "result"} body = {"authCode": postbody["itToken"], "result": "ok", "todo": "result"}
return web.json_response(body) return web.json_response(body)
except Exception as e: except Exception as e:
confserverlog.exception(f"{e}") _LOGGER.exception(f"{e}")
raise HTTPInternalServerError raise HTTPInternalServerError
@ -318,7 +318,7 @@ class WebServer:
) as session: ) as session:
if request.content.total_bytes > 0: if request.content.total_bytes > 0:
read_body = await request.read() read_body = await request.read()
proxymodelog.info( _LOGGER_PROXY.info(
f"HTTP Proxy Request to EcoVacs (body=true) (URL:{request.url}) - {read_body.decode('utf-8')}" 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":
@ -328,7 +328,7 @@ class WebServer:
request.method, request.url, data=fdata request.method, request.url, data=fdata
) as resp: ) as resp:
response = await resp.text() response = await resp.text()
proxymodelog.info( _LOGGER_PROXY.info(
f"HTTP Proxy Response from EcoVacs (URL: {request.url}) - (Status: {resp.status}) - {response}" f"HTTP Proxy Response from EcoVacs (URL: {request.url}) - (Status: {resp.status}) - {response}"
) )
else: else:
@ -337,23 +337,23 @@ class WebServer:
request.method, request.url, json=await request.json() request.method, request.url, json=await request.json()
) as resp: ) as resp:
response = await resp.text() response = await resp.text()
proxymodelog.info( _LOGGER_PROXY.info(
f"HTTP Proxy Response from EcoVacs (URL: {request.url}) - (Status: {resp.status}) - {response}" f"HTTP Proxy Response from EcoVacs (URL: {request.url}) - (Status: {resp.status}) - {response}"
) )
else: else:
proxymodelog.info( _LOGGER_PROXY.info(
f"HTTP Proxy Request to EcoVacs (body=false) (URL:{request.url})" f"HTTP Proxy Request to EcoVacs (body=false) (URL:{request.url})"
) )
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":
proxymodelog.info( _LOGGER_PROXY.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()) return web.Response(body=await resp.read())
else: else:
response = await resp.text() response = await resp.text()
proxymodelog.info( _LOGGER_PROXY.info(
f"HTTP Proxy Response from EcoVacs (URL: {request.url}) - (Status: {resp.status}) - {response}" f"HTTP Proxy Response from EcoVacs (URL: {request.url}) - (Status: {resp.status}) - {response}"
) )
@ -366,12 +366,12 @@ class WebServer:
return web.Response(text=response) return web.Response(text=response)
except asyncio.CancelledError: except asyncio.CancelledError:
proxymodelog.exception( _LOGGER_PROXY.exception(
f"Request cancelled or timeout - {request.url}", exc_info=True f"Request cancelled or timeout - {request.url}", exc_info=True
) )
raise raise
except Exception: except Exception:
proxymodelog.exception("An exception occurred", exc_info=True) _LOGGER_PROXY.exception("An exception occurred", exc_info=True)
raise HTTPInternalServerError raise HTTPInternalServerError

View file

@ -27,7 +27,7 @@ async def test_helperbot_message(mqtt_client: Client):
l.check_present( l.check_present(
( (
"helperbot", "mqtt_messages",
"DEBUG", "DEBUG",
"Received Broadcast - Topic: iot/atr/DustCaseST/bot_serial/ls1ok3/wC3g/x - Message: <ctl ts='1547822804960' td='DustCaseST' st='0'/>", "Received Broadcast - Topic: iot/atr/DustCaseST/bot_serial/ls1ok3/wC3g/x - Message: <ctl ts='1547822804960' td='DustCaseST' st='0'/>",
) )
@ -47,7 +47,7 @@ async def test_helperbot_message(mqtt_client: Client):
l.check_present( l.check_present(
( (
"helperbot", "mqtt_messages",
"DEBUG", "DEBUG",
"Send Command - Topic: iot/p2p/GetWKVer/helperbot/bumper/helperbot/bot_serial/ls1ok3/wC3g/q/iCmuqp/j - Message: {}", "Send Command - Topic: iot/p2p/GetWKVer/helperbot/bumper/helperbot/bot_serial/ls1ok3/wC3g/q/iCmuqp/j - Message: {}",
) )
@ -67,7 +67,7 @@ async def test_helperbot_message(mqtt_client: Client):
l.check_present( l.check_present(
( (
"helperbot", "mqtt_messages",
"DEBUG", "DEBUG",
'Received Response - Topic: iot/p2p/GetWKVer/bot_serial/ls1ok3/wC3g/helperbot/bumper/helperbot/p/iCmuqp/j - Message: {"ret":"ok","ver":"0.13.5"}', 'Received Response - Topic: iot/p2p/GetWKVer/bot_serial/ls1ok3/wC3g/helperbot/bumper/helperbot/p/iCmuqp/j - Message: {"ret":"ok","ver":"0.13.5"}',
) )
@ -83,11 +83,11 @@ async def test_helperbot_message(mqtt_client: Client):
msg_topic_name = "iot/p2p/GetWKVer/bot_serial/ls1ok3/wC3g/TESTBAD/bumper/helperbot/p/iCmuqp/j" msg_topic_name = "iot/p2p/GetWKVer/bot_serial/ls1ok3/wC3g/TESTBAD/bumper/helperbot/p/iCmuqp/j"
mqtt_client.publish(msg_topic_name, msg_payload.encode()) mqtt_client.publish(msg_topic_name, msg_payload.encode())
await asyncio.sleep(0.1) await asyncio.sleep(0.2)
l.check_present( l.check_present(
( (
"helperbot", "mqtt_messages",
"DEBUG", "DEBUG",
"Received Message - Topic: iot/p2p/GetWKVer/bot_serial/ls1ok3/wC3g/TESTBAD/bumper/helperbot/p/iCmuqp/j - Message: test", "Received Message - Topic: iot/p2p/GetWKVer/bot_serial/ls1ok3/wC3g/TESTBAD/bumper/helperbot/p/iCmuqp/j - Message: test",
) )
@ -107,9 +107,9 @@ async def test_helperbot_message(mqtt_client: Client):
l.check_present( l.check_present(
( (
"boterror", "mqtt_messages",
"ERROR", "DEBUG",
"Received Error - Topic: iot/atr/errors/bot_serial/ls1ok3/wC3g/x - Message: <ctl ts='1560904925396' td='errors' old='' new='110'/>", "Received Broadcast - Topic: iot/atr/errors/bot_serial/ls1ok3/wC3g/x - Message: <ctl ts='1560904925396' td='errors' old='' new='110'/>",
) )
) # Check received message was logged ) # Check received message was logged
l.clear() l.clear()
@ -324,7 +324,7 @@ async def test_mqttserver():
l.check_present( l.check_present(
( (
"mqttserver", "mqtt_server",
"INFO", "INFO",
"File Authentication Failed - Username: test-client - ClientID: test-file-auth", "File Authentication Failed - Username: test-client - ClientID: test-file-auth",
), ),
@ -339,7 +339,7 @@ async def test_mqttserver():
l.check_present( l.check_present(
( (
"mqttserver", "mqtt_server",
"INFO", "INFO",
"File Authentication Failed - No Entry - Username: test-client-noexist - ClientID: test-file-auth", "File Authentication Failed - No Entry - Username: test-client-noexist - ClientID: test-file-auth",
), ),