juan ezquerro
juan ezquerro

Reputation: 145

Socat: run script in bidirectional tunnel

I am running a tunnel like this:

socat TCP-LISTEN:9090,fork TCP:192.168.1.3:9090

I would like to run a script to execute code with the strings passing through the tunnel.

The script does not change the strings, only processes strings independently but allows passage without changing between both ends.

Is this possible?

Upvotes: 1

Views: 2941

Answers (1)

vlp
vlp

Reputation: 8106

You should be able to even alter the communication using this approach:

Prepare a helper script helper.sh which gets executed for each connection:

#!/bin/bash

./inFilter.sh | socat - TCP:192.168.1.3:9090 | ./outFilter.sh

And start listening by using:

socat TCP-LISTEN:9090,fork EXEC:"./helper.sh"

The scripts inFilter.sh and outFilter.sh are processing the client and the server parts of the communication.

Example inFilter.sh:

#!/bin/bash

while read -r l ; do echo "IN(${l})" ; done

Example outFilter.sh:

#!/bin/bash

while read -r l ; do echo "OUT(${l})" ; done

This method should work for line-based text communication (as almost everything is line-buffered).

To make it work for binary protocols wrapping all processes with stdbuf -i0 -o0 might help (see .e.g here) and getting rid of shell is probably a good idea in this case.

Good luck!

Upvotes: 1

Related Questions