Add some logging

This commit is contained in:
Sarah 2021-10-17 14:38:48 +02:00
parent 1743539d93
commit 8ee3661615
No known key found for this signature in database
GPG key ID: 708F7ACE058F0186
4 changed files with 21 additions and 6 deletions

View file

@ -1,4 +1,6 @@
import logging
import asyncio import asyncio
import uvloop import uvloop
from hypercorn import Config from hypercorn import Config
from hypercorn.asyncio import serve from hypercorn.asyncio import serve
@ -7,6 +9,7 @@ from peerix.app import app
def run(): def run():
logging.basicConfig(level=logging.DEBUG)
uvloop.install() uvloop.install()
config = Config() config = Config()
config.bind = ["0.0.0.0:12304"] config.bind = ["0.0.0.0:12304"]

View file

@ -1,4 +1,4 @@
import asyncio import logging
import contextlib import contextlib
from starlette.requests import Request from starlette.requests import Request

View file

@ -3,6 +3,7 @@ import typing as t
import contextlib import contextlib
import subprocess import subprocess
import tempfile import tempfile
import logging
import asyncio import asyncio
import shutil import shutil
import base64 import base64
@ -26,6 +27,9 @@ assert nix_serve is not None
assert nix is not None assert nix is not None
logger = logging.getLogger("peerix.local")
class LocalStore(Store): class LocalStore(Store):
def __init__(self, session: aiohttp.ClientSession): def __init__(self, session: aiohttp.ClientSession):

View file

@ -1,6 +1,7 @@
import typing as t import typing as t
import socket import socket
import logging
import asyncio import asyncio
import ipaddress import ipaddress
import contextlib import contextlib
@ -12,6 +13,10 @@ import aiohttp
from peerix.store import NarInfo, Store from peerix.store import NarInfo, Store
logger = logging.getLogger("peerix.remote")
def get_brdcasts(): def get_brdcasts():
for interface, iaddrs in psutil.net_if_addrs().items(): for interface, iaddrs in psutil.net_if_addrs().items():
for iaddr in iaddrs: for iaddr in iaddrs:
@ -56,7 +61,7 @@ class DiscoveryProtocol(asyncio.DatagramProtocol, Store):
def datagram_received(self, data: bytes, addr: t.Tuple[str, int]) -> None: def datagram_received(self, data: bytes, addr: t.Tuple[str, int]) -> None:
if addr[0] in set(get_myself()): if addr[0] in set(get_myself()):
print(f"Ignoring packet from {addr[0]}") logger.debug(f"Ignoring packet from {addr[0]}")
return return
# 0 => Response to a command of mine. # 0 => Response to a command of mine.
@ -79,11 +84,13 @@ class DiscoveryProtocol(asyncio.DatagramProtocol, Store):
async def respond(self, data: bytes, addr: t.Tuple[str, int]) -> None: async def respond(self, data: bytes, addr: t.Tuple[str, int]) -> None:
hsh = data[5:].decode("utf-8") hsh = data[5:].decode("utf-8")
print(f"Got request from {addr[0]}:{addr[1]} for {hsh}") logger.info(f"Got request from {addr[0]}:{addr[1]} for {hsh}")
narinfo = await self.store.narinfo(hsh) narinfo = await self.store.narinfo(hsh)
if narinfo is None: if narinfo is None:
logger.debug(f"{hsh} not found")
return return
logger.debug(f"{hsh} was found.")
self.transport.sendto(b"".join([ self.transport.sendto(b"".join([
b"\x01", b"\x01",
data[1:5], data[1:5],
@ -99,8 +106,9 @@ class DiscoveryProtocol(asyncio.DatagramProtocol, Store):
self.idx = (idx := self.idx)+1 self.idx = (idx := self.idx)+1
self.waiters[idx] = fut self.waiters[idx] = fut
fut.add_done_callback(lambda _: self.waiters.pop(idx, None)) fut.add_done_callback(lambda _: self.waiters.pop(idx, None))
print(f"Requesting {hsh} from direct local network.") logging.info(f"Requesting {hsh} from direct local network.")
for addr in set(get_brdcasts()): for addr in set(get_brdcasts()):
logging.debug(f"Sending request for {hsh} to {addr}:{self.local_port}")
self.transport.sendto(b"".join([b"\x00", idx.to_bytes(4, "big"), hsh.encode("utf-8")]), (addr, self.local_port)) self.transport.sendto(b"".join([b"\x00", idx.to_bytes(4, "big"), hsh.encode("utf-8")]), (addr, self.local_port))
try: try:
@ -108,10 +116,10 @@ class DiscoveryProtocol(asyncio.DatagramProtocol, Store):
# querying of other caches. # querying of other caches.
port, url, addr = await asyncio.wait_for(fut, 0.05) port, url, addr = await asyncio.wait_for(fut, 0.05)
except asyncio.TimeoutError: except asyncio.TimeoutError:
print(f"No response for {hsh}") logging.debug(f"No response for {hsh}")
return None return None
print(f"{addr[0]}:{addr[1]} responded for {hsh} with http://{addr[0]}:{port}/{url}") logging.info(f"{addr[0]}:{addr[1]} responded for {hsh} with http://{addr[0]}:{port}/{url}")
async with self.session.get(f"http://{addr[0]}:{port}/{url}") as resp: async with self.session.get(f"http://{addr[0]}:{port}/{url}") as resp:
if resp.status != 200: if resp.status != 200: