Skip to content

Instantly share code, notes, and snippets.

@kartoch
Last active April 12, 2023 17:59
Show Gist options
  • Save kartoch/c9409a802703282d740ad275e4f5f504 to your computer and use it in GitHub Desktop.
Save kartoch/c9409a802703282d740ad275e4f5f504 to your computer and use it in GitHub Desktop.
UDP server behind traefik

UDP server behind traefik with SSL encryption

Build and start the UDP server and Traefik

docker compose up

Start the UDP client

python udp_client.py IP_ADDRESS 31337
services:
traefik:
image: "traefik:v2.9.10"
command:
- --providers.docker
- --api.insecure=true
- --entryPoints.web.address=:80
- --entryPoints.websecure.address=:443
- --entryPoints.traefik.address=:8080
- --entryPoints.streaming.address=:31337/udp
networks:
- net-traefik
ports:
- "80:80"
- "443:443"
- "31337:31337/udp"
- "8080:8080"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock:ro"
labels:
- "traefik.docker.network=net-traefik"
whoami:
image: traefik/whoami
networks:
- "net-traefik"
labels:
- "traefik.enable=true"
- "traefik.docker.network=net-traefik"
streaming:
build: .
networks:
- net-traefik
labels:
- "traefik.enable=true"
- "traefik.docker.network=net-traefik"
- "traefik.udp.services.streaming.loadBalancer.server.port=31337"
- "traefik.udp.routers.streaming.entrypoints=streaming"
- "traefik.udp.routers.streaming.service=streaming"
networks:
net-traefik:
name: "net-traefik"
FROM python:3
WORKDIR /usr/src/app
COPY udp_server.py .
CMD [ "python", "./udp_server.py" ]
import socket
import sys
client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_address = sys.argv[1]
server_port = int(sys.argv[2])
client_socket.connect((server_address, server_port))
message = 'Hello World'
client_socket.send(message.encode())
response = client_socket.recv(1024).decode()
print("Test passed" if message == response else "Test failed")
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_address = '0.0.0.0'
server_port = 31337
server = (server_address, server_port)
sock.bind(server)
print("Listening on ", server_address, ":", str(server_port), flush=True)
while True:
payload, client_address = sock.recvfrom(1000)
print("Echoing data back to ", str(client_address), ": ", payload)
sent = sock.sendto(payload, client_address)
print("Sent results: ", str(sent), flush=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment