Skip to content

Instantly share code, notes, and snippets.

@metacoma
Created April 11, 2024 09:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save metacoma/1ada20a93a679553a43335702e7b96e1 to your computer and use it in GitHub Desktop.
Save metacoma/1ada20a93a679553a43335702e7b96e1 to your computer and use it in GitHub Desktop.
wstunnel reverse proxy example
# Save this content to the file docker-compose.yml
# docker-compose pull
# docker-compose up
# tldr
# client1 ---.
# --- internet --- aws-host:8081 --- wstunnel reverse tunnel --- internet --- wstunnel --- websocket-application:8080
# client2 ---`
version: '3'
x-websocket-application-ip: &websocket_lan_ip 10.5.0.100
x-websocket-application-port: &websocket_lan_port 8080
x-external-host: &external_host aws-host
x-external-port: &external_port 8888
x-external-websocket-application-port: &external_webapplcation_port 8081
networks:
server-lan:
# server-lan is a network with internet access, but without the ability to forward traffic from the internet inside
driver: bridge
ipam:
driver: default
config:
- subnet: 10.5.0.0/24
gateway: 10.5.0.1
internet:
services:
# workload with websocket application connected to server-lan network
websocket-application:
image: 'skandyla/go-websocket-echo-server'
environment:
PORT: *websocket_lan_port
networks:
server-lan:
ipv4_address: *websocket_lan_ip
# reverse proxy tunnel to amazon host, it connect to the amazon host on port 8888
# and opens a tcp socket on port 8081, to which all connections will be forwarded
# to websocket-application:8080
websocket-application-sidecar:
image: 'ghcr.io/erebe/wstunnel:latest'
networks:
server-lan:
ipv4_address: 10.5.0.7
internet: {}
environment:
WEBSOCKET_LAN_SERVER: *websocket_lan_ip
WEBSOCKET_LAN_PORT: *websocket_lan_port
EXTERNAL_HOST: *external_host
EXTERNAL_PORT: *external_port
EXTERNAL_WEBAPPLICATION_PORT: *external_webapplcation_port
entrypoint:
- /bin/sh
- -c
- ./wstunnel client -R tcp://0.0.0.0:$${EXTERNAL_WEBAPPLICATION_PORT}:$${WEBSOCKET_LAN_SERVER}:$${WEBSOCKET_LAN_PORT} wss://$${EXTERNAL_HOST}:$${EXTERNAL_PORT}
# aws host listens on port 8888
aws-host:
image: 'ghcr.io/erebe/wstunnel:latest'
networks:
- internet
environment:
EXTERNAL_PORT: *external_port
entrypoint:
- /bin/sh
- -c
- ./wstunnel server wss://[::]:$${EXTERNAL_PORT}
# Client1 connected to the internet sends websocket requests to the aws-host:8081, which are forwarded to the websocket-application:8080
client1:
image: 'ghcr.io/vi/websocat:latest'
networks:
- internet
environment:
EXTERNAL_HOST: *external_host
EXTERNAL_PORT: *external_webapplcation_port
entrypoint:
- /bin/sh
- -c
- while :; do echo hello from client1 `date` | websocat ws://$${EXTERNAL_HOST}:$${EXTERNAL_PORT}; sleep 1; done
# Client2 connected to the internet sends websocket requests to the aws-host:8081, which are forwarded to the websocket-application:8080
client2:
image: 'ghcr.io/vi/websocat:latest'
networks:
- internet
environment:
EXTERNAL_HOST: *external_host
EXTERNAL_PORT: *external_webapplcation_port
entrypoint:
- /bin/sh
- -c
- while :; do echo hello from client2 `date` | websocat ws://$${EXTERNAL_HOST}:$${EXTERNAL_PORT}; sleep 1; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment