diff --git a/peerix/app.py b/peerix/app.py index dc33a1b..bca7257 100644 --- a/peerix/app.py +++ b/peerix/app.py @@ -16,7 +16,8 @@ async def _setup_stores(local_port: int): global l_access, r_access async with local() as l: l_access = PrefixStore("local/nar", l) - async with remote(l_access, local_port, "0.0.0.0") as r: + lp = PrefixStore("local", l) + async with remote(lp, local_port, "0.0.0.0", lp.prefix) as r: r_access = PrefixStore("remote", r) yield setup_store = _setup_stores(12304) diff --git a/peerix/remote.py b/peerix/remote.py index 0f7f0a1..704b593 100644 --- a/peerix/remote.py +++ b/peerix/remote.py @@ -41,13 +41,15 @@ class DiscoveryProtocol(asyncio.DatagramProtocol, Store): store: Store session: aiohttp.ClientSession local_port: int + prefix: str - def __init__(self, store: Store, session: aiohttp.ClientSession, local_port: int): + def __init__(self, store: Store, session: aiohttp.ClientSession, local_port: int, prefix: str): self.idx = 0 self.waiters = {} self.store = store self.session = session self.local_port = local_port + self.prefix = prefix def connection_made(self, transport): self.transport = transport @@ -82,7 +84,15 @@ class DiscoveryProtocol(asyncio.DatagramProtocol, Store): if narinfo is None: return - self.transport.sendto(b"\x01" + data[1:5] + self.local_port.to_bytes(4, "big") + narinfo.url.encode("utf-8"), addr) + self.transport.sendto(b"".join([ + b"\x01", + data[1:5], + self.local_port.to_bytes(4, "big"), + self.prefix.encode("utf-8"), + b"/", + hsh.encode("utf-8"), + b".narinfo" + ]), addr) async def narinfo(self, hsh: str) -> t.Optional[NarInfo]: fut = asyncio.get_running_loop().create_future() @@ -120,11 +130,11 @@ class DiscoveryProtocol(asyncio.DatagramProtocol, Store): @contextlib.asynccontextmanager -async def remote(store: Store, local_port: int, local_addr: str="0.0.0.0"): +async def remote(store: Store, local_port: int, local_addr: str="0.0.0.0", prefix: str="local"): protocol: DiscoveryProtocol async with aiohttp.ClientSession() as session: _, protocol = await asyncio.get_running_loop().create_datagram_endpoint( - lambda: DiscoveryProtocol(store, session, local_port), + lambda: DiscoveryProtocol(store, session, local_port, prefix), local_addr=(local_addr, local_port), family=socket.AF_INET, allow_broadcast=True