I have a program when run, starts listing on on port 12345

$ ./cool_script.pl

My linux administrator says it's against the policy to open ports. He says I'm allowed to use ~/cool_script.sock (unix domain socket) for communication. Apparently, his nginx server can read and write to the sock as if it was a tcp port.

Anyway, is there a way to wrap an executable (./cool_script.pl) so that it's attempt at opening a listening port is redirected to a .sock file.

What I tried: read some documentation for socat

  • If it's a Perl script (.pl), it might be simpler and more maintainable to edit the script. – user535733 Dec 29 '17 at 13:55
  • 2
    The "correct" way to do this in general is to use something like LD_PRELOAD with a library that replaces listening TCP connections with Unix domain sockets. That's really ugly, so ask your Linux admin if he has something like that already set up. – barrycarter Dec 29 '17 at 14:32
  • Similar question on Unix & Linux: unix.stackexchange.com/questions/436927/… – Gilles Apr 11 '18 at 7:14

This example should suit you: https://coderwall.com/p/c3wyzq/forwarding-tcp-traffic-to-a-unix-socket


Forwarding port 15432 to socket /srv/mongodb-27017.sock:

socat -d -d TCP4-LISTEN:15432,fork UNIX-CONNECT:/srv/mongodb-27017.sock
  • This doesn't do what the question requires! The application is still listening on the TCP port and thus is still remotely reachable which is against the policy. – Gilles Apr 11 '18 at 7:05
  • @Gilles just listen on and it will not be remotely reachable – Patrick Mevzek Apr 11 '18 at 7:11
  • Which requires changing the application, anyway. – Gilles Apr 11 '18 at 7:14

You'll need to modify how the program works. Make it listen on a Unix socket instead of TCP. Assuming the program is dynamically linked against the standard library (virtually all programs are), you don't need to change the program itself, just the way you run it. You can do this with LD_PRELOAD, by loading a wrapper for the TCP socket opening function that instead opens a Unix socket. ttu does precisely this (according to its documentation, I've never used it).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.