I have a service running on localhost which listens port localhost:10000
What I want to do is to forward all traffic coming to publicip:15000 to localhost:10000 where changing the configuration of service is not available.
Service only listens localhost, however traffic comes from outside.
Service runs on linux by the way.
Edit; I tried to add NAT rule like this but I could not be successful.
To configure NAT I did;
iptables --flush
iptables --table nat --flush
iptables --delete-chain
iptables --table nat --delete-chain
iptables --table nat --append POSTROUTING --out-interface lo -j MASQUERADE
iptables -A FORWARD -i eth0 -o lo -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i lo -o eth0 -j ACCEPT
echo 1 > /proc/sys/net/ipv4/ip_forward
service ufw restart
And to start routing execute this;
iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 15000 -j DNAT --to 127.0.0.1:10000
What do you think I am missing ?
Thanks in advance
Baris
It seems that DNAT for loopback traffic is not possible.
Loopback traffic skip both PREROUTING and OUTPUT chains.
RFC 5735 (page 3) says that network
127.0.0.0/8
could not be routed outside of the host itself :Also, traffic to
loopback interface
are considered asMartian Packets
:Workaround :
A simple alternative is to use an
inted
server on Server side, and itsredirect
feature.This way, you can define un service within
inetd
and set the port where this service will listen to. Then, set theredirect
directive to bind this port to127.0.0.1:port
.In my sample below i will use
xinetd
(on Ubuntu 12.04 LTS) to bind amysql
server running on127.0.0.1:10000
:Step 1 : Install the package :
apt-get install xinetd
Step 2 : Edit de the config file
/etc/xinetd.conf
Add a service definition similar to the one below :
Step 3 : Restart
xinetd
daemon :service xinetd restart
Step 4 : let's check for the listener on port
15000
:Step 5 : Add your
iptables
rules :Let's test it :
For testing i have setup
mysql
to listen on127.0.0.1:10000
. I will try to access it through thexinetd
service on port15000
.First, on Server side, i make sure that
mysql
server is only listening on127.0.0.1:10000
:Then, on Client side, let's check if we can connect using port
15000
:Seems we can ! :)
Let's try to connect to
mysql
server :Done !