BB
2 years ago
2 changed files with 80 additions and 32 deletions
@ -1 +1 @@ |
|||
Subproject commit 0350e46d6dbe398dc395675e348121fe3ae6624a |
|||
Subproject commit e3bbfed2c3548f815d740c9ae261329d708f6d9c |
@ -1,41 +1,89 @@ |
|||
#!/bin/env python3 |
|||
import asyncio |
|||
|
|||
def check_bridge_socket():: |
|||
evt = None |
|||
return evt |
|||
class NGServer: |
|||
def __init__(self): |
|||
self.incoming_queue = asyncio.Queue() |
|||
self.outgoing_queue = asyncio.Queue() |
|||
self.client_reader = None |
|||
self.client_writer = None |
|||
|
|||
def handle_bridge_evt(evt): |
|||
""" |
|||
TODO: Whatever bridge events to handle |
|||
""" |
|||
async def handle_client(self, reader, writer): |
|||
self.client_reader = reader |
|||
self.client_writer = writer |
|||
while True: |
|||
data = await reader.read(1024) |
|||
if not data: |
|||
writer.close() |
|||
break |
|||
await self.incoming_queue.put(data) |
|||
|
|||
async def handle_incoming(self, reader, writer): |
|||
while True: |
|||
data = await self.incoming_queue.get() |
|||
writer.write(data) |
|||
await writer.drain() |
|||
|
|||
def check_debug_socket(): |
|||
dbg_evt = None |
|||
return dbg_evt |
|||
async def handle_outgoing(self, reader, writer): |
|||
while True: |
|||
data = await self.outgoing_queue.get() |
|||
writer.write(data) |
|||
await writer.drain() |
|||
|
|||
def handle_debug_evt(dbg_evt): |
|||
""" TODO: Do something with the events here.""" |
|||
async def read_from_server(self): |
|||
while True: |
|||
data = await self.client_reader.read(1024) |
|||
if not data: |
|||
self.client_writer.close() |
|||
break |
|||
await self.outgoing_queue.put(data) |
|||
|
|||
def run_loop(): |
|||
""" """ |
|||
while True: |
|||
# 1. Check the BridgeSocket |
|||
evt = check_bridge_socket() |
|||
if evt != None: |
|||
handle_bridge_evt(evt) |
|||
dbg_evt = check_debug_socket() |
|||
if dbg_evt != None: |
|||
handle_debug_evt(dbg_evt) |
|||
def main(): |
|||
""" |
|||
1. TODO: Open Server port that the device connects to. |
|||
2. TODO: Open Server port that an admin connects to, i.e., to test sending packets to |
|||
3. TODO (Optional): We might be able to start a Scapy [REPL](https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop) |
|||
and then add these functions. That way the person in debugging the phone can do live testing. |
|||
async def start_server(self): |
|||
server = await asyncio.start_server(self.handle_client, host='localhost', port=8000) |
|||
async with server: |
|||
await server.serve_forever() |
|||
|
|||
async def start_incoming_server(self): |
|||
server = await asyncio.start_server(self.handle_incoming, host='localhost', port=8001) |
|||
async with server: |
|||
await server.serve_forever() |
|||
|
|||
async def start_outgoing_server(self): |
|||
server = await asyncio.start_server(self.handle_outgoing, host='localhost', port=8002) |
|||
async with server: |
|||
await server.serve_forever() |
|||
|
|||
async def do_async_stuff(ng): |
|||
await asyncio.gather(ng.start_server(),ng.start_queue_server()) |
|||
|
|||
|
|||
async def main(): |
|||
""" |
|||
In this code, we're creating and running four separate coroutines: |
|||
|
|||
1. start_server |
|||
2. start_incoming_server |
|||
3. start_outgoing_server |
|||
4. read_from_server. |
|||
|
|||
We're also creating separate tasks for handling incoming and |
|||
outgoing data and adding them to the tasks list. |
|||
|
|||
""" |
|||
ng = NGServer() |
|||
await asyncio.gather( |
|||
ng.start_server(), |
|||
ng.start_incoming_server(), |
|||
ng.start_outgoing_server(), |
|||
) |
|||
""" |
|||
tasks = [ |
|||
ng.read_from_server(), |
|||
ng.handle_incoming(), |
|||
ng.handle_outgoing(), |
|||
] |
|||
await asyncio.gather(*tasks) |
|||
""" |
|||
|
|||
if __name__ == '__main__': |
|||
main() |
|||
def example(): |
|||
""" """ |
|||
# asyncio.run(main()) |
Write
Preview
Loading…
Cancel
Save
Reference in new issue