Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Forwarding TCP-traffic to a UNIX socket

Base Command

socat -d -d TCP4-LISTEN:15432,fork UNIX-CONNECT:/srv/mongodb-27017.sock

Final Script

./forward-port-to-socket.sh 15432 /srv/mongodb-27017.sock

Coderwall Post

#!/bin/sh -e
if [ $# != 2 ]; then
echo "usage: $0 <public-port> <path-private-socket>"
echo "example: $0 15432 /srv/my-service.sock"
exit 0
fi
PUBLIC_PORT=$1
PRIVATE_SOCKET=$2
SOCAT_LOG=/tmp/socat-$PUBLIC_PORT.log
echo "--> socat forwarding:\n\t- from port: $PUBLIC_PORT\n\t- to socket: $PRIVATE_SOCKET"
echo "--> allowed ips:\n\t$(cat socat-allow)"
echo "--> logs: $SOCAT_LOG"
socat -d -d -lf $SOCAT_LOG \
TCP4-LISTEN:$PUBLIC_PORT,reuseaddr,fork,tcpwrap=socat,allow-table=socat-allow,deny-table=socat-deny \
UNIX-CONNECT:$PRIVATE_SOCKET
socat:<allowed-ip-here>
socat:all
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.