mirror of
https://github.com/matrix-org/pantalaimon.git
synced 2025-05-06 08:35:16 -04:00
pantalaimon: Add propper logging support.
This commit is contained in:
parent
3fa6ce9292
commit
f27eb836fe
4 changed files with 36 additions and 9 deletions
|
@ -1,4 +1,5 @@
|
||||||
from typing import Any, Dict
|
from typing import Any, Dict
|
||||||
|
from pprint import pformat
|
||||||
|
|
||||||
from nio import (
|
from nio import (
|
||||||
AsyncClient,
|
AsyncClient,
|
||||||
|
@ -8,6 +9,8 @@ from nio import (
|
||||||
SyncResponse
|
SyncResponse
|
||||||
)
|
)
|
||||||
|
|
||||||
|
from pantalaimon.log import logger
|
||||||
|
|
||||||
|
|
||||||
class PantaClient(AsyncClient):
|
class PantaClient(AsyncClient):
|
||||||
"""A wrapper class around a nio AsyncClient extending its functionality."""
|
"""A wrapper class around a nio AsyncClient extending its functionality."""
|
||||||
|
@ -23,26 +26,28 @@ class PantaClient(AsyncClient):
|
||||||
"""
|
"""
|
||||||
for room_id, room_dict in body["rooms"]["join"].items():
|
for room_id, room_dict in body["rooms"]["join"].items():
|
||||||
if not self.rooms[room_id].encrypted:
|
if not self.rooms[room_id].encrypted:
|
||||||
print("Room {} not encrypted skipping...".format(
|
logger.info("Room {} is not encrypted skipping...".format(
|
||||||
self.rooms[room_id].display_name
|
self.rooms[room_id].display_name
|
||||||
))
|
))
|
||||||
continue
|
continue
|
||||||
|
|
||||||
for event in room_dict["timeline"]["events"]:
|
for event in room_dict["timeline"]["events"]:
|
||||||
if event["type"] != "m.room.encrypted":
|
if event["type"] != "m.room.encrypted":
|
||||||
print("Event not encrypted skipping...")
|
logger.info("Event is not encrypted: "
|
||||||
|
"{}".format(pformat(event)))
|
||||||
continue
|
continue
|
||||||
|
|
||||||
parsed_event = RoomEncryptedEvent.parse_event(event)
|
parsed_event = RoomEncryptedEvent.parse_event(event)
|
||||||
parsed_event.room_id = room_id
|
parsed_event.room_id = room_id
|
||||||
|
|
||||||
if not isinstance(parsed_event, MegolmEvent):
|
if not isinstance(parsed_event, MegolmEvent):
|
||||||
print("Not a megolm event.")
|
logger.warn("Encrypted event is not a megolm event:"
|
||||||
|
"{}".format(pformat(event)))
|
||||||
continue
|
continue
|
||||||
|
|
||||||
try:
|
try:
|
||||||
decrypted_event = self.decrypt_event(parsed_event)
|
decrypted_event = self.decrypt_event(parsed_event)
|
||||||
print("Decrypted event: {}".format(decrypted_event))
|
logger.info("Decrypted event: {}".format(decrypted_event))
|
||||||
event["type"] = "m.room.message"
|
event["type"] = "m.room.message"
|
||||||
|
|
||||||
# TODO support other event types
|
# TODO support other event types
|
||||||
|
@ -63,7 +68,7 @@ class PantaClient(AsyncClient):
|
||||||
event["verified"] = decrypted_event.verified
|
event["verified"] = decrypted_event.verified
|
||||||
|
|
||||||
except EncryptionError as error:
|
except EncryptionError as error:
|
||||||
print("ERROR decrypting {}".format(error))
|
logger.warn(error)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
return body
|
return body
|
||||||
|
|
|
@ -9,6 +9,7 @@ import json
|
||||||
import click
|
import click
|
||||||
from ipaddress import ip_address
|
from ipaddress import ip_address
|
||||||
from urllib.parse import urlparse
|
from urllib.parse import urlparse
|
||||||
|
from logbook import StderrHandler
|
||||||
|
|
||||||
from aiohttp import web, ClientSession
|
from aiohttp import web, ClientSession
|
||||||
from nio import (
|
from nio import (
|
||||||
|
@ -22,6 +23,7 @@ from json import JSONDecodeError
|
||||||
from multidict import CIMultiDict
|
from multidict import CIMultiDict
|
||||||
|
|
||||||
from pantalaimon.client import PantaClient
|
from pantalaimon.client import PantaClient
|
||||||
|
from pantalaimon.log import logger
|
||||||
|
|
||||||
|
|
||||||
@attr.s
|
@attr.s
|
||||||
|
@ -242,9 +244,8 @@ class ProxyDaemon:
|
||||||
if device.deleted:
|
if device.deleted:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
print("Automatically verifying device {}".format(
|
logger.info("Automatically verifying device {} of "
|
||||||
device.id
|
"user {}".format(device.id, user_id))
|
||||||
))
|
|
||||||
client.verify_device(device)
|
client.verify_device(device)
|
||||||
|
|
||||||
json_response = await response.transport_response.json()
|
json_response = await response.transport_response.json()
|
||||||
|
@ -387,13 +388,28 @@ class ipaddress(click.ParamType):
|
||||||
default=8009,
|
default=8009,
|
||||||
help="The listening port for incoming client connections (default: 8009)"
|
help="The listening port for incoming client connections (default: 8009)"
|
||||||
)
|
)
|
||||||
|
@click.option("--log-level", type=click.Choice([
|
||||||
|
"error",
|
||||||
|
"warning",
|
||||||
|
"info",
|
||||||
|
"debug"
|
||||||
|
]), default="error")
|
||||||
@click.argument(
|
@click.argument(
|
||||||
"homeserver",
|
"homeserver",
|
||||||
type=URL(),
|
type=URL(),
|
||||||
)
|
)
|
||||||
def main(proxy, ssl_insecure, listen_address, listen_port, homeserver):
|
def main(
|
||||||
|
proxy,
|
||||||
|
ssl_insecure,
|
||||||
|
listen_address,
|
||||||
|
listen_port,
|
||||||
|
log_level,
|
||||||
|
homeserver
|
||||||
|
):
|
||||||
ssl = None if ssl_insecure is False else False
|
ssl = None if ssl_insecure is False else False
|
||||||
|
|
||||||
|
StderrHandler(level=log_level.upper()).push_application()
|
||||||
|
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
proxy, app = loop.run_until_complete(init(
|
proxy, app = loop.run_until_complete(init(
|
||||||
homeserver.geturl(),
|
homeserver.geturl(),
|
||||||
|
|
5
pantalaimon/log.py
Normal file
5
pantalaimon/log.py
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
import logbook
|
||||||
|
from logbook import Logger
|
||||||
|
|
||||||
|
logger = Logger("pantalaimon")
|
||||||
|
logger.level = logbook.WARNING
|
1
setup.py
1
setup.py
|
@ -17,6 +17,7 @@ setup(
|
||||||
"aiohttp",
|
"aiohttp",
|
||||||
"appdirs",
|
"appdirs",
|
||||||
"click",
|
"click",
|
||||||
|
"logbook",
|
||||||
"typing;python_version<'3.5'",
|
"typing;python_version<'3.5'",
|
||||||
"matrix-nio @ git+https://github.com/poljar/matrix-nio.git@async#egg=matrix-nio-0"
|
"matrix-nio @ git+https://github.com/poljar/matrix-nio.git@async#egg=matrix-nio-0"
|
||||||
],
|
],
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue