Fixed logic and added variables

This commit is contained in:
2025-09-24 13:10:27 +02:00
parent 5176ddeae9
commit b020a30429
4 changed files with 35 additions and 24 deletions

8
.env
View File

@@ -1,4 +1,6 @@
# Socat Proxy Configuration
TARGET_HOST=
TARGET_PORT=
SOCKET_PATH=
TARGET_HOST= # Target hostname/IP to proxy to
TARGET_PORT= # Target port to proxy to
UNIX_SOCKET_NAME= # Name of the socket file
UNIX_SOCKET_PATH= # Path to UNIX socket inside container
HOST_SOCKET_PATH= # Host path for socket mounting

View File

@@ -22,8 +22,9 @@ A lightweight Docker container that creates a UNIX socket proxy to TCP connectio
|----------|---------|-------------|---------|
| `TARGET_HOST` | - | Target hostname/IP to proxy to | `socket-proxy-beszel` |
| `TARGET_PORT` | - | Target port to proxy to | `2375` |
| `UNIX_SOCKET_PATH` | - | Path to UNIX socket inside container | `/socket/docker.sock` |
| `SOCKET_PATH` | - | Host path for socket mounting | `/your/container/sock/` |
| `UNIX_SOCKET_NAME` | - | Name of the socket file | `docker.sock` |
| `UNIX_SOCKET_PATH` | - | Path to UNIX socket inside container | `/socket` |
| `HOST_SOCKET_PATH` | - | Host path for socket mounting | `/docker/beszel-agent/sock` |
## 🚢 Quick Start
@@ -37,8 +38,9 @@ cd socat-proxy
2. Configure environment variables in `.env` file:
```bash
TARGET_HOST= # Target hostname/IP to proxy to
TARGET_HOST= # Target hostname/IP to proxy to
TARGET_PORT= # Target port to proxy to
UNIX_SOCKET_NAME= # Name of the socket file
UNIX_SOCKET_PATH= # Path to UNIX socket inside container
HOST_SOCKET_PATH= # Host path for socket mounting
```
@@ -57,6 +59,7 @@ docker run -d \
--name socat-proxy \
-e TARGET_HOST=your-target-host \
-e TARGET_PORT=your-target-port \
-e UNIX_SOCKET_NAME=your-socket-name \
-e UNIX_SOCKET_PATH=your-unix-socket-path \
-e HOST_SOCKET_PATH=your-socket-host-path \
-v /your-origin-socket-path:/socket \

View File

@@ -6,9 +6,11 @@ services:
environment:
- TARGET_HOST=${TARGET_HOST:}
- TARGET_PORT=${TARGET_PORT:}
- UNIX_SOCKET_NAME=${UNIX_SOCKET_NAME}
- UNIX_SOCKET_PATH=${UNIX_SOCKET_PATH}
- HOST_SOCKET_PATH=${HOST_SOCKET_PATH}
volumes:
- ${HOST_SOCKET_PATH:-/tmp/docker-proxy}:/socket
- ${HOST_SOCKET_PATH}:${UNIX_SOCKET_PATH}
networks:
- proxy-network
restart: unless-stopped

View File

