Fixer wrong variable usage + better logs

This commit is contained in:
2025-09-24 12:32:33 +02:00
parent 7adc49f5b0
commit 444571c311

View File

@@ -4,7 +4,9 @@ set -e
# Set default values if not provided # Set default values if not provided
TARGET_HOST=${TARGET_HOST} TARGET_HOST=${TARGET_HOST}
TARGET_PORT=${TARGET_PORT} TARGET_PORT=${TARGET_PORT}
UNIX_SOCKET_PATH=${UNIX_SOCKET_PATH:-/socket/docker.sock} UNIX_SOCKET_PATH=${UNIX_SOCKET_PATH}
UNIX_SOCKET_NAME=$(basename "$UNIX_SOCKET_PATH")
HOST_SOCKET_PATH=${HOST_SOCKET_PATH}
# Validate required environment variables # Validate required environment variables
if [ -z "$TARGET_HOST" ]; then if [ -z "$TARGET_HOST" ]; then
@@ -17,28 +19,57 @@ if [ -z "$TARGET_PORT" ]; then
exit 1 exit 1
fi fi
if [ -z "$UNIX_SOCKET_PATH" ]; then
echo "ERROR: UNIX_SOCKET_PATH environment variable is required"
exit 1
fi
if [ -z "$HOST_SOCKET_PATH" ]; then
echo "ERROR: HOST_SOCKET_PATH environment variable is required"
exit 1
fi
echo "Starting socat proxy..." echo "Starting socat proxy..."
echo "UNIX socket: $UNIX_SOCKET_PATH" echo "UNIX socket: $UNIX_SOCKET_PATH"
echo "TCP target: $TARGET_HOST:$TARGET_PORT" echo "TCP target: $TARGET_HOST:$TARGET_PORT"
echo "HOST path: $HOST_SOCKET_PATH"
echo "Full socket path: $FULL_SOCKET_PATH"
# Check if socket file/folder exists and handle it # Check if socket file/folder exists and handle it
if [ -e "$UNIX_SOCKET_PATH" ]; then FULL_SOCKET_PATH="$HOST_SOCKET_PATH/$UNIX_SOCKET_NAME"
echo "Socket file/folder $UNIX_SOCKET_PATH exists, removing it..." if [ -e "$FULL_SOCKET_PATH" ]; then
rm -rf "$UNIX_SOCKET_PATH" 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"
else
echo "ERROR: Failed to remove existing socket $FULL_SOCKET_PATH"
exit 1
fi
fi fi
echo "Creating socket directory structure..." echo "Creating socket directory structure..."
# Create directory if needed # Create directory if needed
mkdir -p "$(dirname "$UNIX_SOCKET_PATH")" if mkdir -p "$HOST_SOCKET_PATH"; then
echo "SUCCESS: Created directory $HOST_SOCKET_PATH"
else
echo "ERROR: Failed to create directory $HOST_SOCKET_PATH"
exit 1
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
timeout 1 nc -lU "$UNIX_SOCKET_PATH" || true if timeout 1 nc -lU "$FULL_SOCKET_PATH" 2>/dev/null || true; then
echo "SUCCESS: Socket created at $FULL_SOCKET_PATH"
else
echo "WARNING: Socket creation with netcat had issues, but continuing..."
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 "WARNING: Cannot connect to $TARGET_HOST:$TARGET_PORT - socat will retry automatically"
else
echo "SUCCESS: Connection to $TARGET_HOST:$TARGET_PORT is working"
fi fi
# Signal handler for graceful shutdown # Signal handler for graceful shutdown
@@ -58,11 +89,14 @@ trap cleanup SIGTERM SIGINT
echo "Starting socat proxy..." echo "Starting socat proxy..."
# Start socat with verbose logging and redirect to stdout/stderr # Start socat with verbose logging and redirect to stdout/stderr
socat -d -d UNIX-LISTEN:$UNIX_SOCKET_PATH,fork,unlink-early TCP:$TARGET_HOST:$TARGET_PORT & if socat -d -d UNIX-LISTEN:$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
echo "ERROR: Failed to start socat proxy"
exit 1
fi
# Keep the script alive and wait for socat process # Keep the script alive and wait for socat process
while kill -0 "$SOCAT_PID" 2>/dev/null; do while kill -0 "$SOCAT_PID" 2>/dev/null; do