Better logs and start
This commit is contained in:
@@ -6,6 +6,7 @@ RUN apk add --no-cache socat netcat-openbsd \
|
|||||||
|
|
||||||
# Create socket directory and copy/set permissions in single layers
|
# Create socket directory and copy/set permissions in single layers
|
||||||
COPY entrypoint.sh /entrypoint.sh
|
COPY entrypoint.sh /entrypoint.sh
|
||||||
|
COPY VERSION /VERSION
|
||||||
RUN mkdir -p /socket \
|
RUN mkdir -p /socket \
|
||||||
&& chmod +x /entrypoint.sh
|
&& chmod +x /entrypoint.sh
|
||||||
|
|
||||||
|
|||||||
@@ -4,11 +4,9 @@ A lightweight Docker container that creates a UNIX socket proxy to TCP connectio
|
|||||||
|
|
||||||
## 🚀 Features
|
## 🚀 Features
|
||||||
|
|
||||||
- **Lightweight**: Based on Alpine Linux (~10-15MB image)
|
|
||||||
- **Configurable**: Environment variable driven configuration
|
- **Configurable**: Environment variable driven configuration
|
||||||
- **Socket Management**: Automatic UNIX socket creation and cleanup
|
- **Socket Management**: Automatic UNIX socket creation and cleanup
|
||||||
- **Production Ready**: Includes proper error handling and logging
|
- **Production Ready**: Includes proper error handling and logging
|
||||||
- **Multi-variant**: Standard and minimal Docker images available
|
|
||||||
|
|
||||||
## 📋 Use Cases example
|
## 📋 Use Cases example
|
||||||
|
|
||||||
|
|||||||
+3
-11
@@ -1,11 +1,10 @@
|
|||||||
version: '3.8'
|
---
|
||||||
|
|
||||||
services:
|
services:
|
||||||
socat-proxy:
|
socat-proxy:
|
||||||
build: .
|
build: .
|
||||||
environment:
|
environment:
|
||||||
- TARGET_HOST=${TARGET_HOST:}
|
- TARGET_HOST=${TARGET_HOST}
|
||||||
- TARGET_PORT=${TARGET_PORT:}
|
- TARGET_PORT=${TARGET_PORT}
|
||||||
- UNIX_SOCKET_NAME=${UNIX_SOCKET_NAME}
|
- UNIX_SOCKET_NAME=${UNIX_SOCKET_NAME}
|
||||||
- UNIX_SOCKET_PATH=${UNIX_SOCKET_PATH}
|
- UNIX_SOCKET_PATH=${UNIX_SOCKET_PATH}
|
||||||
- HOST_SOCKET_PATH=${HOST_SOCKET_PATH}
|
- HOST_SOCKET_PATH=${HOST_SOCKET_PATH}
|
||||||
@@ -14,10 +13,3 @@ services:
|
|||||||
networks:
|
networks:
|
||||||
- proxy-network
|
- proxy-network
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
|
||||||
volumes:
|
|
||||||
socket_volume:
|
|
||||||
|
|
||||||
networks:
|
|
||||||
proxy-network:
|
|
||||||
external: false
|
|
||||||
+38
-26
@@ -15,46 +15,58 @@ HOST_SOCKET_PATH=${HOST_SOCKET_PATH%/}
|
|||||||
FULL_HOST_SOCKET_PATH="$HOST_SOCKET_PATH/$UNIX_SOCKET_NAME"
|
FULL_HOST_SOCKET_PATH="$HOST_SOCKET_PATH/$UNIX_SOCKET_NAME"
|
||||||
FULL_UNIX_SOCKET_PATH="$UNIX_SOCKET_PATH/$UNIX_SOCKET_NAME"
|
FULL_UNIX_SOCKET_PATH="$UNIX_SOCKET_PATH/$UNIX_SOCKET_NAME"
|
||||||
|
|
||||||
|
VERSION=$(cat VERSION)
|
||||||
|
|
||||||
|
echo -e "${CYAN}╭────────────────────────────────────────────╮${NC}"
|
||||||
|
echo -e "${CYAN}│${NC} Socat-proxy - Version ${VERSION}${NC} ${CYAN}│${NC}"
|
||||||
|
echo -e "${CYAN}├────────────────────────────────────────────┤${NC}"
|
||||||
|
echo -e "${CYAN}│${NC} Source: https://git.djeex.fr/Djeex/socat-proxy ${CYAN}│${NC}"
|
||||||
|
echo -e "${CYAN}│${NC} Mirror: https://github.com/Djeex/socat-proxy ${CYAN}│${NC}"
|
||||||
|
echo -e "${CYAN}╰────────────────────────────────────────────╯${NC}"
|
||||||
|
copy_default_config
|
||||||
|
start_server
|
||||||
|
|
||||||
|
|
||||||
# Validate required environment variables
|
# Validate required environment variables
|
||||||
if [ -z "$TARGET_HOST" ]; then
|
if [ -z "$TARGET_HOST" ]; then
|
||||||
echo "ERROR: TARGET_HOST environment variable is required"
|
echo "[✗] TARGET_HOST environment variable is required"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ -z "$TARGET_PORT" ]; then
|
if [ -z "$TARGET_PORT" ]; then
|
||||||
echo "ERROR: TARGET_PORT environment variable is required"
|
echo "[✗] TARGET_PORT environment variable is required"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ -z "$UNIX_SOCKET_NAME" ]; then
|
if [ -z "$UNIX_SOCKET_NAME" ]; then
|
||||||
echo "ERROR: UNIX_SOCKET_NAME environment variable is required"
|
echo "[✗] UNIX_SOCKET_NAME environment variable is required"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ -z "$UNIX_SOCKET_PATH" ]; then
|
if [ -z "$UNIX_SOCKET_PATH" ]; then
|
||||||
echo "ERROR: UNIX_SOCKET_PATH environment variable is required"
|
echo "[✗] UNIX_SOCKET_PATH environment variable is required"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ -z "$HOST_SOCKET_PATH" ]; then
|
if [ -z "$HOST_SOCKET_PATH" ]; then
|
||||||
echo "ERROR: HOST_SOCKET_PATH environment variable is required"
|
echo "[✗] HOST_SOCKET_PATH environment variable is required"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "Starting socat proxy..."
|
echo "[~] Starting socat proxy..."
|
||||||
echo "TCP target: $TARGET_HOST:$TARGET_PORT"
|
echo "[i] TCP target: $TARGET_HOST:$TARGET_PORT"
|
||||||
echo "HOST path: $HOST_SOCKET_PATH"
|
echo "[i] HOST path: $HOST_SOCKET_PATH"
|
||||||
# Calculate full socket path
|
# Calculate full socket path
|
||||||
echo "Full host socket path: $FULL_HOST_SOCKET_PATH"
|
echo "[i] Full host socket path: $FULL_HOST_SOCKET_PATH"
|
||||||
echo "Full socket path: $FULL_UNIX_SOCKET_PATH"
|
echo "[i] Full socket path: $FULL_UNIX_SOCKET_PATH"
|
||||||
|
|
||||||
# Check if socket file/folder exists and handle it
|
# Check if socket file/folder exists and handle it
|
||||||
if [ -e "$FULL_UNIX_SOCKET_PATH" ]; then
|
if [ -e "$FULL_UNIX_SOCKET_PATH" ]; then
|
||||||
echo "Socket file/folder $FULL_UNIX_SOCKET_PATH exists, removing it..."
|
echo "[~] Socket file/folder $FULL_UNIX_SOCKET_PATH exists, removing it..."
|
||||||
if rm -rf "$FULL_UNIX_SOCKET_PATH"; then
|
if rm -rf "$FULL_UNIX_SOCKET_PATH"; then
|
||||||
echo "SUCCESS: Removed existing socket $FULL_UNIX_SOCKET_PATH"
|
echo "[✓] Removed existing socket $FULL_UNIX_SOCKET_PATH"
|
||||||
else
|
else
|
||||||
echo "ERROR: Failed to remove existing socket $FULL_UNIX_SOCKET_PATH"
|
echo "[✗] Failed to remove existing socket $FULL_UNIX_SOCKET_PATH"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
@@ -62,37 +74,37 @@ fi
|
|||||||
echo "Creating socket directory structure..."
|
echo "Creating socket directory structure..."
|
||||||
# Create directory if needed
|
# Create directory if needed
|
||||||
if mkdir -p "$UNIX_SOCKET_PATH"; then
|
if mkdir -p "$UNIX_SOCKET_PATH"; then
|
||||||
echo "SUCCESS: Created directory $UNIX_SOCKET_PATH"
|
echo "[✓] Created directory $UNIX_SOCKET_PATH"
|
||||||
else
|
else
|
||||||
echo "ERROR: Failed to create directory $UNIX_SOCKET_PATH"
|
echo "[✗] Failed to create directory $UNIX_SOCKET_PATH"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "Creating socket with netcat..."
|
echo "Creating socket with netcat..."
|
||||||
# Create socket with nc -lU in background and then kill it to create the socket file
|
# Create socket with nc -lU in background and then kill it to create the socket file
|
||||||
if timeout 1 nc -lU "$FULL_UNIX_SOCKET_PATH" 2>/dev/null || true; then
|
if timeout 1 nc -lU "$FULL_UNIX_SOCKET_PATH" 2>/dev/null || true; then
|
||||||
echo "SUCCESS: Socket created at $FULL_UNIX_SOCKET_PATH"
|
echo "[✓] Socket created at $FULL_UNIX_SOCKET_PATH"
|
||||||
else
|
else
|
||||||
echo "WARNING: Socket creation with netcat had issues, but continuing..."
|
echo "[!] Socket creation with netcat had issues, but continuing..."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "Testing connection to target..."
|
echo "Testing connection to target..."
|
||||||
# Test if we can reach the target before starting socat
|
# Test if we can reach the target before starting socat
|
||||||
if ! nc -z "$TARGET_HOST" "$TARGET_PORT" 2>/dev/null; then
|
if ! nc -z "$TARGET_HOST" "$TARGET_PORT" 2>/dev/null; then
|
||||||
echo "WARNING: Cannot connect to $TARGET_HOST:$TARGET_PORT - socat will retry automatically"
|
echo "[!] Cannot connect to $TARGET_HOST:$TARGET_PORT - socat will retry automatically"
|
||||||
else
|
else
|
||||||
echo "SUCCESS: Connection to $TARGET_HOST:$TARGET_PORT is working"
|
echo "[✓] Connection to $TARGET_HOST:$TARGET_PORT is working"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Signal handler for graceful shutdown
|
# Signal handler for graceful shutdown
|
||||||
cleanup() {
|
cleanup() {
|
||||||
echo "Received SIGTERM, shutting down gracefully..."
|
echo "[!] Received SIGTERM, shutting down gracefully..."
|
||||||
if [ ! -z "$SOCAT_PID" ]; then
|
if [ ! -z "$SOCAT_PID" ]; then
|
||||||
echo "Stopping socat process (PID: $SOCAT_PID)..."
|
echo "[~] Stopping socat process (PID: $SOCAT_PID)..."
|
||||||
kill "$SOCAT_PID" 2>/dev/null || true
|
kill "$SOCAT_PID" 2>/dev/null || true
|
||||||
wait "$SOCAT_PID" 2>/dev/null || true
|
wait "$SOCAT_PID" 2>/dev/null || true
|
||||||
fi
|
fi
|
||||||
echo "Cleanup completed, exiting..."
|
echo "[ ] Cleanup completed, exiting..."
|
||||||
exit 0
|
exit 0
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -103,10 +115,10 @@ echo "Starting socat proxy..."
|
|||||||
# Start socat with verbose logging and redirect to stdout/stderr
|
# Start socat with verbose logging and redirect to stdout/stderr
|
||||||
if socat -d -d UNIX-LISTEN:$FULL_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=$!
|
SOCAT_PID=$!
|
||||||
echo "SUCCESS: Socat started with PID: $SOCAT_PID"
|
echo "[✓] Socat started with PID: $SOCAT_PID"
|
||||||
echo "Container is ready and running..."
|
echo "[~] Container is ready and running..."
|
||||||
else
|
else
|
||||||
echo "ERROR: Failed to start socat proxy"
|
echo "[✗] Failed to start socat proxy"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@@ -115,6 +127,6 @@ while kill -0 "$SOCAT_PID" 2>/dev/null; do
|
|||||||
sleep 1
|
sleep 1
|
||||||
done
|
done
|
||||||
|
|
||||||
echo "Socat process has stopped"
|
echo "[✓] Socat process has stopped"
|
||||||
exit 1
|
exit 1
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user