diff --git a/client/build.sh b/client/build.sh index eeddbbc..c5f343c 100755 --- a/client/build.sh +++ b/client/build.sh @@ -16,6 +16,7 @@ EMSCRIPTEN_OPTIONS="-lwebsocket.js -sASYNCIFY -sALLOW_TABLE_GROWTH -sEXPORTED_FU if [ "$1" = "release" ]; then COMPILER_OPTIONS="-O3 $COMPILER_OPTIONS" + EMSCRIPTEN_OPTIONS="-sSINGLE_FILE $EMSCRIPTEN_OPTIONS" fi #ensure deps are compiled diff --git a/client/main.c b/client/main.c index c94b0d3..c2f4110 100644 --- a/client/main.c +++ b/client/main.c @@ -24,8 +24,6 @@ int write_function(void *data, size_t size, size_t nmemb, DataCallback data_call } void perform_request(const char* url, const char* json_params, DataCallback data_callback, EndCallback end_callback, const char* body, int body_length) { - printf("downloading %s\n", url); - CURL *http_handle; CURLM *multi_handle; int still_running = 1; diff --git a/client/main.js b/client/main.js index d8ff917..03548cb 100644 --- a/client/main.js +++ b/client/main.js @@ -4,7 +4,7 @@ window.libcurl = (function() { //emscripten compiled code is inserted here /* __emscripten_output__ */ -const websocket_url = `wss://${location.hostname}/ws`; +const websocket_url = `wss://${location.hostname}/ws/`; const status_messages = { 100: "Continue", diff --git a/server/main.py b/server/main.py index 60ea254..8bf4bc8 100644 --- a/server/main.py +++ b/server/main.py @@ -1,45 +1,55 @@ -import logging -import os +import asyncio +from websockets.server import serve +from websockets.exceptions import ConnectionClosed -from asyncio_socks_server.app import SocksServer -from websockify.websocketproxy import WebSocketProxy +buffer_size = 64*1024 -#start a socks5 proxy as well as websockify +class Connection: + def __init__(self, ws, path): + self.ws = ws + self.path = path -def setup_logging(prefix): - stderr_handler = logging.StreamHandler() - stderr_handler.setLevel(logging.DEBUG) - log_formatter = logging.Formatter(prefix + "%(message)s") - stderr_handler.setFormatter(log_formatter) - root = logging.getLogger() - root.addHandler(stderr_handler) - root.setLevel(logging.INFO) + async def setup_connection(self): + addr_str = self.path.split("/")[-1] + self.tcp_host, self.tcp_port = addr_str.split(":") + self.tcp_port = int(self.tcp_port) -def start_websockify(listen_port, proxy_port): - options = { - "listen_host": "127.0.0.1", - "listen_port": int(listen_port), - "target_host": "127.0.0.1", - "target_port": int(proxy_port) - } + self.tcp_reader, self.tcp_writer = await asyncio.open_connection(host=self.tcp_host, port=self.tcp_port, limit=buffer_size) - server = WebSocketProxy(**options) - server.start_server() + async def handle_ws(self): + while True: + try: + data = await self.ws.recv() + except ConnectionClosed: + break + self.tcp_writer.write(data) + await self.tcp_writer.drain() + print("sent data to tcp") + + self.tcp_writer.close() + + async def handle_tcp(self): + while True: + data = await self.tcp_reader.read(buffer_size) + if len(data) == 0: + break #socket closed + await self.ws.send(data) + print("sent data to ws") + + await self.ws.close() -def start_socks(proxy_port): - socks_app = SocksServer( - LISTEN_HOST="127.0.0.1", - LISTEN_PORT=int(proxy_port) - ) - socks_app.run() +async def connection_handler(websocket, path): + print("incoming connection from "+path) + connection = Connection(websocket, path) + await connection.setup_connection() + ws_handler = asyncio.create_task(connection.handle_ws()) + tcp_handler = asyncio.create_task(connection.handle_tcp()) + + await asyncio.gather(ws_handler, tcp_handler) + +async def main(): + async with serve(connection_handler, "127.0.0.1", 6001): + await asyncio.Future() if __name__ == "__main__": - listen_port = os.environ.get("PORT") or 6001 - proxy_port = os.environ.get("SOCKS5_PORT") or 6002 - - pid = os.fork() - if pid == 0: - setup_logging("[websockify] ") - start_websockify(listen_port, proxy_port) - else: - start_socks(proxy_port) \ No newline at end of file + asyncio.run(main()) diff --git a/server/requirements.txt b/server/requirements.txt index 3241b52..7a38911 100644 --- a/server/requirements.txt +++ b/server/requirements.txt @@ -1,2 +1 @@ -websockify -asyncio-socks-server \ No newline at end of file +websockets \ No newline at end of file diff --git a/server/run.sh b/server/run.sh index 011d553..ca6acd2 100755 --- a/server/run.sh +++ b/server/run.sh @@ -9,11 +9,8 @@ if [ ! -d ".venv" ]; then fi source .venv/bin/activate -if ! python3 -c "import asyncio_socks_server, websockify" 2> /dev/null; then - pip3 install asyncio-socks-server - git clone https://github.com/novnc/websockify -b master --depth=1 - pip3 install ./websockify - rm -rf websockify +if ! python3 -c "import websockets" 2> /dev/null; then + pip3 install -r requirements.txt fi python3 main.py \ No newline at end of file