commit 7f7ddbecc5c4df4475689220bfde9d503770a2b3 Author: Frank Villaro-Dixon Date: Tue Aug 27 21:44:15 2024 +0200 create poc Signed-off-by: Frank Villaro-Dixon diff --git a/Pipfile b/Pipfile new file mode 100644 index 0000000..b17b6d3 --- /dev/null +++ b/Pipfile @@ -0,0 +1,13 @@ +[[source]] +url = "https://pypi.org/simple" +verify_ssl = true +name = "pypi" + +[packages] +websockets = "*" +asyncio = "*" + +[dev-packages] + +[requires] +python_version = "3.12" diff --git a/Pipfile.lock b/Pipfile.lock new file mode 100644 index 0000000..90cfae8 --- /dev/null +++ b/Pipfile.lock @@ -0,0 +1,124 @@ +{ + "_meta": { + "hash": { + "sha256": "2f522d4ca76f3a562a33486191d9a8f4fad53f9b7337675f8762a0a3bb880272" + }, + "pipfile-spec": 6, + "requires": { + "python_version": "3.12" + }, + "sources": [ + { + "name": "pypi", + "url": "https://pypi.org/simple", + "verify_ssl": true + } + ] + }, + "default": { + "asyncio": { + "hashes": [ + "sha256:83360ff8bc97980e4ff25c964c7bd3923d333d177aa4f7fb736b019f26c7cb41", + "sha256:b62c9157d36187eca799c378e572c969f0da87cd5fc42ca372d92cdb06e7e1de", + "sha256:c46a87b48213d7464f22d9a497b9eef8c1928b68320a2fa94240f969f6fec08c", + "sha256:c4d18b22701821de07bd6aea8b53d21449ec0ec5680645e5317062ea21817d2d" + ], + "index": "pypi", + "version": "==3.4.3" + }, + "websockets": { + "hashes": [ + "sha256:02cc9bb1a887dac0e08bf657c5d00aa3fac0d03215d35a599130c2034ae6663a", + "sha256:038e7a0f1bfafc7bf52915ab3506b7a03d1e06381e9f60440c856e8918138151", + "sha256:05c25f7b849702950b6fd0e233989bb73a0d2bc83faa3b7233313ca395205f6d", + "sha256:06b3186e97bf9a33921fa60734d5ed90f2a9b407cce8d23c7333a0984049ef61", + "sha256:06df8306c241c235075d2ae77367038e701e53bc8c1bb4f6644f4f53aa6dedd0", + "sha256:0a8f7d65358a25172db00c69bcc7df834155ee24229f560d035758fd6613111a", + "sha256:1f661a4205741bdc88ac9c2b2ec003c72cee97e4acd156eb733662ff004ba429", + "sha256:265e1f0d3f788ce8ef99dca591a1aec5263b26083ca0934467ad9a1d1181067c", + "sha256:2be1382a4daa61e2f3e2be3b3c86932a8db9d1f85297feb6e9df22f391f94452", + "sha256:2e1cf4e1eb84b4fd74a47688e8b0940c89a04ad9f6937afa43d468e71128cd68", + "sha256:337837ac788d955728b1ab01876d72b73da59819a3388e1c5e8e05c3999f1afa", + "sha256:358d37c5c431dd050ffb06b4b075505aae3f4f795d7fff9794e5ed96ce99b998", + "sha256:35c2221b539b360203f3f9ad168e527bf16d903e385068ae842c186efb13d0ea", + "sha256:3670def5d3dfd5af6f6e2b3b243ea8f1f72d8da1ef927322f0703f85c90d9603", + "sha256:372f46a0096cfda23c88f7e42349a33f8375e10912f712e6b496d3a9a557290f", + "sha256:376a43a4fd96725f13450d3d2e98f4f36c3525c562ab53d9a98dd2950dca9a8a", + "sha256:384129ad0490e06bab2b98c1da9b488acb35bb11e2464c728376c6f55f0d45f3", + "sha256:3a20cf14ba7b482c4a1924b5e061729afb89c890ca9ed44ac4127c6c5986e424", + "sha256:3e6566e79c8c7cbea75ec450f6e1828945fc5c9a4769ceb1c7b6e22470539712", + "sha256:4782ec789f059f888c1e8fdf94383d0e64b531cffebbf26dd55afd53ab487ca4", + "sha256:4d70c89e3d3b347a7c4d3c33f8d323f0584c9ceb69b82c2ef8a174ca84ea3d4a", + "sha256:516062a0a8ef5ecbfa4acbaec14b199fc070577834f9fe3d40800a99f92523ca", + "sha256:5575031472ca87302aeb2ce2c2349f4c6ea978c86a9d1289bc5d16058ad4c10a", + "sha256:587245f0704d0bb675f919898d7473e8827a6d578e5a122a21756ca44b811ec8", + "sha256:602cbd010d8c21c8475f1798b705bb18567eb189c533ab5ef568bc3033fdf417", + "sha256:6058b6be92743358885ad6dcdecb378fde4a4c74d4dd16a089d07580c75a0e80", + "sha256:63b702fb31e3f058f946ccdfa551f4d57a06f7729c369e8815eb18643099db37", + "sha256:6ad684cb7efce227d756bae3e8484f2e56aa128398753b54245efdfbd1108f2c", + "sha256:6fd757f313c13c34dae9f126d3ba4cf97175859c719e57c6a614b781c86b617e", + "sha256:7334752052532c156d28b8eaf3558137e115c7871ea82adff69b6d94a7bee273", + "sha256:788bc841d250beccff67a20a5a53a15657a60111ef9c0c0a97fbdd614fae0fe2", + "sha256:7d14901fdcf212804970c30ab9ee8f3f0212e620c7ea93079d6534863444fb4e", + "sha256:7ea9c9c7443a97ea4d84d3e4d42d0e8c4235834edae652993abcd2aff94affd7", + "sha256:81a11a1ddd5320429db47c04d35119c3e674d215173d87aaeb06ae80f6e9031f", + "sha256:851fd0afb3bc0b73f7c5b5858975d42769a5fdde5314f4ef2c106aec63100687", + "sha256:85a1f92a02f0b8c1bf02699731a70a8a74402bb3f82bee36e7768b19a8ed9709", + "sha256:89d795c1802d99a643bf689b277e8604c14b5af1bc0a31dade2cd7a678087212", + "sha256:9202c0010c78fad1041e1c5285232b6508d3633f92825687549540a70e9e5901", + "sha256:939a16849d71203628157a5e4a495da63967c744e1e32018e9b9e2689aca64d4", + "sha256:93b8c2008f372379fb6e5d2b3f7c9ec32f7b80316543fd3a5ace6610c5cde1b0", + "sha256:94c1c02721139fe9940b38d28fb15b4b782981d800d5f40f9966264fbf23dcc8", + "sha256:9895df6cd0bfe79d09bcd1dbdc03862846f26fbd93797153de954306620c1d00", + "sha256:9cc7f35dcb49a4e32db82a849fcc0714c4d4acc9d2273aded2d61f87d7f660b7", + "sha256:9ed02c604349068d46d87ef4c2012c112c791f2bec08671903a6bb2bd9c06784", + "sha256:a00e1e587c655749afb5b135d8d3edcfe84ec6db864201e40a882e64168610b3", + "sha256:a1ab8f0e0cadc5be5f3f9fa11a663957fecbf483d434762c8dfb8aa44948944a", + "sha256:a4de299c947a54fca9ce1c5fd4a08eb92ffce91961becb13bd9195f7c6e71b47", + "sha256:a7fbf2a8fe7556a8f4e68cb3e736884af7bf93653e79f6219f17ebb75e97d8f0", + "sha256:ad4fa707ff9e2ffee019e946257b5300a45137a58f41fbd9a4db8e684ab61528", + "sha256:ad818cdac37c0ad4c58e51cb4964eae4f18b43c4a83cb37170b0d90c31bd80cf", + "sha256:addf0a16e4983280efed272d8cb3b2e05f0051755372461e7d966b80a6554e16", + "sha256:ae7a519a56a714f64c3445cabde9fc2fc927e7eae44f413eae187cddd9e54178", + "sha256:b32f38bc81170fd56d0482d505b556e52bf9078b36819a8ba52624bd6667e39e", + "sha256:b5407c34776b9b77bd89a5f95eb0a34aaf91889e3f911c63f13035220eb50107", + "sha256:b7bf950234a482b7461afdb2ec99eee3548ec4d53f418c7990bb79c620476602", + "sha256:b89849171b590107f6724a7b0790736daead40926ddf47eadf998b4ff51d6414", + "sha256:bcea3eb58c09c3a31cc83b45c06d5907f02ddaf10920aaa6443975310f699b95", + "sha256:bd4ba86513430513e2aa25a441bb538f6f83734dc368a2c5d18afdd39097aa33", + "sha256:bf8eb5dca4f484a60f5327b044e842e0d7f7cdbf02ea6dc4a4f811259f1f1f0b", + "sha256:c026ee729c4ce55708a14b839ba35086dfae265fc12813b62d34ce33f4980c1c", + "sha256:c210d1460dc8d326ffdef9703c2f83269b7539a1690ad11ae04162bc1878d33d", + "sha256:c8feb8e19ef65c9994e652c5b0324abd657bedd0abeb946fb4f5163012c1e730", + "sha256:cbac2eb7ce0fac755fb983c9247c4a60c4019bcde4c0e4d167aeb17520cc7ef1", + "sha256:cbfe82a07596a044de78bb7a62519e71690c5812c26c5f1d4b877e64e4f46309", + "sha256:d3f3d2e20c442b58dbac593cb1e02bc02d149a86056cc4126d977ad902472e3b", + "sha256:d42a818e634f789350cd8fb413a3f5eec1cf0400a53d02062534c41519f5125c", + "sha256:d4b83cf7354cbbc058e97b3e545dceb75b8d9cf17fd5a19db419c319ddbaaf7a", + "sha256:d9726d2c9bd6aed8cb994d89b3910ca0079406edce3670886ec828a73e7bdd53", + "sha256:da7e501e59857e8e3e9d10586139dc196b80445a591451ca9998aafba1af5278", + "sha256:da7e918d82e7bdfc6f66d31febe1b2e28a1ca3387315f918de26f5e367f61572", + "sha256:dbbac01e80aee253d44c4f098ab3cc17c822518519e869b284cfbb8cd16cc9de", + "sha256:df5c0eff91f61b8205a6c9f7b255ff390cdb77b61c7b41f79ca10afcbb22b6cb", + "sha256:e07e76c49f39c5b45cbd7362b94f001ae209a3ea4905ae9a09cfd53b3c76373d", + "sha256:e1e10b3fbed7be4a59831d3a939900e50fcd34d93716e433d4193a4d0d1d335d", + "sha256:e39d393e0ab5b8bd01717cc26f2922026050188947ff54fe6a49dc489f7750b7", + "sha256:e5ba5e9b332267d0f2c33ede390061850f1ac3ee6cd1bdcf4c5ea33ead971966", + "sha256:e7a1963302947332c3039e3f66209ec73b1626f8a0191649e0713c391e9f5b0d", + "sha256:e7fcad070dcd9ad37a09d89a4cbc2a5e3e45080b88977c0da87b3090f9f55ead", + "sha256:eae368cac85adc4c7dc3b0d5f84ffcca609d658db6447387300478e44db70796", + "sha256:ede95125a30602b1691a4b1da88946bf27dae283cf30f22cd2cb8ca4b2e0d119", + "sha256:f5737c53eb2c8ed8f64b50d3dafd3c1dae739f78aa495a288421ac1b3de82717", + "sha256:f5f9d23fbbf96eefde836d9692670bfc89e2d159f456d499c5efcf6a6281c1af", + "sha256:f66e00e42f25ca7e91076366303e11c82572ca87cc5aae51e6e9c094f315ab41", + "sha256:f9af457ed593e35f467140d8b61d425495b127744a9d65d45a366f8678449a23", + "sha256:fa0839f35322f7b038d8adcf679e2698c3a483688cc92e3bd15ee4fb06669e9a", + "sha256:fd038bc9e2c134847f1e0ce3191797fad110756e690c2fdd9702ed34e7a43abb" + ], + "index": "pypi", + "markers": "python_version >= '3.8'", + "version": "==13.0" + } + }, + "develop": {} +} diff --git a/client.py b/client.py new file mode 100644 index 0000000..4543d84 --- /dev/null +++ b/client.py @@ -0,0 +1,62 @@ +import asyncio +import websockets +import hashlib + +LOCALHOST = '127.0.0.1' +PORT = 8443 +WEBSOCKET_URL = 'ws://localhost:9999/data' + +ALREADY_DONE = False + +async def handle_client(client_reader, client_writer): + global ALREADY_DONE + if ALREADY_DONE: + print('Will ret a 2nd time') + return + ALREADY_DONE = True + + try: + async with websockets.connect(WEBSOCKET_URL) as websocket: + # Forwarding data from client to WebSocket + async def client_to_websocket(): + while True: + data = await client_reader.read(2024) + print('TCP>WS: ', hashlib.md5(data).hexdigest()) + if not data: + break + await websocket.send(data) + + # Forwarding data from WebSocket to client + async def websocket_to_client(): + while True: + message = await websocket.recv() + print('WS>TCP: ', hashlib.md5(message).hexdigest()) + client_writer.write(message) + await client_writer.drain() + + # Run both tasks concurrently + await asyncio.gather(client_to_websocket(), websocket_to_client()) + + except Exception as e: + print(f"Error: {e}") + finally: + client_writer.close() + await client_writer.wait_closed() + +async def main(): + port = PORT + while True: + try: + server = await asyncio.start_server(handle_client, LOCALHOST, port) + break + except OSError: + port += 1 + async with server: + print(f"Server started on {LOCALHOST}:{port}") + await server.serve_forever() + +if __name__ == "__main__": + asyncio.run(main()) + + + diff --git a/pod.py b/pod.py new file mode 100644 index 0000000..0523557 --- /dev/null +++ b/pod.py @@ -0,0 +1,55 @@ +import asyncio +import socket +import websockets +import hashlib + +HOST = '192.168.21.30' +PORT = 6443 +WEBSOCKET_URL = 'ws://localhost:9999/data' + +async def handle_client(tcpreader, tcpwriter, ws): + print(f"New client connected: {tcpreader} {tcpwriter}") + try: + # Forwarding data from client to WebSocket + async def tcp_to_websocket(): + print("tcp_to_websocket...") + while True: + data = await tcpreader.read(2024) + print('TCP>WS: ', hashlib.md5(data).hexdigest()) + if not data: + break + await ws.send(data) + + # Forwarding data from WebSocket to client + async def websocket_to_tcp(): + print("websocket_to_tcp...") + while True: + message = await ws.recv() + print('WS>TCP: ', hashlib.md5(message).hexdigest()) + tcpwriter.write(message) + await tcpwriter.drain() + + print("Running both tasks concurrently...") + # Run both tasks concurrently + await asyncio.gather(tcp_to_websocket(), websocket_to_tcp()) + print('done') + + except Exception as e: + print(f"ASYNCIOD Error: {e}") + +async def main(): + conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + conn.connect((HOST, PORT)) + + + tcpreader, tcpwriter = await asyncio.open_connection(HOST, PORT) + + ws = await websockets.connect(WEBSOCKET_URL) + + await handle_client(tcpreader, tcpwriter, ws) + +if __name__ == "__main__": + asyncio.run(main()) + + + diff --git a/proxy.py b/proxy.py new file mode 100644 index 0000000..3af31f3 --- /dev/null +++ b/proxy.py @@ -0,0 +1,33 @@ +import asyncio +import websockets +import hashlib + +# List to store connected clients +connected_clients = set() + +async def handler(websocket, path): + # Register the new client + print(f"New client connected: {websocket}") + connected_clients.add(websocket) + try: + async for message in websocket: + # Forward the message to all connected clients + for client in connected_clients: + if client != websocket: + print(f"WS>WS: ", hashlib.md5(message).hexdigest()) + await client.send(message) + + except websockets.exceptions.ConnectionClosed as e: + print(f"Connection closed: {e}") + finally: + # Unregister the client + connected_clients.remove(websocket) + +async def main(): + # Start the WebSocket server + server = await websockets.serve(handler, "localhost", 9999) + print("WebSocket server listening on ws://localhost:9999") + await server.wait_closed() + +if __name__ == "__main__": + asyncio.run(main())