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()
async with i2plib.Session(session_name, sam_address=sam_address, loop=loop): async with i2plib.StreamConnection(session_name, url.netloc, loop=loop, sam_address=sam_address) as c: c.write("GET {} HTTP/1.0\nHost: {}\r\n\r\n".format( url.path, url.netloc).encode()) while 1: data = await c.read(buflen) if len(data) > 0: resp += data else: break try: print(resp.split(b"\r\n\r\n", 1)[1].decode()) except IndexError: print(resp.decode()) if __name__ == "__main__": if len(sys.argv) == 2: url = sys.argv[1] if not url.startswith("http://"): url = "http://" + url loop = asyncio.get_event_loop() loop.run_until_complete(wget(i2plib.get_sam_address(), loop, url)) loop.stop() loop.close() else: print("""Fetch I2P URL. Usage: python wget.py http://site.i2p/""")
async def wget(sam_address, loop, url): session_name = "wget" READY = asyncio.Event(loop=loop) asyncio.ensure_future(i2plib.create_session(session_name, sam_address=sam_address, loop=loop, session_ready=READY), loop=loop) await READY.wait() res = await http_get(sam_address, loop, session_name, url) print(res) if __name__ == "__main__": sam_address = i2plib.get_sam_address() if len(sys.argv) == 2: url = sys.argv[1] if not url.startswith("http://"): url = "http://" + url loop = asyncio.get_event_loop() loop.run_until_complete(wget(sam_address, loop, url)) loop.stop() loop.close() else: print("""Fetch I2P URL. Usage: python wget.py http://site.i2p/""")
# connect to remote server, returns socket to use client_sock = ss.connect(dest.base64) client_sock.send("GET {} HTTP/1.0\nHost: {}\r\n\r\n".format( url.path, url.netloc).encode()) buflen, resp = 4096, b"" while 1: data = client_sock.recv(buflen) if len(data) > 0: resp += data else: break resp = resp.decode() try: return resp.split("\r\n\r\n", 1)[1] except IndexError: return resp if __name__ == "__main__": if len(sys.argv) == 2: url = sys.argv[1] if sys.argv[1].startswith( "http://") else "//" + sys.argv[1] else: url = "http://irkvgdnlc6tidoqomre4qr7q4w4qcjfyvbovatgyolk6d4uvcyha.b32.i2p/uploads/BSD" r = http_get(url, i2plib.get_sam_address()) print(r)