Formatting.

This commit is contained in:
retoor 2024-12-08 10:34:11 +01:00
parent 0dffc6bdd8
commit 415c16b4ef
3 changed files with 61 additions and 46 deletions

View File

@ -1,9 +1,8 @@
import logging
from mololog.client import patch
logging.basicConfig(
level = logging.INFO
)
logging.basicConfig(level=logging.INFO)
log = logging.getLogger()
patch("https://mololog.molodetz.nl/")

View File

@ -1,6 +1,8 @@
from shadowssh.app import Application
import asyncio
import argparse
import asyncio
from shadowssh.app import Application
def parse_args():
parser = argparse.ArgumentParser(description="Shadowssh")
@ -17,9 +19,13 @@ def parse_args():
def serve():
args = parse_args()
app = Application()
asyncio.run(app.serve(args.host,args.port,args.host_service,args.port_service,args.host_ssh,args.port_ssh))
asyncio.run(
app.serve(
args.host,
args.port,
args.host_service,
args.port_service,
args.host_ssh,
args.port_ssh,
)
)

View File

@ -1,10 +1,21 @@
import asyncio
from app.app import Application as BaseApplication
from shadowssh import log
class Application(BaseApplication):
def __init__(self, host_forward_service=None, port_forward_service=None, host_forward_ssh=None, port_forward_ssh=None, *args,**kwargs):
def __init__(
self,
host_forward_service=None,
port_forward_service=None,
host_forward_ssh=None,
port_forward_ssh=None,
*args,
**kwargs,
):
self.host_forward_service = host_forward_service
self.port_forward_service = port_forward_service
self.host_forward_ssh = host_forward_ssh
@ -12,7 +23,7 @@ class Application(BaseApplication):
super().__init__(*args, **kwargs)
async def connect(self, host, port):
log.info("Connected to {}:{}.".format(host,port))
log.info(f"Connected to {host}:{port}.")
reader, writer = await asyncio.open_connection(host, port)
return reader, writer
@ -28,20 +39,19 @@ class Application(BaseApplication):
async def forward(self, reader, writer):
data = await reader.read(10)
if not data:
return
port = None
host = None
if b'SSH-2.0' in data:
if b"SSH-2.0" in data:
port = self.port_forward_ssh
host = self.host_forward_ssh
else:
port = self.port_forward_service
host = self.host_forward_service
log.info("Streaming to port: {}.".format(port))
log.info(f"Streaming to port: {port}.")
upstream_reader, upstream_writer = await self.connect(host, port)
upstream_writer.write(data)
tasks = []
@ -49,8 +59,6 @@ class Application(BaseApplication):
tasks.append(asyncio.create_task(self.stream(upstream_reader, writer, port)))
await asyncio.gather(*tasks)
async def route(self, host, port):
async def handle_client(reader, writer):
@ -58,21 +66,23 @@ class Application(BaseApplication):
server = await asyncio.start_server(handle_client, host, port)
addr = server.sockets[0].getsockname()
log.info(f'Serving on {addr}')
log.info(f"Serving on {addr}")
async with server:
await server.serve_forever()
async def serve(self, host,port,host_forward_service, port_forward_service, host_forward_ssh, port_forward_ssh):
async def serve(
self,
host,
port,
host_forward_service,
port_forward_service,
host_forward_ssh,
port_forward_ssh,
):
self.host_forward_service = host_forward_service
self.port_forward_service = port_forward_service
self.host_forward_ssh = host_forward_ssh
self.port_forward_ssh = port_forward_ssh
tasks = [
self.route(host,port)
]
tasks = [self.route(host, port)]
await asyncio.gather(*tasks)