From dbc9e6b36b84a83722cb641f6bf668412b6a8180 Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Sun, 8 Aug 2021 21:30:01 +0300 Subject: initial --- README | 8 ++++++++ msshpd | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 README create mode 100755 msshpd diff --git a/README b/README new file mode 100644 index 0000000..e058a67 --- /dev/null +++ b/README @@ -0,0 +1,8 @@ +msshpd + + My Simple Socket HTTP Proxy Daemon. + + +LICENSE + + MIT diff --git a/msshpd b/msshpd new file mode 100755 index 0000000..cb17a96 --- /dev/null +++ b/msshpd @@ -0,0 +1,67 @@ +#!/usr/bin/env python3 +import logging, json, socket + +from aiohttp import web +from argparse import ArgumentParser + +routes = web.RouteTableDef() +mss_host = None +mss_port = None + + +class MySimpleSocketClient: + def __init__(self, port, host): + self._host = host + self._port = port + self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + self.sock.connect((self._host, self._port)) + + def __del__(self): + self.sock.close() + + def write(self, line): + self.sock.sendall((line+'\r\n').encode()) + + def read(self): + buf = bytearray() + while True: + buf.extend(self.sock.recv(256)) + if b'\r\n' in buf: + break + + response = buf.decode().strip() + return response + + +@routes.get('/{command}/') +async def variable_handler(request): + command = request.match_info['command'] + + client = MySimpleSocketClient(host=mss_host, port=mss_port) + client.write(command) + + try: + response = {'data': client.read()} + except Exception as e: + response = {'error': str(e)} + + return web.json_response(response, dumps=lambda x: json.dumps(x, separators=(',', ':'))) + + +if __name__ == '__main__': + parser = ArgumentParser() + parser.add_argument('--mss-host', required=True, type=str) + parser.add_argument('--mss-port', required=True, type=int) + parser.add_argument('--host', default='0.0.0.0', type=str) + parser.add_argument('--port', default=8080, type=int) + args = parser.parse_args() + + mss_host = args.mss_host + mss_port = args.mss_port + + logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', + level=logging.INFO) + + app = web.Application() + app.add_routes(routes) + web.run_app(app, host=args.host, port=args.port) -- cgit v1.2.3