Skip to content

Instantly share code, notes, and snippets.

@maxclaus
Last active January 8, 2025 00:36
Show Gist options
  • Select an option

  • Save maxclaus/70b564c94aeeeee182fb to your computer and use it in GitHub Desktop.

Select an option

Save maxclaus/70b564c94aeeeee182fb to your computer and use it in GitHub Desktop.

Revisions

  1. @maxcnunes maxcnunes revised this gist Jun 25, 2014. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -8,4 +8,6 @@ socat -d -d TCP4-LISTEN:15432,fork UNIX-CONNECT:/srv/mongodb-27017.sock

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

    [Coderwall Post](https://coderwall.com/p/c3wyzq)
  2. @maxcnunes maxcnunes created this gist Jun 25, 2014.
    11 changes: 11 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    ### Base Command

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

    ### Final Script

    ```shell
    ./forward-port-to-socket.sh 15432 /srv/mongodb-27017.sock
    ```
    19 changes: 19 additions & 0 deletions forward-port-to-socket.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    #!/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
    1 change: 1 addition & 0 deletions socat-allow
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    socat:<allowed-ip-here>
    1 change: 1 addition & 0 deletions socat-deny
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    socat:all