@@ -4,9 +4,11 @@ set -e
# Set default values if not provided
TARGET_HOST=${TARGET_HOST}
TARGET_PORT=${TARGET_PORT}
UNIX_SOCKET_NAME=${UNIX_SOCKET_NAME}
UNIX_SOCKET_PATH=${UNIX_SOCKET_PATH}
UNIX_SOCKET_NAME=$(basename "$UNIX_SOCKET_PATH")
HOST_SOCKET_PATH=${HOST_SOCKET_PATH}
FULL_HOST_SOCKET_PATH="$HOST_SOCKET_PATH/$UNIX_SOCKET_NAME"
FULL_UNIX_SOCKET_PATH="$UNIX_SOCKET_PATH/$UNIX_SOCKET_NAME"
# Validate required environment variables
if [ -z "$TARGET_HOST" ]; then
@@ -19,6 +21,11 @@ if [ -z "$TARGET_PORT" ]; then
exit 1
fi
if [ -z "$UNIX_SOCKET_NAME" ]; then
echo "ERROR: UNIX_SOCKET_NAME environment variable is required"
exit 1
fi
if [ -z "$UNIX_SOCKET_PATH" ]; then
echo "ERROR: UNIX_SOCKET_PATH environment variable is required"
exit 1
@@ -30,39 +37,36 @@ if [ -z "$HOST_SOCKET_PATH" ]; then
fi
echo "Starting socat proxy..."
echo "UNIX socket: $UNIX_SOCKET_PATH"
echo "TCP target: $TARGET_HOST:$TARGET_PORT"
echo "HOST path: $HOST_SOCKET_PATH"
echo "Socket name: $UNIX_SOCKET_NAME"
# Calculate full socket path
FULL_SOCKET_PATH="$HOST_SOCKET_PATH/$UNIX_SOCKET_NAME"
echo "Full socket path: $FULL_SOCKET_PATH"
echo "Full host socket path: $FULL_HOST_SOCKET_PATH"
echo "Full socket path: $FULL_UNIX_SOCKET_PATH"
# Check if socket file/folder exists and handle it
if [ -e "$FULL_SOCKET_PATH" ]; then
echo "Socket file/folder $FULL_SOCKET_PATH exists, removing it..."
if rm -rf "$FULL_SOCKET_PATH"; then
echo "SUCCESS: Removed existing socket $FULL_SOCKET_PATH"
if [ -e "$FULL_UNIX_SOCKET_PATH" ]; then
echo "Socket file/folder $FULL_UNIX_SOCKET_PATH exists, removing it..."
if rm -rf "$FULL_UNIX_SOCKET_PATH"; then
echo "SUCCESS: Removed existing socket $FULL_UNIX_SOCKET_PATH"
else
echo "ERROR: Failed to remove existing socket $FULL_SOCKET_PATH"
echo "ERROR: Failed to remove existing socket $FULL_UNIX_SOCKET_PATH"
exit 1
fi
fi
echo "Creating socket directory structure..."
# Create directory if needed
if mkdir -p "$HOST_SOCKET_PATH"; then
echo "SUCCESS: Created directory $HOST_SOCKET_PATH"
if mkdir -p "$UNIX_SOCKET_PATH"; then
echo "SUCCESS: Created directory $UNIX_SOCKET_PATH"
else
echo "ERROR: Failed to create directory $HOST_SOCKET_PATH"
echo "ERROR: Failed to create directory $UNIX_SOCKET_PATH"
exit 1
fi
echo "Creating socket with netcat..."
# Create socket with nc -lU in background and then kill it to create the socket file
if timeout 1 nc -lU "$FULL_SOCKET_PATH" 2>/dev/null || true; then
echo "SUCCESS: Socket created at $FULL_SOCKET_PATH"
if timeout 1 nc -lU "$FULL_UNIX_SOCKET_PATH" 2>/dev/null || true; then
echo "SUCCESS: Socket created at $FULL_UNIX_SOCKET_PATH"
else
echo "WARNING: Socket creation with netcat had issues, but continuing..."
fi
@@ -92,7 +96,7 @@ trap cleanup SIGTERM SIGINT
echo "Starting socat proxy..."
# Start socat with verbose logging and redirect to stdout/stderr
if socat -d -d UNIX-LISTEN:$UNIX_SOCKET_PATH,fork,unlink-early TCP:$TARGET_HOST:$TARGET_PORT & then
if socat -d -d UNIX-LISTEN:$FULL_UNIX_SOCKET_PATH,fork,unlink-early TCP:$TARGET_HOST:$TARGET_PORT & then
SOCAT_PID=$!
echo "SUCCESS: Socat started with PID: $SOCAT_PID"
echo "Container is ready and running..."