commit 8e8eae11b584cb0e762ee1597bf2ba7cd2ee633b Author: ge Date: Sat Jul 1 20:55:17 2023 +0300 init diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..2c77eec --- /dev/null +++ b/Dockerfile @@ -0,0 +1,13 @@ +FROM debian:stable-slim +COPY wstunnel.conf /etc/nginx/conf.d/ +COPY entrypoint.sh /usr/local/bin/entrypoint.sh +RUN chmod +x /usr/local/bin/entrypoint.sh \ + && apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y wget nginx \ + && wget -O /usr/local/bin/wstunnel \ + https://github.com/erebe/wstunnel/releases/download/v5.0/wstunnel-linux-x64 \ + && chmod +x /usr/local/bin/wstunnel \ + && rm -vf /etc/nginx/sites-available/default /etc/nginx/sites-enabled/default +ENV WS_PATH_PREFIX=${WS_PATH_PREFIX:-} +ENV SERVER_HOST=${SERVER_HOST:-localhost} +ENV SERVER_PORT=${SERVER_PORT:-8080} +ENTRYPOINT ["/usr/local/bin/entrypoint.sh"] diff --git a/README.md b/README.md new file mode 100644 index 0000000..03b5f0b --- /dev/null +++ b/README.md @@ -0,0 +1,30 @@ +# wstunnel with Nginx + +[wstunnel](https://github.com/erebe/wstunnel) with nginx as reverse proxy. + +Run server: + +``` +docker run -d \ + --name wstunnel \ + -p 8080:80 \ + -e SERVER_PORT=8080 \ + -e WS_PATH_PREFIX=md77sthqtq-o_ptobrd65n0ol56fk5bz \ + nxhs/wstunnel +``` + +Connect: + +``` +wstunnel --upgradePathPrefix md77sthqtq-o_ptobrd65n0ol56fk5bz -D 1080 ws://localhost:8080 +``` + +Environment: + +| Variable | Default | Description | +| --- | --- | --- | +| `SERVER_PORT` | Empty | Port what you want to publish | +| `SERVER_HOST` | localhost | Your server IP or domain name | +| `WS_PATH_PREFIX` | Random | Path prefix for websocket. Used as shared secret | + +See `docker logs wstunnel` to view generated client connection command. diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..5bbe298 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +[ -n "$WS_PATH_PREFIX" ] || WS_PATH_PREFIX="$(< /dev/urandom tr -dc 'a-z0-9\-_' | head -c32)" +sed -i /etc/nginx/conf.d/wstunnel.conf -e "s/{{ws_path_prefix}}/$WS_PATH_PREFIX/" + +cat << EOF +------------------------------------------------------------------------------ + +Client connection command: +wstunnel --upgradePathPrefix $WS_PATH_PREFIX -D 1080 ws://$SERVER_HOST:$SERVER_PORT + +------------------------------------------------------------------------------ +EOF + +set -m +nginx -g "daemon off;" & +wstunnel --server ws://0.0.0.0:8080 diff --git a/wstunnel.conf b/wstunnel.conf new file mode 100644 index 0000000..aa79c7a --- /dev/null +++ b/wstunnel.conf @@ -0,0 +1,13 @@ +server { + listen 80 default_server; + server_name _; + location / { + return 404; + } + location /{{ws_path_prefix}}/ { + proxy_pass http://127.0.0.1:8080; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + } +}