if we have two server let's say server1( server2(

I'm working on redirect incoming traffic on server1 port 4444 to server2 port 12345,this is easy with iptables rule on server1 like the following:

sysctl -w net.ipv4.ip_forward=1
iptables -t nat -A PREROUTING -p tcp --dport 4444 -j DNAT --to-destination
iptables -t nat -A POSTROUTING -j MASQUERADE  

this working well

request --> 4444:server1 <===> 12345:server2

what I need to do is using tor netwok between the two server:

request --> 4444:server1 <===> |tor network| <===> 12345:server2

how I can do what?

I tried to use HiddenServicePort on server2 but I can't forward traffic from HiddenServicePort to another tor hostname ( in server1)

also how i can redirect traffic from iptables to tor specific hostname (server2 hostname)

2 Answers 2


This looks like something to use socat, not iptables.

You need to employ socat on server1 to listen on specific port and forward traffic to server2 through tor. Something along the lines:

socat TCP-LISTEN:4444 SOCKS4A:localhost:server2:12345,socksport=9050

The above command assumes tor running on server1 on the usual port 9050.

You may want to have a look at socat man page for additional options, especially for TCP-LISTEN.

  • socat work but for one request only, I tested it on apache2(server2) and when the page open socat close and I need to run it again. how I can apply socat to forward traffic permanently ?
    – tlf05649
    Oct 5, 2020 at 15:11
  • As I mentioned - have a look at socat man page. I think you are looking for fork option from CHILD option group but you'll have to doublecheck on this.
    – Tomek
    Oct 5, 2020 at 20:12

No socat is needed - Tor is like STunnel, so you create a hidden service on server2 and rise an OpenVPN server on it - the rest is as simple as it sounds: you will have an internal network like for the tunnel and using a static client addresses you will just replace the IP in your iptables rules: all the rest of the stuff will be handled by OpenVPN - channel reconnection, interfaces, routing tables e.t.c....

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .