diff options
-rw-r--r-- | README | 8 | ||||
-rwxr-xr-x | msshpd | 67 |
2 files changed, 75 insertions, 0 deletions
@@ -0,0 +1,8 @@ +msshpd + + My Simple Socket HTTP Proxy Daemon. + + +LICENSE + + MIT @@ -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) |