async def coroutines_test(): _, server_session_writer = await i2plib.create_session( "ppserver", sam_address=self.sam_address, loop=self.loop, destination=SERVER_DEST) server_reader, server_writer = await i2plib.stream_accept( "ppserver", sam_address=self.sam_address, loop=self.loop) _, client_session_writer = await i2plib.create_session( "ppclient", sam_address=self.sam_address, loop=self.loop) client_reader, client_writer = await i2plib.stream_connect( "ppclient", SERVER_DEST.base32 + ".b32.i2p", sam_address=self.sam_address, loop=self.loop) client_writer.write(b"PING") incoming = await server_reader.read(BUFFER_SIZE) dest, request = incoming.split(b"\n", 1) remote_destination = i2plib.Destination(dest.decode()) if not request: request = await server_reader.read(BUFFER_SIZE) self.assertEqual(request, b"PING") server_writer.write(b"PONG") response = await client_reader.read(BUFFER_SIZE) self.assertEqual(response, b"PONG") client_writer.close() server_writer.close() server_session_writer.close() client_session_writer.close()
async def ping_pong(sam_address, loop): _, server_session_writer = await i2plib.create_session( "ppserver", sam_address=sam_address, loop=loop, destination=PK_B64) server_reader, server_writer = await i2plib.stream_accept( "ppserver", sam_address=sam_address, loop=loop) _, client_session_writer = await i2plib.create_session( "ppclient", sam_address=sam_address, loop=loop) client_reader, client_writer = await i2plib.stream_connect( "ppclient", DEST_B32, sam_address=sam_address, loop=loop) client_writer.write(b"PING") incoming = await server_reader.read(BUFFER_SIZE) dest, request = incoming.split(b"\n", 1) remote_destination = i2plib.Destination(dest.decode()) if not request: request = await server_reader.read(BUFFER_SIZE) assert request == b"PING" server_writer.write(b"PONG") response = await client_reader.read(BUFFER_SIZE) assert response == b"PONG" client_writer.close() server_writer.close() server_session_writer.close() client_session_writer.close() await asyncio.sleep(0.0001) # wait until all tasks are completed
async def _receive_message(self, reader, writer, destination): with suppress(asyncio.CancelledError): destination = i2plib.Destination(destination.decode()) name = self.addressbook.get_name(destination.base32) if not name and self.ignore_unauthorized: writer.close() return try: data = await asyncio.wait_for(reader.read(MAX_MESSAGE_LENGTH), DEFAULT_TIMEOUT) except asyncio.TimeoutError: writer.close() return try: request = Message.parse(data, destination.base32) request.name = name except ValidationError as e: logger.warning("Invalid request: "+str(e)) writer.close() return if request.uuid.hex in self.uuid_log: logger.debug("Duplicate message: "+ str(request)) writer.write(bytes(Message(code=Message.OK))) writer.close() return self.uuid_log.append(request.uuid.hex) logger.debug("Received message: " + str(request)) if request.code == Message.PING \ or request.code == Message.AUTHORIZATION: writer.write(bytes(Message(code=Message.OK))) writer.close() if request.code == Message.PING: await self.on_ping(request) elif request.code == Message.AUTHORIZATION: await self.on_authorization(request) elif request.name: writer.write(bytes(Message(code=Message.OK))) writer.close() if request.code == Message.PRIVATE: await self.on_private_message(request) elif request.code == Message.PUBLIC: await self.on_public_message(request) elif request.code == Message.UNAUTHORIZED: await self.on_unauthorized(request) else: writer.write(bytes(Message(code=Message.UNAUTHORIZED))) writer.close() return await self._dest_online(request.destination)
async def load_destination(loop, sam_address, datadir, filename="kote.dat"): """Read destination from the disc OR create it""" path = os.path.join(datadir, filename) key_data = await loop.run_in_executor(None, read_file, path) if key_data: dest = i2plib.Destination(key_data, has_private_key=True) else: dest = await i2plib.new_destination(loop=loop, sam_address=sam_address) await loop.run_in_executor(None, write_file, path, dest.private_key.data) return dest
async def server(sam_address, loop, ready): async with i2plib.Session("ppserver", sam_address=sam_address, loop=loop, destination=SERVER_DEST): ready.release() async with i2plib.StreamAcceptor("ppserver", sam_address=sam_address, loop=loop) as s: incoming = await s.read(BUFFER_SIZE) dest, request = incoming.split(b"\n", 1) remote_destination = i2plib.Destination(dest.decode()) if not request: request = await s.read(BUFFER_SIZE) self.assertEqual(request, b"PING") s.write(b"PONG")
def main(args): sam_address = i2plib.get_sam_address() server_address = ('127.0.0.1', i2plib.utils.get_free_port()) if not os.path.isdir(args.web_directory): raise OSError("No such directory {}".format(args.web_directory)) if args.key: dest = i2plib.Destination(path=args.key, has_private_key=True) else: dest = i2plib.utils.get_new_destination(sam_address=sam_address) logging.info("Listening: {}.b32.i2p".format(dest.base32)) logging.info("Server: {}:{}".format(server_address[0], server_address[1])) # run HTTP server http_server_thread = Process(target=serve_directory, args=(server_address, args.web_directory)) http_server_thread.daemon = True http_server_thread.start() loop = asyncio.get_event_loop() tunnel = i2plib.ServerTunnel(server_address, loop=loop, destination=dest, sam_address=sam_address) asyncio.ensure_future(tunnel.run(), loop=loop) try: loop.run_forever() except KeyboardInterrupt: pass finally: loop.stop() loop.close()
import unittest import asyncio import os import i2plib import i2plib.sam import i2plib.utils import i2plib.tunnel BUFFER_SIZE = 65536 REAL_SAM = os.getenv("REAL_SAM") SERVER_DEST_B64 = "5pJLIgm7KCqk-d0As66OdeMRj4moqtD97wOluQh5SXWCbeMfp7cr8cgHU~5rrcN6V~QcIJuqjDpYWojBdjYrc7fAA3iwWpN4fzI05yvE48oOOOLqBq7SvkpyzIhjc0hv81XQIu0LWzXXS~-B61wurJhte-LisF571BzefV5xoaRN8y3A0RidaJyuzVufPP4cKY5NeSsmTY36QRl54PG7iWJSXnLROROlg6qsjoeIV9lyNFY6ZsQKTQzEIInCZaARmNfoJP-MAsOMoj-CRDU2MXhYT~DKdI-rWH579A3wuoEjmlHtHyms7xvwUkb6kIx5UJHZmzF2Hyv3xVrpu0HSkZfUIbzz1lAc4IZ-8jnBjt2RIRpYMNnwZW09HjJXQDd7K-QvpxpK-cqNJcmWehGP7OxLt9Jj6h~8aUFHIJtFI77Zmp~YGf7cO9vCZexeLn7iByqDtfhzTP62IPu0~MJafA4efU83A-DXo8PJhOhl7rYRzH7bWRzB1rhBI~w~TsVOBQAEAAcAAL0me7gfS2H-OZ3FAsPtbUFCFpTcvfLAzBmNxxYU5TflB4KcxNe2isp2UjM7YLCuZg6OCaBSEnoag-ABpJPkY0WIjkqbFzOlowH2oVwevFHrZCFwvf1XVXsyWdupACHmmRHFCHKHMKzolO3Cye0RMH0wIEyMRyIszSThft~keXWyuEwBM4Vros-OKrKN-mBrHNbzQzTiGLS0dVMzdPvG6Pq4t1~wCWqAXrO8n7xU-xQECEpl053Ml5AJyUaCoVj3xqCd4nbrH2~kLmvd9r2nnd-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ENT_DEST_B64, has_private_key=True) UNKNOWN_DEST = "zpk544zs7zsh2pluudh4n64tsg7xtjl3vrwjqu6dggouya5qf3cq.b32.i2p" OFFLINE_DEST = "9ZI1KN~C6ITmw2xGljY1p~36anL3uXpItFUschg0-~-ly4Q0Bh0wtbja6MJNZrAkRrAYUia9e1uugv5U1X9A1Q4Bt3JbrNJ1ouTx~PO4Pv-aWgKaB4rfluN3dPKutpLWRTz6d-rWIC-Wim7Gb8FwauPG29ZVRiWV8tR16ZmKUGNQPaZrL2M5Hy9bgBhkcoPKeNsUl1obc4TBO1sg5rtaxV7qDRUk8cG0kOHrl3u9VIuxAAehhqXYScaIMLfw18GooJZof1IrSQOmCmhiJAk9oEuAh1NAsvZCdVRWe1xRSw~MQRnb6YzdgFERGS0SIqTTBaIk444WEsPFys2ImWUR~e2rp4MTfgZAP3TsS6cdequ5w3lmOu-Ap30Nc7n4yAAR0rYOOT8gRVbE9zLN7VncYRYwkHwNWg~bTMb0yxvDGeXVHRlahAtAJUwaF7VW7oloAmJzbjIgRRfWdp621mAh-IarKzieCS1HxrwtaW3dtBJ8SdICWZ663-YAsXkOL67RBQAEAAcAAA==" SESSION_DEST_MAP = { "ppserver": SERVER_DEST_B64, "ppclient": CLIENT_DEST_B64, "failedaccept": SERVER_DEST_B64, "unknowntest": CLIENT_DEST_B64, "offlinetest": CLIENT_DEST_B64 } async def fake_sam_server_handler(reader, writer):