Pipes used by |
are one-way. When you run nc | nc
, the second connection's output goes to your terminal; the first connection's input is read from your keyboard. Basically, the full form of it would be </dev/tty nc | nc >/dev/tty
.
To create a bidirectional tunnel, you would have to use something else, such as...
socat tcp-l:localhost:1234 tcp:localhost:1235
(Or create a named pipe and use it with two nc
's, but that's less efficient.)
However, that will not do what you want. It will not make 1234 become the source port of the second connection; you will only have it as the destination port of the first connection, and the two connections are unrelated: stunnel
simply receives bytes from both connections and resends them to the opposite end.
If you want simply to specify the source port of a connection, it's simpler:
nc -l 56789 localhost 1235 socat stdio tcp:localhost:1235,sourceport=56789
Since stunnel
does not have a "local port" option, you can take one of the above commands and use it in the exec=
option.