Veja como fazer isso usando namespaces ip (requer privilégios de root, o seguinte deve ser executado como root).
Create a namespace for the server:
ip netns add myservernsAssign an IP address to the loopback in the namespace and turn it on (source: this post):
ip netns exec myserverns ifconfig lo 127.0.0.1 upRun
serverin the namespace (where "serveruser" is the user that should run the server)
ip netns exec myserverns sudo -u serveruser serverLet's assume that the server listens on port
4242. Now, usesocatto bind the loopback to the namespace (note the use ofbind=127.0.0.1which is the entire point) (source: this answer):
socat tcp-listen:4242,bind=127.0.0.1,fork,reuseaddr exec:'ip netns exec myserverns socat STDIO tcp-connect\:127.0.0.1\:4242',nofork
Isso alcança meu objetivo pretendido: mesmo que server tente se ligar a '*' (ou a portas completamente diferentes), quando executamos desta forma, temos certeza de que ela só pode ser acessada na máquina local com porta 4242 .