Skip to content

Instantly share code, notes, and snippets.

@hslatman
Forked from maxcnunes/README.md
Created November 7, 2018 09:48
Show Gist options
  • Save hslatman/a32013a46c79cb029b6861dc7afc9391 to your computer and use it in GitHub Desktop.
Save hslatman/a32013a46c79cb029b6861dc7afc9391 to your computer and use it in GitHub Desktop.
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment