我有一个服务运行在localhost监听端口localhost:10000
我想要做的是将所有来到publicip:15000的stream量转发到localhost:10000,在那里改变服务的configuration不可用。
服务只监听本地主机,但stream量来自外部。
服务在Linux上运行的方式。
编辑; 我试图像这样添加NAT规则,但是我不能成功。
configurationNAT我做了;
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
并开始路由执行这个;
iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 15000 -j DNAT --to 127.0.0.1:10000
你觉得我错过了什么?
提前致谢
巴里斯
似乎DNAT的回环stream量是不可能的。
环回stream量跳过PREROUTING和OUTPUT链。
RFC 5735 (第3页)说,networking127.0.0.0/8不能在主机本身以外路由:
127.0.0.0/8 – 该块被分配用作互联网主机回送地址。 由更高级别的协议发送到该块内任何地址的数据报循环回到主机内部。 这通常只使用127.0.0.1/32进行环回。 如[RFC1122]第3.2.1.3节所述,整个127.0.0.0/8块中的地址不会合法地出现在任何地方的任何networking上。
此外,到loopback interfacestream量被视为Martian Packets :
这些数据包实际上不能像声称的那样发起,或者被传送
解决办法:
一个简单的select是使用服务器端的一个服务器端,以及它的redirectfunction。
这样,您可以在inetd定义un服务并设置此服务将侦听的端口。 然后,设置redirect指令将此端口绑定到127.0.0.1:port 。
在我的示例中,我将使用xinetd (在Ubuntu 12.04 LTS上)绑定运行在127.0.0.1:10000上的mysql服务器:
第1步:安装软件包: apt-get install xinetd
步骤2:编辑configuration文件/etc/xinetd.conf
添加一个类似于下面的服务定义:
service my_redirector { type = UNLISTED disable = no socket_type = stream protocol = tcp user = root wait = no port = 15000 redirect = 127.0.0.1 10000 log_type = FILE /tmp/somefile.log }
第3步:重新启动xinetd守护程序: service xinetd restart
第4步:让我们检查端口15000上的监听器:
# netstat -anp | grep 15000 tcp 0 0 0.0.0.0:15000 0.0.0.0:* LISTEN 4654/xinetd
第5步:添加你的iptables规则:
iptables -A INPUT -i eth0 -p tcp -d 192.168.0.60 --dport 15000 -j ACCEPT iptables -A OUTPUT -o eth0 -p tcp -s 192.168.0.60 --sport 15000 -j ACCEPT
我们来testing一下:
为了testing,我已经安装了mysql来监听127.0.0.1:10000 。 我将尝试通过端口15000上的xinetd服务来访问它。
首先,在服务器端,我确保mysql服务器只在127.0.0.1:10000监听:
# netstat -anp | grep :10000 tcp 0 0 127.0.0.1:10000 0.0.0.0:* LISTEN 4247/mysqld
然后,在客户端,让我们来看看我们是否可以使用端口15000连接:
# telnet 192.168.0.60 15000 Trying 192.168.0.60... Connected to 192.168.0.60. Escape character is '^]'. _ 5.5.35-0ubuntu0.12.04.2-logD46S}<P`.6cr4ITIQ<wcmysql_native_password
看来我们可以! 🙂
让我们尝试连接到mysql服务器:
# mysql -h 192.168.0.60 --port=15000 -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 67 Server version: 5.5.35-0ubuntu0.12.04.2-log (Ubuntu) Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
完成!