You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
168 lines
5.3 KiB
168 lines
5.3 KiB
import asyncio
|
|
import threading
|
|
from scapy.all import *
|
|
import argparse
|
|
|
|
DEBUG=False
|
|
|
|
def print_dbg(s):
|
|
if DEBUG:
|
|
print(s)
|
|
|
|
class NGServer:
|
|
def __init__(self, bridge_ip='127.0.0.1', bridge_port=8000,
|
|
ioip='127.0.0.1',
|
|
incoming_port=8001, outgoing_port=8002):
|
|
self.incoming_queue = None # asyncio.Queue()
|
|
self.outgoing_queue = None # asyncio.Queue()
|
|
self.client_reader = None
|
|
self.client_writer = None
|
|
self.server_reader = None
|
|
self.server_writer = None
|
|
self.server_thread = None
|
|
|
|
self.bridge_ip = bridge_ip
|
|
self.bridge_port = bridge_port
|
|
self.ioip = ioip
|
|
self.incoming_port = incoming_port
|
|
self.outgoing_port = outgoing_port
|
|
|
|
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):
|
|
self.incoming_queue = asyncio.Queue()
|
|
while True:
|
|
data = await self.incoming_queue.get()
|
|
writer.write(data)
|
|
await writer.drain()
|
|
|
|
async def handle_outgoing(self, reader, writer):
|
|
self.server_reader = reader
|
|
self.server_writer = writer
|
|
print_dbg("handle_outgoing - ENTER")
|
|
self.outgoing_queue = asyncio.Queue()
|
|
while True:
|
|
print_dbg("handle_outgoing - IN LOOP")
|
|
data = await reader.read(1024)
|
|
print_dbg("handle_outgoing - IP(data))={}".format(
|
|
IP(data)))
|
|
self.client_writer.write(data)
|
|
await self.client_writer.drain()
|
|
|
|
async def read_from_server(self):
|
|
print_dbg("read_from_server - ENTER")
|
|
while True:
|
|
# Switch reader to writer
|
|
print_dbg("read_from_server - IN LOOP")
|
|
data = await self.client_reader.read(1024)
|
|
print_dbg("read_from_server - IP(data))={}".format(
|
|
IP(data)))
|
|
if not data:
|
|
self.client_writer.close()
|
|
break
|
|
await self.outgoing_queue.put(data)
|
|
|
|
async def start_server(self):
|
|
server = await asyncio.start_server(
|
|
self.handle_client,
|
|
host=self.bridge_ip,
|
|
port=self.bridge_port)
|
|
async with server:
|
|
await server.serve_forever()
|
|
|
|
async def start_incoming_server(self):
|
|
server = await asyncio.start_server(
|
|
self.handle_incoming,
|
|
host=self.ioip,
|
|
port=self.incoming_port)
|
|
async with server:
|
|
await server.serve_forever()
|
|
|
|
async def start_outgoing_server(self):
|
|
server = await asyncio.start_server(
|
|
self.handle_outgoing,
|
|
host=self.ioip,
|
|
port=self.outgoing_port)
|
|
async with server:
|
|
await server.serve_forever()
|
|
|
|
async def start_thread(ng):
|
|
""" """
|
|
await asyncio.gather(
|
|
ng.start_server(),
|
|
ng.start_incoming_server(),
|
|
ng.start_outgoing_server(),
|
|
)
|
|
|
|
await asyncio.gather(
|
|
ng.read_from_server(),
|
|
ng.handle_incoming(),
|
|
ng.handle_outgoing(),
|
|
)
|
|
|
|
def run_thread(ng):
|
|
asyncio.run(start_thread(ng))
|
|
|
|
|
|
def startServerAndWait(args):
|
|
ng = NGServer(bridge_ip=args.bridge_ip,
|
|
bridge_port=args.bridge_port,
|
|
ioip=args.ioip,
|
|
incoming_port=args.incoming_port,
|
|
outgoing_port=args.outgoing_port)
|
|
ng.server_thread = threading.Thread(
|
|
target=run_thread,
|
|
args=(ng,))
|
|
ng.server_thread.start()
|
|
ng.server_thread.join()
|
|
|
|
def startThread(ng):
|
|
|
|
ng.server_thread = threading.Thread(
|
|
target=run_thread,
|
|
args=(ng,))
|
|
ng.server_thread.start()
|
|
|
|
def connectIO(inaddr='127.0.0.1', inport=8001, outaddr='127.0.0.1', outport=8002):
|
|
insock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
|
|
insock.connect((inaddr, inport))
|
|
outsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
|
|
outsock.connect((outaddr, outport))
|
|
return insock, outsock
|
|
|
|
|
|
def parseArgs():
|
|
args = argparse.ArgumentParser()
|
|
# The bridge ip and port
|
|
args.add_argument("-B","--bridge_ip", default='127.0.0.1',
|
|
help='The IP that listens for NetGenie mobile app client connections.')
|
|
args.add_argument("-b","--bridge_port", default=8000, help='The port that listens for NetGenie mobile app client connections.')
|
|
|
|
# The IO ip and ports
|
|
args.add_argument("-I","--ioip", default='127.0.0.1', help='The IP that listens on the server for connections from whoever is debugging the phone.')
|
|
args.add_argument("-i","--incoming_port", default=8001,
|
|
help='The port that the person debugging will read packets from the device.')
|
|
args.add_argument("-o","--outgoing_port", default=8002,
|
|
help='The port that the person debugging will write packets to the device.')
|
|
return args.parse_args()
|
|
|
|
|
|
def main():
|
|
"""
|
|
The NGServer class implements our NetGenie debug/bridge server.
|
|
"""
|
|
# Get the arguments
|
|
args = parseArgs()
|
|
# Start the server.
|
|
startServerAndWait(args)
|
|
|
|
if __name__ == '__main__':
|
|
main()
|