Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using netcat to pipe unix socket to tcp socket

I am trying to expose a unix socket as a tcp socket using this command:

nc -lkv 44444 | nc -Uv /var/run/docker.sock

When I try to access localhost:44444/containers/json from a browser, it doesn't load anything but keeps the connection open (the loading thingy keeps spinning), but the console (because of the -v flag) shows proper http response.

Any ideas on how to get this working?

PS: I know I can use socat, or just tell docker to also listen on a tcp socket, but I am using the project atomic vm image, and it won't let me modify anything except /home.

like image 810
crashandburn Avatar asked Aug 21 '14 18:08

crashandburn


People also ask

Does Netcat use TCP or UDP?

By default Netcat uses the TCP protocol for its communications, but it can also UDP using the -u option. As we mentioned at the previous step, Netcat lets you convert your PC in a server. In this case we're going to establish the connection between the server and the client but using UDP.

Does Unix socket use TCP?

Show activity on this post. When the host is "localhost", MySQL Unix clients use a Unix socket, AKA Unix Domain Socket, rather than a TCP/IP socket for the connection, thus the TCP port doesn't matter.


1 Answers

You are only redirecting incoming data, not outgoing data. try with:

mkfifo myfifo
nc -lkv 44444 <myfifo | nc -Uv /var/run/docker.sock >myfifo

See http://en.wikipedia.org/wiki/Netcat#Proxying

Edit: in a script you would want to generate the name for the fifo at random, and remove it after opening it:

FIFONAME=`mktemp -u`
mkfifo $FIFONAME
nc -lkv 44444 < $FIFONAME | nc -Uv /var/run/docker.sock > $FIFONAME &
rm $FIFONAME
fg
like image 124
pqnet Avatar answered Sep 30 '22 19:09

pqnet